]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeParent.java
26bdbe615135a0bd9a6b0a76ad6280623c07de84
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / TreeParent.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.eclipse.ui;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 /** Parent / children semantic to be used for simple UI Tree structure */
22 public class TreeParent {
23 private String name;
24 private TreeParent parent;
25
26 private List<Object> children;
27
28 /** False until at least one child has been added, then true until cleared */
29 private boolean loaded = false;
30
31 public TreeParent(String name) {
32 this.name = name;
33 children = new ArrayList<Object>();
34 }
35
36 public synchronized void addChild(Object child) {
37 loaded = true;
38 children.add(child);
39 if (child instanceof TreeParent)
40 ((TreeParent) child).setParent(this);
41 }
42
43 /**
44 * Remove this child. The child is disposed.
45 */
46 public synchronized void removeChild(Object child) {
47 children.remove(child);
48 if (child instanceof TreeParent) {
49 ((TreeParent) child).dispose();
50 }
51 }
52
53 public synchronized void clearChildren() {
54 for (Object obj : children) {
55 if (obj instanceof TreeParent)
56 ((TreeParent) obj).dispose();
57 }
58 loaded = false;
59 children.clear();
60 }
61
62 /**
63 * If overridden, <code>super.dispose()</code> must be called, typically
64 * after custom cleaning.
65 */
66 public synchronized void dispose() {
67 clearChildren();
68 parent = null;
69 children = null;
70 }
71
72 public synchronized Object[] getChildren() {
73 return children.toArray(new Object[children.size()]);
74 }
75
76 @SuppressWarnings("unchecked")
77 public synchronized <T> List<T> getChildrenOfType(Class<T> clss) {
78 List<T> lst = new ArrayList<T>();
79 for (Object obj : children) {
80 if (clss.isAssignableFrom(obj.getClass()))
81 lst.add((T) obj);
82 }
83 return lst;
84 }
85
86 public synchronized boolean hasChildren() {
87 return children.size() > 0;
88 }
89
90 public Object getChildByName(String name) {
91 for (Object child : children) {
92 if (child.toString().equals(name))
93 return child;
94 }
95 return null;
96 }
97
98 public synchronized Boolean isLoaded() {
99 return loaded;
100 }
101
102 public String getName() {
103 return name;
104 }
105
106 public void setParent(TreeParent parent) {
107 this.parent = parent;
108 }
109
110 public TreeParent getParent() {
111 return parent;
112 }
113
114 public String toString() {
115 return getName();
116 }
117
118 public int compareTo(TreeParent o) {
119 return name.compareTo(o.name);
120 }
121
122 @Override
123 public int hashCode() {
124 return name.hashCode();
125 }
126
127 @Override
128 public boolean equals(Object obj) {
129 return name.equals(obj.toString());
130 }
131
132 }