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

Last change on this file since 1894 was 1894, checked in by Gubaer, 15 years ago

fixed #3158: modify status is not removed for partial uploads

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.command;
3
4import java.util.Collection;
5import java.util.HashMap;
6import java.util.HashSet;
7import java.util.Map;
8import java.util.Map.Entry;
9
10import javax.swing.tree.MutableTreeNode;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.Relation;
16import org.openstreetmap.josm.data.osm.Way;
17import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
18import org.openstreetmap.josm.gui.layer.Layer;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20
21
22/**
23 * Classes implementing Command modify a dataset in a specific way. A command is
24 * one atomic action on a specific dataset, such as move or delete.
25 *
26 * The command remembers the {@see OsmDataLayer} it is operating on.
27 *
28 * @author imi
29 */
30abstract public class Command {
31
32 private static final class CloneVisitor extends AbstractVisitor {
33 public Map<OsmPrimitive, OsmPrimitive> orig = new HashMap<OsmPrimitive, OsmPrimitive>();
34
35 public void visit(Node n) {
36 orig.put(n, new Node(n));
37 }
38 public void visit(Way w) {
39 orig.put(w, new Way(w));
40 }
41 public void visit(Relation e) {
42 orig.put(e, new Relation(e));
43 }
44 }
45
46 /** the map of OsmPrimitives in the original state to OsmPrimitives in cloned state */
47 private Map<OsmPrimitive, OsmPrimitive> cloneMap = new HashMap<OsmPrimitive, OsmPrimitive>();
48
49 /** the layer which this command is applied to */
50 private OsmDataLayer layer;
51
52 public Command() {
53 this.layer = Main.map.mapView.getEditLayer();
54 }
55
56 /**
57 * Creates a new command in the context of a specific data layer
58 *
59 * @param layer the data layer
60 */
61 public Command(OsmDataLayer layer) {
62 this.layer = layer;
63 }
64
65 /**
66 * Executes the command on the dataset. This implementation will remember all
67 * primitives returned by fillModifiedData for restoring them on undo.
68 */
69 public boolean executeCommand() {
70 CloneVisitor visitor = new CloneVisitor();
71 Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
72 fillModifiedData(all, all, all);
73 for (OsmPrimitive osm : all) {
74 osm.visit(visitor);
75 }
76 cloneMap = visitor.orig;
77 return true;
78 }
79
80 /**
81 * Undoes the command.
82 * It can be assumed that all objects are in the same state they were before.
83 * It can also be assumed that executeCommand was called exactly once before.
84 *
85 * This implementation undoes all objects stored by a former call to executeCommand.
86 */
87 public void undoCommand() {
88 for (Entry<OsmPrimitive, OsmPrimitive> e : cloneMap.entrySet()) {
89 e.getKey().cloneFrom(e.getValue());
90 }
91 getLayer().setModified(true);
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 OsmPrimitive getOrig(OsmPrimitive osm) {
113 OsmPrimitive o = cloneMap.get(osm);
114 if (o != null)
115 return o;
116 Main.debug("unable to find osm with id: " + osm.id + " hashCode: " + osm.hashCode());
117 for (OsmPrimitive t : cloneMap.keySet()) {
118 OsmPrimitive to = cloneMap.get(t);
119 Main.debug("now: " + t.id + " hashCode: " + t.hashCode());
120 Main.debug("orig: " + to.id + " 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}
Note: See TracBrowser for help on using the repository browser.