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

Last change on this file since 1463 was 1169, checked in by stoecker, 15 years ago

removed usage of tab stops

  • Property svn:eol-style set to native
File size: 4.3 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.DataSet;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.data.osm.visitor.Visitor;
19import org.openstreetmap.josm.gui.layer.Layer;
20import org.openstreetmap.josm.gui.layer.OsmDataLayer;
21
22
23/**
24 * Classes implementing Command modify a dataset in a specific way. A command is
25 * one atomic action on a specific dataset, such as move or delete.
26 *
27 * Remember that the command must be executable and undoable, even if the
28 * Main.ds has changed, so the command must save the dataset it operates on
29 * if necessary.
30 *
31 * @author imi
32 */
33abstract public class Command {
34
35 private static final class CloneVisitor implements Visitor {
36 public Map<OsmPrimitive, OsmPrimitive> orig = new HashMap<OsmPrimitive, OsmPrimitive>();
37
38 public void visit(Node n) {
39 orig.put(n, new Node(n));
40 }
41 public void visit(Way w) {
42 orig.put(w, new Way(w));
43 }
44 public void visit(Relation e) {
45 orig.put(e, new Relation(e));
46 }
47 }
48
49 private CloneVisitor orig;
50
51 protected DataSet ds;
52
53 public Command() {
54 this.ds = Main.main.editLayer().data;
55 }
56 /**
57 * Executes the command on the dataset. This implementation will remember all
58 * primitives returned by fillModifiedData for restoring them on undo.
59 */
60 public boolean did_execute = false;
61 public boolean executeCommand() {
62 did_execute = true;
63 orig = new CloneVisitor();
64 Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
65 fillModifiedData(all, all, all);
66 for (OsmPrimitive osm : all)
67 osm.visit(orig);
68 return true;
69 }
70
71 /**
72 * Undoes the command.
73 * It can be assumed that all objects are in the same state they were before.
74 * It can also be assumed that executeCommand was called exactly once before.
75 *
76 * This implementation undoes all objects stored by a former call to executeCommand.
77 */
78 public void undoCommand() {
79 for (Entry<OsmPrimitive, OsmPrimitive> e : orig.orig.entrySet())
80 e.getKey().cloneFrom(e.getValue());
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 public boolean invalidBecauselayerRemoved(Layer oldLayer) {
89 if (!(oldLayer instanceof OsmDataLayer))
90 return false;
91 HashSet<OsmPrimitive> modified = new HashSet<OsmPrimitive>();
92 fillModifiedData(modified, modified, modified);
93 if (modified.isEmpty())
94 return false;
95
96 HashSet<OsmPrimitive> all = new HashSet<OsmPrimitive>(((OsmDataLayer)oldLayer).data.allPrimitives());
97 for (OsmPrimitive osm : all)
98 if (all.contains(osm))
99 return true;
100
101 return false;
102 }
103
104 /**
105 * Lets other commands access the original version
106 * of the object. Usually for undoing.
107 */
108 public OsmPrimitive getOrig(OsmPrimitive osm) {
109 OsmPrimitive o = orig.orig.get(osm);
110 if (o != null)
111 return o;
112 Main.debug("unable to find osm with id: " + osm.id + " hashCode: " + osm.hashCode());
113 for (OsmPrimitive t : orig.orig.keySet()) {
114 OsmPrimitive to = orig.orig.get(t);
115 Main.debug("now: " + t.id + " hashCode: " + t.hashCode());
116 Main.debug("orig: " + to.id + " hashCode: " + to.hashCode());
117 }
118 return o;
119 }
120
121 /**
122 * Fill in the changed data this command operates on.
123 * Add to the lists, don't clear them.
124 *
125 * @param modified The modified primitives
126 * @param deleted The deleted primitives
127 * @param added The added primitives
128 */
129 abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
130 Collection<OsmPrimitive> deleted,
131 Collection<OsmPrimitive> added);
132
133 abstract public MutableTreeNode description();
134}
Note: See TracBrowser for help on using the repository browser.