]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/RemoveElement.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / RemoveElement.java
index 0d6ffc8821ba29f905ab34b9400fa8ed3872b1a8..53afaaf93f2856c9919f4c30adfac81ecdec853e 100644 (file)
@@ -1,96 +1,96 @@
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.diagram.synchronization.graph;\r
-\r
-import java.util.HashMap;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.CommentMetadata;\r
-import org.simantics.db.common.request.IndexRoot;\r
-import org.simantics.db.common.utils.OrderedSetUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.Remover;\r
-import org.simantics.db.layer0.exception.CannotRemoveException;\r
-import org.simantics.db.layer0.util.RemoverUtil;\r
-import org.simantics.diagram.content.ConnectionUtil;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.diagram.synchronization.ModificationAdapter;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.scl.commands.Commands;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class RemoveElement extends ModificationAdapter {\r
-\r
-    Resource diagram;\r
-    Resource removedElement;\r
-\r
-    public RemoveElement(Resource diagram, Resource removed) {\r
-        super(REMOVE_NODE_PRIORITY);\r
-\r
-        if(diagram == null)\r
-            throw new NullPointerException();\r
-        if(removed == null)\r
-            throw new NullPointerException();\r
-        \r
-        this.diagram = diagram;\r
-        this.removedElement = removed;\r
-    }\r
-\r
-    @Override\r
-    public void perform(WriteGraph g) throws DatabaseException {\r
-        Commands.get(g, "Simantics/Diagram/removeElement")\r
-                .execute(g, g.syncRequest(new IndexRoot(diagram)), diagram, removedElement);\r
-    }\r
-    \r
-    public static void removeElement(WriteGraph g, Resource diagram, Resource removedElement) throws DatabaseException {\r
-        boolean connection = g.isInstanceOf(removedElement, DiagramResource.getInstance(g).Connection);\r
-        String elementName = "";\r
-        // Remove element from all layers if possible.\r
-        DiagramResource DIA = DiagramResource.getInstance(g);\r
-        g.deny(removedElement, DIA.IsVisible);\r
-        g.deny(removedElement, DIA.IsFocusable);\r
-\r
-        if (connection) {\r
-            ConnectionUtil cu = new ConnectionUtil(g);\r
-            cu.removeConnection(removedElement);\r
-        } else {\r
-            Remover r = RemoverUtil.getPossibleRemover(g, removedElement);\r
-            if (r != null) {\r
-                String problem = r.canRemove(g, new HashMap<Object, Object>(4));\r
-                if (problem != null) {\r
-                    throw new CannotRemoveException(problem);\r
-                }\r
-            }\r
-\r
-            // Perform custom removals through ElementWriter adaption.\r
-            // FIXME: should be able to use getSingleType if everything else goes correctly??\r
-            Resource elementType = g.getPossibleType(removedElement, DiagramResource.getInstance(g).Element);\r
-            if (elementType != null) {\r
-                ElementWriter writer = g.adapt(elementType, ElementWriter.class);\r
-                writer.removeFromGraph(g, removedElement);\r
-            }\r
-\r
-            // Remove element from diagram\r
-            OrderedSetUtils.remove(g, diagram, removedElement);\r
-            elementName = g.getPossibleRelatedValue2(removedElement, Layer0.getInstance(g).HasName, Bindings.STRING);\r
-            RemoverUtil.remove(g, removedElement);\r
-        }\r
-\r
-        // Add comment to change set.\r
-        CommentMetadata cm = g.getMetadata(CommentMetadata.class);\r
-        g.addMetadata(cm.add("Removed element " + elementName + " " + removedElement));\r
-    }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.diagram.synchronization.graph;
+
+import java.util.HashMap;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.utils.OrderedSetUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.Remover;
+import org.simantics.db.layer0.exception.CannotRemoveException;
+import org.simantics.db.layer0.util.RemoverUtil;
+import org.simantics.diagram.content.ConnectionUtil;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.synchronization.ModificationAdapter;
+import org.simantics.layer0.Layer0;
+import org.simantics.scl.commands.Commands;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class RemoveElement extends ModificationAdapter {
+
+    Resource diagram;
+    Resource removedElement;
+
+    public RemoveElement(Resource diagram, Resource removed) {
+        super(REMOVE_NODE_PRIORITY);
+
+        if(diagram == null)
+            throw new NullPointerException();
+        if(removed == null)
+            throw new NullPointerException();
+        
+        this.diagram = diagram;
+        this.removedElement = removed;
+    }
+
+    @Override
+    public void perform(WriteGraph g) throws DatabaseException {
+        Commands.get(g, "Simantics/Diagram/removeElement")
+                .execute(g, g.syncRequest(new IndexRoot(diagram)), diagram, removedElement);
+    }
+    
+    public static void removeElement(WriteGraph g, Resource diagram, Resource removedElement) throws DatabaseException {
+        boolean connection = g.isInstanceOf(removedElement, DiagramResource.getInstance(g).Connection);
+        String elementName = "";
+        // Remove element from all layers if possible.
+        DiagramResource DIA = DiagramResource.getInstance(g);
+        g.deny(removedElement, DIA.IsVisible);
+        g.deny(removedElement, DIA.IsFocusable);
+
+        if (connection) {
+            ConnectionUtil cu = new ConnectionUtil(g);
+            cu.removeConnection(removedElement);
+        } else {
+            Remover r = RemoverUtil.getPossibleRemover(g, removedElement);
+            if (r != null) {
+                String problem = r.canRemove(g, new HashMap<Object, Object>(4));
+                if (problem != null) {
+                    throw new CannotRemoveException(problem);
+                }
+            }
+
+            // Perform custom removals through ElementWriter adaption.
+            // FIXME: should be able to use getSingleType if everything else goes correctly??
+            Resource elementType = g.getPossibleType(removedElement, DiagramResource.getInstance(g).Element);
+            if (elementType != null) {
+                ElementWriter writer = g.adapt(elementType, ElementWriter.class);
+                writer.removeFromGraph(g, removedElement);
+            }
+
+            // Remove element from diagram
+            OrderedSetUtils.remove(g, diagram, removedElement);
+            elementName = g.getPossibleRelatedValue2(removedElement, Layer0.getInstance(g).HasName, Bindings.STRING);
+            RemoverUtil.remove(g, removedElement);
+        }
+
+        // Add comment to change set.
+        CommentMetadata cm = g.getMetadata(CommentMetadata.class);
+        g.addMetadata(cm.add("Removed element " + elementName + " " + removedElement));
+    }
+}