]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/undo/UndoCutAndMove.java
2a6f9ca10d56cd936861d3426fd6dbb10c92d9c3
[GpsPrune.git] / tim / prune / undo / UndoCutAndMove.java
1 package tim.prune.undo;
2
3 import tim.prune.I18nManager;
4 import tim.prune.UpdateMessageBroker;
5 import tim.prune.data.DataPoint;
6 import tim.prune.data.Track;
7 import tim.prune.data.TrackInfo;
8
9 /**
10  * Undo cut and move of a track section
11  */
12 public class UndoCutAndMove implements UndoOperation
13 {
14         /** Start and end indices of section */
15         private int _startIndex, _endIndex;
16         /** Index of move to point */
17         private int _moveToIndex;
18         /** First track point in section, next track point after where it was */
19         private DataPoint _firstTrackPoint = null, _followingTrackPoint = null;
20         /** Next track point after where it's being moved to */
21         private DataPoint _moveTrackPoint = null;
22         /** Segment flags for these points */
23         private boolean _firstSegmentFlag, _followingSegmentFlag, _moveToSegmentFlag;
24
25
26         /**
27          * Constructor
28          * @param inTrack track object for copying segment flags
29          * @param inStart start index of section
30          * @param inEnd end index of section
31          * @param inMoveTo index of moveTo point
32          */
33         public UndoCutAndMove(Track inTrack, int inStart, int inEnd, int inMoveTo)
34         {
35                 _startIndex = inStart;
36                 _endIndex = inEnd;
37                 _moveToIndex = inMoveTo;
38                 // Look for first track point in section to be moved, store flag
39                 _firstTrackPoint = inTrack.getNextTrackPoint(inStart);
40                 if (_firstTrackPoint != null) {
41                         _firstSegmentFlag = _firstTrackPoint.getSegmentStart();
42                 }
43                 // Look for following track point, store flag
44                 _followingTrackPoint = inTrack.getNextTrackPoint(inEnd + 1);
45                 if (_followingTrackPoint != null) {
46                         _followingSegmentFlag = _followingTrackPoint.getSegmentStart();
47                 }
48                 // Look for next track point after move point, store flag
49                 _moveTrackPoint = inTrack.getNextTrackPoint(inMoveTo);
50                 if (_moveTrackPoint != null) {
51                         _moveToSegmentFlag = _moveTrackPoint.getSegmentStart();
52                 }
53         }
54
55
56         /**
57          * @return description of operation including number of points moved
58          */
59         public String getDescription()
60         {
61                 return I18nManager.getText("undo.cutandmove") + " (" + (_endIndex - _startIndex + 1) + ")";
62         }
63
64
65         /**
66          * Perform the undo operation on the given Track
67          * @param inTrackInfo TrackInfo object on which to perform the operation
68          */
69         public void performUndo(TrackInfo inTrackInfo) throws UndoException
70         {
71                 // Cut and move the section back to where it was before
72                 int numMoved = _endIndex - _startIndex + 1;
73                 // Calculate new positions depending on whether section was moved forward or backward
74                 if (_startIndex > _moveToIndex)
75                 {
76                         inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex, _moveToIndex + numMoved - 1, _startIndex + numMoved);
77                 }
78                 else
79                 {
80                         inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex - numMoved, _moveToIndex - 1, _startIndex);
81                 }
82                 // Restore segment start flags
83                 if (_firstTrackPoint != null) {
84                         _firstTrackPoint.setSegmentStart(_firstSegmentFlag);
85                 }
86                 if (_followingTrackPoint != null) {
87                         _followingTrackPoint.setSegmentStart(_followingSegmentFlag);
88                 }
89                 if (_moveTrackPoint != null) {
90                         _moveTrackPoint.setSegmentStart(_moveToSegmentFlag);
91                 }
92                 inTrackInfo.getSelection().clearAll();
93                 UpdateMessageBroker.informSubscribers();
94         }
95 }