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

Last change on this file since 2512 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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