]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/scenegraph/base/Node.java
f7fe9eded0e0d1e606814e5b6556af2da85b5096
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / scenegraph / base / Node.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 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.g3d.scenegraph.base;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 \r
18 public abstract class Node implements INode {\r
19         public transient static long               IDCOUNTER              = 1;\r
20         protected transient ParentNode<?>          parent                 = null;\r
21         protected transient String                                 parentName                     = null; \r
22         protected Long                             id                     = IDCOUNTER++;\r
23         \r
24 \r
25         public Long getId() {\r
26                 return id;\r
27         }\r
28 \r
29 \r
30         public ParentNode<?> getParent() {\r
31                 return parent;\r
32         }\r
33 \r
34         \r
35         @Override\r
36         public String getParentRel() {\r
37                 return parentName;\r
38         }\r
39         \r
40         public void setParent(ParentNode<?> parent, String name) {\r
41                 this.parent = parent;\r
42                 this.parentName = name;\r
43         }\r
44         \r
45         public ParentNode<?> getRootNode() {\r
46                 return parent != null ? parent.getRootNode() : null;\r
47         }\r
48         \r
49         public void remove() {\r
50                 if (parent != null) {\r
51                         parent.removeNode(parentName, this);\r
52                 }\r
53         }\r
54         \r
55         public void deattach() {\r
56                 if (parent != null) {\r
57                         parent.deattachNode(parentName, this);\r
58                 }\r
59         }\r
60         \r
61         public void init() {\r
62         }\r
63         \r
64         public void cleanup() {\r
65                 if (parent != null) {\r
66                         parent.removeNode(parentName, this);\r
67                 }\r
68         }\r
69         \r
70         @Override\r
71         public String toString() {\r
72                 return getClass().getSimpleName();\r
73         }\r
74         \r
75         protected List<NodeListener> listeners = new ArrayList<NodeListener>();\r
76         \r
77         @Override\r
78         public void addListener(NodeListener listener) {\r
79                 if (!listeners.contains(listener))\r
80                         listeners.add(listener);\r
81         }\r
82         \r
83         @Override\r
84         public void removeListener(NodeListener listener) {\r
85                 listeners.remove(listener);\r
86         }\r
87         \r
88         @Override\r
89         public List<NodeListener> getListeners() {\r
90                 return listeners;\r
91         }\r
92         \r
93         protected void firePropertyChanged(String id) {\r
94                 for (NodeListener listener : listeners) {\r
95                         listener.propertyChanged(this, id);\r
96                 }\r
97         }\r
98         \r
99 }\r