]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/gui/colour/DateColourer.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / gui / colour / DateColourer.java
index 5a6306b5fcfd8e71c96e0525d1bd84a463234fe7..6dfeca63d424a81df43e973be75c112db2f0932a 100644 (file)
@@ -6,20 +6,23 @@ import java.util.Calendar;
 import java.util.HashMap;
 import java.util.TimeZone;
 
+import tim.prune.config.Config;
 import tim.prune.data.DataPoint;
 import tim.prune.data.Timestamp;
 import tim.prune.data.Track;
 import tim.prune.data.TrackInfo;
 
 /**
- * Point colourer giving a different colour to each date
- * Uses the system timezone so may give funny results for
- * data from other timezones (eg far-away holidays)
+ * Point colourer giving a different colour to each date.
+ * Uses the currently selected timezone, so the results
+ * may be different when selecting a different timezone
  */
 public class DateColourer extends DiscretePointColourer
 {
        // Doesn't really matter what format is used here, as long as dates are different
        private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateInstance();
+       // Selected timezone for deciding which date a timestamp falls on
+       private TimeZone _selectedTimezone = null;
 
        /**
         * Constructor
@@ -37,8 +40,21 @@ public class DateColourer extends DiscretePointColourer
         * @param inTrackInfo track info object
         */
        @Override
-       public void calculateColours(TrackInfo inTrackInfo)
+       public synchronized void calculateColours(TrackInfo inTrackInfo)
        {
+               // Note, this method needs to be synchronized because otherwise the
+               // Calendar objects in the different threads get confused and the
+               // wrong colours are generated.
+
+               // TODO: Move this timezone-selection into a helper for use by others?
+               // Select the current timezone
+               final String zoneId = Config.getConfigString(Config.KEY_TIMEZONE_ID);
+               if (zoneId == null || zoneId.equals("")) {
+                       _selectedTimezone = TimeZone.getDefault();
+               }
+               else {
+                       _selectedTimezone = TimeZone.getTimeZone(zoneId);
+               }
                // initialise the array to the right size
                Track track = inTrackInfo == null ? null : inTrackInfo.getTrack();
                final int numPoints = track == null ? 0 : track.getNumPoints();
@@ -88,24 +104,25 @@ public class DateColourer extends DiscretePointColourer
                                setColour(i, dayIndex);
                        }
                }
+
                // generate the colours needed
                generateDiscreteColours(usedDates.size() + 1);
        }
 
 
        /**
-        * Find which date (in the system timezone) the given timestamp falls on
+        * Find which date (in the currently selected timezone) the given timestamp falls on
         * @param inTimestamp timestamp
         * @return String containing description of date, or null
         */
-       private static String getDate(Timestamp inTimestamp)
+       private String getDate(Timestamp inTimestamp)
        {
                if (inTimestamp == null || !inTimestamp.isValid()) {
                        return null;
                }
-               Calendar cal = inTimestamp.getCalendar();
-               // use system time zone
-               cal.setTimeZone(TimeZone.getDefault());
+               Calendar cal = inTimestamp.getCalendar(null);
+               // use selected time zone, not system one
+               DEFAULT_DATE_FORMAT.setTimeZone(_selectedTimezone);
                return DEFAULT_DATE_FORMAT.format(cal.getTime());
        }
 }