KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import javax.swing.AbstractAction JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.swing.JButton JavaDoc;
31
32 import org.netbeans.api.debugger.DebuggerManager;
33 import org.netbeans.spi.debugger.ui.BreakpointType;
34 import org.netbeans.spi.debugger.ui.Controller;
35
36 import org.openide.DialogDescriptor;
37 import org.openide.DialogDisplayer;
38 import org.openide.util.NbBundle;
39
40
41 /**
42  * AddBreakpoint action.
43  *
44  * @author Jan Jancura
45  */

46 public class AddBreakpointAction extends AbstractAction JavaDoc {
47
48     private static AddBreakpointDialogManager abdm;
49
50     
51     public AddBreakpointAction () {
52         putValue (
53             Action.NAME,
54             NbBundle.getMessage (
55                 AddBreakpointAction.class,
56                 "CTL_AddBreakpoint"
57             )
58         );
59     }
60
61     public void actionPerformed (ActionEvent JavaDoc e) {
62         DebuggerManager dm = DebuggerManager.getDebuggerManager ();
63             
64         if (dm.lookup (null, BreakpointType.class).size () == 0)
65             return; // no breakpoint events...
66

67         // create Add Breakpoint Dialog for it
68
if (abdm == null)
69             abdm = new AddBreakpointDialogManager ();
70         abdm.getDialog ().setVisible (true);
71     }
72     
73     // innerclasses .........................................................................
74

75     /**
76     * Dialog manager for adding breakpoints.
77     * This class is final only for performance reasons,
78     * can be happily unfinaled if desired.
79     */

80     static final class AddBreakpointDialogManager extends Object JavaDoc
81         implements ActionListener JavaDoc, PropertyChangeListener JavaDoc {
82
83         /** true if ok was pressed */
84         private boolean okPressed;
85         private Dialog JavaDoc dialog;
86         private AddBreakpointPanel panel;
87         private DialogDescriptor descriptor;
88         private Controller controller;
89         private JButton JavaDoc bOk;
90         private JButton JavaDoc bCancel;
91
92         /** Accessor for managed dialog instance */
93         Dialog JavaDoc getDialog () {
94             dialog = createDialog ();
95             okPressed = false;
96             setValid ();
97             startListening ();
98             panel.addPropertyChangeListener (this);
99             return dialog;
100         }
101
102         /** Constructs managed dialog instance using TopManager.createDialog
103         * and returnrs it */

104         private Dialog JavaDoc createDialog () {
105             ResourceBundle JavaDoc bundle = NbBundle.getBundle (AddBreakpointAction.class);
106
107             panel = new AddBreakpointPanel ();
108             // create dialog descriptor, create & return the dialog
109
descriptor = new DialogDescriptor (
110                 panel,
111                 bundle.getString ("CTL_Breakpoint_Title"), // NOI18N
112
true,
113                 this
114             );
115             descriptor.setOptions (new JButton JavaDoc[] {
116                 bOk = new JButton JavaDoc (bundle.getString ("CTL_Ok")), // NOI18N
117
bCancel = new JButton JavaDoc (bundle.getString ("CTL_Cancel")) // NOI18N
118
});
119             bOk.getAccessibleContext ().setAccessibleDescription (bundle.getString ("ACSD_CTL_Ok")); // NOI18N
120
bCancel.getAccessibleContext ().setAccessibleDescription (bundle.getString ("ACSD_CTL_Cancel")); // NOI18N
121
descriptor.setClosingOptions (new Object JavaDoc [0]);
122             Dialog JavaDoc d = DialogDisplayer.getDefault ().createDialog (descriptor);
123             d.pack ();
124             return d;
125         }
126
127         /** Called when some dialog button was pressed */
128         public void actionPerformed (ActionEvent JavaDoc evt) {
129             okPressed = bOk.equals (evt.getSource ());
130             Controller controller = panel.getController ();
131             boolean close = false;
132             if (okPressed)
133                 close = controller.ok ();
134             else
135                 close = controller.cancel ();
136                 
137             if (!close) return;
138             panel.removePropertyChangeListener (this);
139             stopListening ();
140             dialog.setVisible (false);
141             dialog.dispose ();
142             dialog = null;
143         }
144         
145         public void propertyChange (PropertyChangeEvent JavaDoc e) {
146             if (e.getPropertyName () == AddBreakpointPanel.PROP_TYPE) {
147                 stopListening ();
148                 setValid ();
149                 startListening ();
150                 
151             } else
152             if (e.getPropertyName () == Controller.PROP_VALID) {
153                 setValid ();
154             }
155         }
156         
157         void startListening () {
158             controller = panel.getController ();
159             if (controller == null) return;
160             controller.addPropertyChangeListener (this);
161         }
162         
163         void stopListening () {
164             if (controller == null) return;
165             controller.removePropertyChangeListener (this);
166             controller = null;
167         }
168         
169         void setValid () {
170             Controller controller = panel.getController ();
171             if (controller == null) {
172                 bOk.setEnabled (false);
173                 return;
174             }
175             bOk.setEnabled (controller.isValid ());
176         }
177
178         /** @return true if OK button was pressed in dialog,
179         * false otherwise. */

180         public boolean getOKPressed () {
181             return okPressed;
182         }
183     }
184 }
185
186
187
Popular Tags