[josm-dev] [PATCH 19/24] refactor node insertion in DrawAction

Dave Hansen dave at sr71.net
Sat May 3 20:15:09 BST 2008


Use the new commands to clean up DrawAction.  I think
this makes it much more readable.

---

 core-dave/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java |   99 ++++------
 1 file changed, 47 insertions(+), 52 deletions(-)

diff -puN src/org/openstreetmap/josm/actions/mapmode/DrawAction.java~refactor-node-insertion-in-DrawAction src/org/openstreetmap/josm/actions/mapmode/DrawAction.java
--- core/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java~refactor-node-insertion-in-DrawAction	2008-05-03 12:08:44.000000000 -0700
+++ core-dave/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java	2008-05-03 12:08:44.000000000 -0700
@@ -34,6 +34,7 @@ import javax.swing.KeyStroke;
 
 import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.command.AddCommand;
+import org.openstreetmap.josm.command.AddNodeToWayCommand;
 import org.openstreetmap.josm.command.ChangeCommand;
 import org.openstreetmap.josm.command.Command;
 import org.openstreetmap.josm.command.SequenceCommand;
@@ -135,6 +136,39 @@ public class DrawAction extends MapMode 
 		computeHelperLine();
 	}
 
+	Collection<Command> insertNodeIntoNearbySegment(MouseEvent e, Node n)
+	{
+		Collection<Command> cmds = new LinkedList<Command>();
+		// Insert the node into all the nearby way segments
+		List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(e.getPoint());
+		Map<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>();
+		for (WaySegment ws : wss) {
+			List<Integer> is;
+			if (insertPoints.containsKey(ws.way)) {
+				is = insertPoints.get(ws.way);
+			} else {
+				is = new ArrayList<Integer>();
+				insertPoints.put(ws.way, is);
+			}
+
+			is.add(ws.lowerIndex);
+		}
+
+		Set<Pair<Node,Node>> segSet = new HashSet<Pair<Node,Node>>();
+
+		for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
+			Way w = insertPoint.getKey();
+			List<Integer> is = insertPoint.getValue();
+
+			pruneSuccsAndReverse(is);
+			for (int i : is) segSet.add(
+				Pair.sort(new Pair<Node,Node>(w.nodes.get(i), w.nodes.get(i+1))));
+		}
+
+		adjustNode(segSet, n);
+		return cmds;
+	}
+
 	/**
 	 * If user clicked with the left button, add a node at the current mouse
 	 * position.
@@ -157,10 +191,9 @@ public class DrawAction extends MapMode 
 		Collection<OsmPrimitive> selection = Main.ds.getSelected();
 		Collection<Command> cmds = new LinkedList<Command>();
 
-		ArrayList<Way> reuseWays = new ArrayList<Way>(),
-			replacedWays = new ArrayList<Way>();
 		boolean newNode = false;
 		Node n = null;
+		boolean nodeAddedToWays = false;
 		
 		if (!ctrl) {
 			n = Main.map.mapView.getNearestNode(mousePos);
@@ -189,40 +222,10 @@ public class DrawAction extends MapMode 
 			cmds.add(new AddCommand(n));
 
 			if (!ctrl) {
-				// Insert the node into all the nearby way segments
-				List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(e.getPoint());
-				Map<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>();
-				for (WaySegment ws : wss) {
-					List<Integer> is;
-					if (insertPoints.containsKey(ws.way)) {
-						is = insertPoints.get(ws.way);
-					} else {
-						is = new ArrayList<Integer>();
-						insertPoints.put(ws.way, is);
-					}
-
-					is.add(ws.lowerIndex);
-				}
-
-				Set<Pair<Node,Node>> segSet = new HashSet<Pair<Node,Node>>();
-
-				for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
-					Way w = insertPoint.getKey();
-					List<Integer> is = insertPoint.getValue();
-
-					Way wnew = new Way(w);
-
-					pruneSuccsAndReverse(is);
-					for (int i : is) segSet.add(
-						Pair.sort(new Pair<Node,Node>(w.nodes.get(i), w.nodes.get(i+1))));
-					for (int i : is) wnew.nodes.add(i + 1, n);
-
-					cmds.add(new ChangeCommand(insertPoint.getKey(), wnew));
-					replacedWays.add(insertPoint.getKey());
-					reuseWays.add(wnew);
-				}
-
-				adjustNode(segSet, n);
+				Collection<Command> segmentInsert = insertNodeIntoNearbySegment(e, n);
+				if (segmentInsert.size() > 0)
+					nodeAddedToWays = true;
+					cmds.addAll(segmentInsert);
 			}
 		}
 		
@@ -276,25 +279,18 @@ public class DrawAction extends MapMode 
 			// user wants a new way.
 			
 			Way way = alt ? null : (selectedWay != null) ? selectedWay : getWayForNode(n0);
+			boolean newWay = false;
 			if (way == null) {
+				newWay = true;
 				way = new Way();
-				way.nodes.add(n0);
 				cmds.add(new AddCommand(way));
-			} else {
-				int i;
-				if ((i = replacedWays.indexOf(way)) != -1) {
-					way = reuseWays.get(i);
-				} else {
-					Way wnew = new Way(way);
-					cmds.add(new ChangeCommand(way, wnew));
-					way = wnew;
-				}
+				cmds.add(new AddNodeToWayCommand(way, n0));
 			}
 
-			if (way.nodes.get(way.nodes.size() - 1) == n0) {
-				way.nodes.add(n);
+			if (newWay || (way.lastNode() == n0)) {
+				cmds.add(new AddNodeToWayCommand(way, n));
 			} else {
-				way.nodes.add(0, n);
+				cmds.add(new AddNodeToWayCommand(way, n, 0));
 			}
 
 			extendedWay = true;
@@ -305,16 +301,15 @@ public class DrawAction extends MapMode 
 		if (!extendedWay && !newNode) {
 			return; // We didn't do anything.
 		} else if (!extendedWay) {
-			if (reuseWays.isEmpty()) {
+			if (nodeAddedToWays) {
 				title = tr("Add node");
 			} else {
 				title = tr("Add node into way");
 			}
-			for (Way w : reuseWays) w.selected = false;
 			Main.ds.setSelected(n);
 		} else if (!newNode) {
 			title = tr("Connect existing way to node");
-		} else if (reuseWays.isEmpty()) {
+		} else if (nodeAddedToWays) {
 			title = tr("Add a new node to an existing way");
 		} else {
 			title = tr("Add node into way and connect");
_




More information about the josm-dev mailing list