View Javadoc

1   /*
2    * Created on Oct 3, 2004
3    *
4    */
5   package org.extremecomponents.tree.bean;
6   
7   import java.beans.PropertyDescriptor;
8   
9   import java.io.Serializable;
10  
11  import java.util.ArrayList;
12  import java.util.HashMap;
13  import java.util.List;
14  import java.util.Map;
15  
16  import org.apache.commons.beanutils.BeanUtils;
17  import org.apache.commons.beanutils.PropertyUtils;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  
23  /***
24   * org.extremecomponents.tree.bean.TreeNode.java -
25   *
26   * @author Paul Horn
27   */
28  public class TreeNode extends HashMap implements Map, Serializable
29  {
30  	private static Log logger = LogFactory.getLog(TreeNode.class);
31  	private static final long serialVersionUID = -1012054863887704736L;
32  	private Object identifier;
33  	private Object bean;
34  	private TreeNode parent;
35  	private List children;
36  	private int depth;
37  	private boolean open;
38  
39  	public TreeNode()
40  	{
41  		super();
42  	}
43  
44  	public TreeNode(Object bean, Object identifier, int depth)
45  	{
46  		setBean(bean);
47  		this.identifier = identifier;
48  		this.depth = depth;
49  	}
50  
51  	/***
52  	 * @param child
53  	 */
54  	public void addChild(Object child)
55  	{
56  		if (children == null)
57  		{
58  			children = new ArrayList();
59  		}
60  
61  		children.add(child);
62  	}
63  
64  	/***
65  	 * @return Returns the bean.
66  	 */
67  	public Object getBean()
68  	{
69  		return bean;
70  	}
71  
72  	/***
73  	 * @param bean The bean to set.
74  	 */
75  	public void setBean(Object bean)
76  	{
77  		this.bean = bean;
78  
79  		PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean);
80  
81  		for (int i = 0; i < descriptors.length; i++)
82  		{
83  			try
84  			{
85  				String propertyName = descriptors[i].getDisplayName();
86  				Object val = BeanUtils.getProperty(bean, propertyName);
87  				this.put(propertyName, val);
88  			}
89  			catch (Exception e)
90  			{
91  				logger.error("TreeNode.setBean() Problem", e);
92  			}
93  		}
94  	}
95  
96  	/***
97  	 * @return Returns the children.
98  	 */
99  	public List getChildren()
100 	{
101 		return children;
102 	}
103 
104 	/***
105 	 * @param children The children to set.
106 	 */
107 	public void setChildren(List children)
108 	{
109 		this.children = children;
110 	}
111 
112 	/***
113 	 * @return Returns the parent.
114 	 */
115 	public TreeNode getParent()
116 	{
117 		return parent;
118 	}
119 
120 	/***
121 	 * @param parent The parent to set.
122 	 */
123 	public void setParent(TreeNode parent)
124 	{
125 		this.parent = parent;
126 	}
127 
128 	/***
129 	 * @return Returns the depth.
130 	 */
131 	public int getDepth()
132 	{
133 		return depth;
134 	}
135 
136 	/***
137 	 * @param depth The depth to set.
138 	 */
139 	public void setDepth(int depth)
140 	{
141 		this.depth = depth;
142 	}
143 
144 	/***
145 	 * @return Returns the open.
146 	 */
147 	public boolean isOpen()
148 	{
149 		return open;
150 	}
151 
152 	/***
153 	 * @param open The open to set.
154 	 */
155 	public void setOpen(boolean open)
156 	{
157 		this.open = open;
158 	}
159 
160 	/***
161 	 * @return
162 	 */
163 	public Object getIdentifier()
164 	{
165 		return identifier;
166 	}
167 
168 	/***
169 	 * @param object
170 	 */
171 	public void setIdentifier(Object object)
172 	{
173 		identifier = object;
174 	}
175 
176 	/***
177 	 * @param object
178 	 */
179 	public boolean equals(Object obj)
180 	{
181 		TreeNode node = (TreeNode) obj;
182 
183 		return (super.equals(obj) || this.identifier.equals(node.getIdentifier()));
184 	}
185 }