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

Last change on this file since 298 was 298, checked in by imi, 17 years ago
  • added license description to head of each source file
File size: 2.5 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.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.Segment;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.data.osm.visitor.Visitor;
17
18
19/**
20 * Classes implementing Command modify a dataset in a specific way. A command is
21 * one atomic action on a specific dataset, such as move or delete.
22 *
23 * Remember, that the command must be executable and undoable, even if the
24 * Main.ds has changed, so the command must save the dataset it operates on
25 * if necessary.
26 *
27 * @author imi
28 */
29abstract public class Command {
30
31 private static final class CloneVisitor implements Visitor {
32 public Map<OsmPrimitive, OsmPrimitive> orig = new HashMap<OsmPrimitive, OsmPrimitive>();
33
34 public void visit(Node n) {
35 orig.put(n, new Node(n));
36 }
37 public void visit(Segment s) {
38 orig.put(s, new Segment(s));
39 }
40 public void visit(Way w) {
41 orig.put(w, new Way(w));
42 }
43 }
44
45 private CloneVisitor orig;
46
47 /**
48 * Executes the command on the dataset. This implementation will remember all
49 * primitives returned by fillModifiedData for restoring them on undo.
50 */
51 public void executeCommand() {
52 orig = new CloneVisitor();
53 Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
54 fillModifiedData(all, all, all);
55 for (OsmPrimitive osm : all)
56 osm.visit(orig);
57 }
58
59 /**
60 * Undoes the command.
61 * It can be assumed, that all objects are in the same state they were before.
62 * It can also be assumed that executeCommand was called exactly once before.
63 *
64 * This implementation undoes all objects stored by a former call to executeCommand.
65 */
66 public void undoCommand() {
67 for (Entry<OsmPrimitive, OsmPrimitive> e : orig.orig.entrySet())
68 e.getKey().cloneFrom(e.getValue());
69 }
70
71 /**
72 * Fill in the changed data this command operates on.
73 * Add to the lists, don't clear them.
74 *
75 * @param modified The modified primitives
76 * @param deleted The deleted primitives
77 * @param added The added primitives
78 */
79 abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
80 Collection<OsmPrimitive> deleted,
81 Collection<OsmPrimitive> added);
82
83 abstract public MutableTreeNode description();
84}
Note: See TracBrowser for help on using the repository browser.