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.models;
043:
044: import java.awt.event.ActionEvent;
045: import javax.swing.AbstractAction;
046: import javax.swing.Action;
047: import javax.swing.KeyStroke;
048:
049: import org.netbeans.api.debugger.Breakpoint;
050: import org.netbeans.api.debugger.DebuggerManager;
051: import org.netbeans.modules.debugger.ui.actions.AddBreakpointAction;
052: import org.netbeans.spi.viewmodel.Models;
053: import org.netbeans.spi.viewmodel.TreeModel;
054: import org.netbeans.spi.viewmodel.NodeActionsProvider;
055: import org.netbeans.spi.viewmodel.ModelListener;
056: import org.netbeans.spi.viewmodel.UnknownTypeException;
057: import org.openide.DialogDisplayer;
058: import org.openide.NotifyDescriptor;
059: import org.openide.util.NbBundle;
060: import org.openide.util.RequestProcessor;
061:
062: /**
063: * @author Jan Jancura
064: */
065: public class BreakpointsActionsProvider implements NodeActionsProvider {
066:
067: private static final Action NEW_BREEAKPOINT_ACTION = new AbstractAction(
068: NbBundle.getBundle(BreakpointsActionsProvider.class)
069: .getString("CTL_BreakpointAction_New_Label")) {
070: public void actionPerformed(ActionEvent e) {
071: new AddBreakpointAction().actionPerformed(null);
072: }
073: };
074: private static final Action ENABLE_ALL_ACTION = new AbstractAction(
075: NbBundle.getBundle(BreakpointsActionsProvider.class)
076: .getString("CTL_BreakpointAction_EnableAll_Label")) {
077: public boolean isEnabled() {
078: DebuggerManager dm = DebuggerManager.getDebuggerManager();
079: Breakpoint[] bs = dm.getBreakpoints();
080: int i, k = bs.length;
081: for (i = 0; i < k; i++) {
082: if (!bs[i].isEnabled()) {
083: return true;
084: }
085: }
086: return false;
087: }
088:
089: public void actionPerformed(ActionEvent e) {
090: DebuggerManager dm = DebuggerManager.getDebuggerManager();
091: Breakpoint[] bs = dm.getBreakpoints();
092: int i, k = bs.length;
093: for (i = 0; i < k; i++)
094: bs[i].enable();
095: }
096: };
097: private static final Action DISABLE_ALL_ACTION = new AbstractAction(
098: NbBundle.getBundle(BreakpointsActionsProvider.class)
099: .getString("CTL_BreakpointAction_DisableAll_Label")) {
100: public boolean isEnabled() {
101: DebuggerManager dm = DebuggerManager.getDebuggerManager();
102: Breakpoint[] bs = dm.getBreakpoints();
103: int i, k = bs.length;
104: for (i = 0; i < k; i++) {
105: if (bs[i].isEnabled()) {
106: return true;
107: }
108: }
109: return false;
110: }
111:
112: public void actionPerformed(ActionEvent e) {
113: DebuggerManager dm = DebuggerManager.getDebuggerManager();
114: Breakpoint[] bs = dm.getBreakpoints();
115: int i, k = bs.length;
116: for (i = 0; i < k; i++)
117: bs[i].disable();
118: }
119: };
120: private static final Action DELETE_ALL_ACTION = new AbstractAction(
121: NbBundle.getBundle(BreakpointsActionsProvider.class)
122: .getString("CTL_BreakpointAction_DeleteAll_Label")) {
123: public boolean isEnabled() {
124: DebuggerManager dm = DebuggerManager.getDebuggerManager();
125: Breakpoint[] bs = dm.getBreakpoints();
126: return bs.length > 0;
127: }
128:
129: public void actionPerformed(ActionEvent e) {
130: DebuggerManager dm = DebuggerManager.getDebuggerManager();
131: Breakpoint[] bs = dm.getBreakpoints();
132: int i, k = bs.length;
133: for (i = 0; i < k; i++)
134: dm.removeBreakpoint(bs[i]);
135: }
136: };
137: private static final Action ENABLE_ACTION = Models.createAction(
138: NbBundle.getBundle(BreakpointsActionsProvider.class)
139: .getString("CTL_BreakpointAction_Enable_Label"),
140: new Models.ActionPerformer() {
141: public boolean isEnabled(Object node) {
142: return true;
143: }
144:
145: public void perform(Object[] nodes) {
146: int i, k = nodes.length;
147: for (i = 0; i < k; i++)
148: ((Breakpoint) nodes[i]).enable();
149: }
150: }, Models.MULTISELECTION_TYPE_ANY);
151: private static final Action DISABLE_ACTION = Models.createAction(
152: NbBundle.getBundle(BreakpointsActionsProvider.class)
153: .getString("CTL_BreakpointAction_Disable_Label"),
154: new Models.ActionPerformer() {
155: public boolean isEnabled(Object node) {
156: return true;
157: }
158:
159: public void perform(Object[] nodes) {
160: int i, k = nodes.length;
161: for (i = 0; i < k; i++)
162: ((Breakpoint) nodes[i]).disable();
163: }
164: }, Models.MULTISELECTION_TYPE_ANY);
165: private static final Action DELETE_ACTION = Models.createAction(
166: NbBundle.getBundle(BreakpointsActionsProvider.class)
167: .getString("CTL_BreakpointAction_Delete_Label"),
168: new Models.ActionPerformer() {
169: public boolean isEnabled(Object node) {
170: return true;
171: }
172:
173: public void perform(final Object[] nodes) {
174: RequestProcessor.getDefault().post(new Runnable() {
175: public void run() {
176: DebuggerManager dm = DebuggerManager
177: .getDebuggerManager();
178: int i, k = nodes.length;
179: for (i = 0; i < k; i++)
180: dm
181: .removeBreakpoint((Breakpoint) nodes[i]);
182: }
183: });
184: }
185: }, Models.MULTISELECTION_TYPE_ANY);
186: static {
187: DELETE_ACTION.putValue(Action.ACCELERATOR_KEY, KeyStroke
188: .getKeyStroke("DELETE"));
189: };
190: private static final Action SET_GROUP_NAME_ACTION = Models
191: .createAction(NbBundle.getBundle(
192: BreakpointsActionsProvider.class).getString(
193: "CTL_BreakpointAction_SetGroupName_Label"),
194: new Models.ActionPerformer() {
195: public boolean isEnabled(Object node) {
196: return true;
197: }
198:
199: public void perform(Object[] nodes) {
200: setGroupName(nodes);
201: }
202: }, Models.MULTISELECTION_TYPE_ALL);
203: private static final Action DELETE_ALL_ACTION_S = Models
204: .createAction(NbBundle.getBundle(
205: BreakpointsActionsProvider.class).getString(
206: "CTL_BreakpointAction_DeleteAll_Label"),
207: new Models.ActionPerformer() {
208: public boolean isEnabled(Object node) {
209: return true;
210: }
211:
212: public void perform(Object[] nodes) {
213: String groupName = (String) nodes[0];
214: DebuggerManager dm = DebuggerManager
215: .getDebuggerManager();
216: Breakpoint[] bs = dm.getBreakpoints();
217: int i, k = bs.length;
218: for (i = 0; i < k; i++)
219: if (bs[i].getGroupName().equals(
220: groupName))
221: dm.removeBreakpoint(bs[i]);
222: }
223: }, Models.MULTISELECTION_TYPE_EXACTLY_ONE);
224: private static final Action ENABLE_ALL_ACTION_S = Models
225: .createAction(NbBundle.getBundle(
226: BreakpointsActionsProvider.class).getString(
227: "CTL_BreakpointAction_EnableAll_Label"),
228: new Models.ActionPerformer() {
229: public boolean isEnabled(Object node) {
230: String groupName = (String) node;
231: DebuggerManager dm = DebuggerManager
232: .getDebuggerManager();
233: Breakpoint[] bs = dm.getBreakpoints();
234: int i, k = bs.length;
235: for (i = 0; i < k; i++) {
236: if (bs[i].getGroupName().equals(
237: groupName)) {
238: if (!bs[i].isEnabled()) {
239: return true;
240: }
241: }
242: }
243: return false;
244: }
245:
246: public void perform(Object[] nodes) {
247: String groupName = (String) nodes[0];
248: Breakpoint[] bs = DebuggerManager
249: .getDebuggerManager()
250: .getBreakpoints();
251: int i, k = bs.length;
252: for (i = 0; i < k; i++)
253: if (bs[i].getGroupName().equals(
254: groupName))
255: bs[i].enable();
256: }
257: }, Models.MULTISELECTION_TYPE_EXACTLY_ONE);
258: private static final Action DISABLE_ALL_ACTION_S = Models
259: .createAction(NbBundle.getBundle(
260: BreakpointsActionsProvider.class).getString(
261: "CTL_BreakpointAction_DisableAll_Label"),
262: new Models.ActionPerformer() {
263: public boolean isEnabled(Object node) {
264: String groupName = (String) node;
265: DebuggerManager dm = DebuggerManager
266: .getDebuggerManager();
267: Breakpoint[] bs = dm.getBreakpoints();
268: int i, k = bs.length;
269: for (i = 0; i < k; i++) {
270: if (bs[i].getGroupName().equals(
271: groupName)) {
272: if (bs[i].isEnabled()) {
273: return true;
274: }
275: }
276: }
277: return false;
278: }
279:
280: public void perform(Object[] nodes) {
281: String groupName = (String) nodes[0];
282: Breakpoint[] bs = DebuggerManager
283: .getDebuggerManager()
284: .getBreakpoints();
285: int i, k = bs.length;
286: for (i = 0; i < k; i++)
287: if (bs[i].getGroupName().equals(
288: groupName))
289: bs[i].disable();
290: }
291: }, Models.MULTISELECTION_TYPE_EXACTLY_ONE);
292:
293: //private Vector listeners = new Vector ();
294:
295: public Action[] getActions(Object node) throws UnknownTypeException {
296: if (node == TreeModel.ROOT)
297: return new Action[] { NEW_BREEAKPOINT_ACTION, null,
298: ENABLE_ALL_ACTION, DISABLE_ALL_ACTION,
299: DELETE_ALL_ACTION, null };
300: if (node instanceof String)
301: return new Action[] { SET_GROUP_NAME_ACTION, null,
302: ENABLE_ALL_ACTION_S, DISABLE_ALL_ACTION_S,
303: DELETE_ALL_ACTION_S, null };
304: if (node instanceof Breakpoint)
305: if (((Breakpoint) node).isEnabled())
306: return new Action[] { DISABLE_ACTION, DELETE_ACTION,
307: SET_GROUP_NAME_ACTION, null,
308: NEW_BREEAKPOINT_ACTION, null,
309: ENABLE_ALL_ACTION, DISABLE_ALL_ACTION,
310: DELETE_ALL_ACTION, null };
311: else
312: return new Action[] { ENABLE_ACTION, DELETE_ACTION,
313: SET_GROUP_NAME_ACTION, null,
314: NEW_BREEAKPOINT_ACTION, null,
315: ENABLE_ALL_ACTION, DISABLE_ALL_ACTION,
316: DELETE_ALL_ACTION, null };
317: throw new UnknownTypeException(node);
318: }
319:
320: public void performDefaultAction(Object node)
321: throws UnknownTypeException {
322: if (node == TreeModel.ROOT)
323: return;
324: if (node instanceof String)
325: return;
326: if (node instanceof Breakpoint)
327: return;
328: throw new UnknownTypeException(node);
329: }
330:
331: public void addModelListener(ModelListener l) {
332: //listeners.add (l);
333: }
334:
335: public void removeModelListener(ModelListener l) {
336: //listeners.remove (l);
337: }
338:
339: // public void fireTreeChanged () {
340: // Vector v = (Vector) listeners.clone ();
341: // int i, k = v.size ();
342: // for (i = 0; i < k; i++)
343: // ((TreeModelListener) v.get (i)).treeChanged ();
344: // }
345:
346: private static void setGroupName(Object[] nodes) {
347: NotifyDescriptor.InputLine descriptor = new NotifyDescriptor.InputLine(
348: NbBundle
349: .getBundle(BreakpointsActionsProvider.class)
350: .getString(
351: "CTL_BreakpointAction_GroupDialog_NameLabel"),
352: NbBundle
353: .getBundle(BreakpointsActionsProvider.class)
354: .getString(
355: "CTL_BreakpointAction_GroupDialog_Title"));
356: if (DialogDisplayer.getDefault().notify(descriptor) == NotifyDescriptor.OK_OPTION) {
357: if (nodes[0] instanceof String) {
358: String oldName = (String) nodes[0];
359: Breakpoint[] bs = DebuggerManager.getDebuggerManager()
360: .getBreakpoints();
361: int j, jj = bs.length;
362: for (j = 0; j < jj; j++)
363: if (((Breakpoint) bs[j]).getGroupName().equals(
364: oldName))
365: ((Breakpoint) bs[j]).setGroupName(descriptor
366: .getInputText());
367: return;
368: }
369: int i, k = nodes.length;
370: for (i = 0; i < k; i++)
371: ((Breakpoint) nodes[i]).setGroupName(descriptor
372: .getInputText());
373: // fireTreeChanged ();
374: }
375: }
376: }
|