KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > nodes > TomcatWebModuleChildren


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tomcat5.nodes;
21
22 import java.util.TreeSet JavaDoc;
23 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
24 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
25 import javax.enterprise.deploy.spi.Target JavaDoc;
26 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
27 import org.netbeans.modules.tomcat5.TomcatManager;
28 import org.netbeans.modules.tomcat5.TomcatModule;
29 import org.openide.ErrorManager;
30 import org.openide.nodes.AbstractNode;
31 import org.openide.nodes.Children;
32 import org.openide.nodes.Node;
33 import org.openide.util.Lookup;
34 import org.openide.util.NbBundle;
35 import org.openide.util.RequestProcessor;
36 import org.netbeans.modules.tomcat5.AuthorizationException;
37 import org.openide.DialogDisplayer;
38 import org.openide.NotifyDescriptor;
39
40 /**
41  *
42  * @author Petr Pisl
43  */

44 public class TomcatWebModuleChildren extends Children.Keys {
45     
46     private static final String JavaDoc WAIT_NODE = "wait_node"; //NOI18N
47
private Lookup lookup;
48     
49     TomcatWebModuleChildren(Lookup lkp) {
50         lookup = lkp;
51     }
52     
53     public void updateKeys(){
54        TreeSet JavaDoc ts = new TreeSet JavaDoc();
55        ts.add(WAIT_NODE);
56        
57        setKeys(ts);
58        
59         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
60             public void run () {
61                 DeploymentManager JavaDoc manager = (DeploymentManager JavaDoc)lookup.lookup(DeploymentManager JavaDoc.class);
62                 Target JavaDoc target = (Target JavaDoc)lookup.lookup(Target JavaDoc.class);
63                 TreeSet JavaDoc list = new TreeSet JavaDoc(new TomcatWebModule.TomcatWebModuleComparator());
64
65                 if (manager instanceof TomcatManager && target != null){
66                     TomcatManager tm = (TomcatManager)manager;
67
68                     if (tm.isSuspended() || !tm.isRunning(true)) {
69                         removeNotify();
70                         return;
71                     }
72                     try{
73                         TargetModuleID JavaDoc[] modules = manager.getRunningModules(ModuleType.WAR, new Target JavaDoc[]{target});
74                          for (int i = 0; i < modules.length; i ++){
75                              list.add(new TomcatWebModule (manager, (TomcatModule)modules[i], true));
76                          }
77
78                          modules = manager.getNonRunningModules(ModuleType.WAR, new Target JavaDoc[]{target});
79                          for (int i = 0; i < modules.length; i ++){
80                              list.add(new TomcatWebModule (manager, (TomcatModule)modules[i], false));
81                          }
82
83                        }
84                     
85                     catch (Exception JavaDoc e) {
86                         if (e.getCause() instanceof AuthorizationException) {
87                             // connection to tomcat manager has not been allowed
88
String JavaDoc errMsg = NbBundle.getMessage(TomcatWebModuleChildren.class,
89                                     "MSG_AuthorizationFailed");
90                             NotifyDescriptor notDesc = new NotifyDescriptor.Message(
91                                     errMsg, NotifyDescriptor.ERROR_MESSAGE);
92                             DialogDisplayer.getDefault().notify(notDesc);
93                         } else {
94                             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
95                         }
96                     }
97                 }
98
99        
100                setKeys(list);
101        
102             }
103         }, 0);
104        
105     }
106     
107     protected void addNotify() {
108         updateKeys();
109     }
110    
111     protected void removeNotify() {
112         setKeys(java.util.Collections.EMPTY_SET);
113     }
114         
115     protected org.openide.nodes.Node[] createNodes(Object JavaDoc key) {
116         if (key instanceof TomcatWebModule){
117             TomcatWebModule tm = (TomcatWebModule)key;
118             TomcatWebModuleNode node = new TomcatWebModuleNode(tm);
119             tm.setRepresentedNode(node);
120             return new Node[]{node};
121         }
122         if (key instanceof String JavaDoc && key.equals(WAIT_NODE)){
123             return new Node[]{createWaitNode ()};
124         }
125         return null;
126     }
127     
128     /* Creates and returns the instance of the node
129     * representing the status 'WAIT' of the node.
130     * It is used when it spent more time to create elements hierarchy.
131     * @return the wait node.
132     */

133     private Node createWaitNode () {
134         AbstractNode n = new AbstractNode(Children.LEAF);
135         n.setName(NbBundle.getMessage(TomcatWebModuleChildren.class, "LBL_WaitNode_DisplayName")); //NOI18N
136
n.setIconBaseWithExtension("org/netbeans/modules/tomcat5/resources/wait.gif"); // NOI18N
137
return n;
138     }
139 }
140
Popular Tags