]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.scenegraph.loader;\r
13 \r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.WriteGraph;\r
16 import org.simantics.db.common.utils.ListUtils;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.db.layer0.adapter.Remover;\r
19 import org.simantics.db.layer0.adapter.impl.AbstractRemover;\r
20 import org.simantics.db.layer0.adapter.impl.EntityRemover;\r
21 import org.simantics.layer0.Layer0;\r
22 import org.simantics.scenegraph.ontology.ScenegraphResources;\r
23 \r
24 /**\r
25  * A {@link Remover} implementation for removing SG.Node instances.\r
26  * \r
27  * <p>\r
28  * In addition to removing the node instance itself, this remover first tries to\r
29  * remove the node from the <em>children</em> linked list of its possible parent\r
30  * node.\r
31  * \r
32  * @author Tuukka Lehtonen\r
33  */\r
34 public class NodeRemover extends AbstractRemover {\r
35 \r
36     private static final boolean DEBUG = false;\r
37 \r
38     public NodeRemover(Resource node) throws DatabaseException {\r
39         super(node);\r
40     }\r
41 \r
42     @Override\r
43     public void remove(WriteGraph graph) throws DatabaseException {\r
44         if (DEBUG)\r
45             System.out.println(this + " removing scene graph node");\r
46 \r
47         Layer0 L0 = Layer0.getInstance(graph);\r
48         ScenegraphResources SG = ScenegraphResources.getInstance(graph);\r
49 \r
50         // 1. Remove node from possible parent resource linked list.\r
51         for (Resource parentNode : graph.getObjects(resource, L0.PartOf)) {\r
52             for (Resource list : graph.getObjects(parentNode, SG.Node_children)) {\r
53                 ListUtils.removeElement(graph, list, resource);\r
54             }\r
55         }\r
56 \r
57         // 2. Delete node itself\r
58         EntityRemover.remove(graph, resource);\r
59     }\r
60 \r
61     @Override\r
62     public String toString() {\r
63         return getClass().getSimpleName() + resource;\r
64     }\r
65 \r
66 }\r