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