1 11 package org.eclipse.debug.internal.ui.actions.breakpoints; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IAdaptable; 15 import org.eclipse.core.runtime.IAdapterManager; 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.debug.core.DebugPlugin; 18 import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget; 19 import org.eclipse.jface.action.IAction; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.swt.widgets.Event; 23 import org.eclipse.ui.IActionDelegate2; 24 import org.eclipse.ui.IObjectActionDelegate; 25 import org.eclipse.ui.IWorkbenchPart; 26 27 35 public abstract class ToggleBreakpointObjectActionDelegate implements IObjectActionDelegate, IActionDelegate2 { 36 37 private IWorkbenchPart fPart; 38 private IStructuredSelection fSelection; 39 40 43 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 44 fPart = targetPart; 45 } 46 49 public void run(IAction action) { 50 IAdaptable adaptable = (IAdaptable) fSelection.getFirstElement(); 51 IToggleBreakpointsTarget target = (IToggleBreakpointsTarget) adaptable.getAdapter(IToggleBreakpointsTarget.class); 52 if (target == null) { 53 IAdapterManager adapterManager = Platform.getAdapterManager(); 54 target = (IToggleBreakpointsTarget) adapterManager.loadAdapter(adaptable, IToggleBreakpointsTarget.class.getName()); 55 } 56 if (target != null) { 57 try { 58 performAction(target, fPart, fSelection); 59 } catch (CoreException e) { 60 DebugPlugin.log(e); 61 } 62 } 63 } 64 65 73 protected abstract void performAction(IToggleBreakpointsTarget target, IWorkbenchPart part, ISelection selection) throws CoreException; 74 75 78 public void selectionChanged(IAction action, ISelection selection) { 79 boolean enabled = false; 80 if (selection instanceof IStructuredSelection) { 81 IStructuredSelection ss = (IStructuredSelection) selection; 82 this.fSelection = ss; 83 if (!ss.isEmpty()) { 84 Object object = ss.getFirstElement(); 85 if (object instanceof IAdaptable) { 86 IAdaptable adaptable = (IAdaptable) object; 87 IToggleBreakpointsTarget target = (IToggleBreakpointsTarget) adaptable.getAdapter(IToggleBreakpointsTarget.class); 88 if (target == null) { 89 IAdapterManager adapterManager = Platform.getAdapterManager(); 90 enabled = adapterManager.hasAdapter(adaptable, IToggleBreakpointsTarget.class.getName()); 91 } else { 92 enabled = true; 93 } 94 } 95 } 96 } 97 action.setEnabled(enabled); 98 } 99 102 public void init(IAction action) { 103 } 104 105 108 public void dispose() { 109 fSelection = null; 110 fPart = null; 111 } 112 115 public void runWithEvent(IAction action, Event event) { 116 run(action); 117 } 118 } 119 | Popular Tags |