KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > breakpoints > 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.breakpoints;
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.actions.ActionMessages;
29 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointContainer;
30 import org.eclipse.jface.action.IAction;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.IPartListener;
35 import org.eclipse.ui.IViewActionDelegate;
36 import org.eclipse.ui.IViewPart;
37 import org.eclipse.ui.IWorkbenchPart;
38 import org.eclipse.ui.IWorkbenchWindow;
39
40 public class EnableBreakpointsAction implements IViewActionDelegate, IPartListener, IBreakpointsListener {
41     
42     private IViewPart fView;
43     private IAction fAction;
44     
45     public EnableBreakpointsAction() {
46     }
47         
48     protected IViewPart getView() {
49         return fView;
50     }
51
52     protected void setView(IViewPart view) {
53         fView = view;
54     }
55
56     /**
57      * @see IViewActionDelegate#init(IViewPart)
58      */

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

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

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

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

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

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

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

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

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

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

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

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

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