KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > navtree > ConsoleTreeModel


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.navtree;
23
24
25
26 import org.jboss.console.manager.interfaces.TreeInfo;
27
28 /**
29  * TreeModel used to represent management information
30  *
31  * @see org.jboss.console.navtree.AdminTreeBrowser
32  *
33  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
34  * @version $Revision: 37459 $
35  *
36  * <p><b>Revisions:</b>
37  *
38  * <p><b>17 decembre 2002 Sacha Labourey:</b>
39  * <ul>
40  * <li> First implementation </li>
41  * </ul>
42  */

43
44 public class ConsoleTreeModel implements javax.swing.tree.TreeModel JavaDoc
45 {
46    
47    // Constants -----------------------------------------------------
48

49    // Attributes ----------------------------------------------------
50

51    protected TreeInfo tree = null;
52    protected RootWrapper root = null;
53    protected java.util.Vector JavaDoc treeModelListeners = new java.util.Vector JavaDoc();
54    //protected InitialContext ctx = null;
55
//protected String pluginMgrJmxName = null;
56
protected TreeContext context = null;
57    protected TreeReopenerMemory reopenerMemory = null;
58    
59    protected javax.management.ObjectName JavaDoc targetPM = null;
60    
61    // Static --------------------------------------------------------
62

63    // Constructors --------------------------------------------------
64

65    public ConsoleTreeModel (TreeContext context) throws Exception JavaDoc
66    {
67       //this.pluginMgrJmxName = context.getServiceJmxName();
68
this.targetPM = new javax.management.ObjectName JavaDoc (context.getServiceJmxName());
69       //this.ctx = new InitialContext (jndiProps);
70
this.context = context;
71       //this.reopenerMemory = reopenerMemory;
72

73       this.tree = loadTree ();
74       this.root = new RootWrapper (this.tree);
75    }
76    
77    public boolean refreshTree (boolean force) throws Exception JavaDoc
78    {
79       TreeInfo tmpTree = null;
80       
81       if (!force && this.tree != null)
82       {
83          // first check if that is necessary
84
//
85
tmpTree = conditionalLoadTree (this.tree.getTreeVersion());
86       }
87       else
88       {
89          tmpTree = loadTree ();
90       }
91       
92       if (tmpTree != null)
93       {
94          RootWrapper oldRoot = this.root;
95          
96          this.tree = tmpTree;
97          this.root = new RootWrapper (this.tree);
98          
99          fireTreeStructureChanged (oldRoot);
100          return true;
101       }
102       else
103       {
104          return false;
105       }
106    }
107    
108    public TreeInfo conditionalLoadTree (long version) throws Exception JavaDoc
109    {
110       return (TreeInfo)context.getRemoteMBeanInvoker ().invoke (targetPM, "getUpdateTreeForProfile",
111             new Object JavaDoc[] {"WEB", new Long JavaDoc(version)},
112             new String JavaDoc[] {"java.lang.String", "long"});
113       //return getPM ().getUpdateTreeForProfile ("WEB", version);
114
}
115    
116    public TreeInfo loadTree () throws Exception JavaDoc
117    {
118       return (TreeInfo)context.getRemoteMBeanInvoker ().invoke (targetPM, "getTreeForProfile",
119             new Object JavaDoc[] {"WEB"},
120             new String JavaDoc[] {"java.lang.String"});
121       //return getPM ().getTreeForProfile ("WEB");
122
}
123    
124    /** Adds a listener for the <code>TreeModelEvent</code>
125     * posted after the tree changes.
126     *
127     * @param l the listener to add
128     * @see #removeTreeModelListener
129     *
130     */

131    public void addTreeModelListener (javax.swing.event.TreeModelListener JavaDoc l)
132    {
133       treeModelListeners.addElement(l);
134    }
135    
136    /** Returns the child of <code>parent</code> at index <code>index</code>
137     * in the parent's
138     * child array. <code>parent</code> must be a node previously obtained
139     * from this data source. This should not return <code>null</code>
140     * if <code>index</code>
141     * is a valid index for <code>parent</code> (that is <code>index >= 0 &&
142     * index < getChildCount(parent</code>)).
143     *
144     * @param parent a node in the tree, obtained from this data source
145     * @return the child of <code>parent</code> at index <code>index</code>
146     *
147     */

148    public Object JavaDoc getChild (Object JavaDoc parent, int index)
149    {
150       NodeWrapper n = (NodeWrapper)parent;
151       return n.getChild (index);
152    }
153    
154    /** Returns the number of children of <code>parent</code>.
155     * Returns 0 if the node
156     * is a leaf or if it has no children. <code>parent</code> must be a node
157     * previously obtained from this data source.
158     *
159     * @param parent a node in the tree, obtained from this data source
160     * @return the number of children of the node <code>parent</code>
161     *
162     */

163    public int getChildCount (Object JavaDoc parent)
164    {
165       NodeWrapper n = (NodeWrapper)parent;
166       return n.getChildCount ();
167    }
168    
169    /** Returns the index of child in parent. If <code>parent</code>
170     * is <code>null</code> or <code>child</code> is <code>null</code>,
171     * returns -1.
172     *
173     * @param parent a note in the tree, obtained from this data source
174     * @param child the node we are interested in
175     * @return the index of the child in the parent, or -1 if either
176     * <code>child</code> or <code>parent</code> are <code>null</code>
177     *
178     */

179    public int getIndexOfChild (Object JavaDoc parent, Object JavaDoc child)
180    {
181       NodeWrapper n = (NodeWrapper)parent;
182       return n.getIndexOfChild (child);
183    }
184    
185    /** Returns the root of the tree. Returns <code>null</code>
186     * only if the tree has no nodes.
187     *
188     * @return the root of the tree
189     *
190     */

191    public Object JavaDoc getRoot ()
192    {
193       return this.root;
194    }
195    
196    /** Returns <code>true</code> if <code>node</code> is a leaf.
197     * It is possible for this method to return <code>false</code>
198     * even if <code>node</code> has no children.
199     * A directory in a filesystem, for example,
200     * may contain no files; the node representing
201     * the directory is not a leaf, but it also has no children.
202     *
203     * @param node a node in the tree, obtained from this data source
204     * @return true if <code>node</code> is a leaf
205     *
206     */

207    public boolean isLeaf (Object JavaDoc node)
208    {
209       NodeWrapper n = (NodeWrapper)node;
210       return n.isLeaf ();
211    }
212    
213    /** Removes a listener previously added with
214     * <code>addTreeModelListener</code>.
215     *
216     * @see #addTreeModelListener
217     * @param l the listener to remove
218     *
219     */

220    public void removeTreeModelListener (javax.swing.event.TreeModelListener JavaDoc l)
221    {
222         treeModelListeners.removeElement(l);
223    }
224    
225    /** Messaged when the user has altered the value for the item identified
226     * by <code>path</code> to <code>newValue</code>.
227     * If <code>newValue</code> signifies a truly new value
228     * the model should post a <code>treeNodesChanged</code> event.
229     *
230     * @param path path to the node that the user has altered
231     * @param newValue the new value from the TreeCellEditor
232     *
233     */

234    public void valueForPathChanged (javax.swing.tree.TreePath JavaDoc path, Object JavaDoc newValue)
235    {
236       // not used
237
}
238    
239    // Public --------------------------------------------------------
240

241    // Z implementation ----------------------------------------------
242

243    // Y overrides ---------------------------------------------------
244

245    // Package protected ---------------------------------------------
246

247    // Protected -----------------------------------------------------
248

249     protected void fireTreeStructureChanged(RootWrapper oldRoot) {
250         int len = treeModelListeners.size();
251         
252         javax.swing.event.TreeModelEvent JavaDoc e = new javax.swing.event.TreeModelEvent JavaDoc(this,
253                                               new Object JavaDoc[] {oldRoot});
254         for (int i = 0; i < len; i++) {
255             ((javax.swing.event.TreeModelListener JavaDoc)treeModelListeners.elementAt(i)).
256                     treeStructureChanged(e);
257         }
258     }
259
260    /*protected PluginManagerMBean getPM () throws Exception
261    {
262       return (PluginManagerMBean)ctx.lookup (this.pluginMgrJndiName);
263    }*/

264    
265    // Private -------------------------------------------------------
266

267    // Inner classes -------------------------------------------------
268

269 }
270
Popular Tags