]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ElementRemover.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / ElementRemover.java
index 2699071465f9d90036a9f6ef7aa0e3b94a80c466..9b1ef75ea31a7ee9e24130820e6fa1f7e46f4efb 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012 Association for Decentralized Information Management in\r
- * 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.modeling.adapters;\r
-\r
-import gnu.trove.set.hash.THashSet;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Set;\r
-\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\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.adapter.impl.AbstractRemover;\r
-import org.simantics.db.layer0.adapter.impl.EntityRemover;\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.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-\r
-/**\r
- * A {@link Remover} implementation for removing DIA.Element instances.\r
- * \r
- * The implementation has separate paths for STR.Connection instances and other\r
- * DIA.Element instances. For DIA.Element this remover will:\r
- * <ol>\r
- * <li>List connections/monitors the element is attached to</li>\r
- * <li>Disconnect the element from its owner diagram(s)</li>\r
- * <li>Remove the element itself with EntityRemover default logic</li>\r
- * <li>Recursively remove all related degenerate connections, i.e. connections\r
- * that only have one connector after remove the one connected to this removed\r
- * element.</li>\r
- * <li>Recursively remove monitors attached to the removed element</li>\r
- * </ol>\r
- * \r
- * <p>\r
- * For STR.Connections this remover will:\r
- * <ol>\r
- * <li>List all related data (monitors, mapped data)</li> \r
- * <li>Remove the connection itself</li>\r
- * <li>Remove the related data</li>\r
- * </ol>\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class ElementRemover extends AbstractRemover {\r
-\r
-    private static final boolean DEBUG = false;\r
-\r
-    public ElementRemover(Resource element) {\r
-        super(element);\r
-    }\r
-\r
-    @Override\r
-    public void remove(WriteGraph graph) throws DatabaseException {\r
-        // 0. Find all connections to the removed element\r
-        StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
-\r
-        if (graph.isInstanceOf(resource, STR.Connection)) {\r
-            removeConnection(graph);\r
-        } else {\r
-            removeElement(graph);\r
-        }\r
-    }\r
-\r
-    public void removeElement(WriteGraph graph) throws DatabaseException {\r
-        if (DEBUG)\r
-            System.out.println(this + " removing element");\r
-\r
-        Set<Resource> connectors = null;\r
-\r
-        StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
-        DiagramResource DIA = DiagramResource.getInstance(graph);\r
-\r
-        Collection<Resource> connectedTo = graph.getObjects(resource, STR.IsConnectedTo);\r
-        for (Resource connector : connectedTo) {\r
-            if (connectors == null)\r
-                connectors = new THashSet<Resource>(connectedTo.size());\r
-            connectors.add(connector);\r
-        }\r
-        Collection<Resource> monitors = graph.getObjects(resource, DIA.HasMonitorComponent_Inverse);\r
-\r
-        if (DEBUG) {\r
-            if (connectors != null)\r
-                System.out.println(this + " found " + connectors.size() + " connectors connected to element");\r
-            if (!monitors.isEmpty())\r
-                System.out.println(this + " found " + monitors.size() + " monitors attached to element");\r
-        }\r
-\r
-        // 1. Disconnect element from diagrams\r
-        for (Resource diagram : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {\r
-            OrderedSetUtils.remove(graph, diagram, resource);\r
-        }\r
-\r
-        // 2. Delete element itself\r
-        EntityRemover.remove(graph, resource);\r
-\r
-        // 3. Recursively remove all related degenerate connections\r
-        // i.e. connections that only have one connector after remove the\r
-        // one connected to this removed element.\r
-        ConnectionUtil cu = null;\r
-        Set<Resource> connectionsToRemove = null;\r
-        if (connectors != null) {\r
-            cu = new ConnectionUtil(graph);\r
-            Set<Resource> touchedConnections = new THashSet<Resource>(connectors.size());\r
-            for (Resource connector : connectors) {\r
-                Resource connection = ConnectionUtil.tryGetConnection(graph, connector);\r
-                if (connection != null)\r
-                    touchedConnections.add(connection);\r
-                cu.removeConnectionPart(connector);\r
-            }\r
-            for (Resource connection : touchedConnections) {\r
-                int removedConnectors = cu.removeUnusedConnectors(connection);\r
-                if (DEBUG)\r
-                    System.out.println(this + " PRUNED " + removedConnectors + " UNUSED CONNECTORS FROM TOUCHED CONNECTION " + connection);\r
-                while (true) {\r
-                    int removedInteriorRouteNodes = cu.removeExtraInteriorRouteNodes(connection);\r
-                    if (DEBUG)\r
-                        System.out.println(this + " PRUNED " + removedInteriorRouteNodes + " INTERIOR ROUTE NODES FROM TOUCHED CONNECTION " + connection);\r
-                    if (removedInteriorRouteNodes == 0)\r
-                        break;\r
-                }\r
-                int connectorCount = cu.getConnectedConnectors(connection, null).size();\r
-                if (DEBUG)\r
-                    System.out.println(this + " \t" + connectorCount + " CONNECTORS LEFT");\r
-                if (connectorCount < 2) {\r
-                    if (connectionsToRemove == null)\r
-                        connectionsToRemove = new THashSet<Resource>(touchedConnections.size());\r
-                    connectionsToRemove.add(connection);\r
-                }\r
-            }\r
-        }\r
-        if (connectionsToRemove != null) {\r
-            for (Resource connection : connectionsToRemove) {\r
-                if (DEBUG)\r
-                    System.out.println(this + " REMOVING connection " + connection);\r
-                RemoverUtil.remove(graph, connection);\r
-            }\r
-        }\r
-\r
-        // 4. Recursively remove monitors attached to element\r
-        if (!monitors.isEmpty())\r
-            for (Resource monitor : monitors)\r
-                RemoverUtil.remove(graph, monitor);\r
-\r
-    }\r
-\r
-    public void removeConnection(WriteGraph graph) throws DatabaseException {\r
-        if (DEBUG)\r
-            System.out.println(this + " removing connection element");\r
-\r
-        DiagramResource DIA = DiagramResource.getInstance(graph);\r
-        ModelingResources MOD = ModelingResources.getInstance(graph);\r
-\r
-        // 0. Get set of related data\r
-        Collection<Resource> objects = new ArrayList<Resource>();\r
-        for (Resource relation : new Resource[] {\r
-                DIA.HasMonitorComponent_Inverse,\r
-                MOD.ElementToComponent,\r
-                MOD.DiagramConnectionToConnection,\r
-                MOD.DiagramConnectionToConnectionSpecial\r
-        }) {\r
-            for (Resource object : graph.getObjects(resource, relation)) {\r
-                if (!object.equals(resource))\r
-                    objects.add(object);\r
-            }\r
-        }\r
-\r
-        // 1. Remove connection itself\r
-        new ConnectionUtil(graph).removeConnection(resource);\r
-\r
-        // 2. Recursively remove related data\r
-        if (!objects.isEmpty())\r
-            for (Resource object : objects)\r
-                RemoverUtil.remove(graph, object);\r
-    }\r
-\r
-    @Override\r
-    public String toString() {\r
-        return getClass().getSimpleName() + resource;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012 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.modeling.adapters;
+
+import gnu.trove.set.hash.THashSet;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Set;
+
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+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.adapter.impl.AbstractRemover;
+import org.simantics.db.layer0.adapter.impl.EntityRemover;
+import org.simantics.db.layer0.util.RemoverUtil;
+import org.simantics.diagram.content.ConnectionUtil;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.structural.stubs.StructuralResource2;
+
+/**
+ * A {@link Remover} implementation for removing DIA.Element instances.
+ * 
+ * The implementation has separate paths for STR.Connection instances and other
+ * DIA.Element instances. For DIA.Element this remover will:
+ * <ol>
+ * <li>List connections/monitors the element is attached to</li>
+ * <li>Disconnect the element from its owner diagram(s)</li>
+ * <li>Remove the element itself with EntityRemover default logic</li>
+ * <li>Recursively remove all related degenerate connections, i.e. connections
+ * that only have one connector after remove the one connected to this removed
+ * element.</li>
+ * <li>Recursively remove monitors attached to the removed element</li>
+ * </ol>
+ * 
+ * <p>
+ * For STR.Connections this remover will:
+ * <ol>
+ * <li>List all related data (monitors, mapped data)</li> 
+ * <li>Remove the connection itself</li>
+ * <li>Remove the related data</li>
+ * </ol>
+ * 
+ * @author Tuukka Lehtonen
+ */
+public class ElementRemover extends AbstractRemover {
+
+    private static final boolean DEBUG = false;
+
+    public ElementRemover(Resource element) {
+        super(element);
+    }
+
+    @Override
+    public void remove(WriteGraph graph) throws DatabaseException {
+        // 0. Find all connections to the removed element
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+        if (graph.isInstanceOf(resource, STR.Connection)) {
+            removeConnection(graph);
+        } else {
+            removeElement(graph);
+        }
+    }
+
+    public void removeElement(WriteGraph graph) throws DatabaseException {
+        if (DEBUG)
+            System.out.println(this + " removing element");
+
+        Set<Resource> connectors = null;
+
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+
+        Collection<Resource> connectedTo = graph.getObjects(resource, STR.IsConnectedTo);
+        for (Resource connector : connectedTo) {
+            if (connectors == null)
+                connectors = new THashSet<Resource>(connectedTo.size());
+            connectors.add(connector);
+        }
+        Collection<Resource> monitors = graph.getObjects(resource, DIA.HasMonitorComponent_Inverse);
+
+        if (DEBUG) {
+            if (connectors != null)
+                System.out.println(this + " found " + connectors.size() + " connectors connected to element");
+            if (!monitors.isEmpty())
+                System.out.println(this + " found " + monitors.size() + " monitors attached to element");
+        }
+
+        // 1. Disconnect element from diagrams
+        for (Resource diagram : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {
+            OrderedSetUtils.remove(graph, diagram, resource);
+        }
+
+        // 2. Delete element itself
+        EntityRemover.remove(graph, resource);
+
+        // 3. Recursively remove all related degenerate connections
+        // i.e. connections that only have one connector after remove the
+        // one connected to this removed element.
+        ConnectionUtil cu = null;
+        Set<Resource> connectionsToRemove = null;
+        if (connectors != null) {
+            cu = new ConnectionUtil(graph);
+            Set<Resource> touchedConnections = new THashSet<Resource>(connectors.size());
+            for (Resource connector : connectors) {
+                Resource connection = ConnectionUtil.tryGetConnection(graph, connector);
+                if (connection != null)
+                    touchedConnections.add(connection);
+                cu.removeConnectionPart(connector);
+            }
+            for (Resource connection : touchedConnections) {
+                int removedConnectors = cu.removeUnusedConnectors(connection);
+                if (DEBUG)
+                    System.out.println(this + " PRUNED " + removedConnectors + " UNUSED CONNECTORS FROM TOUCHED CONNECTION " + connection);
+                while (true) {
+                    int removedInteriorRouteNodes = cu.removeExtraInteriorRouteNodes(connection);
+                    if (DEBUG)
+                        System.out.println(this + " PRUNED " + removedInteriorRouteNodes + " INTERIOR ROUTE NODES FROM TOUCHED CONNECTION " + connection);
+                    if (removedInteriorRouteNodes == 0)
+                        break;
+                }
+                int connectorCount = cu.getConnectedConnectors(connection, null).size();
+                if (DEBUG)
+                    System.out.println(this + " \t" + connectorCount + " CONNECTORS LEFT");
+                if (connectorCount < 2) {
+                    if (connectionsToRemove == null)
+                        connectionsToRemove = new THashSet<Resource>(touchedConnections.size());
+                    connectionsToRemove.add(connection);
+                }
+            }
+        }
+        if (connectionsToRemove != null) {
+            for (Resource connection : connectionsToRemove) {
+                if (DEBUG)
+                    System.out.println(this + " REMOVING connection " + connection);
+                RemoverUtil.remove(graph, connection);
+            }
+        }
+
+        // 4. Recursively remove monitors attached to element
+        if (!monitors.isEmpty())
+            for (Resource monitor : monitors)
+                RemoverUtil.remove(graph, monitor);
+
+    }
+
+    public void removeConnection(WriteGraph graph) throws DatabaseException {
+        if (DEBUG)
+            System.out.println(this + " removing connection element");
+
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+        ModelingResources MOD = ModelingResources.getInstance(graph);
+
+        // 0. Get set of related data
+        Collection<Resource> objects = new ArrayList<Resource>();
+        for (Resource relation : new Resource[] {
+                DIA.HasMonitorComponent_Inverse,
+                MOD.ElementToComponent,
+                MOD.DiagramConnectionToConnection,
+                MOD.DiagramConnectionToConnectionSpecial
+        }) {
+            for (Resource object : graph.getObjects(resource, relation)) {
+                if (!object.equals(resource))
+                    objects.add(object);
+            }
+        }
+
+        // 1. Remove connection itself
+        new ConnectionUtil(graph).removeConnection(resource);
+
+        // 2. Recursively remove related data
+        if (!objects.isEmpty())
+            for (Resource object : objects)
+                RemoverUtil.remove(graph, object);
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + resource;
+    }
+
+}