File: EditPointCmd.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 (92 lines) | stat: -rw-r--r-- 2,572 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
package tim.prune.cmd;

import tim.prune.DataSubscriber;
import tim.prune.data.DataPoint;
import tim.prune.data.Field;
import tim.prune.data.Photo;
import tim.prune.data.TrackInfo;
import tim.prune.data.UnitSet;
import tim.prune.function.edit.FieldEdit;

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

/**
 * Command to edit a single point with one or more fields
 */
public class EditPointCmd extends Command
{
	private final int _pointIndex;
	private final List<FieldEdit> _editList;
	private final UnitSet _unitSet;


	public EditPointCmd(int inPointIndex, List<FieldEdit> inEditList) {
		this(inPointIndex, inEditList, null);
	}

	public EditPointCmd(int inPointIndex, FieldEdit inEdit) {
		this(inPointIndex, List.of(inEdit), null);
	}

	public EditPointCmd(int inPointIndex, List<FieldEdit> inEditList, UnitSet inUnitSet) {
		this(null, inPointIndex, inEditList, inUnitSet);
	}

	protected EditPointCmd(EditPointCmd inParent, int inPointIndex, List<FieldEdit> inEditList, UnitSet inUnitSet)
	{
		super(inParent);
		_pointIndex = inPointIndex;
		_editList = inEditList;
		_unitSet = inUnitSet;
	}

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

	@Override
	protected boolean executeCommand(TrackInfo inInfo)
	{
		DataPoint point = inInfo.getTrack().getPoint(_pointIndex);
		if (point == null) {
			return false;
		}
		boolean coordsChanged = false;
		for (FieldEdit edit : _editList)
		{
			Field field = edit.getField();
			point.setFieldValue(field, edit.getValue(), _unitSet, isUndo());
			inInfo.getTrack().getFieldList().addField(field);
			coordsChanged |= (field.equals(Field.LATITUDE)
				|| field.equals(Field.LONGITUDE) || field.equals(Field.ALTITUDE));
		}
		// set photo status if coordinates have changed
		if (coordsChanged)
		{
			if (point.getPhoto() != null) {
				point.getPhoto().setCurrentStatus(Photo.Status.CONNECTED);
			}
		}
		inInfo.getTrack().requestRescale();
		return true;
	}

	@Override
	protected Command makeInverse(TrackInfo inInfo)
	{
		DataPoint pointToEdit = inInfo.getTrack().getPoint(_pointIndex);
		return new EditPointCmd(this, _pointIndex, makeOppositeEdits(pointToEdit), _unitSet);
	}

	private List<FieldEdit> makeOppositeEdits(DataPoint pointToEdit)
	{
		List<FieldEdit> opposite = new ArrayList<>();
		for (FieldEdit fieldEdit : _editList) {
			final Field field = fieldEdit.getField();
			opposite.add(new FieldEdit(field, pointToEdit.getFieldValue(field)));
		}
		return opposite;
	}
}