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