]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeParent.java
a086c17aa54ba19b4a293a2f31f7972b42340daf
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / TreeParent.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.eclipse.ui;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 /** Parent / children semantic to be used for simple UI Tree structure */
23 public class TreeParent {
24 private String name;
25 private TreeParent parent;
26
27 private List<Object> children;
28
29 /** False until at least one child has been added, then true until cleared */
30 private boolean loaded = false;
31
32 public TreeParent(String name) {
33 this.name = name;
34 children = new ArrayList<Object>();
35 }
36
37 public synchronized void addChild(Object child) {
38 loaded = true;
39 children.add(child);
40 if (child instanceof TreeParent)
41 ((TreeParent) child).setParent(this);
42 }
43
44 /**
45 * Remove this child. The child is disposed.
46 */
47 public synchronized void removeChild(Object child) {
48 children.remove(child);
49 if (child instanceof TreeParent) {
50 ((TreeParent) child).dispose();
51 }
52 }
53
54 public synchronized void clearChildren() {
55 for (Object obj : children) {
56 if (obj instanceof TreeParent)
57 ((TreeParent) obj).dispose();
58 }
59 loaded = false;
60 children.clear();
61 }
62
63 /**
64 * If overridden, <code>super.dispose()</code> must be called, typically
65 * after custom cleaning.
66 */
67 public synchronized void dispose() {
68 clearChildren();
69 parent = null;
70 children = null;
71 }
72
73 public synchronized Object[] getChildren() {
74 return children.toArray(new Object[children.size()]);
75 }
76
77 @SuppressWarnings("unchecked")
78 public synchronized <T> List<T> getChildrenOfType(Class<T> clss) {
79 List<T> lst = new ArrayList<T>();
80 for (Object obj : children) {
81 if (clss.isAssignableFrom(obj.getClass()))
82 lst.add((T) obj);
83 }
84 return lst;
85 }
86
87 public synchronized boolean hasChildren() {
88 return children.size() > 0;
89 }
90
91 public Object getChildByName(String name) {
92 for (Object child : children) {
93 if (child.toString().equals(name))
94 return child;
95 }
96 return null;
97 }
98
99 public synchronized Boolean isLoaded() {
100 return loaded;
101 }
102
103 public String getName() {
104 return name;
105 }
106
107 public void setParent(TreeParent parent) {
108 this.parent = parent;
109 }
110
111 public TreeParent getParent() {
112 return parent;
113 }
114
115 public String toString() {
116 return getName();
117 }
118
119 public int compareTo(TreeParent o) {
120 return name.compareTo(o.name);
121 }
122
123 @Override
124 public int hashCode() {
125 return name.hashCode();
126 }
127
128 @Override
129 public boolean equals(Object obj) {
130 return name.equals(obj.toString());
131 }
132
133 }