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.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049: import java.util.ResourceBundle;
050: import javax.swing.AbstractAction;
051: import javax.swing.Action;
052: import javax.swing.JButton;
053:
054: import org.netbeans.api.debugger.DebuggerManager;
055: import org.netbeans.spi.debugger.ui.BreakpointType;
056: import org.netbeans.spi.debugger.ui.Controller;
057:
058: import org.openide.DialogDescriptor;
059: import org.openide.DialogDisplayer;
060: import org.openide.util.NbBundle;
061:
062: /**
063: * AddBreakpoint action.
064: *
065: * @author Jan Jancura
066: */
067: public class AddBreakpointAction extends AbstractAction {
068:
069: private static AddBreakpointDialogManager abdm;
070:
071: public AddBreakpointAction() {
072: putValue(Action.NAME, NbBundle.getMessage(
073: AddBreakpointAction.class, "CTL_AddBreakpoint"));
074: }
075:
076: public void actionPerformed(ActionEvent e) {
077: DebuggerManager dm = DebuggerManager.getDebuggerManager();
078:
079: if (dm.lookup(null, BreakpointType.class).size() == 0)
080: return; // no breakpoint events...
081:
082: // create Add Breakpoint Dialog for it
083: if (abdm == null)
084: abdm = new AddBreakpointDialogManager();
085: abdm.getDialog().setVisible(true);
086: }
087:
088: // innerclasses .........................................................................
089:
090: /**
091: * Dialog manager for adding breakpoints.
092: * This class is final only for performance reasons,
093: * can be happily unfinaled if desired.
094: */
095: static final class AddBreakpointDialogManager extends Object
096: implements ActionListener, PropertyChangeListener {
097:
098: /** true if ok was pressed */
099: private boolean okPressed;
100: private Dialog dialog;
101: private AddBreakpointPanel panel;
102: private DialogDescriptor descriptor;
103: private Controller controller;
104: private JButton bOk;
105: private JButton bCancel;
106:
107: /** Accessor for managed dialog instance */
108: Dialog getDialog() {
109: dialog = createDialog();
110: okPressed = false;
111: setValid();
112: startListening();
113: panel.addPropertyChangeListener(this );
114: return dialog;
115: }
116:
117: /** Constructs managed dialog instance using TopManager.createDialog
118: * and returnrs it */
119: private Dialog createDialog() {
120: ResourceBundle bundle = NbBundle
121: .getBundle(AddBreakpointAction.class);
122:
123: panel = new AddBreakpointPanel();
124: // create dialog descriptor, create & return the dialog
125: descriptor = new DialogDescriptor(panel, bundle
126: .getString("CTL_Breakpoint_Title"), // NOI18N
127: true, this );
128: descriptor.setOptions(new JButton[] {
129: bOk = new JButton(bundle.getString("CTL_Ok")), // NOI18N
130: bCancel = new JButton(bundle
131: .getString("CTL_Cancel")) // NOI18N
132: });
133: bOk.getAccessibleContext().setAccessibleDescription(
134: bundle.getString("ACSD_CTL_Ok")); // NOI18N
135: bCancel.getAccessibleContext().setAccessibleDescription(
136: bundle.getString("ACSD_CTL_Cancel")); // NOI18N
137: descriptor.setClosingOptions(new Object[0]);
138: Dialog d = DialogDisplayer.getDefault().createDialog(
139: descriptor);
140: d.pack();
141: return d;
142: }
143:
144: /** Called when some dialog button was pressed */
145: public void actionPerformed(ActionEvent evt) {
146: okPressed = bOk.equals(evt.getSource());
147: Controller controller = panel.getController();
148: boolean close = false;
149: if (okPressed)
150: close = controller.ok();
151: else
152: close = controller.cancel();
153:
154: if (!close)
155: return;
156: panel.removePropertyChangeListener(this );
157: stopListening();
158: dialog.setVisible(false);
159: dialog.dispose();
160: dialog = null;
161: }
162:
163: public void propertyChange(PropertyChangeEvent e) {
164: if (e.getPropertyName() == AddBreakpointPanel.PROP_TYPE) {
165: stopListening();
166: setValid();
167: startListening();
168:
169: } else if (e.getPropertyName() == Controller.PROP_VALID) {
170: setValid();
171: }
172: }
173:
174: void startListening() {
175: controller = panel.getController();
176: if (controller == null)
177: return;
178: controller.addPropertyChangeListener(this );
179: }
180:
181: void stopListening() {
182: if (controller == null)
183: return;
184: controller.removePropertyChangeListener(this );
185: controller = null;
186: }
187:
188: void setValid() {
189: Controller controller = panel.getController();
190: if (controller == null) {
191: bOk.setEnabled(false);
192: return;
193: }
194: bOk.setEnabled(controller.isValid());
195: }
196:
197: /** @return true if OK button was pressed in dialog,
198: * false otherwise. */
199: public boolean getOKPressed() {
200: return okPressed;
201: }
202: }
203: }
|