KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > plugins > api > UISupport


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.j2ee.deployment.plugins.api;
21
22 import java.awt.Image JavaDoc;
23 import java.beans.BeanInfo JavaDoc;
24 import java.util.WeakHashMap JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
27 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
28 import org.netbeans.modules.j2ee.deployment.impl.ui.ServerRegistryNode;
29 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.*;
30 import org.openide.filesystems.Repository;
31 import org.openide.loaders.DataFolder;
32 import org.openide.nodes.Node;
33 import org.openide.util.Utilities;
34 import org.openide.windows.IOProvider;
35 import org.openide.windows.InputOutput;
36
37 /**
38  * UI support for plugins provided by the j2eeserver.
39  *
40  * @author sherold
41  * @since 1.7
42  */

43 public final class UISupport {
44     
45     private static final WeakHashMap JavaDoc ioWeakMap = new WeakHashMap JavaDoc();
46     
47     /**
48      * Server icon constants.
49      *
50      * @since 1.19
51      */

52     public enum ServerIcon {
53         EJB_ARCHIVE, WAR_ARCHIVE, EAR_ARCHIVE,
54         EJB_FOLDER, EAR_FOLDER, WAR_FOLDER,
55         EJB_OPENED_FOLDER, EAR_OPENED_FOLDER, WAR_OPENED_FOLDER
56     };
57     
58     /** Do not allow to create instances of this class */
59     private UISupport() {
60     }
61     
62     /**
63      * Returns the specified icon.
64      *
65      * @return The specified icon.
66      *
67      * @since 1.19
68      */

69     public static Image JavaDoc getIcon(ServerIcon serverIcon) {
70         switch (serverIcon) {
71             case EJB_ARCHIVE :
72                 return Utilities.loadImage("org/netbeans/modules/j2ee/deployment/impl/ui/resources/ejb.png"); // NOI18N
73
case WAR_ARCHIVE :
74                 return Utilities.loadImage("org/netbeans/modules/j2ee/deployment/impl/ui/resources/war.png"); // NOI18N
75
case EAR_ARCHIVE :
76                 return Utilities.loadImage("org/netbeans/modules/j2ee/deployment/impl/ui/resources/ear.png"); // NOI18N
77
default :
78                 return computeIcon(serverIcon);
79         }
80     }
81     
82     private static Image JavaDoc computeIcon(ServerIcon serverIcon) {
83         // get the default folder icon
84
Node folderNode = DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().getRoot()).getNodeDelegate();
85         Image JavaDoc folder;
86         if (serverIcon == ServerIcon.EJB_OPENED_FOLDER || serverIcon == ServerIcon.WAR_OPENED_FOLDER
87                 || serverIcon == ServerIcon.EAR_OPENED_FOLDER) {
88             folder = folderNode.getOpenedIcon(BeanInfo.ICON_COLOR_16x16);
89         } else {
90             folder = folderNode.getIcon(BeanInfo.ICON_COLOR_16x16);
91         }
92         Image JavaDoc badge;
93         if (serverIcon == ServerIcon.EJB_FOLDER || serverIcon == ServerIcon.EJB_OPENED_FOLDER) {
94             badge = Utilities.loadImage("org/netbeans/modules/j2ee/deployment/impl/ui/resources/ejbBadge.png"); // NOI18N
95
} else if (serverIcon == ServerIcon.WAR_FOLDER || serverIcon == ServerIcon.WAR_OPENED_FOLDER) {
96             badge = Utilities.loadImage("org/netbeans/modules/j2ee/deployment/impl/ui/resources/warBadge.png"); // NOI18N
97
} else if (serverIcon == ServerIcon.EAR_FOLDER || serverIcon == ServerIcon.EAR_OPENED_FOLDER) {
98             badge = Utilities.loadImage("org/netbeans/modules/j2ee/deployment/impl/ui/resources/earBadge.png" ); // NOI18N
99
} else {
100             return null;
101         }
102         return Utilities.mergeImages(folder, badge, 7, 7);
103     }
104     
105     /**
106      * Get a named instance of InputOutput, which represents an output tab in
107      * the output window. The output tab will expose server state management
108      * actions for the given server: start, debug, restart, stop and refresh.
109      * Streams for reading/writing can be accessed via getters on the returned
110      * instance. If the InputOutput already exists for the given server, the
111      * existing instance will be returned. The display name of the given server
112      * will be used as a name for the tab.
113      *
114      * @param url server instance id (DeploymentManager url).
115      *
116      * @return an <code>InputOutput</code> instance for accessing the new tab,
117      * null if there is no registered server instance with the given url.
118      *
119      */

120     public static InputOutput getServerIO(String JavaDoc url) {
121
122         ServerInstance si = ServerRegistry.getInstance().getServerInstance(url);
123
124         if (si == null) {
125             return null;
126         }
127         
128         // look in the cache
129
InputOutput io = (InputOutput)ioWeakMap.get(si);
130         if (io != null) {
131             return io;
132         }
133         
134         // look up the node that belongs to the given server instance
135
Node node = ServerRegistryNode.getServerRegistryNode().getChildren().findChild(si.getUrl());
136         
137         // it looks like that the server instance has been removed
138
if (node == null) {
139             return null;
140         }
141         
142         Action JavaDoc[] actions = new Action JavaDoc[] {
143             new StartAction.OutputAction(node),
144             new DebugAction.OutputAction(node),
145             new RestartAction.OutputAction(node),
146             new StopAction.OutputAction(node),
147             new RefreshAction.OutputAction(node)
148         };
149         InputOutput newIO = IOProvider.getDefault().getIO(si.getDisplayName(), actions);
150         
151         // put the newIO in the cache
152
ioWeakMap.put(si, newIO);
153         return newIO;
154     }
155 }
156
Popular Tags