1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.scenegraph.base;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.simantics.utils.datastructures.MapList;
19 public abstract class ParentNode<T extends INode> extends Node {
21 private MapList<String, T> children = new MapList<String, T>();
23 public synchronized void addNode(String relName, T child) {
25 throw new NullPointerException("Cannot add null child");
26 if (child.getParent() != null) {
27 if (child.getParent() == this)
28 throw new IllegalArgumentException("Given node is already a child");
29 child.getParent().deattachNode(child.getParentRel(), child);
32 child.setParent(this, relName);
33 children.add(relName, (T) child);
36 fireNodeAdded(child, relName);
40 * Removes child node and it's hierarchy.
45 @SuppressWarnings("unchecked")
46 public synchronized final boolean removeNode(String relName, INode child) {
47 if (children.remove(relName, (T) child)) {
48 fireNodeRemoved(child, relName);
50 child.setParent(null, null);
57 * Removes child node. The child nodes hierarchy is left intact.
62 @SuppressWarnings("unchecked")
63 public synchronized final boolean deattachNode(String relName, INode child) {
64 if (children.remove(relName, (T) child)) {
65 fireNodeRemoved(child, relName);
66 child.setParent(null, null);
72 public synchronized final boolean removeNodes(String relName) {
73 List<T> nodes = children.getValues(relName);
74 for (T child : nodes) {
75 if (children.remove(relName, (T) child)) {
76 fireNodeRemoved(child, relName);
78 child.setParent(null, null);
82 return nodes.size() > 0;
85 public synchronized final void removeNodes() {
86 synchronized (children) {
87 boolean changed = false;
88 for (String key : children.getKeys()) {
89 for (T child : children.getValues(key)) {
92 if (child instanceof ParentNode<?>) {
93 ((ParentNode<?>) child).removeNodes();
96 child.setParent(null, null);
97 // if (propertyChangeListener != null) {
98 // propertyChangeListener.propertyChange(new
99 // PropertyChangeEvent(this,
100 // "children["+child.getId()+"]", child.getClass(),
101 // NULL)); // "children" is a special field name
112 public synchronized List<T> getNodes(String rel) {
113 return children.getValues(rel);
116 public synchronized List<T> getNodes() {
117 List<T> result = new ArrayList<T>();
118 for (String s : children.getKeys())
119 result.addAll(children.getValues(s));
123 protected void childrenChanged() {
128 public void remove() {
129 synchronized (children) {
130 List<T> toRemove = new ArrayList<T>();
132 for (String key : children.getKeys()) {
134 for (T child : children.getValues(key)) {
141 for (T n : toRemove) {
146 if (toRemove.size() > 0)
156 protected void fireNodeAdded(INode node, String rel) {
157 for (NodeListener listener : listeners) {
158 listener.nodeAdded(this, node, rel);
162 protected void fireNodeRemoved(INode node, String rel) {
163 for (NodeListener listener : listeners) {
164 listener.nodeRemoved(this, node, rel);