View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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  	/**
29  	 * Unique id within the context of a tree display. If set, equals() and
30  	 * hashCode() methods will be based on it
31  	 */
32  	private String path = null;
33  
34  	/** False until at least one child has been added, then true until cleared */
35  	private boolean loaded = false;
36  
37  	public TreeParent(String name) {
38  		this.name = name;
39  		children = new ArrayList<Object>();
40  	}
41  
42  	public synchronized void addChild(Object child) {
43  		loaded = true;
44  		children.add(child);
45  		if (child instanceof TreeParent)
46  			((TreeParent) child).setParent(this);
47  	}
48  
49  	/**
50  	 * Remove this child. The child is disposed.
51  	 */
52  	public synchronized void removeChild(Object child) {
53  		children.remove(child);
54  		if (child instanceof TreeParent) {
55  			((TreeParent) child).dispose();
56  		}
57  	}
58  
59  	public synchronized void clearChildren() {
60  		for (Object obj : children) {
61  			if (obj instanceof TreeParent)
62  				((TreeParent) obj).dispose();
63  		}
64  		loaded = false;
65  		children.clear();
66  	}
67  
68  	/**
69  	 * If overridden, <code>super.dispose()</code> must be called, typically
70  	 * after custom cleaning.
71  	 */
72  	public synchronized void dispose() {
73  		clearChildren();
74  		parent = null;
75  		children = null;
76  	}
77  
78  	public synchronized Object[] getChildren() {
79  		return children.toArray(new Object[children.size()]);
80  	}
81  
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()))
87  				lst.add((T) obj);
88  		}
89  		return lst;
90  	}
91  
92  	public synchronized boolean hasChildren() {
93  		return children.size() > 0;
94  	}
95  
96  	public Object getChildByName(String name) {
97  		for (Object child : children) {
98  			if (child.toString().equals(name))
99  				return child;
100 		}
101 		return null;
102 	}
103 
104 	public synchronized Boolean isLoaded() {
105 		return loaded;
106 	}
107 
108 	public String getName() {
109 		return name;
110 	}
111 
112 	public void setParent(TreeParent parent) {
113 		this.parent = parent;
114 		if (parent != null && parent.path != null)
115 			this.path = parent.path + '/' + name;
116 		else
117 			this.path = '/' + name;
118 	}
119 
120 	public TreeParent getParent() {
121 		return parent;
122 	}
123 
124 	public String toString() {
125 		return getName();
126 	}
127 
128 	public int compareTo(TreeParent o) {
129 		return name.compareTo(o.name);
130 	}
131 
132 	@Override
133 	public int hashCode() {
134 		if (path != null)
135 			return path.hashCode();
136 		else
137 			return name.hashCode();
138 	}
139 
140 	@Override
141 	public boolean equals(Object obj) {
142 		if (path != null && obj instanceof TreeParent)
143 			return path.equals(((TreeParent) obj).path);
144 		else
145 			return name.equals(obj.toString());
146 	}
147 
148 }