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

Last change on this file since 628 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 3.2 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.data.osm.Relation;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.data.osm.visitor.Visitor;
17import org.openstreetmap.josm.gui.layer.Layer;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19
20
21/**
22 * Classes implementing Command modify a dataset in a specific way. A command is
23 * one atomic action on a specific dataset, such as move or delete.
24 *
25 * Remember, that the command must be executable and undoable, even if the
26 * Main.ds has changed, so the command must save the dataset it operates on
27 * if necessary.
28 *
29 * @author imi
30 */
31abstract public class Command {
32
33 private static final class CloneVisitor implements Visitor {
34 public Map<OsmPrimitive, OsmPrimitive> orig = new HashMap<OsmPrimitive, OsmPrimitive>();
35
36 public void visit(Node n) {
37 orig.put(n, new Node(n));
38 }
39 public void visit(Way w) {
40 orig.put(w, new Way(w));
41 }
42 public void visit(Relation e) {
43 orig.put(e, new Relation(e));
44 }
45 }
46
47 private CloneVisitor orig;
48
49 /**
50 * Executes the command on the dataset. This implementation will remember all
51 * primitives returned by fillModifiedData for restoring them on undo.
52 */
53 public void executeCommand() {
54 orig = new CloneVisitor();
55 Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
56 fillModifiedData(all, all, all);
57 for (OsmPrimitive osm : all)
58 osm.visit(orig);
59 }
60
61 /**
62 * Undoes the command.
63 * It can be assumed, that all objects are in the same state they were before.
64 * It can also be assumed that executeCommand was called exactly once before.
65 *
66 * This implementation undoes all objects stored by a former call to executeCommand.
67 */
68 public void undoCommand() {
69 for (Entry<OsmPrimitive, OsmPrimitive> e : orig.orig.entrySet())
70 e.getKey().cloneFrom(e.getValue());
71 }
72
73
74 /**
75 * Called, when a layer has been removed to have the command remove itself from
76 * any buffer if it is not longer applicable to the dataset (e.g. it was part of
77 * the removed layer)
78 */
79 public boolean invalidBecauselayerRemoved(Layer oldLayer) {
80 if (!(oldLayer instanceof OsmDataLayer))
81 return false;
82 HashSet<OsmPrimitive> modified = new HashSet<OsmPrimitive>();
83 fillModifiedData(modified, modified, modified);
84 if (modified.isEmpty())
85 return false;
86
87 HashSet<OsmPrimitive> all = new HashSet<OsmPrimitive>(((OsmDataLayer)oldLayer).data.allPrimitives());
88 for (OsmPrimitive osm : all)
89 if (all.contains(osm))
90 return true;
91
92 return false;
93 }
94
95 /**
96 * Fill in the changed data this command operates on.
97 * Add to the lists, don't clear them.
98 *
99 * @param modified The modified primitives
100 * @param deleted The deleted primitives
101 * @param added The added primitives
102 */
103 abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
104 Collection<OsmPrimitive> deleted,
105 Collection<OsmPrimitive> added);
106
107 abstract public MutableTreeNode description();
108}
Note: See TracBrowser for help on using the repository browser.