[josm-dev] [PATCH 12/24] MergeWaysCommand
Dave Hansen
dave at sr71.net
Sat May 3 20:15:07 BST 2008
New command. Title says it all. :)
This is a pretty common operation. I would like to convert MergeWaysAction over
to use it, eventually.
---
core-dave/./src/org/openstreetmap/josm/command/MergeWaysCommand.java | 100 ++++++++++
1 file changed, 100 insertions(+)
diff -puN /dev/null ./src/org/openstreetmap/josm/command/MergeWaysCommand.java
--- /dev/null 2008-02-29 08:37:01.000000000 -0800
+++ core-dave/./src/org/openstreetmap/josm/command/MergeWaysCommand.java 2008-05-03 12:08:42.000000000 -0700
@@ -0,0 +1,100 @@
+// 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.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+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;
+
+/**
+ * This is a common operation, and we need to consolidate all of the code
+ * that performs it into one spot.
+ *
+ * @author daveh
+ */
+public class MergeWaysCommand extends Command {
+
+ // container object in which to replace a sub object
+ private final List<Way> ways;
+ SequenceCommand cmd;
+
+ public MergeWaysCommand(List<Way> _ways) {
+ ways = new ArrayList<Way>(_ways);
+ }
+ public MergeWaysCommand(Way... sequenz) {
+ this(Arrays.asList(sequenz));
+ }
+ public SequenceCommand mergeWays(Way dst, Way src)
+ {
+ //Main.debug("mergeWays() a");
+ List<Command> cmds = new ArrayList<Command>();
+
+ if (dst.deleted || dst.incomplete || dst.nodes.size() == 0)
+ return null;
+ //Main.debug("mergeWays() b");
+ if (src.deleted || src.incomplete || dst.nodes.size() == 0)
+ return null;
+ //Main.debug("mergeWays() c");
+ if (dst.lastNode() == src.firstNode()) {
+ boolean first = true;
+ for (Node n : src.nodes) {
+ // The first node is already present in the
+ // destination way, so just skip it
+ if (first) {
+ first = false;
+ continue;
+ }
+ //Main.debug(" add node "+ n.id +" to " + dst.id);
+ cmds.add(new AddNodeToWayCommand(dst, n));
+ }
+ } else {
+ // In this case, we need to prepend the
+ // src nodes onto the dst way. Add Node 0,
+ // Node 1, Node 2, etc...
+ int nr = 0;
+ for (Node n : src.nodes) {
+ //Main.debug(" add node "+ n.id +" to " + dst.id);
+ cmds.add(new AddNodeToWayCommand(dst, n, nr++));
+ }
+ }
+ //Main.debug("mergeWays() e");
+ cmds.add(new DeleteCommand(src));
+ return new SequenceCommand("Merge Ways", cmds);
+ }
+
+ @Override public boolean executeCommand() {
+ Way dst = ways.get(0);
+ Way src = ways.get(1);
+ cmd = this.mergeWays(src, dst);
+ boolean ret = cmd.executeCommand();
+ // Wait to do this until after we have cmd set
+ // and performed
+ super.executeCommand();
+ return ret;
+ }
+ @Override public void undoCommand() {
+ cmd.undoCommand();
+ }
+
+ @Override public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
+ cmd.fillModifiedData(modified, deleted, added);
+ deleted.add(ways.get(1));
+ modified.add(ways.get(0));
+ }
+
+ @Override public MutableTreeNode description() {
+ return cmd.description();
+ }
+}
_
More information about the josm-dev
mailing list