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.Main;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
21 |
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Classes implementing Command modify a dataset in a specific way. A command is
|
---|
25 | * one atomic action on a specific dataset, such as move or delete.
|
---|
26 | *
|
---|
27 | * Remember that the command must be executable and undoable, even if the
|
---|
28 | * Main.ds has changed, so the command must save the dataset it operates on
|
---|
29 | * if necessary.
|
---|
30 | *
|
---|
31 | * @author imi
|
---|
32 | */
|
---|
33 | abstract public class Command {
|
---|
34 |
|
---|
35 | private static final class CloneVisitor implements Visitor {
|
---|
36 | public Map<OsmPrimitive, OsmPrimitive> orig = new HashMap<OsmPrimitive, OsmPrimitive>();
|
---|
37 |
|
---|
38 | public void visit(Node n) {
|
---|
39 | orig.put(n, new Node(n));
|
---|
40 | }
|
---|
41 | public void visit(Way w) {
|
---|
42 | orig.put(w, new Way(w));
|
---|
43 | }
|
---|
44 | public void visit(Relation e) {
|
---|
45 | orig.put(e, new Relation(e));
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private CloneVisitor orig;
|
---|
50 |
|
---|
51 | protected DataSet ds;
|
---|
52 |
|
---|
53 | public Command() {
|
---|
54 | this.ds = Main.main.editLayer().data;
|
---|
55 | }
|
---|
56 | /**
|
---|
57 | * Executes the command on the dataset. This implementation will remember all
|
---|
58 | * primitives returned by fillModifiedData for restoring them on undo.
|
---|
59 | */
|
---|
60 | public boolean did_execute = false;
|
---|
61 | public boolean executeCommand() {
|
---|
62 | did_execute = true;
|
---|
63 | orig = new CloneVisitor();
|
---|
64 | Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
|
---|
65 | fillModifiedData(all, all, all);
|
---|
66 | for (OsmPrimitive osm : all)
|
---|
67 | osm.visit(orig);
|
---|
68 | return true;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Undoes the command.
|
---|
73 | * It can be assumed that all objects are in the same state they were before.
|
---|
74 | * It can also be assumed that executeCommand was called exactly once before.
|
---|
75 | *
|
---|
76 | * This implementation undoes all objects stored by a former call to executeCommand.
|
---|
77 | */
|
---|
78 | public void undoCommand() {
|
---|
79 | for (Entry<OsmPrimitive, OsmPrimitive> e : orig.orig.entrySet())
|
---|
80 | e.getKey().cloneFrom(e.getValue());
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Called when a layer has been removed to have the command remove itself from
|
---|
85 | * any buffer if it is not longer applicable to the dataset (e.g. it was part of
|
---|
86 | * the removed layer)
|
---|
87 | */
|
---|
88 | public boolean invalidBecauselayerRemoved(Layer oldLayer) {
|
---|
89 | if (!(oldLayer instanceof OsmDataLayer))
|
---|
90 | return false;
|
---|
91 | HashSet<OsmPrimitive> modified = new HashSet<OsmPrimitive>();
|
---|
92 | fillModifiedData(modified, modified, modified);
|
---|
93 | if (modified.isEmpty())
|
---|
94 | return false;
|
---|
95 |
|
---|
96 | HashSet<OsmPrimitive> all = new HashSet<OsmPrimitive>(((OsmDataLayer)oldLayer).data.allPrimitives());
|
---|
97 | for (OsmPrimitive osm : all)
|
---|
98 | if (all.contains(osm))
|
---|
99 | return true;
|
---|
100 |
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Lets other commands access the original version
|
---|
106 | * of the object. Usually for undoing.
|
---|
107 | */
|
---|
108 | public OsmPrimitive getOrig(OsmPrimitive osm) {
|
---|
109 | OsmPrimitive o = orig.orig.get(osm);
|
---|
110 | if (o != null)
|
---|
111 | return o;
|
---|
112 | Main.debug("unable to find osm with id: " + osm.id + " hashCode: " + osm.hashCode());
|
---|
113 | for (OsmPrimitive t : orig.orig.keySet()) {
|
---|
114 | OsmPrimitive to = orig.orig.get(t);
|
---|
115 | Main.debug("now: " + t.id + " hashCode: " + t.hashCode());
|
---|
116 | Main.debug("orig: " + to.id + " hashCode: " + to.hashCode());
|
---|
117 | }
|
---|
118 | return o;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Fill in the changed data this command operates on.
|
---|
123 | * Add to the lists, don't clear them.
|
---|
124 | *
|
---|
125 | * @param modified The modified primitives
|
---|
126 | * @param deleted The deleted primitives
|
---|
127 | * @param added The added primitives
|
---|
128 | */
|
---|
129 | abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
|
---|
130 | Collection<OsmPrimitive> deleted,
|
---|
131 | Collection<OsmPrimitive> added);
|
---|
132 |
|
---|
133 | abstract public MutableTreeNode description();
|
---|
134 | }
|
---|