1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.IMarker; 17 import org.eclipse.core.resources.IMarkerDelta; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.IBreakpointManager; 21 import org.eclipse.debug.core.IBreakpointsListener; 22 import org.eclipse.debug.core.model.IBreakpoint; 23 import org.eclipse.jdt.debug.core.IJavaBreakpoint; 24 import org.eclipse.jdt.internal.debug.ui.ExceptionHandler; 25 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 26 import org.eclipse.jface.action.IAction; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.IStructuredSelection; 29 import org.eclipse.ui.IObjectActionDelegate; 30 import org.eclipse.ui.IPartListener; 31 import org.eclipse.ui.IWorkbenchPart; 32 33 36 public abstract class BreakpointToggleAction implements IObjectActionDelegate, IBreakpointsListener, IPartListener { 37 38 private IStructuredSelection fSelection; 39 private IAction fAction; 40 private IWorkbenchPart fPart; 41 42 45 public void run(IAction action) { 46 IStructuredSelection selection= getStructuredSelection(); 47 Iterator itr= selection.iterator(); 48 while (itr.hasNext()) { 49 try { 50 IJavaBreakpoint breakpoint= (IJavaBreakpoint) itr.next(); 51 doAction(breakpoint); 52 } catch (CoreException e) { 53 String title= ActionMessages.BreakpointAction_Breakpoint_configuration_1; 54 String message= ActionMessages.BreakpointAction_Exceptions_occurred_attempting_to_modify_breakpoint__2; 55 ExceptionHandler.handle(e, title, message); 56 } 57 } 58 } 59 60 63 public void selectionChanged(IAction action, ISelection selection) { 64 setAction(action); 65 if (selection.isEmpty()) { 66 setStructuredSelection(null); 67 return; 68 } 69 if (selection instanceof IStructuredSelection) { 70 setStructuredSelection((IStructuredSelection)selection); 71 boolean enabled = isEnabledFor(getStructuredSelection()); 72 action.setEnabled(enabled); 73 if (enabled && isToggleAction()) { 74 IBreakpoint breakpoint = (IBreakpoint)getStructuredSelection().getFirstElement(); 75 if (breakpoint instanceof IJavaBreakpoint) { 76 try { 77 action.setChecked(getToggleState((IJavaBreakpoint) breakpoint)); 78 } catch (CoreException e) { 79 JDIDebugUIPlugin.log(e); 80 } 81 } 82 } 83 } 84 } 85 86 92 protected boolean isToggleAction() { 93 return true; 94 } 95 96 99 public abstract void doAction(IJavaBreakpoint breakpoint) throws CoreException; 100 101 104 protected abstract boolean getToggleState(IJavaBreakpoint breakpoint) throws CoreException; 105 106 109 protected IStructuredSelection getStructuredSelection() { 110 return fSelection; 111 } 112 113 117 protected void setStructuredSelection(IStructuredSelection selection) { 118 fSelection= selection; 119 } 120 121 126 public abstract boolean isEnabledFor(IStructuredSelection selection); 127 128 131 protected IBreakpointManager getBreakpointManager() { 132 return DebugPlugin.getDefault().getBreakpointManager(); 133 } 134 135 138 protected IBreakpoint getBreakpoint(IMarker marker) { 139 return getBreakpointManager().getBreakpoint(marker); 140 } 141 142 146 protected IAction getAction() { 147 return fAction; 148 } 149 150 154 protected void setAction(IAction action) { 155 fAction = action; 156 } 157 158 161 public void breakpointsAdded(IBreakpoint[] breakpoints) { 162 } 163 164 167 public void breakpointsChanged(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) { 168 if (getAction() != null) { 169 IStructuredSelection selection= getStructuredSelection(); 170 if (selection != null) { 171 IBreakpoint selectedBreakpoint= (IBreakpoint)selection.getFirstElement(); 172 for (int i = 0; i < breakpoints.length; i++) { 173 IBreakpoint breakpoint = breakpoints[i]; 174 if (selectedBreakpoint.equals(breakpoint)) { 175 selectionChanged(getAction(), selection); 176 return; 177 } 178 } 179 } 180 } 181 } 182 183 186 public void breakpointsRemoved(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) { 187 } 188 189 193 protected IWorkbenchPart getPart() { 194 return fPart; 195 } 196 197 201 protected void setPart(IWorkbenchPart part) { 202 fPart = part; 203 } 204 205 208 public void partActivated(IWorkbenchPart part) { 209 } 210 211 214 public void partBroughtToTop(IWorkbenchPart part) { 215 } 216 217 220 public void partClosed(IWorkbenchPart part) { 221 if (part == getPart()) { 222 getBreakpointManager().removeBreakpointListener(this); 223 part.getSite().getPage().removePartListener(this); 224 } 225 } 226 227 230 public void partDeactivated(IWorkbenchPart part) { 231 } 232 233 236 public void partOpened(IWorkbenchPart part) { 237 } 238 239 242 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 243 IWorkbenchPart oldPart= getPart(); 244 if (oldPart != null) { 245 getPart().getSite().getPage().removePartListener(this); 246 } 247 248 getBreakpointManager().addBreakpointListener(this); 249 setPart(targetPart); 250 targetPart.getSite().getPage().addPartListener(this); 251 } 252 } 253 254 | Popular Tags |