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.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
16 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
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 | * The command remembers the {@see OsmDataLayer} it is operating on.
|
---|
28 | *
|
---|
29 | * @author imi
|
---|
30 | */
|
---|
31 | abstract public class Command {
|
---|
32 |
|
---|
33 | private static final class CloneVisitor extends AbstractVisitor {
|
---|
34 | public Map<OsmPrimitive, PrimitiveData> orig = new HashMap<OsmPrimitive, PrimitiveData>();
|
---|
35 |
|
---|
36 | public void visit(Node n) {
|
---|
37 | orig.put(n, n.save());
|
---|
38 | }
|
---|
39 | public void visit(Way w) {
|
---|
40 | orig.put(w, w.save());
|
---|
41 | }
|
---|
42 | public void visit(Relation e) {
|
---|
43 | orig.put(e, e.save());
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /** the map of OsmPrimitives in the original state to OsmPrimitives in cloned state */
|
---|
48 | private Map<OsmPrimitive, PrimitiveData> cloneMap = new HashMap<OsmPrimitive, PrimitiveData>();
|
---|
49 |
|
---|
50 | /** the layer which this command is applied to */
|
---|
51 | private OsmDataLayer layer;
|
---|
52 |
|
---|
53 | public Command() {
|
---|
54 | this.layer = Main.map.mapView.getEditLayer();
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Creates a new command in the context of a specific data layer
|
---|
59 | *
|
---|
60 | * @param layer the data layer
|
---|
61 | */
|
---|
62 | public Command(OsmDataLayer layer) {
|
---|
63 | this.layer = layer;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Executes the command on the dataset. This implementation will remember all
|
---|
68 | * primitives returned by fillModifiedData for restoring them on undo.
|
---|
69 | */
|
---|
70 | public boolean executeCommand() {
|
---|
71 | CloneVisitor visitor = new CloneVisitor();
|
---|
72 | Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
|
---|
73 | fillModifiedData(all, all, all);
|
---|
74 | for (OsmPrimitive osm : all) {
|
---|
75 | osm.visit(visitor);
|
---|
76 | }
|
---|
77 | cloneMap = visitor.orig;
|
---|
78 | return true;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Undoes the command.
|
---|
83 | * It can be assumed that all objects are in the same state they were before.
|
---|
84 | * It can also be assumed that executeCommand was called exactly once before.
|
---|
85 | *
|
---|
86 | * This implementation undoes all objects stored by a former call to executeCommand.
|
---|
87 | */
|
---|
88 | public void undoCommand() {
|
---|
89 | for (Entry<OsmPrimitive, PrimitiveData> e : cloneMap.entrySet()) {
|
---|
90 | e.getKey().load(e.getValue(), layer.data);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Called when a layer has been removed to have the command remove itself from
|
---|
96 | * any buffer if it is not longer applicable to the dataset (e.g. it was part of
|
---|
97 | * the removed layer)
|
---|
98 | *
|
---|
99 | * @param oldLayer the old layer
|
---|
100 | * @return true if this command
|
---|
101 | */
|
---|
102 | public boolean invalidBecauselayerRemoved(Layer oldLayer) {
|
---|
103 | if (!(oldLayer instanceof OsmDataLayer))
|
---|
104 | return false;
|
---|
105 | return layer == oldLayer;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Lets other commands access the original version
|
---|
110 | * of the object. Usually for undoing.
|
---|
111 | */
|
---|
112 | public PrimitiveData getOrig(OsmPrimitive osm) {
|
---|
113 | PrimitiveData o = cloneMap.get(osm);
|
---|
114 | if (o != null)
|
---|
115 | return o;
|
---|
116 | Main.debug("unable to find osm with id: " + osm.getId() + " hashCode: " + osm.hashCode());
|
---|
117 | for (OsmPrimitive t : cloneMap.keySet()) {
|
---|
118 | PrimitiveData to = cloneMap.get(t);
|
---|
119 | Main.debug("now: " + t.getId() + " hashCode: " + t.hashCode());
|
---|
120 | Main.debug("orig: " + to.getId() + " hashCode: " + to.hashCode());
|
---|
121 | }
|
---|
122 | return o;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Replies the layer this command is (or was) applied to.
|
---|
127 | *
|
---|
128 | * @return
|
---|
129 | */
|
---|
130 | protected OsmDataLayer getLayer() {
|
---|
131 | return layer;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Fill in the changed data this command operates on.
|
---|
136 | * Add to the lists, don't clear them.
|
---|
137 | *
|
---|
138 | * @param modified The modified primitives
|
---|
139 | * @param deleted The deleted primitives
|
---|
140 | * @param added The added primitives
|
---|
141 | */
|
---|
142 | abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
|
---|
143 | Collection<OsmPrimitive> deleted,
|
---|
144 | Collection<OsmPrimitive> added);
|
---|
145 |
|
---|
146 | abstract public MutableTreeNode description();
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|
150 | }
|
---|