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.Collection;
16 import java.util.List;
18 import org.simantics.utils.datastructures.MapList;
20 public abstract class ParentNode<T extends INode> extends Node {
22 private MapList<String, T> children = new MapList<String, T>();
24 public synchronized void addNode(String relName, T child) {
25 if (child.getParent() != null)
26 child.getParent().deattachNode(child.getParentRel(), child);
28 child.setParent(this, relName);
29 children.add(relName, (T) child);
32 fireNodeAdded(child, relName);
36 * Removes child node and it's hierarchy.
41 @SuppressWarnings("unchecked")
42 public synchronized final boolean removeNode(String relName, INode child) {
43 if (children.remove(relName, (T) child)) {
44 fireNodeRemoved(child, relName);
46 child.setParent(null, null);
53 * Removes child node. The child nodes hierarchy is left intact.
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);
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);
74 child.setParent(null, null);
78 return nodes.size() > 0;
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)) {
88 if (child instanceof ParentNode<?>) {
89 ((ParentNode<?>) child).removeNodes();
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
108 public synchronized List<T> getNodes(String rel) {
109 return children.getValues(rel);
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));
119 protected void childrenChanged() {
124 public void remove() {
125 synchronized (children) {
126 List<T> toRemove = new ArrayList<T>();
128 for (String key : children.getKeys()) {
130 for (T child : children.getValues(key)) {
137 for (T n : toRemove) {
142 if (toRemove.size() > 0)
152 protected void fireNodeAdded(INode node, String rel) {
153 for (NodeListener listener : listeners) {
154 listener.nodeAdded(this, node, rel);
158 protected void fireNodeRemoved(INode node, String rel) {
159 for (NodeListener listener : listeners) {
160 listener.nodeRemoved(this, node, rel);