]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/gui/colour/ColourerFactory.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / gui / colour / ColourerFactory.java
1 package tim.prune.gui.colour;
2
3 import java.awt.Color;
4
5 import tim.prune.config.ColourUtils;
6
7 /**
8  * Factory for the creation of PointColourer objects
9  */
10 public abstract class ColourerFactory
11 {
12         /** Enumeration of colourer types */
13         public enum ColourerId
14         {
15                 NONE,
16                 BY_FILE,
17                 BY_SEGMENT,
18                 BY_ALTITUDE,
19                 BY_SPEED,
20                 BY_VSPEED,
21                 BY_GRADIENT,
22                 BY_DATE
23         }
24
25         /**
26          * Does the specified colourer need a field for maximum number of colours?
27          * @param inId id of colourer
28          * @return true if max colours required, false otherwise
29          */
30         public static boolean isMaxColoursRequired(ColourerId inId)
31         {
32                 switch (inId)
33                 {
34                         case NONE:        return false;
35                         case BY_FILE:     return FileColourer.isMaxColoursRequired();
36                         case BY_SEGMENT:  return SegmentColourer.isMaxColoursRequired();
37                         case BY_ALTITUDE: return AltitudeColourer.isMaxColoursRequired();
38                         case BY_SPEED:    return SpeedColourer.isMaxColoursRequired();
39                         case BY_VSPEED:   return VertSpeedColourer.isMaxColoursRequired();
40                         case BY_GRADIENT: return GradientColourer.isMaxColoursRequired();
41                         case BY_DATE:     return DateColourer.isMaxColoursRequired();
42                 }
43                 return false;
44         }
45
46         /**
47          * Does the specified colourer need fields for start and end colours?
48          * @param inId id of colourer
49          * @return true if colours required, false otherwise
50          */
51         public static boolean areColoursRequired(ColourerId inId)
52         {
53                 // all of them except NONE need start and end colours
54                 return inId != ColourerId.NONE;
55         }
56
57         /**
58          * @param inDesc Single character used as a code (in Config string)
59          * @return associated ColourerId
60          */
61         private static ColourerId getColourerId(char inDesc)
62         {
63                 switch (inDesc)
64                 {
65                         case 'f': return ColourerId.BY_FILE;
66                         case 's': return ColourerId.BY_SEGMENT;
67                         case 'a': return ColourerId.BY_ALTITUDE;
68                         case 'p': return ColourerId.BY_SPEED;
69                         case 'v': return ColourerId.BY_VSPEED;
70                         case 'g': return ColourerId.BY_GRADIENT;
71                         case 'd': return ColourerId.BY_DATE;
72                 }
73                 return ColourerId.NONE;
74         }
75
76         /**
77          * Create a new PointColourer object given the parameters
78          * @param inId id of colourer to create
79          * @param inStartColour start colour
80          * @param inEndColour end colour
81          * @param inMaxColours maximum number of colours
82          * @return PointColourer object, or null
83          */
84         public static PointColourer createColourer(ColourerId inId, Color inStartColour, Color inEndColour, String inMaxColours)
85         {
86                 try
87                 {
88                         switch (inId)
89                         {
90                                 case NONE: return null;
91                                 case BY_FILE: return new FileColourer(inStartColour, inEndColour, Integer.parseInt(inMaxColours));
92                                 case BY_SEGMENT: return new SegmentColourer(inStartColour, inEndColour, Integer.parseInt(inMaxColours));
93                                 case BY_ALTITUDE: return new AltitudeColourer(inStartColour, inEndColour);
94                                 case BY_SPEED: return new SpeedColourer(inStartColour, inEndColour);
95                                 case BY_VSPEED: return new VertSpeedColourer(inStartColour, inEndColour);
96                                 case BY_GRADIENT: return new GradientColourer(inStartColour, inEndColour);
97                                 case BY_DATE: return new DateColourer(inStartColour, inEndColour, Integer.parseInt(inMaxColours));
98                         }
99                 }
100                 catch (NumberFormatException nfe) {} // drop out to return null
101                 return null;
102         }
103
104         /**
105          * Create a PointColourer object from the given description
106          * @param inString string from config
107          * @return PointColourer object, or null if string was invalid
108          */
109         public static PointColourer createColourer(String inString)
110         {
111                 try
112                 {
113                         String[] comps = inString.split(";");
114                         if (comps.length == 4)
115                         {
116                                 ColourerId colourerType = getColourerId(comps[0].charAt(0));
117                                 Color startColour = ColourUtils.colourFromHex(comps[1]);
118                                 Color endColour   = ColourUtils.colourFromHex(comps[2]);
119                                 String maxColours = comps[3];
120                                 return createColourer(colourerType, startColour, endColour, maxColours);
121                         }
122                 }
123                 catch (NullPointerException npe) {}
124                 catch (NumberFormatException nfe) {}
125                 return null;
126         }
127
128         /**
129          * Convert the given PointColourer object into a string for the config
130          * @param inColourer PointColourer object
131          * @return string describing object (for later re-creation) or null
132          */
133         public static String pointColourerToString(PointColourer inColourer)
134         {
135                 if (inColourer != null)
136                 {
137                         final String startColour = ColourUtils.makeHexCode(inColourer.getStartColour());
138                         final String endColour = ColourUtils.makeHexCode(inColourer.getEndColour());
139                         final int maxColours = inColourer.getMaxColours();
140                         if (inColourer instanceof FileColourer) {
141                                 return "f;" + startColour + ";" + endColour + ";" + maxColours;
142                         }
143                         else if (inColourer instanceof SegmentColourer) {
144                                 return "s;" + startColour + ";" + endColour + ";" + maxColours;
145                         }
146                         else if (inColourer instanceof AltitudeColourer) {
147                                 return "a;" + startColour + ";" + endColour + ";0";
148                         }
149                         else if (inColourer instanceof SpeedColourer) {
150                                 return "p;" + startColour + ";" + endColour + ";0";
151                         }
152                         else if (inColourer instanceof VertSpeedColourer) {
153                                 return "v;" + startColour + ";" + endColour + ";0";
154                         }
155                         else if (inColourer instanceof GradientColourer) {
156                                 return "g;" + startColour + ";" + endColour + ";0";
157                         }
158                         else if (inColourer instanceof DateColourer) {
159                                 return "d;" + startColour + ";" + endColour + ";" + maxColours;
160                         }
161                 }
162                 return null;
163         }
164
165         /**
166          * Get the colourer-specific end of the description key for translation
167          * @param inId id of colourer
168          * @return end of description key for combobox text
169          */
170         public static String getDescriptionKey(ColourerId inId)
171         {
172                 switch (inId)
173                 {
174                         case NONE:        return "none";
175                         case BY_FILE:     return "byfile";
176                         case BY_SEGMENT:  return "bysegment";
177                         case BY_ALTITUDE: return "byaltitude";
178                         case BY_SPEED:    return "byspeed";
179                         case BY_VSPEED:   return "byvertspeed";
180                         case BY_GRADIENT: return "bygradient";
181                         case BY_DATE:     return "bydate";
182                 }
183                 return null;
184         }
185
186         /**
187          * Get the id of the given colourer, according to its class
188          * @param inColourer point colourer object, or null
189          * @return id, for example for selection in dropdown
190          */
191         public static ColourerId getId(PointColourer inColourer)
192         {
193                 if (inColourer != null)
194                 {
195                         if (inColourer instanceof FileColourer)      {return ColourerId.BY_FILE;}
196                         if (inColourer instanceof SegmentColourer)   {return ColourerId.BY_SEGMENT;}
197                         if (inColourer instanceof AltitudeColourer)  {return ColourerId.BY_ALTITUDE;}
198                         if (inColourer instanceof SpeedColourer)     {return ColourerId.BY_SPEED;}
199                         if (inColourer instanceof VertSpeedColourer) {return ColourerId.BY_VSPEED;}
200                         if (inColourer instanceof GradientColourer)  {return ColourerId.BY_GRADIENT;}
201                         if (inColourer instanceof DateColourer)      {return ColourerId.BY_DATE;}
202                 }
203                 return ColourerId.NONE;
204         }
205 }