KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Debug server action starts the server in the debug mode.
41  *
42  * @author sherold
43  */

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

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

132         public void stateChanged(final int oldState, final int newState) {
133             Utils.runInEventDispatchThread(new Runnable JavaDoc() {
134                 public void run() {
135                     firePropertyChange(
136                         PROP_ENABLED,
137                         null,
138                         isEnabled() ? Boolean.TRUE : Boolean.FALSE);
139                 }
140             });
141         }
142     }
143 }
144
Popular Tags