1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.naming.context;
11
12 import java.beans.BeanInfo;
13 import java.beans.Introspector;
14 import java.beans.PropertyDescriptor;
15
16 import javax.naming.Binding;
17 import javax.naming.Name;
18 import javax.naming.NamingEnumeration;
19 import javax.naming.NamingException;
20 import javax.naming.event.EventContext;
21 import javax.naming.event.NamespaceChangeListener;
22 import javax.naming.event.NamingEvent;
23 import javax.naming.event.NamingExceptionEvent;
24 import javax.swing.tree.DefaultMutableTreeNode;
25
26 import nl.tudelft.simulation.event.EventType;
27 import nl.tudelft.simulation.logger.Logger;
28
29 /***
30 * A node in the context.
31 * <p>
32 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
33 * University of Technology </a>, the Netherlands. <br>
34 * See for project information <a href="http://www.simulation.tudelft.nl">
35 * www.simulation.tudelft.nl </a> <br>
36 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
37 * License (GPL) </a>, no warranty <br>
38 *
39 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
40 * Jacobs </a>
41 * @version 1.5 2004-03-24
42 * @since 1.0
43 */
44 public class ContextNode extends DefaultMutableTreeNode implements
45 NamespaceChangeListener
46 {
47 /*** NODE_CHANGED_EVENT */
48 public static final EventType NODE_CHANGED_EVENT = new EventType(
49 "NODE_CHANGED_EVENT");
50
51 /*** the context */
52 private EventContext context = null;
53
54 /*** displayClasses the classes to display */
55 private Class[] displayClasses = null;
56
57 /*** display the fields ?.. */
58 private boolean displayFields = false;
59
60 /*** the treeModel */
61 private ContextTreeModel treeModel = null;
62
63 /***
64 * constructs a new ContextNode
65 *
66 * @param treeModel the treeModel
67 * @param name the name
68 * @param context the context
69 * @param displayClasses the classes to display
70 * @param displayFields the fields to show
71 * @throws NamingException on failure
72 */
73 public ContextNode(final ContextTreeModel treeModel, final String name,
74 final EventContext context, final Class[] displayClasses,
75 final boolean displayFields) throws NamingException
76 {
77 super(name);
78 this.treeModel = treeModel;
79 this.context = context;
80 this.displayClasses = displayClasses;
81 this.displayFields = displayFields;
82
83 NamingEnumeration bindings = this.context.listBindings("");
84 while (bindings.hasMore())
85 {
86 Binding binding = (Binding) bindings.next();
87 this.objectAdded(new NamingEvent(this.context,
88 NamingEvent.OBJECT_ADDED, binding, null, null));
89 }
90 this.context.addNamingListener("", EventContext.OBJECT_SCOPE, this);
91 this.context.addNamingListener("", EventContext.SUBTREE_SCOPE, this);
92 }
93
94 /***
95 * constructs a new ContextNode
96 *
97 * @param userObject the userObject
98 */
99 public ContextNode(final Object userObject)
100 {
101 super(userObject, true);
102 }
103
104 /***
105 * @see javax.naming.event.NamespaceChangeListener
106 * #objectAdded(javax.naming.event.NamingEvent)
107 */
108 public void objectAdded(final NamingEvent event)
109 {
110 Binding item = event.getNewBinding();
111 if (item.getObject() instanceof EventContext)
112 {
113 EventContext context = (EventContext) item.getObject();
114 try
115 {
116 Name name = context.getNameParser("").parse(
117 context.getNameInNamespace());
118 this.add(new ContextNode(this.treeModel, name.get(
119 name.size() - 1).toString(), context,
120 this.displayClasses, this.displayFields));
121 } catch (NamingException exception)
122 {
123 Logger.warning(this, "objectAdded", exception);
124 }
125 } else if (this.display(item.getObject()))
126 {
127 this.contructObject(this, item.getObject());
128 }
129 this.treeModel.fireTreeStructureChanged(this, this.getPath(), null,
130 null);
131 }
132
133 /***
134 * @see javax.naming.event.NamespaceChangeListener
135 * #objectRemoved(javax.naming.event.NamingEvent)
136 */
137 public void objectRemoved(final NamingEvent event)
138 {
139 Binding item = event.getOldBinding();
140 if (!(item.getObject() instanceof EventContext))
141 {
142 this.remove(item.getObject());
143 }
144 this.treeModel.fireTreeStructureChanged(this, this.getPath(), null,
145 null);
146 }
147
148 /***
149 * removes the child from context
150 *
151 * @param object the object
152 */
153 private void remove(final Object object)
154 {
155 if (!display(object))
156 {
157 return;
158 }
159 for (int i = 0; i < this.getChildCount(); i++)
160 {
161 DefaultMutableTreeNode node = (DefaultMutableTreeNode) this
162 .getChildAt(i);
163 if (node.getUserObject().equals(object))
164 {
165 this.remove(i);
166 return;
167 }
168 }
169 throw new NullPointerException("Could not find " + object);
170 }
171
172 /***
173 * @see javax.naming.event.NamespaceChangeListener
174 * #objectRenamed(javax.naming.event.NamingEvent)
175 */
176 public void objectRenamed(final NamingEvent event)
177 {
178 throw new RuntimeException("objectRenamed(" + event.toString()
179 + ") not implemented yet");
180 }
181
182 /***
183 * @see javax.naming.event.NamingListener
184 * #namingExceptionThrown(javax.naming.event.NamingExceptionEvent)
185 */
186 public void namingExceptionThrown(final NamingExceptionEvent event)
187 {
188 Logger.warning(this, "namingExceptionThrown", event.getException());
189 }
190
191 /***
192 * display this object.
193 *
194 * @param object the object
195 * @return boolean
196 */
197 private boolean display(final Object object)
198 {
199 return this.display(object.getClass());
200 }
201
202 /***
203 * display this class.
204 *
205 * @param myClass the class
206 * @return boolean
207 */
208 private boolean display(final Class myClass)
209 {
210 if (this.displayClasses == null)
211 {
212 return true;
213 }
214 if (myClass == null)
215 {
216 return false;
217 }
218 for (int i = 0; i < this.displayClasses.length; i++)
219 {
220 if (this.displayClasses[i].equals(myClass))
221 {
222 return true;
223 }
224 }
225 Class[] interfaces = myClass.getInterfaces();
226 for (int i = 0; i < interfaces.length; i++)
227 {
228 for (int j = 0; j < this.displayClasses.length; j++)
229 {
230 if (interfaces[i].equals(this.displayClasses[j]))
231 {
232 return true;
233 }
234 }
235 }
236 return display(myClass.getSuperclass());
237 }
238
239 /***
240 * constructs an Object
241 *
242 * @param root the root element
243 * @param object the object
244 * @return root
245 */
246 private DefaultMutableTreeNode contructObject(
247 final DefaultMutableTreeNode root, final Object object)
248 {
249 if (!this.displayFields)
250 {
251 root.add(new DefaultMutableTreeNode(object));
252 } else
253 {
254 DefaultMutableTreeNode child = new DefaultMutableTreeNode(object);
255 try
256 {
257 BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
258 PropertyDescriptor[] descriptors = beanInfo
259 .getPropertyDescriptors();
260 for (int i = 0; i < descriptors.length; i++)
261 {
262 String name = "attr:" + descriptors[i].getName();
263 String value = " value:"
264 + descriptors[i].getReadMethod().invoke(object,
265 null).toString();
266 child.add(new DefaultMutableTreeNode(name + " " + value));
267 }
268 root.add(child);
269 } catch (Exception exception)
270 {
271 exception = null;
272
273 }
274 }
275 return root;
276 }
277 }