]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ux/src/org/argeo/cms/ux/widgets/TreeParent.java
1 package org
.argeo
.cms
.ux
.widgets
;
3 import java
.util
.ArrayList
;
6 /** Parent / children semantic to be used for simple UI Tree structure */
7 public class TreeParent
{
9 private TreeParent parent
;
11 private List
<Object
> children
;
14 * Unique id within the context of a tree display. If set, equals() and
15 * hashCode() methods will be based on it
17 private String path
= null;
19 /** False until at least one child has been added, then true until cleared */
20 private boolean loaded
= false;
22 public TreeParent(String name
) {
24 children
= new ArrayList
<Object
>();
27 public synchronized void addChild(Object child
) {
30 if (child
instanceof TreeParent
)
31 ((TreeParent
) child
).setParent(this);
35 * Remove this child. The child is disposed.
37 public synchronized void removeChild(Object child
) {
38 children
.remove(child
);
39 if (child
instanceof TreeParent
) {
40 ((TreeParent
) child
).dispose();
44 public synchronized void clearChildren() {
45 for (Object obj
: children
) {
46 if (obj
instanceof TreeParent
)
47 ((TreeParent
) obj
).dispose();
54 * If overridden, <code>super.dispose()</code> must be called, typically
55 * after custom cleaning.
57 public synchronized void dispose() {
63 public synchronized Object
[] getChildren() {
64 return children
.toArray(new Object
[children
.size()]);
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()))
77 public synchronized boolean hasChildren() {
78 return children
.size() > 0;
81 public Object
getChildByName(String name
) {
82 for (Object child
: children
) {
83 if (child
.toString().equals(name
))
89 public synchronized Boolean
isLoaded() {
93 public String
getName() {
97 public void setParent(TreeParent parent
) {
99 if (parent
!= null && parent
.path
!= null)
100 this.path
= parent
.path
+ '/' + name
;
102 this.path
= '/' + name
;
105 public TreeParent
getParent() {
109 public String
toString() {
113 public int compareTo(TreeParent o
) {
114 return name
.compareTo(o
.name
);
118 public int hashCode() {
120 return path
.hashCode();
122 return name
.hashCode();
126 public boolean equals(Object obj
) {
127 if (path
!= null && obj
instanceof TreeParent
)
128 return path
.equals(((TreeParent
) obj
).path
);
130 return name
.equals(obj
.toString());