source: josm/trunk/src/org/openstreetmap/josm/command/Command.java@ 2474

Last change on this file since 2474 was 2405, checked in by jttt, 14 years ago

Save reference to dataset in OsmPrimitive

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.HashMap;
8import java.util.HashSet;
9import java.util.Map;
10import java.util.Map.Entry;
11
12import javax.swing.tree.MutableTreeNode;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.PrimitiveData;
18import org.openstreetmap.josm.data.osm.Relation;
19import org.openstreetmap.josm.data.osm.Way;
20import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
21import org.openstreetmap.josm.gui.layer.Layer;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23
24
25/**
26 * Classes implementing Command modify a dataset in a specific way. A command is
27 * one atomic action on a specific dataset, such as move or delete.
28 *
29 * The command remembers the {@see OsmDataLayer} it is operating on.
30 *
31 * @author imi
32 */
33abstract public class Command {
34
35 private static final class CloneVisitor extends AbstractVisitor {
36 public Map<OsmPrimitive, PrimitiveData> orig = new HashMap<OsmPrimitive, PrimitiveData>();
37
38 public void visit(Node n) {
39 orig.put(n, n.save());
40 }
41 public void visit(Way w) {
42 orig.put(w, w.save());
43 }
44 public void visit(Relation e) {
45 orig.put(e, e.save());
46 }
47 }
48
49 /** the map of OsmPrimitives in the original state to OsmPrimitives in cloned state */
50 private Map<OsmPrimitive, PrimitiveData> cloneMap = new HashMap<OsmPrimitive, PrimitiveData>();
51
52 /** the layer which this command is applied to */
53 private OsmDataLayer layer;
54
55 public Command() {
56 this.layer = Main.map.mapView.getEditLayer();
57 }
58
59 /**
60 * Creates a new command in the context of a specific data layer
61 *
62 * @param layer the data layer. Must not be null.
63 * @throws IllegalArgumentException thrown if layer is null
64 */
65 public Command(OsmDataLayer layer) throws IllegalArgumentException {
66 if (layer == null)
67 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "layer"));
68 this.layer = layer;
69 }
70
71 /**
72 * Executes the command on the dataset. This implementation will remember all
73 * primitives returned by fillModifiedData for restoring them on undo.
74 */
75 public boolean executeCommand() {
76 CloneVisitor visitor = new CloneVisitor();
77 Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
78 fillModifiedData(all, all, all);
79 for (OsmPrimitive osm : all) {
80 osm.visit(visitor);
81 }
82 cloneMap = visitor.orig;
83 return true;
84 }
85
86 /**
87 * Undoes the command.
88 * It can be assumed that all objects are in the same state they were before.
89 * It can also be assumed that executeCommand was called exactly once before.
90 *
91 * This implementation undoes all objects stored by a former call to executeCommand.
92 */
93 public void undoCommand() {
94 for (Entry<OsmPrimitive, PrimitiveData> e : cloneMap.entrySet()) {
95 e.getKey().load(e.getValue());
96 }
97 }
98
99 /**
100 * Called when a layer has been removed to have the command remove itself from
101 * any buffer if it is not longer applicable to the dataset (e.g. it was part of
102 * the removed layer)
103 *
104 * @param oldLayer the old layer
105 * @return true if this command
106 */
107 public boolean invalidBecauselayerRemoved(Layer oldLayer) {
108 if (!(oldLayer instanceof OsmDataLayer))
109 return false;
110 return layer == oldLayer;
111 }
112
113 /**
114 * Lets other commands access the original version
115 * of the object. Usually for undoing.
116 */
117 public PrimitiveData getOrig(OsmPrimitive osm) {
118 PrimitiveData o = cloneMap.get(osm);
119 if (o != null)
120 return o;
121 Main.debug("unable to find osm with id: " + osm.getId() + " hashCode: " + osm.hashCode());
122 for (OsmPrimitive t : cloneMap.keySet()) {
123 PrimitiveData to = cloneMap.get(t);
124 Main.debug("now: " + t.getId() + " hashCode: " + t.hashCode());
125 Main.debug("orig: " + to.getId() + " hashCode: " + to.hashCode());
126 }
127 return o;
128 }
129
130 /**
131 * Replies the layer this command is (or was) applied to.
132 *
133 * @return
134 */
135 protected OsmDataLayer getLayer() {
136 return layer;
137 }
138
139 /**
140 * Fill in the changed data this command operates on.
141 * Add to the lists, don't clear them.
142 *
143 * @param modified The modified primitives
144 * @param deleted The deleted primitives
145 * @param added The added primitives
146 */
147 abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
148 Collection<OsmPrimitive> deleted,
149 Collection<OsmPrimitive> added);
150
151 abstract public MutableTreeNode description();
152
153
154
155}
Note: See TracBrowser for help on using the repository browser.