1 package tim.prune.gui.colour;
4 import java.text.DateFormat;
5 import java.util.Calendar;
6 import java.util.HashMap;
7 import java.util.TimeZone;
9 import tim.prune.data.DataPoint;
10 import tim.prune.data.Timestamp;
11 import tim.prune.data.Track;
12 import tim.prune.data.TrackInfo;
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)
19 public class DateColourer extends DiscretePointColourer
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();
26 * @param inStartColour start colour of scale
27 * @param inEndColour end colour of scale
28 * @param inWrapLength number of unique colours before wrap
30 public DateColourer(Color inStartColour, Color inEndColour, int inWrapLength)
32 super(inStartColour, inEndColour, inWrapLength);
36 * Calculate the colours for each of the points in the given track
37 * @param inTrackInfo track info object
40 public void calculateColours(TrackInfo inTrackInfo)
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();
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;
52 // loop over track points
54 for (int i=0; i<numPoints; i++)
56 DataPoint p = track.getPoint(i);
57 if (p != null && !p.isWaypoint())
59 dayIndex = 0; // default index 0 will be used if no date found
60 String date = getDate(p.getTimestamp());
63 // Check if it's the previous one
64 if (prevDate != null && date.equals(prevDate)) {
69 // Look up in the hashmap to see if it's been used before
70 Integer foundIndex = usedDates.get(date);
71 if (foundIndex == null)
73 // not been used before, so add it
74 dayIndex = usedDates.size() + 1;
75 usedDates.put(date, dayIndex);
80 dayIndex = foundIndex;
82 // Remember what we've got for the next point
87 // if date is null (no timestamp or invalid) then dayIndex remains 0
88 setColour(i, dayIndex);
91 // generate the colours needed
92 generateDiscreteColours(usedDates.size() + 1);
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
101 private static String getDate(Timestamp inTimestamp)
103 if (inTimestamp == null || !inTimestamp.isValid()) {
106 Calendar cal = inTimestamp.getCalendar();
107 // use system time zone
108 cal.setTimeZone(TimeZone.getDefault());
109 return DEFAULT_DATE_FORMAT.format(cal.getTime());