]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/RemoveElement.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / RemoveElement.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/RemoveElement.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/RemoveElement.java
new file mode 100644 (file)
index 0000000..0d6ffc8
--- /dev/null
@@ -0,0 +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