]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/undo/UndoCutAndMove.java
Version 6, October 2008
[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                 // System.out.println("Construct undo with params " + inStart + ", "+  inEnd + ", " + inMoveTo);
36                 _startIndex = inStart;
37                 _endIndex = inEnd;
38                 _moveToIndex = inMoveTo;
39                 // Look for first track point in section to be moved, store flag
40                 _firstTrackPoint = inTrack.getNextTrackPoint(inStart);
41                 if (_firstTrackPoint != null) {
42                         _firstSegmentFlag = _firstTrackPoint.getSegmentStart();
43                 }
44                 // Look for following track point, store flag
45                 _followingTrackPoint = inTrack.getNextTrackPoint(inEnd + 1);
46                 if (_followingTrackPoint != null) {
47                         _followingSegmentFlag = _followingTrackPoint.getSegmentStart();
48                 }
49                 // Look for next track point after move point, store flag
50                 _moveTrackPoint = inTrack.getNextTrackPoint(inMoveTo);
51                 if (_moveTrackPoint != null) {
52                         _moveToSegmentFlag = _moveTrackPoint.getSegmentStart();
53                 }
54         }
55
56
57         /**
58          * @return description of operation including number of points moved
59          */
60         public String getDescription()
61         {
62                 return I18nManager.getText("undo.cutandmove") + " (" + (_endIndex - _startIndex + 1) + ")";
63         }
64
65
66         /**
67          * Perform the undo operation on the given Track
68          * @param inTrackInfo TrackInfo object on which to perform the operation
69          */
70         public void performUndo(TrackInfo inTrackInfo) throws UndoException
71         {
72                 // Cut and move the section back to where it was before
73                 int numMoved = _endIndex - _startIndex + 1;
74                 // Calculate new positions depending on whether section was moved forward or backward
75                 if (_startIndex > _moveToIndex)
76                 {
77                         inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex, _moveToIndex + numMoved - 1, _startIndex + numMoved);
78                 }
79                 else
80                 {
81                         inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex - numMoved, _moveToIndex - 1, _startIndex);
82                 }
83                 // Restore segment start flags
84                 if (_firstTrackPoint != null) {
85                         _firstTrackPoint.setSegmentStart(_firstSegmentFlag);
86                 }
87                 if (_followingTrackPoint != null) {
88                         _followingTrackPoint.setSegmentStart(_followingSegmentFlag);
89                 }
90                 if (_moveTrackPoint != null) {
91                         _moveTrackPoint.setSegmentStart(_moveToSegmentFlag);
92                 }
93                 UpdateMessageBroker.informSubscribers();
94         }
95 }