KickJava   Java API By Example, From Geeks To Geeks.

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


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.impl.ui;
21
22 import java.awt.Image JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import org.netbeans.modules.j2ee.deployment.config.Utils;
28 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
29 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
30 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.DebugAction;
31 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.CustomizerAction;
32 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.ProfileAction;
33 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.RefreshAction;
34 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.RemoveInstanceAction;
35 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.RestartAction;
36 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.StartAction;
37 import org.netbeans.modules.j2ee.deployment.impl.ui.actions.StopAction;
38 import org.openide.nodes.FilterNode;
39 import org.openide.nodes.Node;
40 import org.openide.util.Utilities;
41 import org.openide.util.actions.SystemAction;
42
43 /**
44  * This filter node is used to add additional features to the InstanceNode and
45  * InstanceTargetNode. This filter node defines the node name, displaName,
46  * enhances the original node set of actions with the genaral server instance
47  * actions. Registers a server state changes listener, which will display a server
48  * status badge over the original node icon. Everything else is delegated to the
49  * original node.
50  *
51  * @author sherold
52  */

53 public class InstanceNodeDecorator extends FilterNode
54         implements ServerInstance.StateListener {
55     
56     private static final String JavaDoc WAITING_ICON
57             = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/waiting.png"; // NOI18N
58
private static final String JavaDoc RUNNING_ICON
59             = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/running.png"; // NOI18N
60
private static final String JavaDoc DEBUGGING_ICON
61             = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/debugging.png"; // NOI18N
62
private static final String JavaDoc SUSPENDED_ICON
63             = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/suspended.png"; // NOI18N
64
private static final String JavaDoc PROFILING_ICON
65             = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/profiling.png"; // NOI18N
66
private static final String JavaDoc PROFILER_BLOCKING_ICON
67             = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/profilerblocking.png"; // NOI18N
68

69     private ServerInstance si;
70     
71     /** Creates a new instance of InstanceNodeDecorator */
72     public InstanceNodeDecorator(Node original, ServerInstance si) {
73         super(original);
74         this.si = si;
75         si.addStateListener(this);
76     }
77     
78     public String JavaDoc getDisplayName() {
79         return si.getDisplayName();
80     }
81     
82     public String JavaDoc getName() {
83         return si.getUrl(); // unique identifier
84
}
85     
86     public Action JavaDoc[] getActions(boolean context) {
87         List JavaDoc actions = new ArrayList JavaDoc();
88         actions.addAll(Arrays.asList(new Action JavaDoc[] {
89                                         SystemAction.get(StartAction.class),
90                                         SystemAction.get(DebugAction.class)
91         }));
92         if (si.isProfileSupported()) {
93             actions.add(
94                                         SystemAction.get(ProfileAction.class)
95             );
96         }
97         actions.addAll(Arrays.asList(new Action JavaDoc[] {
98                                         SystemAction.get(RestartAction.class),
99                                         SystemAction.get(StopAction.class),
100                                         SystemAction.get(RefreshAction.class),
101                                         null,
102                                         SystemAction.get(RemoveInstanceAction.class)
103         }));
104         actions.addAll(Arrays.asList(getOriginal().getActions(context)));
105         actions.add(null);
106         actions.add(SystemAction.get(CustomizerAction.class));
107         return (Action JavaDoc[])actions.toArray(new Action JavaDoc[actions.size()]);
108     }
109     
110     public Image JavaDoc getIcon(int type) {
111         return badgeIcon(getOriginal().getIcon(type));
112     }
113     
114     public Image JavaDoc getOpenedIcon(int type) {
115         return badgeIcon(getOriginal().getOpenedIcon(type));
116     }
117     
118     // private helper methods -------------------------------------------------
119

120     private Image JavaDoc badgeIcon(Image JavaDoc origImg) {
121         Image JavaDoc badge = null;
122         switch (si.getServerState()) {
123             case ServerInstance.STATE_WAITING :
124                 badge = Utilities.loadImage(WAITING_ICON);
125                 break;
126             case ServerInstance.STATE_RUNNING :
127                 badge = Utilities.loadImage(RUNNING_ICON);
128                 break;
129             case ServerInstance.STATE_DEBUGGING :
130                 badge = Utilities.loadImage(DEBUGGING_ICON);
131                 break;
132             case ServerInstance.STATE_SUSPENDED :
133                 badge = Utilities.loadImage(SUSPENDED_ICON);
134                 break;
135             case ServerInstance.STATE_PROFILING :
136                 badge = Utilities.loadImage(PROFILING_ICON);
137                 break;
138             case ServerInstance.STATE_PROFILER_BLOCKING :
139                 badge = Utilities.loadImage(PROFILER_BLOCKING_ICON);
140                 break;
141             case ServerInstance.STATE_PROFILER_STARTING :
142                 badge = Utilities.loadImage(WAITING_ICON);
143                 break;
144         }
145         return badge != null ? Utilities.mergeImages(origImg, badge, 15, 8) : origImg;
146     }
147     
148     // StateListener implementation -------------------------------------------
149

150     public void stateChanged(int oldState, int newState) {
151         // invoke icon change - this causes the server status icon badge to be updated
152
Utils.runInEventDispatchThread(new Runnable JavaDoc() {
153             public void run() {
154                 fireIconChange();
155             }
156         });
157     }
158 }
159
Popular Tags