]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/TreeParent.java
2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 /** Parent / children semantic to be used for simple UI Tree structure */
22 public class TreeParent
{
24 private TreeParent parent
;
26 private List
<Object
> children
;
29 * Unique id within the context of a tree display. If set, equals() and
30 * hashCode() methods will be based on it
32 private String path
= null;
34 /** False until at least one child has been added, then true until cleared */
35 private boolean loaded
= false;
37 public TreeParent(String name
) {
39 children
= new ArrayList
<Object
>();
42 public synchronized void addChild(Object child
) {
45 if (child
instanceof TreeParent
)
46 ((TreeParent
) child
).setParent(this);
50 * Remove this child. The child is disposed.
52 public synchronized void removeChild(Object child
) {
53 children
.remove(child
);
54 if (child
instanceof TreeParent
) {
55 ((TreeParent
) child
).dispose();
59 public synchronized void clearChildren() {
60 for (Object obj
: children
) {
61 if (obj
instanceof TreeParent
)
62 ((TreeParent
) obj
).dispose();
69 * If overridden, <code>super.dispose()</code> must be called, typically
70 * after custom cleaning.
72 public synchronized void dispose() {
78 public synchronized Object
[] getChildren() {
79 return children
.toArray(new Object
[children
.size()]);
82 @SuppressWarnings("unchecked")
83 public synchronized <T
> List
<T
> getChildrenOfType(Class
<T
> clss
) {
84 List
<T
> lst
= new ArrayList
<T
>();
85 for (Object obj
: children
) {
86 if (clss
.isAssignableFrom(obj
.getClass()))
92 public synchronized boolean hasChildren() {
93 return children
.size() > 0;
96 public Object
getChildByName(String name
) {
97 for (Object child
: children
) {
98 if (child
.toString().equals(name
))
104 public synchronized Boolean
isLoaded() {
108 public String
getName() {
112 public void setParent(TreeParent parent
) {
113 this.parent
= parent
;
114 if (parent
!= null && parent
.path
!= null)
115 this.path
= parent
.path
+ '/' + name
;
117 this.path
= '/' + name
;
120 public TreeParent
getParent() {
124 public String
toString() {
128 public int compareTo(TreeParent o
) {
129 return name
.compareTo(o
.name
);
133 public int hashCode() {
135 return path
.hashCode();
137 return name
.hashCode();
141 public boolean equals(Object obj
) {
142 if (path
!= null && obj
instanceof TreeParent
)
143 return path
.equals(((TreeParent
) obj
).path
);
145 return name
.equals(obj
.toString());