1 11 package org.eclipse.debug.internal.ui.actions.breakpoints; 12 13 14 import java.util.Iterator ; 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 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 72 protected boolean isEnableAction() { 73 return true; 74 } 75 76 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 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 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 } 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); } else { 126 DebugUIPlugin.log(ms); 127 } 128 } 129 } 130 131 private IStructuredSelection getSelection() { 132 return (IStructuredSelection)getView().getViewSite().getSelectionProvider().getSelection(); 133 } 134 135 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 itr= sel.iterator(); 146 boolean allEnabled= true; 147 boolean allDisabled= true; 148 while (itr.hasNext()) { 149 Object 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); } else { 192 DebugUIPlugin.log(ce); 193 } 194 } 195 196 197 200 public void dispose() { 201 DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(this); 202 getView().getViewSite().getPage().removePartListener(this); 203 } 204 205 208 public void breakpointsAdded(IBreakpoint[] breakpoints) { 209 } 210 211 214 public void breakpointsRemoved(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) { 215 asynchUpdate(); 216 } 217 218 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 r= new Runnable () { 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 264 public void partActivated(IWorkbenchPart part) { 265 } 266 267 270 public void partBroughtToTop(IWorkbenchPart part) { 271 } 272 273 276 public void partClosed(IWorkbenchPart part) { 277 if (part.equals(getView())) { 278 dispose(); 279 } 280 } 281 282 285 public void partDeactivated(IWorkbenchPart part) { 286 } 287 288 291 public void partOpened(IWorkbenchPart part) { 292 } 293 } 294 295 | Popular Tags |