[josm-dev] [PATCH 07/26] AddNodeToWayCommand

Dave Hansen dave at sr71.net
Tue Apr 29 03:02:48 BST 2008



---

 core-dave/src/org/openstreetmap/josm/command/AddNodeToWayCommand.java |   72 ++++++++++
 1 file changed, 72 insertions(+)

diff -puN /dev/null src/org/openstreetmap/josm/command/AddNodeToWayCommand.java
--- /dev/null	2008-02-29 08:37:01.000000000 -0800
+++ core-dave/src/org/openstreetmap/josm/command/AddNodeToWayCommand.java	2008-04-28 18:59:26.000000000 -0700
@@ -0,0 +1,72 @@
+// License: GPL. Copyright 2007 by Immanuel Scholz 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 adds a node to a way, possibly in a particular location
+ *
+ * @author daveh
+ */
+public class AddNodeToWayCommand 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 int location = -1;
+
+	public AddNodeToWayCommand(Way _way, Node _node) {
+		this.way = _way;
+		this.node = _node;
+    }
+	public AddNodeToWayCommand(Way _way, Node _node, int _location) {
+		this.way = _way;
+		this.node = _node;
+		location = _location;
+	}
+
+	@Override public boolean executeCommand() {
+	    super.executeCommand();
+		if (location == -1) {
+			location = way.nodes.size();
+			way.addNode(node);
+		} else
+			way.addNodeNr(location, node);
+		Main.ds.rl.addWayToNodeMap(node, way);
+	    way.modified = true;
+		return true;
+    }
+
+	@Override public void undoCommand() {
+		Node removed = way.removeNode(location);
+		if (removed != node)
+			Main.debug("removed wrong node");
+		Main.ds.rl.removeWayFromNodeMap(removed, way);
+	    way.modified = this.getOrig(way).modified;
+    }
+
+	@Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
+		modified.add(way);
+	}
+
+	@Override public MutableTreeNode description() {
+		NameVisitor v = new NameVisitor();
+		way.visit(v);
+		return new DefaultMutableTreeNode(new JLabel(tr("AddNodeToWay")+" "+tr(v.className)+" "+v.name, v.icon, JLabel.HORIZONTAL));
+    }
+}
_




More information about the josm-dev mailing list