]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/data/Selection.java
Version 7, February 2009
[GpsPrune.git] / tim / prune / data / Selection.java
index c375e820b4a05b022682deef2b675ee27a82b8d8..c07c1a91e3aea422e5b469bef027afe4b2db3247 100644 (file)
@@ -1,5 +1,6 @@
 package tim.prune.data;
 
+import tim.prune.DataSubscriber;
 import tim.prune.UpdateMessageBroker;
 
 /**
@@ -9,26 +10,24 @@ import tim.prune.UpdateMessageBroker;
 public class Selection
 {
        private Track _track = null;
-       private UpdateMessageBroker _broker = null;
        private int _currentPoint = -1;
        private boolean _valid = false;
        private int _startIndex = -1, _endIndex = -1;
+       private int _currentPhotoIndex = -1;
        private IntegerRange _altitudeRange = null;
        private int _climb = -1, _descent = -1;
-       private int _altitudeFormat = Altitude.FORMAT_NONE;
-       private long _seconds = 0L;
-       private double _angDistance = -1.0; //, _averageSpeed = -1.0;
+       private Altitude.Format _altitudeFormat = Altitude.Format.NO_FORMAT;
+       private long _totalSeconds = 0L, _movingSeconds = 0L;
+       private double _angDistance = -1.0, _angMovingDistance = -1.0;
 
 
        /**
         * Constructor
         * @param inTrack track object
-        * @param inBroker broker object
         */
-       public Selection(Track inTrack, UpdateMessageBroker inBroker)
+       public Selection(Track inTrack)
        {
                _track = inTrack;
-               _broker = inBroker;
        }
 
 
@@ -110,7 +109,7 @@ public class Selection
         */
        private void recalculate()
        {
-               _altitudeFormat = Altitude.FORMAT_NONE;
+               _altitudeFormat = Altitude.Format.NO_FORMAT;
                if (_track.getNumPoints() > 0 && hasRangeSelected())
                {
                        _altitudeRange = new IntegerRange();
@@ -118,8 +117,10 @@ public class Selection
                        _descent = 0;
                        Altitude altitude = null;
                        Timestamp time = null, startTime = null, endTime = null;
+                       Timestamp previousTime = null;
                        DataPoint lastPoint = null, currPoint = null;
-                       _angDistance = 0.0;
+                       _angDistance = 0.0; _angMovingDistance = 0.0;
+                       _totalSeconds = 0L; _movingSeconds = 0L;
                        int altValue = 0;
                        int lastAltValue = 0;
                        boolean foundAlt = false;
@@ -131,7 +132,7 @@ public class Selection
                                if (!currPoint.isWaypoint() && altitude.isValid())
                                {
                                        altValue = altitude.getValue(_altitudeFormat);
-                                       if (_altitudeFormat == Altitude.FORMAT_NONE)
+                                       if (_altitudeFormat == Altitude.Format.NO_FORMAT)
                                                _altitudeFormat = altitude.getFormat();
                                        _altitudeRange.addValue(altValue);
                                        if (foundAlt)
@@ -148,26 +149,30 @@ public class Selection
                                time = currPoint.getTimestamp();
                                if (time.isValid())
                                {
-                                       if (startTime == null) startTime = time;
-                                       endTime = time;
+                                       if (startTime == null || startTime.isAfter(time)) startTime = time;
+                                       if (endTime == null || time.isAfter(endTime)) endTime = time;
+                                       // add moving time
+                                       if (!currPoint.getSegmentStart() && previousTime != null && time.isAfter(previousTime)) {
+                                               _movingSeconds += time.getSecondsSince(previousTime);
+                                       }
+                                       previousTime = time;
                                }
                                // Calculate distances, again ignoring waypoints
                                if (!currPoint.isWaypoint())
                                {
                                        if (lastPoint != null)
                                        {
-                                               _angDistance += DataPoint.calculateRadiansBetween(lastPoint, currPoint);
+                                               double radians = DataPoint.calculateRadiansBetween(lastPoint, currPoint);
+                                               _angDistance += radians;
+                                               if (!currPoint.getSegmentStart()) {
+                                                       _angMovingDistance += radians;
+                                               }
                                        }
                                        lastPoint = currPoint;
                                }
                        }
-                       if (endTime != null)
-                       {
-                               _seconds = endTime.getSecondsSince(startTime);
-                       }
-                       else
-                       {
-                               _seconds = 0L;
+                       if (endTime != null) {
+                               _totalSeconds = endTime.getSecondsSince(startTime);
                        }
                }
                _valid = true;
@@ -197,7 +202,7 @@ public class Selection
        /**
         * @return the altitude format, ie feet or metres
         */
-       public int getAltitudeFormat()
+       public Altitude.Format getAltitudeFormat()
        {
                return _altitudeFormat;
        }
@@ -237,19 +242,35 @@ public class Selection
        public long getNumSeconds()
        {
                if (!_valid) recalculate();
-               return _seconds;
+               return _totalSeconds;
        }
 
+       /**
+        * @return number of seconds spanned by segments within selection
+        */
+       public long getMovingSeconds()
+       {
+               if (!_valid) recalculate();
+               return _movingSeconds;
+       }
 
        /**
-        * @param inFormat distance units to use, from class Distance
+        * @param inUnits distance units to use, from class Distance
         * @return distance of Selection in specified units
         */
-       public double getDistance(int inUnits)
+       public double getDistance(Distance.Units inUnits)
        {
-               return Distance.convertRadians(_angDistance, inUnits);
+               return Distance.convertRadiansToDistance(_angDistance, inUnits);
        }
 
+       /**
+        * @param inUnits distance units to use, from class Distance
+        * @return moving distance of Selection in specified units
+        */
+       public double getMovingDistance(Distance.Units inUnits)
+       {
+               return Distance.convertRadiansToDistance(_angMovingDistance, inUnits);
+       }
 
        /**
         * Clear selected point and range
@@ -258,6 +279,7 @@ public class Selection
        {
                _currentPoint = -1;
                deselectRange();
+               deselectPhoto();
        }
 
 
@@ -301,7 +323,7 @@ public class Selection
                        }
                }
                reset();
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
        }
 
 
@@ -334,7 +356,7 @@ public class Selection
                        }
                }
                reset();
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers();
        }
 
 
@@ -377,28 +399,75 @@ public class Selection
        }
 
 
+       /**
+        * Deselect photo
+        */
+       public void deselectPhoto()
+       {
+               _currentPhotoIndex = -1;
+               check();
+       }
+
+
+       /**
+        * Select the specified photo and point
+        * @param inPhotoIndex index of selected photo in PhotoList
+        * @param inPointIndex index of selected point
+        */
+       public void selectPhotoAndPoint(int inPhotoIndex, int inPointIndex)
+       {
+               _currentPhotoIndex = inPhotoIndex;
+               if (inPointIndex > -1)
+               {
+                       // select associated point, if any
+                       selectPoint(inPointIndex);
+               }
+               else
+               {
+                       // Check if not already done
+                       check();
+               }
+       }
+
+
+       /**
+        * @return currently selected photo index
+        */
+       public int getCurrentPhotoIndex()
+       {
+               return _currentPhotoIndex;
+       }
+
        /**
         * Check that the selection still makes sense
         * and fire update message to listeners
         */
        private void check()
        {
-               if (_track != null && _track.getNumPoints() > 0)
+               if (_track != null)
                {
-                       int maxIndex = _track.getNumPoints() - 1;
-                       if (_currentPoint > maxIndex)
-                       {
-                               _currentPoint = maxIndex;
-                       }
-                       if (_endIndex > maxIndex)
+                       if (_track.getNumPoints() > 0)
                        {
-                               _endIndex = maxIndex;
+                               int maxIndex = _track.getNumPoints() - 1;
+                               if (_currentPoint > maxIndex)
+                               {
+                                       _currentPoint = maxIndex;
+                               }
+                               if (_endIndex > maxIndex)
+                               {
+                                       _endIndex = maxIndex;
+                               }
+                               if (_startIndex > maxIndex)
+                               {
+                                       _startIndex = maxIndex;
+                               }
                        }
-                       if (_startIndex > maxIndex)
+                       else
                        {
-                               _startIndex = maxIndex;
+                               // track is empty, clear selections
+                               _currentPoint = _startIndex = _endIndex = -1;
                        }
                }
-               _broker.informSubscribers();
+               UpdateMessageBroker.informSubscribers(DataSubscriber.SELECTION_CHANGED);
        }
 }