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

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

removed dependencies to Main.ds, removed Main.ds
removed AddVisitor, NameVisitor, DeleteVisitor - unnecessary double dispatching for these simple cases

  • Property svn:eol-style set to native
File size: 4.4 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 * Executes the command on the dataset. This implementation will remember all
57 * primitives returned by fillModifiedData for restoring them on undo.
58 */
59 public boolean executeCommand() {
60 CloneVisitor visitor = new CloneVisitor();
61 Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
62 fillModifiedData(all, all, all);
63 for (OsmPrimitive osm : all) {
64 osm.visit(visitor);
65 }
66 cloneMap = visitor.orig;
67 return true;
68 }
69
70 /**
71 * Undoes the command.
72 * It can be assumed that all objects are in the same state they were before.
73 * It can also be assumed that executeCommand was called exactly once before.
74 *
75 * This implementation undoes all objects stored by a former call to executeCommand.
76 */
77 public void undoCommand() {
78 for (Entry<OsmPrimitive, OsmPrimitive> e : cloneMap.entrySet()) {
79 e.getKey().cloneFrom(e.getValue());
80 }
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 * @param oldLayer the old layer
89 * @return true if this command
90 */
91 public boolean invalidBecauselayerRemoved(Layer oldLayer) {
92 if (!(oldLayer instanceof OsmDataLayer))
93 return false;
94 return layer == oldLayer;
95 }
96
97 /**
98 * Lets other commands access the original version
99 * of the object. Usually for undoing.
100 */
101 public OsmPrimitive getOrig(OsmPrimitive osm) {
102 OsmPrimitive o = cloneMap.get(osm);
103 if (o != null)
104 return o;
105 Main.debug("unable to find osm with id: " + osm.id + " hashCode: " + osm.hashCode());
106 for (OsmPrimitive t : cloneMap.keySet()) {
107 OsmPrimitive to = cloneMap.get(t);
108 Main.debug("now: " + t.id + " hashCode: " + t.hashCode());
109 Main.debug("orig: " + to.id + " hashCode: " + to.hashCode());
110 }
111 return o;
112 }
113
114 /**
115 * Replies the layer this command is (or was) applied to.
116 *
117 * @return
118 */
119 protected OsmDataLayer getLayer() {
120 return layer;
121 }
122
123 /**
124 * Fill in the changed data this command operates on.
125 * Add to the lists, don't clear them.
126 *
127 * @param modified The modified primitives
128 * @param deleted The deleted primitives
129 * @param added The added primitives
130 */
131 abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
132 Collection<OsmPrimitive> deleted,
133 Collection<OsmPrimitive> added);
134
135 abstract public MutableTreeNode description();
136
137
138
139}
Note: See TracBrowser for help on using the repository browser.