]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/undo/UndoReverseSection.java
Version 5, May 2008
[GpsPrune.git] / tim / prune / undo / UndoReverseSection.java
1 package tim.prune.undo;
2
3 import tim.prune.I18nManager;
4 import tim.prune.data.DataPoint;
5 import tim.prune.data.Track;
6 import tim.prune.data.TrackInfo;
7
8 /**
9  * Undo reversal of track section
10  */
11 public class UndoReverseSection implements UndoOperation
12 {
13         /** Start and end indices of section */
14         private int _startIndex, _endIndex;
15         /** First and last track point in section and next track point after */
16         private DataPoint _firstTrackPoint, _lastTrackPoint, _nextTrackPoint;
17         /** Segment flags for these points */
18         private boolean _firstSegmentFlag, _lastSegmentFlag, _nextSegmentFlag;
19
20
21         /**
22          * Constructor
23          * @param inTrack track object for copying segment flags
24          * @param inStart start index of section
25          * @param inEnd end index of section
26          */
27         public UndoReverseSection(Track inTrack, int inStart, int inEnd)
28         {
29                 _startIndex = inStart;
30                 _endIndex = inEnd;
31                 // Look for first track point in section to be reversed, store flag
32                 _firstTrackPoint = inTrack.getNextTrackPoint(inStart);
33                 if (_firstTrackPoint != null) {
34                         _firstSegmentFlag = _firstTrackPoint.getSegmentStart();
35                 }
36                 // Look for last track point in section to be reversed, store flag
37                 _lastTrackPoint = inTrack.getPreviousTrackPoint(inEnd);
38                 if (_lastTrackPoint != null) {
39                         _lastSegmentFlag = _lastTrackPoint.getSegmentStart();
40                 }
41                 // Look for following track point, store flag
42                 _nextTrackPoint = inTrack.getNextTrackPoint(inEnd + 1);
43                 if (_nextTrackPoint != null) {
44                         _nextSegmentFlag = _nextTrackPoint.getSegmentStart();
45                 }
46         }
47
48
49         /**
50          * @return description of operation
51          */
52         public String getDescription()
53         {
54                 return I18nManager.getText("undo.reverse");
55         }
56
57
58         /**
59          * Perform the undo operation on the given Track
60          * @param inTrackInfo TrackInfo object on which to perform the operation
61          */
62         public void performUndo(TrackInfo inTrackInfo) throws UndoException
63         {
64                 if (!inTrackInfo.getTrack().reverseRange(_startIndex, _endIndex))
65                 {
66                         throw new UndoException(getDescription());
67                 }
68                 // Restore segment start flags
69                 if (_firstTrackPoint != null) {
70                         _firstTrackPoint.setSegmentStart(_firstSegmentFlag);
71                 }
72                 if (_lastTrackPoint != null) {
73                         _lastTrackPoint.setSegmentStart(_lastSegmentFlag);
74                 }
75                 if (_nextTrackPoint != null) {
76                         _nextTrackPoint.setSegmentStart(_nextSegmentFlag);
77                 }
78         }
79 }