File: UndoCutAndMove.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (95 lines) | stat: -rw-r--r-- 3,109 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package tim.prune.undo;

import tim.prune.I18nManager;
import tim.prune.UpdateMessageBroker;
import tim.prune.data.DataPoint;
import tim.prune.data.Track;
import tim.prune.data.TrackInfo;

/**
 * Undo cut and move of a track section
 */
public class UndoCutAndMove implements UndoOperation
{
	/** Start and end indices of section */
	private int _startIndex, _endIndex;
	/** Index of move to point */
	private int _moveToIndex;
	/** First track point in section, next track point after where it was */
	private DataPoint _firstTrackPoint = null, _followingTrackPoint = null;
	/** Next track point after where it's being moved to */
	private DataPoint _moveTrackPoint = null;
	/** Segment flags for these points */
	private boolean _firstSegmentFlag, _followingSegmentFlag, _moveToSegmentFlag;


	/**
	 * Constructor
	 * @param inTrack track object for copying segment flags
	 * @param inStart start index of section
	 * @param inEnd end index of section
	 * @param inMoveTo index of moveTo point
	 */
	public UndoCutAndMove(Track inTrack, int inStart, int inEnd, int inMoveTo)
	{
		_startIndex = inStart;
		_endIndex = inEnd;
		_moveToIndex = inMoveTo;
		// Look for first track point in section to be moved, store flag
		_firstTrackPoint = inTrack.getNextTrackPoint(inStart);
		if (_firstTrackPoint != null) {
			_firstSegmentFlag = _firstTrackPoint.getSegmentStart();
		}
		// Look for following track point, store flag
		_followingTrackPoint = inTrack.getNextTrackPoint(inEnd + 1);
		if (_followingTrackPoint != null) {
			_followingSegmentFlag = _followingTrackPoint.getSegmentStart();
		}
		// Look for next track point after move point, store flag
		_moveTrackPoint = inTrack.getNextTrackPoint(inMoveTo);
		if (_moveTrackPoint != null) {
			_moveToSegmentFlag = _moveTrackPoint.getSegmentStart();
		}
	}


	/**
	 * @return description of operation including number of points moved
	 */
	public String getDescription()
	{
		return I18nManager.getText("undo.cutandmove") + " (" + (_endIndex - _startIndex + 1) + ")";
	}


	/**
	 * Perform the undo operation on the given Track
	 * @param inTrackInfo TrackInfo object on which to perform the operation
	 */
	public void performUndo(TrackInfo inTrackInfo) throws UndoException
	{
		// Cut and move the section back to where it was before
		int numMoved = _endIndex - _startIndex + 1;
		// Calculate new positions depending on whether section was moved forward or backward
		if (_startIndex > _moveToIndex)
		{
			inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex, _moveToIndex + numMoved - 1, _startIndex + numMoved);
		}
		else
		{
			inTrackInfo.getTrack().cutAndMoveSection(_moveToIndex - numMoved, _moveToIndex - 1, _startIndex);
		}
		// Restore segment start flags
		if (_firstTrackPoint != null) {
			_firstTrackPoint.setSegmentStart(_firstSegmentFlag);
		}
		if (_followingTrackPoint != null) {
			_followingTrackPoint.setSegmentStart(_followingSegmentFlag);
		}
		if (_moveTrackPoint != null) {
			_moveTrackPoint.setSegmentStart(_moveToSegmentFlag);
		}
		inTrackInfo.getSelection().clearAll();
		UpdateMessageBroker.informSubscribers();
	}
}