001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005 *
006 * The contents of this file are subject to the terms of either the GNU
007 * General Public License Version 2 only ("GPL") or the Common
008 * Development and Distribution License("CDDL") (collectively, the
009 * "License"). You may not use this file except in compliance with the
010 * License. You can obtain a copy of the License at
011 * http://www.netbeans.org/cddl-gplv2.html
012 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013 * specific language governing permissions and limitations under the
014 * License. When distributing the software, include this License Header
015 * Notice in each file and include the License file at
016 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017 * particular file as subject to the "Classpath" exception as provided
018 * by Sun in the GPL Version 2 section of the License file that
019 * accompanied this code. If applicable, add the following below the
020 * License Header, with the fields enclosed by brackets [] replaced by
021 * your own identifying information:
022 * "Portions Copyrighted [year] [name of copyright owner]"
023 *
024 * Contributor(s):
025 *
026 * The Original Software is NetBeans. The Initial Developer of the Original
027 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028 * Microsystems, Inc. All Rights Reserved.
029 *
030 * If you wish your version of this file to be governed by only the CDDL
031 * or only the GPL Version 2, indicate your decision by adding
032 * "[Contributor] elects to include this software in this distribution
033 * under the [CDDL or GPL Version 2] license." If you do not indicate a
034 * single choice of license, a recipient has the option to distribute
035 * your version of this file under either the CDDL, the GPL Version 2 or
036 * to extend the choice of license to its licensees as provided above.
037 * However, if you add GPL Version 2 code and therefore, elected the GPL
038 * Version 2 license, then the option applies only if the new code is
039 * made subject to such option by the copyright holder.
040 */
041
042 package org.netbeans.modules.j2ee.deployment.impl.ui.actions;
043
044 import java.awt.event.ActionEvent;
045 import javax.swing.AbstractAction;
046 import javax.swing.ImageIcon;
047 import org.netbeans.modules.j2ee.deployment.impl.ServerException;
048 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
049 import org.netbeans.modules.j2ee.deployment.impl.ui.ProgressUI;
050 import org.openide.DialogDisplayer;
051 import org.openide.NotifyDescriptor;
052 import org.openide.nodes.Node;
053 import org.openide.util.HelpCtx;
054 import org.openide.util.Mutex;
055 import org.openide.util.NbBundle;
056 import org.openide.util.RequestProcessor;
057 import org.openide.util.Utilities;
058 import org.openide.util.actions.NodeAction;
059
060 /**
061 * Start action starts the server in the normal mode.
062 *
063 * @author sherold
064 */
065 public class StartAction extends NodeAction {
066
067 public String getName() {
068 return NbBundle.getMessage(StartAction.class, "LBL_Start");
069 }
070
071 protected void performAction(Node[] nodes) {
072 performActionImpl(nodes);
073 }
074
075 protected boolean enable(Node[] nodes) {
076 return enableImpl(nodes);
077 }
078
079 public HelpCtx getHelpCtx() {
080 return HelpCtx.DEFAULT_HELP;
081 }
082
083 protected boolean asynchronous() {
084 return false;
085 }
086
087 // private helper methods -------------------------------------------------
088
089 private static void performActionImpl(Node[] nodes) {
090 for (int i = 0; i < nodes.length; i++) {
091 final ServerInstance si = (ServerInstance) nodes[i]
092 .getCookie(ServerInstance.class);
093 performActionImpl(si);
094 }
095 }
096
097 private static void performActionImpl(final ServerInstance si) {
098 if (si != null) {
099 si.setServerState(ServerInstance.STATE_WAITING);
100
101 RequestProcessor.getDefault().post(new Runnable() {
102 public void run() {
103 String title = NbBundle.getMessage(
104 StartAction.class, "LBL_Starting", si
105 .getDisplayName());
106 ProgressUI progressUI = new ProgressUI(title, false);
107 try {
108 progressUI.start();
109 si.start(progressUI);
110 } catch (ServerException ex) {
111 String msg = ex.getLocalizedMessage();
112 NotifyDescriptor desc = new NotifyDescriptor.Message(
113 msg, NotifyDescriptor.ERROR_MESSAGE);
114 DialogDisplayer.getDefault().notify(desc);
115 } finally {
116 progressUI.finish();
117 }
118 }
119 });
120 }
121 }
122
123 private static boolean enableImpl(Node[] nodes) {
124 for (int i = 0; i < nodes.length; i++) {
125 ServerInstance si = (ServerInstance) nodes[i]
126 .getCookie(ServerInstance.class);
127 if (!enableImpl(si)) {
128 return false;
129 }
130 }
131 return true;
132 }
133
134 private static boolean enableImpl(final ServerInstance si) {
135 if (si == null || !si.canStartServer()
136 || si.getServerState() != ServerInstance.STATE_STOPPED) {
137 return false;
138 }
139 return true;
140 }
141
142 /** This action will be displayed in the server output window */
143 public static class OutputAction extends AbstractAction implements
144 ServerInstance.StateListener {
145
146 private static final String ICON = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/start.png"; // NOI18N
147 private static final String PROP_ENABLED = "enabled"; // NOI18N
148 private final ServerInstance instance;
149
150 public OutputAction(ServerInstance instance) {
151 super (NbBundle.getMessage(StartAction.class,
152 "LBL_StartOutput"), new ImageIcon(Utilities
153 .loadImage(ICON)));
154 putValue(SHORT_DESCRIPTION, NbBundle.getMessage(
155 StartAction.class, "LBL_StartOutputDesc"));
156 this .instance = instance;
157
158 // start listening to changes
159 instance.addStateListener(this );
160 }
161
162 public void actionPerformed(ActionEvent e) {
163 performActionImpl(instance);
164 }
165
166 public boolean isEnabled() {
167 return enableImpl(instance);
168 }
169
170 // ServerInstance.StateListener implementation --------------------------
171
172 public void stateChanged(final int oldState, final int newState) {
173 Mutex.EVENT.readAccess(new Runnable() {
174 public void run() {
175 firePropertyChange(PROP_ENABLED, null,
176 isEnabled() ? Boolean.TRUE : Boolean.FALSE);
177 }
178 });
179 }
180 }
181 }
|