]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph.loader/src/org/simantics/scenegraph/loader/NodeRemover.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph.loader / src / org / simantics / scenegraph / loader / NodeRemover.java
diff --git a/bundles/org.simantics.scenegraph.loader/src/org/simantics/scenegraph/loader/NodeRemover.java b/bundles/org.simantics.scenegraph.loader/src/org/simantics/scenegraph/loader/NodeRemover.java
new file mode 100644 (file)
index 0000000..7bb4b2f
--- /dev/null
@@ -0,0 +1,66 @@
+/*******************************************************************************\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.scenegraph.loader;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.ListUtils;\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.layer0.Layer0;\r
+import org.simantics.scenegraph.ontology.ScenegraphResources;\r
+\r
+/**\r
+ * A {@link Remover} implementation for removing SG.Node instances.\r
+ * \r
+ * <p>\r
+ * In addition to removing the node instance itself, this remover first tries to\r
+ * remove the node from the <em>children</em> linked list of its possible parent\r
+ * node.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class NodeRemover extends AbstractRemover {\r
+\r
+    private static final boolean DEBUG = false;\r
+\r
+    public NodeRemover(Resource node) throws DatabaseException {\r
+        super(node);\r
+    }\r
+\r
+    @Override\r
+    public void remove(WriteGraph graph) throws DatabaseException {\r
+        if (DEBUG)\r
+            System.out.println(this + " removing scene graph node");\r
+\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        ScenegraphResources SG = ScenegraphResources.getInstance(graph);\r
+\r
+        // 1. Remove node from possible parent resource linked list.\r
+        for (Resource parentNode : graph.getObjects(resource, L0.PartOf)) {\r
+            for (Resource list : graph.getObjects(parentNode, SG.Node_children)) {\r
+                ListUtils.removeElement(graph, list, resource);\r
+            }\r
+        }\r
+\r
+        // 2. Delete node itself\r
+        EntityRemover.remove(graph, resource);\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return getClass().getSimpleName() + resource;\r
+    }\r
+\r
+}\r