File: EditAltitudeCmd.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (97 lines) | stat: -rw-r--r-- 2,548 bytes parent folder | download | duplicates (4)
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
96
97
package tim.prune.cmd;

import tim.prune.DataSubscriber;
import tim.prune.data.DataPoint;
import tim.prune.data.Field;
import tim.prune.data.Track;
import tim.prune.data.TrackInfo;
import tim.prune.data.Unit;
import tim.prune.function.edit.PointAltitudeEdit;

import java.util.ArrayList;
import java.util.List;

/**
 * Command to edit the altitude values of one or more points
 */
public class EditAltitudeCmd extends Command
{
	/** List of all the edits to be made */
	private final List<PointAltitudeEdit> _editList;

	/**
	 * Constructor
	 * @param inEditList list of edits
	 */
	public EditAltitudeCmd(List<PointAltitudeEdit> inEditList) {
		this(null, inEditList);
	}

	/**
	 * Constructor giving parent as well (to construct an Undo)
	 * @param inParent parent command
	 * @param inEditList edit list
	 */
	protected EditAltitudeCmd(EditAltitudeCmd inParent, List<PointAltitudeEdit> inEditList)
	{
		super(inParent);
		_editList = inEditList;
	}

	@Override
	public int getUpdateFlags() {
		return DataSubscriber.DATA_EDITED;
	}

	@Override
	protected boolean executeCommand(TrackInfo inInfo) {
		return executeCommand(inInfo.getTrack());
	}

	/**
	 * Additional entry point when the App isn't involved
	 * @param inTrack track object (for terrain data)
	 * @return true on success
	 */
	public boolean executeCommand(Track inTrack)
	{
		for (PointAltitudeEdit edit : _editList)
		{
			DataPoint point = inTrack.getPoint(edit.getPointIndex());
			point.setAltitude(edit.getValue(), edit.getUnit(), isUndo());
		}
		inTrack.requestRescale();
		return !_editList.isEmpty();
	}

	@Override
	protected Command makeInverse(TrackInfo inInfo) {
		return new EditAltitudeCmd(this, makeOppositeEdits(inInfo.getTrack()));
	}

	/**
	 * Copy the altitudes from the current track to make an Undo command
	 * @param inTrack track object
	 * @return reverse edit list
	 */
	private List<PointAltitudeEdit> makeOppositeEdits(Track inTrack)
	{
		List<PointAltitudeEdit> opposite = new ArrayList<>();
		for (PointAltitudeEdit edit : _editList)
		{
			DataPoint point = inTrack.getPoint(edit.getPointIndex());
			final String currValue;
			final Unit currUnit;
			if (point.hasAltitude()) {
				currValue = point.getFieldValue(Field.ALTITUDE);
				currUnit = point.getAltitude().getUnit();
			}
			else {
				currValue = null;
				currUnit = null;
			}
			opposite.add(new PointAltitudeEdit(edit.getPointIndex(), currValue, currUnit));
		}
		return opposite;
	}
}