1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.gui.panels.projections;
17
18 import java.awt.BorderLayout;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.swing.JPanel;
23 import javax.swing.tree.DefaultMutableTreeNode;
24 import javax.swing.tree.DefaultTreeModel;
25 import javax.swing.tree.TreeNode;
26 import javax.swing.tree.TreePath;
27
28 import p3j.misc.Misc;
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class ProjectionTreeNode<E> extends DefaultMutableTreeNode {
43
44
45 private static final long serialVersionUID = 1987698685076459120L;
46
47
48 private final E entity;
49
50
51 private final Class<?> entityClass;
52
53
54 private JPanel contentPanel;
55
56
57
58
59
60
61
62
63
64 public ProjectionTreeNode(E pppmEntity, String name) {
65 super(name);
66 entity = pppmEntity;
67 entityClass = pppmEntity.getClass();
68 }
69
70 public E getEntity() {
71 return entity;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public void selected(TreePath selectionPath, JPanel generalPanel,
87 IProjectionTree projTree) {
88 contentPanel = selected(selectionPath, projTree);
89 if (contentPanel != null) {
90 generalPanel.add(contentPanel, BorderLayout.CENTER);
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public JPanel selected(TreePath selectionPath, IProjectionTree projTree) {
109 return null;
110 }
111
112
113
114
115
116
117
118 protected void refreshRepresentation() {
119 }
120
121
122
123
124
125
126 public void deselected() {
127 }
128
129 public Class<?> getEntityClass() {
130 return entityClass;
131 }
132
133 public JPanel getContentPanel() {
134 return contentPanel;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148 public <T> T getProjectionEntity(Class<T> targetClass) {
149 return getProjectionEntity(targetClass, this);
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 @SuppressWarnings("unchecked")
167 public <T> T getProjectionEntity(Class<T> targetClass,
168 ProjectionTreeNode<?> sourceNode) {
169 if (Misc.checkClassEquality(sourceNode.getEntityClass(), targetClass)) {
170 return (T) sourceNode.getEntity();
171 }
172 TreeNode parent = sourceNode.getParent();
173 if (!(parent instanceof ProjectionTreeNode<?>)) {
174 return null;
175 }
176
177 return getProjectionEntity(targetClass, (ProjectionTreeNode<?>) parent);
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191 @SuppressWarnings("unchecked")
192 public <T> T getProjectionTreeNodeOnPath(Class<T> targetClass) {
193 TreeNode treeNode = getParent();
194 while (treeNode != null) {
195 if (treeNode.getClass().equals(targetClass)) {
196 return (T) treeNode;
197 }
198 treeNode = treeNode.getParent();
199 }
200 return null;
201 }
202
203
204
205
206
207
208
209
210
211 public ProjectionTreeNode<?> getChildWithEntity(Object childEntity) {
212 if (getEntity() == childEntity) {
213 return this;
214 }
215 for (int i = 0; i < this.getChildCount(); i++) {
216 ProjectionTreeNode<?> child = (ProjectionTreeNode<?>) this.getChildAt(i);
217 ProjectionTreeNode<?> result = child.getChildWithEntity(childEntity);
218 if (result != null) {
219 return result;
220 }
221 }
222 return null;
223 }
224
225
226
227
228
229
230
231
232
233
234
235 public <X> List<ProjectionTreeNode<X>> getChildsByType(Class<X> entityType) {
236 return getChildsByType(new ArrayList<ProjectionTreeNode<X>>(), entityType);
237 }
238
239 @SuppressWarnings("unchecked")
240 protected <X> List<ProjectionTreeNode<X>> getChildsByType(
241 List<ProjectionTreeNode<X>> list, Class<X> entityType) {
242 if (getEntity() != null
243 && Misc.checkClassEquality(this.getEntityClass(), entityType)) {
244 list.add((ProjectionTreeNode<X>) this);
245 }
246 for (int i = 0; i < this.getChildCount(); i++) {
247 ProjectionTreeNode<?> node = (ProjectionTreeNode<?>) getChildAt(i);
248 node.getChildsByType(list, entityType);
249 }
250 return list;
251 }
252
253
254
255
256
257
258
259
260 protected void refreshRecursively(DefaultTreeModel treeModel) {
261 refreshRepresentation();
262 treeModel.nodeChanged(this);
263 for (int i = 0; i < getChildCount(); i++) {
264 ProjectionTreeNode<?> node = (ProjectionTreeNode<?>) getChildAt(i);
265 node.refreshRecursively(treeModel);
266 }
267 }
268
269
270
271
272
273
274 public List<ProjectionTreeNode<?>> getChilds() {
275 List<ProjectionTreeNode<?>> result = new ArrayList<ProjectionTreeNode<?>>();
276 for (int i = 0; i < getChildCount(); i++) {
277 result.add((ProjectionTreeNode<?>) getChildAt(i));
278 }
279 return result;
280 }
281
282
283
284
285
286
287 public String getEntityLabel() {
288 return this.getEntity().toString();
289 }
290
291 }