[josm-dev] [PATCH 16/26] clean up DeleteAction

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


This starts using the new Commands in DeleteAction and
cleans it up a bit in the process.

---

 core-dave/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java |   95 ++++------
 1 file changed, 45 insertions(+), 50 deletions(-)

diff -puN src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java~deleteaction src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java
--- core/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java~deleteaction	2008-04-28 18:59:29.000000000 -0700
+++ core-dave/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java	2008-04-28 18:59:29.000000000 -0700
@@ -166,69 +166,62 @@ public class DeleteAction extends MapMod
 		if (selection.isEmpty()) return null;
 
 		Collection<OsmPrimitive> del = new HashSet<OsmPrimitive>(selection);
-		Collection<Way> waysToBeChanged = new HashSet<Way>();
 
 		if (alsoDeleteNodesInWay) {
-			// Delete untagged nodes that are to be unreferenced.
-			Collection<OsmPrimitive> delNodes = new HashSet<OsmPrimitive>();
+			// we need this additional collection because otherwise this throws
+			// ConcurrentModifificationExceptions
+			Collection<OsmPrimitive> delNodes = new HashSet<OsmPrimitive>(selection);
 			for (OsmPrimitive osm : del) {
-				if (osm instanceof Way) {
-					for (Node n : ((Way)osm).nodes) {
-						if (!n.tagged) {
-							CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
-							n.visit(v);
-							v.data.removeAll(del);
-							if (v.data.isEmpty()) {
-								delNodes.add(n);
-							}
+				if (!(osm instanceof Way))
+					continue;
+				for (Node n : ((Way)osm).nodes) {
+					if (!n.tagged) {
+						CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
+						n.visit(v);
+						v.data.removeAll(del);
+						if (v.data.isEmpty()) {
+							delNodes.add(n);
 						}
 					}
 				}
 			}
 			del.addAll(delNodes);
 		}
-		
 		for (OsmPrimitive osm : del) {
 			CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
 			osm.visit(v);
 			for (OsmPrimitive ref : v.data) {
-				if (del.contains(ref)) continue;
-				if (ref instanceof Way) {
-					waysToBeChanged.add((Way) ref);
-				} else if (ref instanceof Relation) {
+				if (del.contains(ref))
+					continue;
+				// we'll deal with ways in a second
+				if (ref instanceof Way)
+					continue;
+				if (ref instanceof Relation) {
 					JOptionPane.showMessageDialog(Main.parent,
 						tr("Cannot delete: Selection is used by relation"));
 					return null;
-				} else {
-					return null;
 				}
+				return null;
 			}
 		}
 
 		Collection<Command> cmds = new LinkedList<Command>();
-		for (Way w : waysToBeChanged) {
-			Way wnew = new Way(w);
-			wnew.nodes.removeAll(del);
-			if (wnew.nodes.size() < 2) {
-				del.add(w);
-
-				CollectBackReferencesVisitor v = new CollectBackReferencesVisitor(Main.ds, false);
-				w.visit(v);
-				for (OsmPrimitive ref : v.data) {
-					if (del.contains(ref)) continue;
-					if (ref instanceof Relation) {
-						JOptionPane.showMessageDialog(Main.parent,
-							tr("Cannot delete: Selection is used by relation"));
-					} else {
-						return null;
-					}
-				}
-			} else {
-				cmds.add(new ChangeCommand(w, wnew));
+		HashSet<Way> checkForDel = new HashSet<Way>();
+		for (OsmPrimitive osm : del) {
+			if (!(osm instanceof Node))
+				continue;
+			Node n = (Node)osm;
+			for (Way w : Main.ds.rl.waysUsingNode(n)) {
+				cmds.add(new RemoveNodeInWayCommand(w, n));
+				checkForDel.add(w);
 			}
 		}
+		// we need to do this reverse lookup stuff elsewhere
+		// perhaps in way.mayDelete()?
 
 		if (!del.isEmpty()) cmds.add(new DeleteCommand(del));
+		if (!checkForDel.isEmpty())
+			cmds.add(new ConditionalDeleteCommand(checkForDel));
 
 		return new SequenceCommand(tr("Delete"), cmds);
 	}
@@ -244,28 +237,30 @@ public class DeleteAction extends MapMod
 			return new DeleteCommand(Collections.singleton(ws.way));
 		}
 		
-		Way wnew = new Way(ws.way);
-		wnew.nodes.clear();
-
 		if (n1.size() < 2) {
-			wnew.nodes.addAll(n2);
-			return new ChangeCommand(ws.way, wnew);
+			Collection<Command> cmds = new LinkedList<Command>();
+			for (Node n : n2)
+				cmds.add(new AddNodeToWayCommand(ws.way, n));
+			return new SequenceCommand(tr("delete way segment"), cmds);
 		} else if (n2.size() < 2) {
-			wnew.nodes.addAll(n1);
-			return new ChangeCommand(ws.way, wnew);
+			Collection<Command> cmds = new LinkedList<Command>();
+			for (Node n : n1)
+				cmds.add(new AddNodeToWayCommand(ws.way, n));
+			// We might want to add a AddNodesToWayCommand instead of this...
+			return new SequenceCommand("add nodes", cmds);
 		} else {
 			Collection<Command> cmds = new LinkedList<Command>();
 
-			wnew.nodes.addAll(n1);
-			cmds.add(new ChangeCommand(ws.way, wnew));
+			for (Node n : n1)
+				cmds.add(new AddNodeToWayCommand(ws.way, n));
 
 			Way wnew2 = new Way();
-			if (wnew.keys != null) {
-				wnew2.keys = new HashMap<String, String>(wnew.keys);
+			if (ws.way.keys != null) {
+				wnew2.keys = new HashMap<String, String>(ws.way.keys);
 				wnew2.checkTagged();
                                 wnew2.checkDirectionTagged();
 			}
-			wnew2.nodes.addAll(n2);
+			wnew2.addNodes(n2);
 			cmds.add(new AddCommand(wnew2));
 
 			return new SequenceCommand(tr("Split way segment"), cmds);
_




More information about the josm-dev mailing list