KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > management > browser > model > Node


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.management.browser.model;
8
9 import java.awt.Component JavaDoc;
10 import java.beans.Customizer JavaDoc;
11 import java.beans.beancontext.BeanContextChildComponentProxy JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import org.apache.log4j.Logger;
19 import org.ejtools.adwt.GenericCustomizer;
20 import org.ejtools.beans.beancontext.CustomBeanContextServicesSupport;
21
22 /**
23  * Description of the Class
24  *
25  * @author Laurent Etiemble
26  * @version $Revision: 1.6 $
27  * @todo Javadoc to complete
28  */

29 public abstract class Node extends CustomBeanContextServicesSupport implements BeanContextChildComponentProxy JavaDoc
30 {
31    /** Description of the Field */
32    protected transient Customizer JavaDoc c = null;
33    /** Description of the Field */
34    protected String JavaDoc name = "<undefined>";
35    /** Description of the Field */
36    private static Logger logger = Logger.getLogger(Node.class);
37
38
39    /** Constructor for the Node object */
40    public Node()
41    {
42       super();
43    }
44
45
46    /**
47     * Gets the component attribute of the Node object
48     *
49     * @return The component value
50     */

51    public Component JavaDoc getComponent()
52    {
53       if (c == null)
54       {
55          c = new GenericCustomizer(true, this);
56       }
57       return (Component JavaDoc) c;
58    }
59
60
61    /**
62     * Gets the name attribute of the Node object
63     *
64     * @return The name value
65     */

66    public String JavaDoc getName()
67    {
68       return this.name;
69    }
70
71
72    /**
73     * Description of the Method
74     *
75     * @return Description of the Returned Value
76     */

77    public Iterator JavaDoc iterator()
78    {
79       Iterator JavaDoc iterator = super.iterator();
80
81       // Create a vector from the iterator
82
Vector JavaDoc vector = new Vector JavaDoc();
83       while (iterator.hasNext())
84       {
85          vector.addElement(iterator.next());
86       }
87
88       // Apply the comparator
89
Collections.sort(vector,
90          new Comparator JavaDoc()
91          {
92             public int compare(Object JavaDoc o1, Object JavaDoc o2)
93             {
94                Class JavaDoc c1 = o1.getClass();
95                Class JavaDoc c2 = o2.getClass();
96
97                if ((c1 == ManagedObject.class) && (c2 == ManagedObject.class))
98                {
99                   int ret = ((ManagedObject) o1).getJ2EEType().compareTo(((ManagedObject) o2).getJ2EEType());
100                   if (ret == 0)
101                   {
102                      ret = o1.toString().compareTo(o2.toString());
103                   }
104                   return ret;
105                }
106                else
107                {
108                   int ret = o1.getClass().getName().compareTo(o2.getClass().getName());
109                   if (ret == 0)
110                   {
111                      ret = o1.toString().compareTo(o2.toString());
112                   }
113                   return ret;
114                }
115             }
116
117
118             public boolean equals(Object JavaDoc obj)
119             {
120                return true;
121                // Ever called?
122
}
123          });
124
125       // Return the result
126
return vector.iterator();
127    }
128
129
130    /**
131     * Description of the Method
132     *
133     * @return Description of the Returned Value
134     */

135    public String JavaDoc toString()
136    {
137       return (name == null || "".equals(name)) ? "Default" : name;
138    }
139
140
141    /** Description of the Method */
142    protected void createHierarchy()
143    {
144       logger.debug("Create Hierarchy in " + this.getName() + " parent is " + this.getBeanContext());
145
146       ArrayList JavaDoc moved = new ArrayList JavaDoc();
147
148       // Create the hierachy
149
ArrayList JavaDoc copy = new ArrayList JavaDoc(this);
150       for (int i = 0; i < copy.size(); i++)
151       {
152          ManagedObject resource = (ManagedObject) copy.get(i);
153          if (!moved.contains(resource))
154          {
155             String JavaDoc search = resource.getJ2EEType() + "=" + resource.getName();
156
157             ArrayList JavaDoc toMove = new ArrayList JavaDoc(this);
158             for (int j = 0; j < toMove.size(); j++)
159             {
160                ManagedObject other = (ManagedObject) toMove.get(j);
161                String JavaDoc canonical = other.getCanonicalName();
162                if (canonical.indexOf(search) >= 0)
163                {
164                   this.remove(other);
165                   resource.add(other);
166                   moved.add(other);
167                }
168             }
169          }
170       }
171
172       // Recurse through all children which have children
173
Iterator JavaDoc iterator = this.iterator();
174       while (iterator.hasNext())
175       {
176          ManagedObject resource = (ManagedObject) iterator.next();
177          if (!resource.isEmpty())
178          {
179             resource.createHierarchy();
180          }
181       }
182    }
183
184
185    /**
186     * Description of the Method
187     *
188     * @param spacer Description of the Parameter
189     */

190    protected void dumpHierarchy(String JavaDoc spacer)
191    {
192       Iterator JavaDoc iterator = this.iterator();
193       while (iterator.hasNext())
194       {
195          Node node = (Node) iterator.next();
196          logger.info(spacer + node.getName());
197          node.dumpHierarchy(spacer + "+-");
198       }
199    }
200
201
202    /**
203     * Setter for the name attribute
204     *
205     * @param name The new name value
206     */

207    protected void setName(String JavaDoc name)
208    {
209       String JavaDoc old = this.name;
210       this.name = name;
211       this.firePropertyChange("name", old, this.name);
212    }
213 }
214
Popular Tags