KickJava   Java API By Example, From Geeks To Geeks.

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


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.actions;
21 import java.awt.event.ActionEvent JavaDoc;
22 import javax.swing.AbstractAction JavaDoc;
23 import javax.swing.ImageIcon JavaDoc;
24 import org.netbeans.modules.j2ee.deployment.impl.*;
25 import org.netbeans.modules.j2ee.deployment.impl.ui.*;
26 import org.openide.DialogDisplayer;
27 import org.openide.NotifyDescriptor;
28 import org.openide.nodes.Node;
29 import org.openide.util.HelpCtx;
30 import org.openide.util.Mutex;
31 import org.openide.util.RequestProcessor;
32 import org.openide.util.NbBundle;
33 import org.openide.util.Utilities;
34 import org.openide.util.actions.NodeAction;
35
36
37 /**
38  * Stop action stops the server.
39  *
40  * @author sherold
41  */

42 public class StopAction extends NodeAction {
43     
44     public String JavaDoc getName() {
45         return NbBundle.getMessage(StopAction.class, "LBL_Stop");
46     }
47     
48     protected void performAction(Node[] nodes) {
49         performActionImpl(nodes);
50     }
51     
52     protected boolean enable(Node[] nodes) {
53         return enableImpl(nodes);
54     }
55     
56     public HelpCtx getHelpCtx() {
57         return HelpCtx.DEFAULT_HELP;
58     }
59     
60     protected boolean asynchronous() {
61         return false;
62     }
63     
64     // private helper methods -------------------------------------------------
65

66     private static void performActionImpl(Node[] nodes) {
67         for (int i = 0; i < nodes.length; i++) {
68             final ServerInstance si = (ServerInstance)nodes[i].getCookie(ServerInstance.class);
69             si.setServerState(ServerInstance.STATE_WAITING);
70             if (si != null) {
71                 RequestProcessor.getDefault().post(new Runnable JavaDoc() {
72                     public void run() {
73                         String JavaDoc title = NbBundle.getMessage(StopAction.class, "LBL_Stopping", si.getDisplayName());
74                         ProgressUI progressUI = new ProgressUI(title, false);
75                         try {
76                             progressUI.start();
77                             si.stop(progressUI);
78                         } catch (ServerException ex) {
79                             String JavaDoc msg = ex.getLocalizedMessage();
80                             NotifyDescriptor desc = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
81                             DialogDisplayer.getDefault().notify(desc);
82                         } finally {
83                             progressUI.finish();
84                         }
85                     }
86                 });
87             }
88         }
89     }
90     
91     private static boolean enableImpl(Node[] nodes) {
92         for (int i = 0; i < nodes.length; i++) {
93             ServerInstance si = (ServerInstance)nodes[i].getCookie(ServerInstance.class);
94             if (si == null || !si.canStartServer()) {
95                 return false;
96             }
97             int state = si.getServerState();
98             if (state != ServerInstance.STATE_RUNNING
99                 && state != ServerInstance.STATE_DEBUGGING
100                 && state != ServerInstance.STATE_PROFILING
101                 && state != ServerInstance.STATE_PROFILER_BLOCKING) {
102                 return false;
103             }
104         }
105         return true;
106     }
107     
108     /** This action will be displayed in the server output window */
109     public static class OutputAction extends AbstractAction JavaDoc implements ServerInstance.StateListener {
110     
111         private static final String JavaDoc ICON =
112                 "org/netbeans/modules/j2ee/deployment/impl/ui/resources/stop.png"; // NOI18N
113
private static final String JavaDoc PROP_ENABLED = "enabled"; // NOI18N
114
private Node node;
115         
116         public OutputAction(Node node) {
117             super(NbBundle.getMessage(StopAction.class, "LBL_StopOutput"),
118                   new ImageIcon JavaDoc(Utilities.loadImage(ICON)));
119             putValue(SHORT_DESCRIPTION, NbBundle.getMessage(StopAction.class, "LBL_StopOutputDesc"));
120             this.node = node;
121             
122             // start listening to changes
123
ServerInstance si = (ServerInstance)node.getCookie(ServerInstance.class);
124             si.addStateListener(this);
125         }
126
127         public void actionPerformed(ActionEvent JavaDoc e) {
128             performActionImpl(new Node[] {node});
129         }
130
131         public boolean isEnabled() {
132             return enableImpl(new Node[] {node});
133         }
134         
135         // ServerInstance.StateListener implementation --------------------------
136

137         public void stateChanged(final int oldState, final int newState) {
138             Mutex.EVENT.readAccess(new Runnable JavaDoc() {
139                 public void run() {
140                     firePropertyChange(
141                         PROP_ENABLED,
142                         null,
143                         isEnabled() ? Boolean.TRUE : Boolean.FALSE);
144                 }
145             });
146         }
147     }
148 }
149
Popular Tags