KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > plugins > helpers > TreeNodeFactory


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.plugins.helpers;
23
24 import org.jboss.console.manager.interfaces.ManageableResource;
25 import org.jboss.console.manager.interfaces.ResourceTreeNode;
26 import org.jboss.console.manager.interfaces.TreeAction;
27 import org.jboss.console.manager.interfaces.TreeNode;
28 import org.jboss.console.manager.interfaces.TreeNodeMenuEntry;
29 import org.jboss.console.manager.interfaces.impl.HttpLinkTreeAction;
30 import org.jboss.console.manager.interfaces.impl.MBeanResource;
31 import org.jboss.console.manager.interfaces.impl.SeparatorTreeNodeMenuEntry;
32 import org.jboss.console.manager.interfaces.impl.SimpleResourceTreeNode;
33 import org.jboss.console.manager.interfaces.impl.SimpleTreeNode;
34 import org.jboss.console.manager.interfaces.impl.SimpleTreeNodeMenuEntryImpl;
35
36 import javax.management.ObjectName JavaDoc;
37
38 /**
39  * <description>
40  *
41  * @see <related>
42  *
43  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
44  * @version $Revision: 37459 $
45  *
46  * <p><b>Revisions:</b>
47  *
48  * <p><b>31 dec 2002 Sacha Labourey:</b>
49  * <ul>
50  * <li> First implementation </li>
51  * </ul>
52  */

53 public class TreeNodeFactory
54 {
55
56    public final static int NAME = 0;
57    public final static int DESCRIPTION = 1;
58    public final static int ICON_URL = 2;
59    public final static int DEFAULT_URL = 3;
60    public final static int MENU_ENTRIES = 4;
61    public final static int SUB_NODES = 5;
62    public final static int SUB_RESOURCES = 6;
63    public final static int MANAGEABLE_RESOURCES = 7;
64    
65    public static TreeNode createTreeNode (Object JavaDoc[] content) throws Exception JavaDoc
66    {
67       if (content.length != 7 && content.length != 8)
68          throw new Exception JavaDoc ("Bad number of parameters");
69          
70    
71       String JavaDoc name = (String JavaDoc)content[NAME];
72       String JavaDoc description = (String JavaDoc)content[DESCRIPTION];
73       String JavaDoc iconUrl = (String JavaDoc)content[ICON_URL];
74       String JavaDoc defaultUrl = (String JavaDoc)content[DEFAULT_URL];
75       
76       TreeAction action = new HttpLinkTreeAction (defaultUrl);
77       
78       // menu entries
79
//
80
TreeNodeMenuEntry[] menuEntries = createTreeMenus ((Object JavaDoc[])content[MENU_ENTRIES]);
81       
82       // sub nodes
83
//
84
TreeNode[] subNodes = null;
85       Object JavaDoc[] genericSubNodes = (Object JavaDoc[])content[SUB_NODES];
86       if (genericSubNodes != null && genericSubNodes.length > 0)
87       {
88          subNodes = new TreeNode[genericSubNodes.length];
89          for (int i=0; i< genericSubNodes.length; i++)
90          {
91             subNodes[i] = createTreeNode ( (Object JavaDoc[])genericSubNodes[i] );
92          }
93       }
94       else
95       {
96          subNodes = new TreeNode[0];
97       }
98       
99       // sub resources nodes
100
//
101
ResourceTreeNode[] subResNodes = null;
102       Object JavaDoc[] genericSubResNodes = (Object JavaDoc[])content[SUB_RESOURCES];
103       if (genericSubResNodes != null && genericSubResNodes.length > 0)
104       {
105          subResNodes = new ResourceTreeNode[genericSubResNodes.length];
106          for (int i=0; i< genericSubResNodes.length; i++)
107          {
108             subResNodes[i] = (ResourceTreeNode)createTreeNode ( (Object JavaDoc[])genericSubResNodes[i] );
109          }
110       }
111       else
112       {
113          subResNodes = new ResourceTreeNode[0];
114       }
115       
116       if ((content.length-1) == MANAGEABLE_RESOURCES)
117       {
118          // we are a resource tree node
119
//
120
ManageableResource res = createManageableResource (content[MANAGEABLE_RESOURCES]);
121          return new SimpleResourceTreeNode (name, description, iconUrl, action, menuEntries, subNodes, subResNodes, res);
122          
123       }
124       else
125       {
126          // we are not a resource tree node, but simply a tree node!
127
//
128
return new SimpleTreeNode (name, description, iconUrl, action, menuEntries, subNodes, subResNodes);
129       }
130       
131    }
132    
133    public static ManageableResource createManageableResource (Object JavaDoc content) throws Exception JavaDoc
134    {
135       Object JavaDoc[] realContent = (Object JavaDoc[])content;
136       return new MBeanResource (new ObjectName JavaDoc((String JavaDoc)realContent[0]), (String JavaDoc)realContent[1]);
137    }
138    
139    protected static TreeNodeMenuEntry[] createTreeMenus (Object JavaDoc[] content) throws Exception JavaDoc
140    {
141           
142       TreeNodeMenuEntry[] menuEntries = null;
143       
144       if (content != null && content.length > 0)
145       {
146          menuEntries = new TreeNodeMenuEntry[content.length];
147          int i=0;
148          while (i< content.length)
149          {
150             if (content[i] == null)
151             {
152                menuEntries[i] = new SeparatorTreeNodeMenuEntry();
153                i++;
154             }
155             else
156             {
157                String JavaDoc text = (String JavaDoc)content[i];
158                TreeAction action = new HttpLinkTreeAction((String JavaDoc)content[i+1]);
159                menuEntries[i] = new SimpleTreeNodeMenuEntryImpl ( text, action );
160                i+=2;
161             }
162          }
163       }
164       else
165       {
166          menuEntries = new TreeNodeMenuEntry[0];
167       }
168       return menuEntries;
169    }
170 }
171
Popular Tags