KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > RemoveBreakpointAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.actions;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IWorkspaceRunnable;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.eclipse.debug.core.DebugPlugin;
26 import org.eclipse.debug.core.model.IBreakpoint;
27 import org.eclipse.debug.internal.ui.DebugUIPlugin;
28 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointContainer;
29 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsView;
30 import org.eclipse.jface.action.IAction;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.ui.IWorkbenchWindow;
34
35 public class RemoveBreakpointAction extends AbstractRemoveActionDelegate {
36
37     /* (non-Javadoc)
38      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
39      */

40     public void run(IAction action) {
41         IStructuredSelection selection= getSelection();
42         if (selection.isEmpty()) {
43             return;
44         }
45         final List JavaDoc state = ((BreakpointsView)getView()).getSelectionState();
46         final Iterator JavaDoc itr= selection.iterator();
47         final CoreException[] exception= new CoreException[1];
48         IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
49             public void run(IProgressMonitor monitor) {
50                 List JavaDoc breakpointsToDelete= new ArrayList JavaDoc();
51                 boolean deleteContainers= false;
52                 while (itr.hasNext()) {
53                         Object JavaDoc next= itr.next();
54                         if (next instanceof IBreakpoint) {
55                             breakpointsToDelete.add(next);
56                         } else if (next instanceof BreakpointContainer) {
57                             if (!deleteContainers) {
58                                 // Prompt the user to delete containers only once.
59
deleteContainers = MessageDialog.openConfirm(getView().getSite().getShell(), ActionMessages.RemoveBreakpointAction_0, ActionMessages.RemoveBreakpointAction_1); //$NON-NLS-1$ //$NON-NLS-2$
60
if (!deleteContainers) {
61                                     // User cancelled. Do nothing
62
return;
63                                 }
64                             }
65                             // To get here, deletion has to have been confirmed.
66
IBreakpoint[] breakpoints = ((BreakpointContainer) next).getBreakpoints();
67                             for (int i = 0; i < breakpoints.length; i++) {
68                                 breakpointsToDelete.add(breakpoints[i]);
69                             }
70                         }
71                 }
72                 final IBreakpoint[] breakpoints= (IBreakpoint[]) breakpointsToDelete.toArray(new IBreakpoint[0]);
73                 new Job(ActionMessages.RemoveBreakpointAction_2) { //$NON-NLS-1$
74
protected IStatus run(IProgressMonitor pmonitor) {
75                         try {
76                             DebugPlugin.getDefault().getBreakpointManager().removeBreakpoints(breakpoints, true);
77                             if (state != null) {
78                                 Runnable JavaDoc r = new Runnable JavaDoc() {
79                                 
80                                     public void run() {
81                                         ((BreakpointsView) getView()).preserveSelectionState(state);
82                                     }
83                                 };
84                                 DebugUIPlugin.getStandardDisplay().asyncExec(r);
85                             }
86                             return Status.OK_STATUS;
87                         } catch (CoreException e) {
88                             DebugUIPlugin.log(e);
89                         }
90                         return Status.CANCEL_STATUS;
91                     }
92                 }.schedule();
93             }
94         };
95         try {
96             ResourcesPlugin.getWorkspace().run(runnable, null, 0, null);
97         } catch (CoreException ce) {
98             exception[0]= ce;
99         }
100         if (exception[0] != null) {
101             IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow();
102             if (window != null) {
103                 DebugUIPlugin.errorDialog(window.getShell(), ActionMessages.RemoveBreakpointAction_Removing_a_breakpoint_4,ActionMessages.RemoveBreakpointAction_Exceptions_occurred_attempting_to_remove_a_breakpoint__5 , exception[0]); //$NON-NLS-1$ //$NON-NLS-2$
104
} else {
105                 DebugUIPlugin.log(exception[0]);
106             }
107         }
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.debug.internal.ui.actions.AbstractDebugActionDelegate#doAction(java.lang.Object)
112      */

113     protected void doAction(Object JavaDoc element) {
114         //not used
115
}
116
117     protected boolean isEnabledFor(Object JavaDoc element) {
118         if (element instanceof BreakpointContainer)
119             return ((BreakpointContainer)element).getChildren().length > 0;
120         return super.isEnabledFor(element);
121     }
122 }
123
Popular Tags