KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > ui > ServerRegistryNode


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
21 /*
22  * ServerRegNode.java -- synopsis
23  *
24  */

25 package org.netbeans.modules.j2ee.deployment.impl.ui;
26
27 import java.awt.Image JavaDoc;
28 import org.netbeans.modules.j2ee.deployment.plugins.api.StartServer;
29 import org.openide.nodes.*;
30 import org.openide.filesystems.*;
31 import org.openide.loaders.*;
32 import org.openide.util.NbBundle;
33 import org.openide.util.actions.SystemAction;
34 import org.openide.util.HelpCtx;
35 import org.openide.ErrorManager;
36 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.*;
37 import org.netbeans.modules.j2ee.deployment.impl.*;
38 import java.util.*;
39 import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener;
40 import org.openide.util.Utilities;
41
42 /**
43  * The server registry node is a node representing the registry in global options.
44  * @author Joe Cortopazzi
45  * @author George FinKlang
46  */

47
48 public class ServerRegistryNode extends AbstractNode
49 implements ServerRegistry.PluginListener, InstanceListener {
50     
51     static final String JavaDoc SERVERS_ICON = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/Servers.png";//NOI18N
52

53     private transient Map serverNodes = new HashMap();
54     private transient HelpCtx helpCtx;
55     private boolean expandablePassTargetNode = true;
56     
57     /** Creates a new instance of ServerRegistryNode2 */
58     public ServerRegistryNode() {
59         super(new ServerChildren());
60         setName(""); //NOI18N
61
setDisplayName(NbBundle.getMessage(ServerRegistryNode.class, "SERVER_REGISTRY_NODE"));
62         setShortDescription(NbBundle.getMessage(ServerRegistryNode.class, "SERVER_REGISTRY_SHORT_DESCRIPTION"));
63         setIconBaseWithExtension(SERVERS_ICON);
64     }
65
66     public void serverAdded(Server server) {
67         updateKeys();
68     }
69     public void serverRemoved(Server server) {
70         updateKeys();
71     }
72     
73     public boolean isExpandablePassTargetNode() {
74         return expandablePassTargetNode;
75     }
76     public void setExpandablePassTargetNode(boolean v) {
77         expandablePassTargetNode = v;
78         setChildren(new ServerChildren());
79         serverNodes.clear();
80     }
81     
82     public void instanceAdded(String JavaDoc instance) {
83         updateKeys();
84     }
85     public void instanceRemoved(String JavaDoc instance) {
86         updateKeys();
87     }
88     public void changeDefaultInstance(String JavaDoc oldInstance, String JavaDoc newInstance) {
89     }
90     
91     private void updateKeys() {
92         ((ServerChildren) getChildren()).updateKeys();
93     }
94     
95     public SystemAction[] createActions() {
96         return new SystemAction[] {
97             SystemAction.get(AddServerInstanceAction.class)
98         };
99     }
100     
101     public HelpCtx getHelpCtx() {
102         if(helpCtx == null)
103             helpCtx = new HelpCtx(NbBundle.getBundle(ServerRegistryNode.class).getString("nodes_server_registry_node_html"));//NOI18N
104
return helpCtx;
105     }
106         
107     private static class ServerChildren extends Children.Keys {
108         private boolean listenersAdded = false;
109         
110         public ServerChildren() {
111         }
112         
113         protected void addNotify() {
114             updateKeys();
115             
116             if (! listenersAdded) {
117                 ServerRegistryNode parent = (ServerRegistryNode) getNode();
118                 ServerRegistry.getInstance().addPluginListener(parent);
119                 ServerRegistry.getInstance().addInstanceListener(parent);
120                 listenersAdded = true;
121             }
122         }
123         
124         protected Node[] createNodes(Object JavaDoc obj) {
125             ServerInstance instance = (ServerInstance) obj;
126             if (instance == null)
127                 return new Node[0];
128             
129             Node childNode;
130             StartServer startServer = instance.getStartServer();
131             if (startServer == null) {
132                 return new Node[0];
133             }
134             if (startServer.isAlsoTargetServer(null)) {
135                 childNode = instance.getServer().getNodeProvider().createInstanceTargetNode(instance);
136             } else {
137                 childNode = instance.getServer().getNodeProvider().createInstanceNode(instance);
138             }
139             instance.refresh(); // detect the server instance status
140
return new Node[] { childNode };
141         }
142         
143         public void updateKeys() {
144             List instances = new ArrayList();
145             Iterator serverIter = ServerRegistry.getInstance().getServers().iterator();
146             while (serverIter.hasNext()) {
147                 Server server = (Server)serverIter.next();
148                 ServerInstance[] serverInstances = server.getInstances();
149                 for (int i = 0; i < serverInstances.length; i++) {
150                     instances.add(serverInstances[i]);
151                 }
152             }
153             Collections.sort(instances);
154             setKeys(instances);
155         }
156     }
157     
158     public static ServerRegistryNode getServerRegistryNode() {
159         try {
160             FileSystem defaultFileSystem = Repository.getDefault().getDefaultFileSystem();
161             FileObject fo = defaultFileSystem.findResource("UI/Runtime"); //NOI18N
162
DataFolder df = (DataFolder) DataObject.find(fo);
163             org.openide.util.Lookup l = new FolderLookup(df).getLookup();
164             return (ServerRegistryNode) l.lookup(ServerRegistryNode.class);
165         } catch (DataObjectNotFoundException e) {
166             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
167             return null;
168         }
169     }
170 }
171
Popular Tags