KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > ui > actions > ConnectAction


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.debugger.ui.actions;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import org.netbeans.modules.debugger.ui.Utils;
31 import org.netbeans.spi.debugger.ui.Controller;
32 import org.openide.DialogDescriptor;
33 import org.openide.DialogDisplayer;
34 import org.openide.util.NbBundle;
35
36
37 /**
38 * Connects debugger to some currently running VM.
39 * This class is final only for performance reasons,
40 * can be happily unfinaled if desired.
41 *
42 * @author Jan Jancura
43 */

44 public final class ConnectAction extends AbstractAction JavaDoc {
45     
46     private Dialog JavaDoc dialog;
47     private JButton JavaDoc bOk;
48     private JButton JavaDoc bCancel;
49
50     
51     public ConnectAction () {
52         putValue (
53             Action.NAME,
54             NbBundle.getMessage (
55                 ConnectAction.class,
56                 "CTL_Connect"
57             )
58         );
59         putValue (
60             Action.SMALL_ICON,
61             Utils.getIcon (
62                 "org/netbeans/modules/debugger/resources/actions/Attach" // NOI18N
63
)
64         );
65         putValue (
66             "iconBase", // NOI18N
67
"org/netbeans/modules/debugger/resources/actions/Attach.gif" // NOI18N
68
);
69     }
70     
71     public void actionPerformed (ActionEvent JavaDoc evt) {
72         bOk = new JButton JavaDoc (NbBundle.getMessage (ConnectAction.class, "CTL_Ok")); // NOI18N
73
bCancel = new JButton JavaDoc (NbBundle.getMessage (ConnectAction.class, "CTL_Cancel")); // NOI18N
74
bOk.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage (ConnectAction.class, "ACSD_CTL_Ok")); // NOI18N
75
bCancel.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage (ConnectAction.class, "ACSD_CTL_Cancel")); // NOI18N
76
ConnectorPanel cp = new ConnectorPanel ();
77         DialogDescriptor descr = new DialogDescriptor (
78             cp,
79             NbBundle.getMessage (ConnectAction.class, "CTL_Connect_to_running_process"),
80             true, // modal
81
new ConnectListener (cp)
82         );
83         descr.setOptions (new JButton JavaDoc[] {
84             bOk, bCancel
85         });
86         descr.setClosingOptions (new Object JavaDoc [0]);
87         dialog = DialogDisplayer.getDefault ().createDialog (descr);
88         dialog.setVisible(true);
89     }
90
91
92     // innerclasses ............................................................
93
private class ConnectListener implements ActionListener JavaDoc, PropertyChangeListener JavaDoc {
94         
95         ConnectorPanel connectorPanel;
96         Controller controller;
97         
98         ConnectListener (ConnectorPanel connectorPanel) {
99             this.connectorPanel = connectorPanel;
100             startListening();
101             setValid();
102             connectorPanel.addPropertyChangeListener(this);
103         }
104         
105         public void actionPerformed (ActionEvent JavaDoc e) {
106             boolean okPressed = bOk.equals (e.getSource ());
107             Controller controller = connectorPanel.getController ();
108             boolean close = false;
109             if (okPressed) {
110                 close = controller.ok ();
111             } else {
112                 close = controller.cancel ();
113             }
114             if (!close) return;
115             connectorPanel.removePropertyChangeListener (this);
116             stopListening ();
117             dialog.setVisible (false);
118             dialog.dispose ();
119             dialog = null;
120         }
121         
122         void startListening () {
123             controller = connectorPanel.getController ();
124             if (controller == null) return;
125             controller.addPropertyChangeListener (this);
126         }
127         
128         void stopListening () {
129             if (controller == null) return;
130             controller.removePropertyChangeListener (this);
131             controller = null;
132         }
133
134         void setValid () {
135             Controller controller = connectorPanel.getController ();
136             if (controller == null) {
137                 bOk.setEnabled (false);
138                 return;
139             }
140             bOk.setEnabled (controller.isValid ());
141         }
142
143         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
144             if (evt.getPropertyName () == ConnectorPanel.PROP_TYPE) {
145                 stopListening ();
146                 setValid ();
147                 startListening ();
148             } else if (evt.getPropertyName () == Controller.PROP_VALID) {
149                 setValid ();
150             }
151         }
152         
153     }
154 }
155
156
157
Popular Tags