KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.awt.event.ActionEvent JavaDoc;
23 import javax.swing.AbstractAction JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import org.netbeans.modules.j2ee.deployment.config.Utils;
26 import org.netbeans.modules.j2ee.deployment.impl.ServerException;
27 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
28 import org.netbeans.modules.j2ee.deployment.impl.ui.ProgressUI;
29 import org.openide.DialogDisplayer;
30 import org.openide.NotifyDescriptor;
31 import org.openide.nodes.Node;
32 import org.openide.util.HelpCtx;
33 import org.openide.util.NbBundle;
34 import org.openide.util.RequestProcessor;
35 import org.openide.util.Utilities;
36 import org.openide.util.actions.NodeAction;
37
38
39 /**
40  * Restart action stops the server and then starts it again in the mode it
41  * was running in before (normal or debug).
42  *
43  * @author sherold
44  */

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

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

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