KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IMarkerDelta;
17 import org.eclipse.core.resources.IWorkspaceRunnable;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.debug.core.DebugException;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.IBreakpointsListener;
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.jface.action.IAction;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.IPartListener;
34 import org.eclipse.ui.IViewActionDelegate;
35 import org.eclipse.ui.IViewPart;
36 import org.eclipse.ui.IWorkbenchPart;
37 import org.eclipse.ui.IWorkbenchWindow;
38
39 public class EnableBreakpointsAction implements IViewActionDelegate, IPartListener, IBreakpointsListener {
40     
41     private IViewPart fView;
42     private IAction fAction;
43     
44     public EnableBreakpointsAction() {
45     }
46         
47     protected IViewPart getView() {
48         return fView;
49     }
50
51     protected void setView(IViewPart view) {
52         fView = view;
53     }
54
55     /**
56      * @see IViewActionDelegate#init(IViewPart)
57      */

58     public void init(IViewPart view) {
59         setView(view);
60         DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this);
61         view.getViewSite().getPage().addPartListener(this);
62     }
63
64     protected void update() {
65         selectionChanged(getAction(), getView().getViewSite().getSelectionProvider().getSelection());
66     }
67     
68     /**
69      * This action enables breakpoints.
70      */

71     protected boolean isEnableAction() {
72         return true;
73     }
74     
75     /**
76      * @see IActionDelegate#run(IAction)
77      */

78     public void run(IAction action) {
79         IStructuredSelection selection= getSelection();
80         final int size= selection.size();
81         if (size == 0) {
82             return;
83         }
84         
85         final Iterator JavaDoc itr= selection.iterator();
86         final MultiStatus ms= new MultiStatus(DebugUIPlugin.getUniqueIdentifier(), DebugException.REQUEST_FAILED, ActionMessages.EnableBreakpointAction_Enable_breakpoint_s__failed_2, null); //$NON-NLS-1$
87
IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
88             public void run(IProgressMonitor monitor) {
89                 while (itr.hasNext()) {
90                     Object JavaDoc element= itr.next();
91                     try {
92                         IBreakpoint[] breakpoints= null;
93                         if (element instanceof IBreakpoint) {
94                             breakpoints= new IBreakpoint[] { (IBreakpoint) element };
95                         } else if (element instanceof BreakpointContainer) {
96                             breakpoints= ((BreakpointContainer) element).getBreakpoints();
97                         }
98                         if (breakpoints != null) {
99                             setEnabled(breakpoints);
100                         }
101                     } catch (CoreException e) {
102                         ms.merge(e.getStatus());
103                     }
104                 }
105             }
106             public void setEnabled(IBreakpoint[] breakpoints) throws CoreException {
107                 boolean enable= isEnableAction();
108                 for (int i = 0; i < breakpoints.length; i++) {
109                     breakpoints[i].setEnabled(enable);
110                 }
111             }
112         };
113         
114         try {
115             ResourcesPlugin.getWorkspace().run(runnable, null, 0, new NullProgressMonitor());
116         } catch (CoreException e) {
117             // Exceptions are handled by runnable
118
}
119         
120         if (!ms.isOK()) {
121             IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow();
122             if (window != null) {
123                 DebugUIPlugin.errorDialog(window.getShell(), ActionMessages.EnableBreakpointAction_Enabling_breakpoints_3, ActionMessages.EnableBreakpointAction_Exceptions_occurred_enabling_the_breakpoint_s___4, ms); //$NON-NLS-2$ //$NON-NLS-1$
124
} else {
125                 DebugUIPlugin.log(ms);
126             }
127         }
128     }
129
130     private IStructuredSelection getSelection() {
131         return (IStructuredSelection)getView().getViewSite().getSelectionProvider().getSelection();
132     }
133
134     /* (non-Javadoc)
135      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
136      */

137     public void selectionChanged(IAction action, ISelection selection) {
138         setAction(action);
139         if (!(selection instanceof IStructuredSelection)) {
140             return;
141         }
142         IStructuredSelection sel= (IStructuredSelection)selection;
143         
144         Iterator JavaDoc itr= sel.iterator();
145         boolean allEnabled= true;
146         boolean allDisabled= true;
147         while (itr.hasNext()) {
148             Object JavaDoc selected= itr.next();
149             if (selected instanceof BreakpointContainer) {
150                 IBreakpoint[] breakpoints = ((BreakpointContainer) selected).getBreakpoints();
151                 for (int i = 0; i < breakpoints.length; i++) {
152                     try {
153                         if (breakpoints[i].isEnabled()) {
154                             allDisabled= false;
155                         } else {
156                             allEnabled= false;
157                         }
158                     } catch (CoreException ce) {
159                         handleException(ce);
160                     }
161                 }
162             } else if (selected instanceof IBreakpoint) {
163                 IBreakpoint bp= (IBreakpoint)selected;
164                 try {
165                     if (bp.isEnabled()) {
166                         allDisabled= false;
167                     } else {
168                         allEnabled= false;
169                     }
170                 } catch (CoreException ce) {
171                     handleException(ce);
172                 }
173             } else {
174                 return;
175             }
176             
177         }
178             
179         if (isEnableAction()) {
180             action.setEnabled(!allEnabled);
181         } else {
182             action.setEnabled(!allDisabled);
183         }
184     }
185     
186     private void handleException(CoreException ce) {
187         IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow();
188         if (window != null) {
189             DebugUIPlugin.errorDialog(window.getShell(), ActionMessages.EnableBreakpointAction_Enabling_breakpoints_3, ActionMessages.EnableBreakpointAction_Exceptions_occurred_enabling_the_breakpoint_s___4, ce); //$NON-NLS-2$ //$NON-NLS-1$
190
} else {
191             DebugUIPlugin.log(ce);
192         }
193     }
194     
195
196     /**
197      * Removes this action as a breakpoint and part listener.
198      */

199     public void dispose() {
200         DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(this);
201         getView().getViewSite().getPage().removePartListener(this);
202     }
203     
204     /**
205      * @see IBreakpointsListener#breakpointsAdded(IBreakpoint[])
206      */

207     public void breakpointsAdded(IBreakpoint[] breakpoints) {
208     }
209     
210     /**
211      * @see IBreakpointsListener#breakpointsRemoved(IBreakpoint[], IMarkerDelta[])
212      */

213     public void breakpointsRemoved(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) {
214         asynchUpdate();
215     }
216     
217     /**
218      * @see IBreakpointsListener#breakpointsChanged(IBreakpoint[], IMarkerDelta[])
219      */

220     public void breakpointsChanged(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) {
221         asynchUpdate();
222     }
223     
224     protected void asynchUpdate() {
225         if (getAction() == null) {
226             return;
227         }
228         IWorkbenchWindow window= getView().getViewSite().getPage().getWorkbenchWindow();
229         if (window == null) {
230             return;
231         }
232         Shell shell= window.getShell();
233         if (shell == null || shell.isDisposed()) {
234             return;
235         }
236         Runnable JavaDoc r= new Runnable JavaDoc() {
237             public void run() {
238                 IWorkbenchWindow ww= getView().getViewSite().getPage().getWorkbenchWindow();
239                 if (ww == null) {
240                     return;
241                 }
242                 Shell s= ww.getShell();
243                 if (s == null || s.isDisposed()) {
244                     return;
245                 }
246                 update();
247             }
248         };
249         
250         shell.getDisplay().asyncExec(r);
251     }
252     
253     protected IAction getAction() {
254         return fAction;
255     }
256
257     protected void setAction(IAction action) {
258         fAction = action;
259     }
260     /**
261      * @see IPartListener#partActivated(IWorkbenchPart)
262      */

263     public void partActivated(IWorkbenchPart part) {
264     }
265
266     /**
267      * @see IPartListener#partBroughtToTop(IWorkbenchPart)
268      */

269     public void partBroughtToTop(IWorkbenchPart part) {
270     }
271
272     /**
273      * @see IPartListener#partClosed(IWorkbenchPart)
274      */

275     public void partClosed(IWorkbenchPart part) {
276         if (part.equals(getView())) {
277             dispose();
278         }
279     }
280
281     /**
282      * @see IPartListener#partDeactivated(IWorkbenchPart)
283      */

284     public void partDeactivated(IWorkbenchPart part) {
285     }
286
287     /**
288      * @see IPartListener#partOpened(IWorkbenchPart)
289      */

290     public void partOpened(IWorkbenchPart part) {
291     }
292 }
293
294
Popular Tags