X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fdata%2FSelection.java;h=6924d133abca3c541a44d5bf257955a7e1bd3440;hb=112bb0c9b46894adca9a33ed8c99ea712b253185;hp=4849a4a42bcddb948172bf494f9180d5639eb9a0;hpb=5625a1abadb5f2ca5f017fe7dbda1d5141cb637b;p=GpsPrune.git diff --git a/tim/prune/data/Selection.java b/tim/prune/data/Selection.java index 4849a4a..6924d13 100644 --- a/tim/prune/data/Selection.java +++ b/tim/prune/data/Selection.java @@ -1,5 +1,6 @@ package tim.prune.data; +import tim.prune.DataSubscriber; import tim.prune.UpdateMessageBroker; /** @@ -9,85 +10,37 @@ 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; + private boolean _multipleSegments = false; /** * Constructor * @param inTrack track object - * @param inBroker broker object */ - public Selection(Track inTrack, UpdateMessageBroker inBroker) + public Selection(Track inTrack) { _track = inTrack; - _broker = inBroker; } /** - * Reset selection to be recalculated + * Mark selection invalid so it will be recalculated */ - private void reset() + public void markInvalid() { _valid = false; } - /** - * Select the point at the given index - * @param inIndex index number of selected point - */ - public void selectPoint(int inIndex) - { - if (inIndex >= -1) - { - _currentPoint = inIndex; - check(); - } - } - - /** - * Select the specified point and range in one go - * @param inPointIndex point selection - * @param inStart range start - * @param inEnd range end - */ - public void select(int inPointIndex, int inStart, int inEnd) - { - _currentPoint = inPointIndex; - _startIndex = inStart; - _endIndex = inEnd; - reset(); - check(); - } - - - /** - * Select the previous point - */ - public void selectPreviousPoint() - { - if (_currentPoint > 0) - selectPoint(_currentPoint - 1); - } - - /** - * Select the next point - */ - public void selectNextPoint() - { - selectPoint(_currentPoint + 1); - } - /** * @return the current point index */ @@ -111,7 +64,8 @@ public class Selection */ private void recalculate() { - _altitudeFormat = Altitude.FORMAT_NONE; + _altitudeFormat = Altitude.Format.NO_FORMAT; + _multipleSegments = false; if (_track.getNumPoints() > 0 && hasRangeSelected()) { _altitudeRange = new IntegerRange(); @@ -119,8 +73,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; @@ -132,7 +88,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) @@ -149,26 +105,33 @@ 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()) { + _multipleSegments = true; + } + else { + _angMovingDistance += radians; + } } lastPoint = currPoint; } } - if (endTime != null) - { - _seconds = endTime.getSecondsSince(startTime); - } - else - { - _seconds = 0L; + if (endTime != null) { + _totalSeconds = endTime.getSecondsSince(startTime); } } _valid = true; @@ -198,7 +161,7 @@ public class Selection /** * @return the altitude format, ie feet or metres */ - public int getAltitudeFormat() + public Altitude.Format getAltitudeFormat() { return _altitudeFormat; } @@ -238,38 +201,66 @@ 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.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.convertRadians(_angDistance, inUnits); + return Distance.convertRadiansToDistance(_angMovingDistance, inUnits); } + /** + * @return true if track has multiple segments + */ + public boolean getHasMultipleSegments() + { + return _multipleSegments; + } /** - * Clear selected point and range + * Clear selected point, range and photo */ public void clearAll() { _currentPoint = -1; - deselectRange(); - deselectPhoto(); + selectRange(-1, -1); + _currentPhotoIndex = -1; + check(); } /** * Deselect range + * @param inStartIndex index of start of range + * @param inEndIndex index of end of range */ - public void deselectRange() + public void selectRange(int inStartIndex, int inEndIndex) { - _startIndex = _endIndex = -1; - reset(); + _startIndex = inStartIndex; + _endIndex = inEndIndex; + markInvalid(); check(); } @@ -287,7 +278,7 @@ public class Selection * Set the index for the start of the range selection * @param inStartIndex start index */ - public void selectRangeStart(int inStartIndex) + private void selectRangeStart(int inStartIndex) { if (inStartIndex < 0) { @@ -302,8 +293,8 @@ public class Selection _endIndex = _track.getNumPoints() - 1; } } - reset(); - _broker.informSubscribers(); + markInvalid(); + UpdateMessageBroker.informSubscribers(); } @@ -330,13 +321,12 @@ public class Selection { _endIndex = inEndIndex; // Move start of selection to min if necessary - if (_startIndex > _endIndex || _startIndex < 0) - { + if (_startIndex > _endIndex || _startIndex < 0) { _startIndex = 0; } } - reset(); - _broker.informSubscribers(); + markInvalid(); + UpdateMessageBroker.informSubscribers(); } @@ -373,22 +363,12 @@ public class Selection _endIndex--; if (_currentPoint < _startIndex) _startIndex--; - reset(); + markInvalid(); } check(); } - /** - * Deselect photo - */ - public void deselectPhoto() - { - _currentPhotoIndex = -1; - check(); - } - - /** * Select the specified photo and point * @param inPhotoIndex index of selected photo in PhotoList @@ -397,16 +377,8 @@ public class Selection 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(); - } + _currentPoint = inPointIndex; + check(); } @@ -415,11 +387,9 @@ public class Selection */ public int getCurrentPhotoIndex() { - // System.out.println("Current photo index = " + _currentPhotoIndex); return _currentPhotoIndex; } - /** * Check that the selection still makes sense * and fire update message to listeners @@ -450,6 +420,6 @@ public class Selection _currentPoint = _startIndex = _endIndex = -1; } } - _broker.informSubscribers(); + UpdateMessageBroker.informSubscribers(DataSubscriber.SELECTION_CHANGED); } }