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 * Debug server action starts the server in the debug mode.
062 *
063 * @author sherold
064 */
065 public class DebugAction extends NodeAction {
066
067 public String getName() {
068 return NbBundle.getMessage(DebugAction.class, "LBL_Debug");
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 RequestProcessor.getDefault().post(new Runnable() {
100 public void run() {
101 String title = NbBundle.getMessage(
102 DebugAction.class, "LBL_Debugging", si
103 .getDisplayName());
104 ProgressUI progressUI = new ProgressUI(title, false);
105 try {
106 progressUI.start();
107 si.startDebug(progressUI);
108 } catch (ServerException ex) {
109 String msg = ex.getLocalizedMessage();
110 NotifyDescriptor desc = new NotifyDescriptor.Message(
111 msg, NotifyDescriptor.ERROR_MESSAGE);
112 DialogDisplayer.getDefault().notify(desc);
113 } finally {
114 progressUI.finish();
115 }
116 }
117 });
118 }
119 }
120
121 private static boolean enableImpl(Node[] nodes) {
122 for (int i = 0; i < nodes.length; i++) {
123 ServerInstance si = (ServerInstance) nodes[i]
124 .getCookie(ServerInstance.class);
125 if (!enableImpl(si)) {
126 return false;
127 }
128 }
129 return true;
130 }
131
132 private static boolean enableImpl(final ServerInstance si) {
133 if (si == null
134 || si.getServerState() != ServerInstance.STATE_STOPPED
135 || !si.isDebugSupported()) {
136 return false;
137 }
138 return true;
139 }
140
141 /** This action will be displayed in the server output window */
142 public static class OutputAction extends AbstractAction implements
143 ServerInstance.StateListener {
144
145 private static final String ICON = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/debug.png"; // NOI18N
146 private static final String PROP_ENABLED = "enabled"; // NOI18N
147 private final ServerInstance instance;
148
149 public OutputAction(ServerInstance instance) {
150 super (NbBundle.getMessage(DebugAction.class,
151 "LBL_DebugOutput"), new ImageIcon(Utilities
152 .loadImage(ICON)));
153 putValue(SHORT_DESCRIPTION, NbBundle.getMessage(
154 DebugAction.class, "LBL_DebugOutputDesc"));
155 this .instance = instance;
156
157 // start listening to changes
158 instance.addStateListener(this );
159 }
160
161 public void actionPerformed(ActionEvent e) {
162 performActionImpl(instance);
163 }
164
165 public boolean isEnabled() {
166 return enableImpl(instance);
167 }
168
169 // ServerInstance.StateListener implementation --------------------------
170
171 public void stateChanged(final int oldState, final int newState) {
172 Mutex.EVENT.readAccess(new Runnable() {
173 public void run() {
174 firePropertyChange(PROP_ENABLED, null,
175 isEnabled() ? Boolean.TRUE : Boolean.FALSE);
176 }
177 });
178 }
179 }
180 }
|