]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/colour/DateColourer.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / gui / colour / DateColourer.java
1 package tim.prune.gui.colour;
2
3 import java.awt.Color;
4 import java.text.DateFormat;
5 import java.util.Calendar;
6 import java.util.HashMap;
7 import java.util.TimeZone;
8
9 import tim.prune.data.DataPoint;
10 import tim.prune.data.Timestamp;
11 import tim.prune.data.Track;
12 import tim.prune.data.TrackInfo;
13
14 /**
15  * Point colourer giving a different colour to each date
16  * Uses the system timezone so may give funny results for
17  * data from other timezones (eg far-away holidays)
18  */
19 public class DateColourer extends DiscretePointColourer
20 {
21         // Doesn't really matter what format is used here, as long as dates are different
22         private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateInstance();
23
24         /**
25          * Constructor
26          * @param inStartColour start colour of scale
27          * @param inEndColour end colour of scale
28          * @param inWrapLength number of unique colours before wrap
29          */
30         public DateColourer(Color inStartColour, Color inEndColour, int inWrapLength)
31         {
32                 super(inStartColour, inEndColour, inWrapLength);
33         }
34
35         /**
36          * Calculate the colours for each of the points in the given track
37          * @param inTrackInfo track info object
38          */
39         @Override
40         public void calculateColours(TrackInfo inTrackInfo)
41         {
42                 // initialise the array to the right size
43                 Track track = inTrackInfo == null ? null : inTrackInfo.getTrack();
44                 final int numPoints = track == null ? 0 : track.getNumPoints();
45                 init(numPoints);
46                 // Make a hashmap of the already-used dates
47                 HashMap<String, Integer> usedDates = new HashMap<String, Integer>(20);
48                 // Also store the previous one, because they're probably consecutive
49                 String prevDate = null;
50                 int prevIndex = -1;
51
52                 // loop over track points
53                 int dayIndex = -1;
54                 for (int i=0; i<numPoints; i++)
55                 {
56                         DataPoint p = track.getPoint(i);
57                         if (p != null && !p.isWaypoint())
58                         {
59                                 dayIndex = 0; // default index 0 will be used if no date found
60                                 String date = getDate(p.getTimestamp());
61                                 if (date != null)
62                                 {
63                                         // Check if it's the previous one
64                                         if (prevDate != null && date.equals(prevDate)) {
65                                                 dayIndex = prevIndex;
66                                         }
67                                         else
68                                         {
69                                                 // Look up in the hashmap to see if it's been used before
70                                                 Integer foundIndex = usedDates.get(date);
71                                                 if (foundIndex == null)
72                                                 {
73                                                         // not been used before, so add it
74                                                         dayIndex = usedDates.size() + 1;
75                                                         usedDates.put(date, dayIndex);
76                                                 }
77                                                 else
78                                                 {
79                                                         // found it
80                                                         dayIndex = foundIndex;
81                                                 }
82                                                 // Remember what we've got for the next point
83                                                 prevDate = date;
84                                                 prevIndex = dayIndex;
85                                         }
86                                 }
87                                 // if date is null (no timestamp or invalid) then dayIndex remains 0
88                                 setColour(i, dayIndex);
89                         }
90                 }
91                 // generate the colours needed
92                 generateDiscreteColours(usedDates.size() + 1);
93         }
94
95
96         /**
97          * Find which date (in the system timezone) the given timestamp falls on
98          * @param inTimestamp timestamp
99          * @return String containing description of date, or null
100          */
101         private static String getDate(Timestamp inTimestamp)
102         {
103                 if (inTimestamp == null || !inTimestamp.isValid()) {
104                         return null;
105                 }
106                 Calendar cal = inTimestamp.getCalendar();
107                 // use system time zone
108                 cal.setTimeZone(TimeZone.getDefault());
109                 return DEFAULT_DATE_FORMAT.format(cal.getTime());
110         }
111 }