File: CompoundCommand.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 (103 lines) | stat: -rw-r--r-- 2,326 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
98
99
100
101
102
103
package tim.prune.cmd;

import tim.prune.data.TrackInfo;

import java.util.ArrayList;

public class CompoundCommand extends Command
{
	private final ArrayList<Command> _commands = new ArrayList<>();
	private final int _updateFlags;

	public CompoundCommand() {
		this(0);
	}

	public CompoundCommand(int inUpdateFlags)
	{
		super(null);
		_updateFlags = inUpdateFlags;
	}

	protected CompoundCommand(CompoundCommand inParent)
	{
		super(inParent);
		_updateFlags = 0;
	}

	/**
	 * @param inCommand command to add to the list
	 */
	public CompoundCommand addCommand(Command inCommand)
	{
		if (inCommand != null) {
			_commands.add(inCommand);
		}
		return this;
	}

	@Override
	public int getUpdateFlags()
	{
		int flags = _updateFlags;
		for (Command command : _commands) {
			flags |= command.getUpdateFlags();
		}
		return flags;
	}

	@Override
	protected boolean executeCommand(TrackInfo inInfo)
	{
		boolean success = true;
		for (Command command : _commands) {
			success &= command.executeCommand(inInfo);
		}
		return success;
	}

	@Override
	protected Command makeInverse(TrackInfo inInfo)
	{
		CompoundCommand undo = new CompoundCommand(this);
		// Undo in the opposite order
		for (int i=_commands.size()-1; i>= 0; i--)
		{
			Command command = _commands.get(i);
			Command opposite = command.makeInverse(inInfo);
			command.setInverse(opposite);
			undo.addCommand(opposite);
		}
		return undo;
	}

	/**
	 * Make the inverse command based on subcommands specified by subclass
	 * @param inInfo track info
	 * @param inInverses list of inverses supplied by subclass
	 * @return inverse of compound
	 */
	protected CompoundCommand makeInverse(TrackInfo inInfo, Command ... inInverses)
	{
		CompoundCommand inverse = new CompoundCommand(this);
		// Undo in the opposite order
		for (int i=_commands.size()-1; i>= 0; i--)
		{
			Command command = _commands.get(i);
			Command opposite = inInverses[i];
			if (opposite == null) {
				opposite = command.makeInverse(inInfo);
			}
			command.setInverse(opposite);
			inverse.addCommand(opposite);
		}
		return inverse;
	}

	/**
	 * Allow CompoundCommands to get their subcommands
	 */
	protected Command getCommand(int inIndex) {
		return _commands.get(inIndex);
	}
}