[josm-dev] [PATCH 05/26] ReplaceNodeInWayCommand
Dave Hansen
dave at sr71.net
Tue Apr 29 03:02:47 BST 2008
---
core-dave/src/org/openstreetmap/josm/command/ReplaceNodeInWayCommand.java | 95 ++++++++++
1 file changed, 95 insertions(+)
diff -puN src/org/openstreetmap/josm/command/ReplaceNodeInWayCommand.java~ReplaceNodeInWayCommand src/org/openstreetmap/josm/command/ReplaceNodeInWayCommand.java
--- core/src/org/openstreetmap/josm/command/ReplaceNodeInWayCommand.java~ReplaceNodeInWayCommand 2008-04-28 18:59:25.000000000 -0700
+++ core-dave/src/org/openstreetmap/josm/command/ReplaceNodeInWayCommand.java 2008-04-28 18:59:25.000000000 -0700
@@ -0,0 +1,95 @@
+// License: GPL. Copyright 2007 by Dave Hansen and others
+package org.openstreetmap.josm.command;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.util.Collection;
+
+import javax.swing.JLabel;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.MutableTreeNode;
+
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Way;
+import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
+
+/**
+ * Command that basically replaces one node in a way
+ *
+ * @author daveh
+ */
+public class ReplaceNodeInWayCommand extends Command {
+
+ // container object in which to replace a sub object
+ private final Way way;
+
+ // the sub-object to be replaced
+ private final Node node;
+ // its replacement
+ private final Node newNode;
+ private int replaced_at;
+ private int replace_at;
+
+ public ReplaceNodeInWayCommand(Way _way, Node _node, Node _newNode, int _replace_at) {
+ this.way = _way;
+ this.node = _node;
+ this.newNode = _newNode;
+ this.replace_at = _replace_at;
+ }
+
+ public ReplaceNodeInWayCommand(Way _way, Node _node, Node _newNode) {
+ this.way = _way;
+ this.node = _node;
+ this.newNode = _newNode;
+ this.replace_at = -1;
+ }
+
+ @Override public boolean executeCommand() {
+ super.executeCommand();
+ ds.rl.check(node);
+ ds.rl.check(newNode);
+ if (replace_at != -1)
+ replaced_at = way.replaceNode(node, newNode, replace_at);
+ else
+ replaced_at = way.replaceNode(node, newNode);
+ ds.rl.removeWayFromNodeMap_nocheck(node, way);
+ ds.rl.addWayToNodeMap_nocheck(newNode, way);
+ ds.rl.check(node);
+ ds.rl.check(newNode);
+ if (replaced_at < 0) {
+ String location = "";
+ if (replace_at != -1)
+ location = " at location";
+ Main.debug("error replacing node " + node.id +
+ location + " in way " +way.id);
+ return false;
+ }
+ way.modified = true;
+ return true;
+ }
+
+ @Override public void undoCommand() {
+ way.replaceNode(newNode, node, replaced_at);
+ //stem.out.println("undo ReplaceNode at: " + replaced_at);
+ ds.rl.removeWayFromNodeMap_nocheck(newNode, way);
+ ds.rl.addWayToNodeMap_nocheck(node, way);
+ ds.rl.check(node);
+ ds.rl.check(newNode);
+ Way orig_way = (Way)this.getOrig(way);
+ Main.debug("ReplaceNodeInWay() orig_way: " + orig_way);
+ way.modified = orig_way.modified;
+ }
+
+ @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
+ modified.add(way);
+ deleted.add(node);
+ }
+
+ @Override public MutableTreeNode description() {
+ NameVisitor v = new NameVisitor();
+ way.visit(v);
+ return new DefaultMutableTreeNode(new JLabel(tr("ReplaceNodeInWay")+" "+tr(v.className)+" "+v.name, v.icon, JLabel.HORIZONTAL));
+ }
+}
_
More information about the josm-dev
mailing list