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 _startIndex = inStart;
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();
43 // Look for following track point, store flag
44 _followingTrackPoint = inTrack.getNextTrackPoint(inEnd + 1);
45 if (_followingTrackPoint != null) {
46 _followingSegmentFlag = _followingTrackPoint.getSegmentStart();
48 // Look for next track point after move point, store flag
49 _moveTrackPoint = inTrack.getNextTrackPoint(inMoveTo);
50 if (_moveTrackPoint != null) {
51 _moveToSegmentFlag = _moveTrackPoint.getSegmentStart();
57 * @return description of operation including number of points moved
59 public String getDescription()
61 return I18nManager.getText("undo.cutandmove") + " (" + (_endIndex - _startIndex + 1) + ")";
66 * Perform the undo operation on the given Track
67 * @param inTrackInfo TrackInfo object on which to perform the operation
69 public void performUndo(TrackInfo inTrackInfo) throws UndoException
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)
76 inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex, _moveToIndex + numMoved - 1, _startIndex + numMoved);
80 inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex - numMoved, _moveToIndex - 1, _startIndex);
82 // Restore segment start flags
83 if (_firstTrackPoint != null) {
84 _firstTrackPoint.setSegmentStart(_firstSegmentFlag);
86 if (_followingTrackPoint != null) {
87 _followingTrackPoint.setSegmentStart(_followingSegmentFlag);
89 if (_moveTrackPoint != null) {
90 _moveTrackPoint.setSegmentStart(_moveToSegmentFlag);
92 UpdateMessageBroker.informSubscribers();