1 | package org.openstreetmap.josm.command;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.HashSet;
|
---|
5 | import java.util.Map.Entry;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
8 | import org.openstreetmap.josm.data.osm.visitor.CloneVisitor;
|
---|
9 |
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Classes implementing Command modify a dataset in a specific way. A command is
|
---|
13 | * one atomic action on a specific dataset, such as move or delete.
|
---|
14 | *
|
---|
15 | * Remember, that the command must be executable and undoable, even if the
|
---|
16 | * Main.main.ds has changed, so the command must save the dataset it operates on
|
---|
17 | * if necessary.
|
---|
18 | *
|
---|
19 | * @author imi
|
---|
20 | */
|
---|
21 | abstract public class Command {
|
---|
22 |
|
---|
23 | private CloneVisitor orig;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Executes the command on the dataset. This implementation will remember all
|
---|
27 | * primitives returned by fillModifiedData for restoring them on undo.
|
---|
28 | */
|
---|
29 | public void executeCommand() {
|
---|
30 | orig = new CloneVisitor();
|
---|
31 | Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
|
---|
32 | fillModifiedData(all, all, all);
|
---|
33 | for (OsmPrimitive osm : all)
|
---|
34 | osm.visit(orig);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Undoes the command.
|
---|
39 | * It can be assumed, that all objects are in the same state they were before.
|
---|
40 | * It can also be assumed that executeCommand was called exactly once before.
|
---|
41 | *
|
---|
42 | * This implementation undoes all objects stored by a former call to executeCommand.
|
---|
43 | */
|
---|
44 | public void undoCommand() {
|
---|
45 | for (Entry<OsmPrimitive, OsmPrimitive> e : orig.orig.entrySet())
|
---|
46 | e.getKey().cloneFrom(e.getValue());
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Fill in the changed data this command operates on.
|
---|
51 | * Add to the lists, don't clear them.
|
---|
52 | *
|
---|
53 | * @param modified The modified primitives
|
---|
54 | * @param deleted The deleted primitives
|
---|
55 | * @param added The added primitives
|
---|
56 | */
|
---|
57 | abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
|
---|
58 | Collection<OsmPrimitive> deleted,
|
---|
59 | Collection<OsmPrimitive> added);
|
---|
60 | }
|
---|