1 11 package org.eclipse.debug.internal.ui.actions; 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 38 public abstract class ToggleBreakpointObjectActionDelegate implements IObjectActionDelegate, IActionDelegate2 { 39 40 private IWorkbenchPart fPart; 41 private IStructuredSelection fSelection; 42 43 46 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 47 fPart = targetPart; 48 } 49 52 public void run(IAction action) { 53 IAdaptable adaptable = (IAdaptable) fSelection.getFirstElement(); 54 IToggleBreakpointsTarget target = (IToggleBreakpointsTarget) adaptable.getAdapter(IToggleBreakpointsTarget.class); 55 if (target == null) { 56 IAdapterManager adapterManager = Platform.getAdapterManager(); 57 target = (IToggleBreakpointsTarget) adapterManager.loadAdapter(adaptable, IToggleBreakpointsTarget.class.getName()); 58 } 59 if (target != null) { 60 try { 61 performAction(target, fPart, fSelection); 62 } catch (CoreException e) { 63 DebugPlugin.log(e); 64 } 65 } 66 } 67 68 76 protected abstract void performAction(IToggleBreakpointsTarget target, IWorkbenchPart part, ISelection selection) throws CoreException; 77 78 81 public void selectionChanged(IAction action, ISelection selection) { 82 boolean enabled = false; 83 if (selection instanceof IStructuredSelection) { 84 IStructuredSelection ss = (IStructuredSelection) selection; 85 this.fSelection = ss; 86 if (!ss.isEmpty()) { 87 Object object = ss.getFirstElement(); 88 if (object instanceof IAdaptable) { 89 IAdaptable adaptable = (IAdaptable) object; 90 IToggleBreakpointsTarget target = (IToggleBreakpointsTarget) adaptable.getAdapter(IToggleBreakpointsTarget.class); 91 if (target == null) { 92 IAdapterManager adapterManager = Platform.getAdapterManager(); 93 enabled = adapterManager.hasAdapter(adaptable, IToggleBreakpointsTarget.class.getName()); 94 } else { 95 enabled = true; 96 } 97 } 98 } 99 } 100 action.setEnabled(enabled); 101 } 102 105 public void init(IAction action) { 106 } 107 108 111 public void dispose() { 112 fSelection = null; 113 fPart = null; 114 } 115 118 public void runWithEvent(IAction action, Event event) { 119 run(action); 120 } 121 } 122 | Popular Tags |