1 package tim.prune.data;
3 import tim.prune.DataSubscriber;
4 import tim.prune.UpdateMessageBroker;
7 * Class to represent a selected portion of a Track
10 public class Selection
12 private Track _track = null;
13 private int _currentPoint = -1;
14 private int _prevNumPoints = 0;
15 private int _startIndex = -1, _endIndex = -1;
16 private int _currentPhotoIndex = -1;
17 private int _currentAudioIndex = -1;
18 private RangeStats _rangeStats = null;
23 * @param inTrack track object
25 public Selection(Track inTrack)
32 * Mark selection invalid so it will be recalculated
34 public void markInvalid()
41 * @return the current point index
43 public int getCurrentPointIndex()
50 * @return true if range is selected
52 public boolean hasRangeSelected()
54 return _startIndex >= 0 && _endIndex > _startIndex;
59 * Recalculate all selection details
61 private void recalculate()
63 if (_rangeStats != null) {
66 final int numPoints = _track.getNumPoints();
67 // Recheck if the number of points has changed
68 if (numPoints != _prevNumPoints)
70 _prevNumPoints = numPoints;
73 if (numPoints > 0 && hasRangeSelected())
75 _rangeStats = new RangeStats(_track, _startIndex, _endIndex);
79 _rangeStats = new RangeStats();
104 * @return altitude range
106 public AltitudeRange getAltitudeRange()
109 return _rangeStats.getTotalAltitudeRange();
114 * @return number of seconds spanned by segments within selection
116 public long getMovingSeconds()
119 return _rangeStats.getMovingDurationInSeconds();
123 * @return moving distance of Selection in current units
125 public double getMovingDistance()
127 return _rangeStats.getMovingDistance();
131 * Clear selected point, range, photo and audio
133 public void clearAll()
137 _currentPhotoIndex = -1;
138 _currentAudioIndex = -1;
145 * Select range from start to end
146 * @param inStartIndex index of start of range
147 * @param inEndIndex index of end of range
149 public void selectRange(int inStartIndex, int inEndIndex)
151 _startIndex = inStartIndex;
152 _endIndex = inEndIndex;
159 * Select the range from the current point
161 public void selectRangeStart()
163 selectRangeStart(_currentPoint);
168 * Set the index for the start of the range selection
169 * @param inStartIndex start index
171 private void selectRangeStart(int inStartIndex)
173 if (inStartIndex < 0)
175 _startIndex = _endIndex = -1;
179 _startIndex = inStartIndex;
180 // Move end of selection to max if necessary
181 if (_endIndex <= _startIndex)
183 _endIndex = _track.getNumPoints() - 1;
187 UpdateMessageBroker.informSubscribers();
192 * Select the range up to the current point
194 public void selectRangeEnd()
196 selectRangeEnd(_currentPoint);
201 * Set the index for the end of the range selection
202 * @param inEndIndex end index
204 public void selectRangeEnd(int inEndIndex)
208 _startIndex = _endIndex = -1;
212 _endIndex = inEndIndex;
213 // Move start of selection to min if necessary
214 if (_startIndex > _endIndex || _startIndex < 0) {
219 UpdateMessageBroker.informSubscribers();
224 * Modify the selection given that the selected range has been deleted
226 public void modifyRangeDeleted()
228 // Modify current point, if any
229 if (_currentPoint > _endIndex)
231 _currentPoint -= (_endIndex - _startIndex);
233 else if (_currentPoint > _startIndex)
235 _currentPoint = _startIndex;
237 // Clear selected range
238 _startIndex = _endIndex = -1;
239 // Check for consistency and fire update
246 * Modify the selection when a point is deleted
248 public void modifyPointDeleted()
250 // current point index doesn't change, just gets checked
251 // range needs to get altered if deleted point is inside or before
252 if (hasRangeSelected() && _currentPoint <= _endIndex)
255 if (_currentPoint < _startIndex)
264 * Select the specified photo and point
265 * @param inPointIndex index of selected point
266 * @param inPhotoIndex index of selected photo in PhotoList
267 * @param inAudioIndex index of selected audio item
269 public void selectPointPhotoAudio(int inPointIndex, int inPhotoIndex, int inAudioIndex)
271 _currentPoint = inPointIndex;
272 _currentPhotoIndex = inPhotoIndex;
273 _currentAudioIndex = inAudioIndex;
279 * @return currently selected photo index
281 public int getCurrentPhotoIndex()
283 return _currentPhotoIndex;
287 * @return currently selected audio index
289 public int getCurrentAudioIndex()
291 return _currentAudioIndex;
295 * Check that the selection still makes sense
296 * and fire update message to listeners
300 if (_track != null && _track.getNumPoints() > 0)
302 int maxIndex = _track.getNumPoints() - 1;
303 if (_currentPoint > maxIndex)
305 _currentPoint = maxIndex;
307 if (_endIndex > maxIndex)
309 _endIndex = maxIndex;
311 if (_startIndex > maxIndex)
313 _startIndex = maxIndex;
318 // track is empty, clear selections
319 _currentPoint = _startIndex = _endIndex = -1;
322 UpdateMessageBroker.informSubscribers(DataSubscriber.SELECTION_CHANGED);