A cleanup class.  It lets us have some objects examine themselves
to see if they are able to be deleted, like when a Way has no nodes
left.

---

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

diff -puN /dev/null 
src/org/openstreetmap/josm/command/ConditionalDeleteCommand.java
--- /dev/null   2008-02-29 08:37:01.000000000 -0800
+++ core-dave/src/org/openstreetmap/josm/command/ConditionalDeleteCommand.java  
2008-04-28 18:59:26.000000000 -0700
@@ -0,0 +1,84 @@
+// License: GPL. Copyright 2007 by Immanuel Scholz and others
+package org.openstreetmap.josm.command;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+import static org.openstreetmap.josm.tools.I18n.trn;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.swing.JLabel;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.MutableTreeNode;
+
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
+import org.openstreetmap.josm.tools.ImageProvider;
+
+/**
+ * A command to delete a primitive from the dataset, but only if it
+ * is "ready" to go, such as a Way with no nodes in it.
+ *
+ * @author daveh
+ */
+public class ConditionalDeleteCommand extends Command {
+
+       /**
+        * The primitive that get deleted.
+        */
+       private final Collection<? extends OsmPrimitive> data;
+       private final Collection<OsmPrimitive> deleted_data;
+
+       public ConditionalDeleteCommand(Collection<? extends OsmPrimitive> 
data) {
+               this.data = data;
+               deleted_data = new ArrayList<OsmPrimitive>();
+       }
+
+       @Override public boolean executeCommand() {
+               super.executeCommand();
+               for (OsmPrimitive osm : data) {
+                       if (!osm.mayDelete())
+                               continue;
+                       osm.delete(true);
+                       deleted_data.add(osm);
+               }
+               return true;
+       }
+
+       @Override public void undoCommand() {
+               super.executeCommand();
+               for (OsmPrimitive osm : deleted_data) {
+                       osm.delete(false);
+               }
+               deleted_data.clear();
+       }
+
+       @Override public MutableTreeNode description() {
+               NameVisitor v = new NameVisitor();
+
+               if (data.size() == 1) {
+                       data.iterator().next().visit(v);
+                       return new DefaultMutableTreeNode(new 
JLabel(tr("Conditional Delete")+" "+tr(v.className)+" "+v.name, v.icon, 
JLabel.HORIZONTAL));
+               }
+
+               String cname = null;
+               for (OsmPrimitive osm : data) {
+                       osm.visit(v);
+                       if (cname == null)
+                               cname = v.className;
+                       else if (!cname.equals(v.className))
+                               cname = "object";
+               }
+               DefaultMutableTreeNode root = new DefaultMutableTreeNode(new 
JLabel(
+                               tr("Delete")+" "+data.size()+" "+trn(cname, 
cname+"s", data.size()), ImageProvider.get("data", cname), JLabel.HORIZONTAL));
+               for (OsmPrimitive osm : data) {
+                       osm.visit(v);
+                       root.add(new DefaultMutableTreeNode(v.toLabel()));
+               }
+               return root;
+       }
+
+       @Override public void fillModifiedData(Collection<OsmPrimitive> 
modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
+
+    }
+}
_

_______________________________________________
josm-dev mailing list
josm-dev@openstreetmap.org
http://lists.openstreetmap.org/cgi-bin/mailman/listinfo/josm-dev

Reply via email to