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