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

Last change on this file since 2702 was 2683, checked in by mjulius, 14 years ago

fixes #4149 - exception when undoing reversal of way with tag corrections

don't try to undo Command for a primitive that does not belong to a dataset

fixes issue with ChangeRelationMemberRoleCommand.undoCommand() doing nothing except resetting modified flag
don't reverse tags on way nodes when reversing way
reversing way now also reverses 'incline=*' tags

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