KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.jboss.console.manager.interfaces.ResourceTreeNode;
25 import org.jboss.console.manager.interfaces.TreeAction;
26 import org.jboss.console.manager.interfaces.TreeInfo;
27 import org.jboss.console.manager.interfaces.TreeNode;
28 import org.jboss.console.manager.interfaces.TreeNodeMenuEntry;
29
30 /**
31  * NodeWrapper implementation for nodes that are not root nodes
32  *
33  * @see org.jboss.console.navtree.NodeWrapper
34  *
35  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
36  * @version $Revision: 37459 $
37  *
38  * <p><b>Revisions:</b>
39  *
40  * <p><b>20 decembre 2002 Sacha Labourey:</b>
41  * <ul>
42  * <li> First implementation </li>
43  * </ul>
44  */

45
46 public class StdNodeWrapper
47    implements NodeWrapper
48 {
49    // Constants -----------------------------------------------------
50

51    // Attributes ----------------------------------------------------
52

53    TreeNode node = null;
54    NodeWrapper[] sons = null;
55    TreeNode[] realSons = null;
56    TreeInfo master = null;
57    String JavaDoc path = null;
58    
59    // Static --------------------------------------------------------
60

61    // Constructors --------------------------------------------------
62

63    public StdNodeWrapper (TreeNode node, TreeInfo master, String JavaDoc parentPath)
64    {
65       this.node = node;
66       this.master = master;
67       this.path = parentPath + "/" + this.node.getName ();
68
69       TreeNode[] plugged = null;
70       if (node instanceof ResourceTreeNode)
71       {
72          plugged = master.getTreesForResource( ((ResourceTreeNode)node).getResource() );
73          
74          // Now we check which kind of visibility we want to give
75
//
76
int visibility = ((ResourceTreeNode)node).getVisibility();
77          if (visibility == ResourceTreeNode.INVISIBLE_IF_SUBNODE_EXISTS &&
78              plugged != null && plugged.length > 0)
79          {
80             this.node = getMasterFromPluggins (plugged);
81             plugged = removeMasterFromList (plugged, this.node);
82          }
83       }
84             
85       TreeNode[] res = this.node.getNodeManagableResources ();
86       TreeNode[] sub = this.node.getSubNodes ();
87       
88       if (res == null) res = new TreeNode[0];
89       if (sub == null) sub = new TreeNode[0];
90       if (plugged == null) plugged = new TreeNode[0];
91
92       realSons = new TreeNode[res.length + sub.length + plugged.length];
93       sons = new NodeWrapper[res.length + sub.length + plugged.length];
94
95       for (int i=0; i<res.length; i++)
96          realSons[i] = res[i];
97       for (int i=0; i<sub.length; i++)
98          realSons[res.length+i] = sub[i];
99       for (int i=0; i<plugged.length; i++)
100          realSons[res.length+sub.length+i] = plugged[i];
101    }
102
103    // Public --------------------------------------------------------
104

105    // Z implementation ----------------------------------------------
106

107    public Object JavaDoc getChild (int index)
108    {
109       if (index >= sons.length)
110          return null;
111
112       if (sons[index] == null)
113          sons[index] = new StdNodeWrapper(realSons[index], this.master, this.path);
114
115       return sons[index];
116    }
117
118    public int getChildCount ()
119    {
120       return this.realSons.length;
121    }
122
123    public int getIndexOfChild (Object JavaDoc child)
124    {
125       for (int i=0; i<this.sons.length; i++)
126       {
127          if (this.sons[i] == child)
128             return i;
129       }
130       return -1;
131    }
132
133    public boolean isLeaf ()
134    {
135       return this.sons.length == 0;
136    }
137
138    public String JavaDoc toString ()
139    {
140       return this.node.getName ();
141    }
142
143    public String JavaDoc getIconUrl ()
144    {
145       return this.node.getIcon ();
146    }
147
148    public TreeAction getAssociatedAction ()
149    {
150       return this.node.getAction ();
151    }
152    
153    public String JavaDoc getDescription ()
154    {
155       return this.node.getDescription ();
156    }
157    
158    public TreeNodeMenuEntry[] getMenuEntries ()
159    {
160       return this.node.getMenuEntries ();
161    }
162    
163    public String JavaDoc getPath ()
164    {
165       return this.path;
166    }
167    
168    // Y overrides ---------------------------------------------------
169

170    // Package protected ---------------------------------------------
171

172    // Protected -----------------------------------------------------
173

174    protected TreeNode getMasterFromPluggins (TreeNode[] plugged)
175    {
176       for (int i=0; i<plugged.length; i++)
177       {
178          if (plugged[i].isMasterNode())
179             return plugged[i];
180       }
181       
182       return plugged[0];
183       
184    }
185    
186    protected TreeNode[] removeMasterFromList (TreeNode[] all, TreeNode main)
187    {
188       TreeNode[] result = new TreeNode[all.length-1];
189       
190       int cursor = 0;
191       for (int i=0; i<all.length; i++)
192       {
193          if (all[i] != main)
194          {
195             result[cursor] = all[i];
196             cursor++;
197          }
198       }
199       
200       return result;
201    }
202    
203    // Private -------------------------------------------------------
204

205    // Inner classes -------------------------------------------------
206

207 }
208    
209     
210
Popular Tags