]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/scenegraph/base/ParentNode.java
White space clean-up
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / scenegraph / base / ParentNode.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 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.g3d.scenegraph.base;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.simantics.utils.datastructures.MapList;
19
20 public abstract class ParentNode<T extends INode> extends Node {
21
22         private MapList<String, T> children = new MapList<String, T>();
23
24         public synchronized void addNode(String relName, T child) {
25                 if (child.getParent() != null)
26                         child.getParent().deattachNode(child.getParentRel(), child);
27
28                 child.setParent(this, relName);
29                 children.add(relName, (T) child);
30
31                 childrenChanged();
32                 fireNodeAdded(child, relName);
33         }
34
35         /**
36          * Removes child node and it's hierarchy.
37          * @param relName
38          * @param child
39          * @return
40          */
41         @SuppressWarnings("unchecked")
42         public synchronized final boolean removeNode(String relName, INode child) {
43                 if (children.remove(relName, (T) child)) {
44                         fireNodeRemoved(child, relName);
45                         child.remove();
46                         child.setParent(null, null);
47                         return true;
48                 }
49                 return false;
50         }
51         
52         /**
53          * Removes child node. The child nodes hierarchy is left intact.
54          * @param relName
55          * @param child
56          * @return
57          */
58         @SuppressWarnings("unchecked")
59         public synchronized final boolean deattachNode(String relName, INode child) {
60                 if (children.remove(relName, (T) child)) {
61                         fireNodeRemoved(child, relName);
62                         child.setParent(null, null);
63                         return true;
64                 }
65                 return false;
66         }
67         
68         public synchronized final boolean removeNodes(String relName) {
69                 List<T> nodes = children.getValues(relName);
70                 for (T child : nodes) {
71                         if (children.remove(relName, (T) child)) {
72                                 fireNodeRemoved(child, relName);
73                                 child.remove();
74                                 child.setParent(null, null);
75                                 
76                         }
77                 }
78                 return nodes.size() > 0;
79         }
80
81         public synchronized final void removeNodes() {
82                 synchronized (children) {
83                         boolean changed = false;
84                         for (String key : children.getKeys()) {
85                                 for (T child : children.getValues(key)) {
86                                         if (child != null) {
87                                                 changed = true;
88                                                 if (child instanceof ParentNode<?>) {
89                                                         ((ParentNode<?>) child).removeNodes();
90                                                 }
91                                                 child.cleanup();
92                                                 child.setParent(null, null);
93                                                 // if (propertyChangeListener != null) {
94                                                 // propertyChangeListener.propertyChange(new
95                                                 // PropertyChangeEvent(this,
96                                                 // "children["+child.getId()+"]", child.getClass(),
97                                                 // NULL)); // "children" is a special field name
98                                                 // }
99                                         }
100                                 }
101                         }
102                         children.clear();
103                         if (changed)
104                                 childrenChanged();
105                 }
106         }
107
108         public synchronized List<T> getNodes(String rel) {
109                 return children.getValues(rel);
110         }
111
112         public synchronized List<T> getNodes() {
113                 List<T> result = new ArrayList<T>();
114                 for (String s : children.getKeys())
115                         result.addAll(children.getValues(s));
116                 return result;
117         }
118
119         protected void childrenChanged() {
120         }
121
122
123         @Override
124         public void remove() {
125                 synchronized (children) {
126                         List<T> toRemove = new ArrayList<T>();
127                 
128                         for (String key : children.getKeys()) {
129                 
130                                 for (T child : children.getValues(key)) {
131                                         if (child != null) {
132                                                 toRemove.add(child);
133                                         }
134                                 }
135                         }
136
137                         for (T n : toRemove) {
138                                 n.remove();
139                         }
140                         
141                         children.clear();
142                         if (toRemove.size() > 0)
143                                 childrenChanged();
144                         super.remove();
145                         
146                 }
147         }
148         
149         
150         
151         
152         protected void fireNodeAdded(INode node, String rel) {
153                 for (NodeListener listener : listeners) {
154                         listener.nodeAdded(this, node, rel);
155                 }
156         }
157         
158         protected void fireNodeRemoved(INode node, String rel) {
159                 for (NodeListener listener : listeners) {
160                         listener.nodeRemoved(this, node, rel);
161                 }
162         }
163         
164 }