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

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

new: improved dialog for uploading/saving modified layers on exit
new: improved dialog for uploading/saving modified layers if layers are deleted
new: new progress monitor which can delegate rendering to any Swing component
more setters/getters for properties in OSM data classes (fields are @deprecated); started to update references in the code base

  • 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 }
92
93 /**
94 * Called when a layer has been removed to have the command remove itself from
95 * any buffer if it is not longer applicable to the dataset (e.g. it was part of
96 * the removed layer)
97 *
98 * @param oldLayer the old layer
99 * @return true if this command
100 */
101 public boolean invalidBecauselayerRemoved(Layer oldLayer) {
102 if (!(oldLayer instanceof OsmDataLayer))
103 return false;
104 return layer == oldLayer;
105 }
106
107 /**
108 * Lets other commands access the original version
109 * of the object. Usually for undoing.
110 */
111 public OsmPrimitive getOrig(OsmPrimitive osm) {
112 OsmPrimitive o = cloneMap.get(osm);
113 if (o != null)
114 return o;
115 Main.debug("unable to find osm with id: " + osm.getId() + " hashCode: " + osm.hashCode());
116 for (OsmPrimitive t : cloneMap.keySet()) {
117 OsmPrimitive to = cloneMap.get(t);
118 Main.debug("now: " + t.getId() + " hashCode: " + t.hashCode());
119 Main.debug("orig: " + to.getId() + " hashCode: " + to.hashCode());
120 }
121 return o;
122 }
123
124 /**
125 * Replies the layer this command is (or was) applied to.
126 *
127 * @return
128 */
129 protected OsmDataLayer getLayer() {
130 return layer;
131 }
132
133 /**
134 * Fill in the changed data this command operates on.
135 * Add to the lists, don't clear them.
136 *
137 * @param modified The modified primitives
138 * @param deleted The deleted primitives
139 * @param added The added primitives
140 */
141 abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
142 Collection<OsmPrimitive> deleted,
143 Collection<OsmPrimitive> added);
144
145 abstract public MutableTreeNode description();
146
147
148
149}
Note: See TracBrowser for help on using the repository browser.