1 package tim.prune.undo;
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;
10 * Undo cut and move of a track section
12 public class UndoCutAndMove implements UndoOperation
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;
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
33 public UndoCutAndMove(Track inTrack, int inStart, int inEnd, int inMoveTo)
35 // System.out.println("Construct undo with params " + inStart + ", "+ inEnd + ", " + inMoveTo);
36 _startIndex = inStart;
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();
44 // Look for following track point, store flag
45 _followingTrackPoint = inTrack.getNextTrackPoint(inEnd + 1);
46 if (_followingTrackPoint != null) {
47 _followingSegmentFlag = _followingTrackPoint.getSegmentStart();
49 // Look for next track point after move point, store flag
50 _moveTrackPoint = inTrack.getNextTrackPoint(inMoveTo);
51 if (_moveTrackPoint != null) {
52 _moveToSegmentFlag = _moveTrackPoint.getSegmentStart();
58 * @return description of operation including number of points moved
60 public String getDescription()
62 return I18nManager.getText("undo.cutandmove") + " (" + (_endIndex - _startIndex + 1) + ")";
67 * Perform the undo operation on the given Track
68 * @param inTrackInfo TrackInfo object on which to perform the operation
70 public void performUndo(TrackInfo inTrackInfo) throws UndoException
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)
77 inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex, _moveToIndex + numMoved - 1, _startIndex + numMoved);
81 inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex - numMoved, _moveToIndex - 1, _startIndex);
83 // Restore segment start flags
84 if (_firstTrackPoint != null) {
85 _firstTrackPoint.setSegmentStart(_firstSegmentFlag);
87 if (_followingTrackPoint != null) {
88 _followingTrackPoint.setSegmentStart(_followingSegmentFlag);
90 if (_moveTrackPoint != null) {
91 _moveTrackPoint.setSegmentStart(_moveToSegmentFlag);
93 UpdateMessageBroker.informSubscribers();