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.debugger.ui.actions;
043:
044: import java.awt.Dialog;
045: import java.awt.event.ActionListener;
046: import java.awt.event.ActionEvent;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049: import java.util.List;
050: import javax.swing.AbstractAction;
051: import javax.swing.Action;
052: import javax.swing.JButton;
053: import org.netbeans.api.debugger.DebuggerManager;
054: import org.netbeans.modules.debugger.ui.Utils;
055: import org.netbeans.spi.debugger.ui.AttachType;
056: import org.netbeans.spi.debugger.ui.Controller;
057: import org.openide.DialogDescriptor;
058: import org.openide.DialogDisplayer;
059: import org.openide.util.NbBundle;
060:
061: /**
062: * Connects debugger to some currently running VM.
063: * This class is final only for performance reasons,
064: * can be happily unfinaled if desired.
065: *
066: * @author Jan Jancura
067: */
068: public final class ConnectAction extends AbstractAction {
069:
070: private Dialog dialog;
071: private JButton bOk;
072: private JButton bCancel;
073:
074: public ConnectAction() {
075: putValue(Action.NAME, NbBundle.getMessage(ConnectAction.class,
076: "CTL_Connect"));
077: putValue(
078: Action.SMALL_ICON,
079: Utils
080: .getIcon("org/netbeans/modules/debugger/resources/actions/Attach" // NOI18N
081: ));
082: putValue("iconBase", // NOI18N
083: "org/netbeans/modules/debugger/resources/actions/Attach.gif" // NOI18N
084: );
085: }
086:
087: public boolean isEnabled() {
088: List attachTypes = DebuggerManager.getDebuggerManager().lookup(
089: null, AttachType.class);
090: return attachTypes.size() > 0;
091: }
092:
093: public void actionPerformed(ActionEvent evt) {
094: bOk = new JButton(NbBundle.getMessage(ConnectAction.class,
095: "CTL_Ok")); // NOI18N
096: bCancel = new JButton(NbBundle.getMessage(ConnectAction.class,
097: "CTL_Cancel")); // NOI18N
098: bOk.getAccessibleContext()
099: .setAccessibleDescription(
100: NbBundle.getMessage(ConnectAction.class,
101: "ACSD_CTL_Ok")); // NOI18N
102: bCancel.getAccessibleContext().setAccessibleDescription(
103: NbBundle.getMessage(ConnectAction.class,
104: "ACSD_CTL_Cancel")); // NOI18N
105: ConnectorPanel cp = new ConnectorPanel();
106: DialogDescriptor descr = new DialogDescriptor(cp, NbBundle
107: .getMessage(ConnectAction.class,
108: "CTL_Connect_to_running_process"), true, // modal
109: new ConnectListener(cp));
110: descr.setOptions(new JButton[] { bOk, bCancel });
111: descr.setClosingOptions(new Object[0]);
112: dialog = DialogDisplayer.getDefault().createDialog(descr);
113: dialog.setVisible(true);
114: }
115:
116: // innerclasses ............................................................
117: private class ConnectListener implements ActionListener,
118: PropertyChangeListener {
119:
120: ConnectorPanel connectorPanel;
121: Controller controller;
122:
123: ConnectListener(ConnectorPanel connectorPanel) {
124: this .connectorPanel = connectorPanel;
125: startListening();
126: setValid();
127: connectorPanel.addPropertyChangeListener(this );
128: }
129:
130: public void actionPerformed(ActionEvent e) {
131: boolean okPressed = bOk.equals(e.getSource());
132: boolean close = false;
133: if (okPressed) {
134: close = connectorPanel.ok();
135: } else {
136: close = connectorPanel.cancel();
137: }
138: if (!close)
139: return;
140: connectorPanel.removePropertyChangeListener(this );
141: stopListening();
142: dialog.setVisible(false);
143: dialog.dispose();
144: dialog = null;
145: }
146:
147: void startListening() {
148: controller = connectorPanel.getController();
149: if (controller == null)
150: return;
151: controller.addPropertyChangeListener(this );
152: }
153:
154: void stopListening() {
155: if (controller == null)
156: return;
157: controller.removePropertyChangeListener(this );
158: controller = null;
159: }
160:
161: void setValid() {
162: Controller controller = connectorPanel.getController();
163: if (controller == null) {
164: bOk.setEnabled(false);
165: return;
166: }
167: bOk.setEnabled(controller.isValid());
168: }
169:
170: public void propertyChange(PropertyChangeEvent evt) {
171: if (evt.getPropertyName() == ConnectorPanel.PROP_TYPE) {
172: stopListening();
173: setValid();
174: startListening();
175: } else if (evt.getPropertyName() == Controller.PROP_VALID) {
176: setValid();
177: }
178: }
179:
180: }
181: }
|