1 12 package org.eclipse.debug.ui.actions; 13 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IAdapterManager; 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.debug.core.model.ISuspendResume; 18 import org.eclipse.debug.internal.ui.DebugUIPlugin; 19 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 20 import org.eclipse.debug.internal.ui.actions.ActionMessages; 21 import org.eclipse.debug.ui.DebugUITools; 22 import org.eclipse.debug.ui.contexts.DebugContextEvent; 23 import org.eclipse.debug.ui.contexts.IDebugContextListener; 24 import org.eclipse.debug.ui.contexts.IDebugContextManager; 25 import org.eclipse.debug.ui.contexts.IDebugContextService; 26 import org.eclipse.jface.action.IAction; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.ISelectionProvider; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.swt.widgets.Event; 31 import org.eclipse.ui.IActionDelegate2; 32 import org.eclipse.ui.IEditorActionDelegate; 33 import org.eclipse.ui.IEditorPart; 34 import org.eclipse.ui.IViewActionDelegate; 35 import org.eclipse.ui.IViewPart; 36 import org.eclipse.ui.IWorkbenchPart; 37 import org.eclipse.ui.IWorkbenchPartSite; 38 import org.eclipse.ui.IWorkbenchWindow; 39 40 53 public class RunToLineActionDelegate implements IEditorActionDelegate, IActionDelegate2, IViewActionDelegate { 54 55 private IWorkbenchPart fActivePart = null; 56 private IRunToLineTarget fPartTarget = null; 57 private IAction fAction = null; 58 private DebugContextListener fContextListener = new DebugContextListener(); 59 private ISuspendResume fTargetElement = null; 60 61 class DebugContextListener implements IDebugContextListener { 62 63 protected void contextActivated(ISelection selection) { 64 fTargetElement = null; 65 if (selection instanceof IStructuredSelection) { 66 IStructuredSelection ss = (IStructuredSelection) selection; 67 if (ss.size() == 1) { 68 Object object = ss.getFirstElement(); 69 if (object instanceof ISuspendResume) { 70 fTargetElement = (ISuspendResume) object; 71 } 72 } 73 } 74 update(); 75 } 76 77 public void debugContextChanged(DebugContextEvent event) { 78 contextActivated(event.getContext()); 79 } 80 81 } 82 83 86 public void dispose() { 87 DebugUITools.getDebugContextManager().getContextService(fActivePart.getSite().getWorkbenchWindow()).removeDebugContextListener(fContextListener); 88 fActivePart = null; 89 fPartTarget = null; 90 91 } 92 95 public void run(IAction action) { 96 if (fPartTarget != null && fTargetElement != null) { 97 try { 98 fPartTarget.runToLine(fActivePart, fActivePart.getSite().getSelectionProvider().getSelection(), fTargetElement); 99 } catch (CoreException e) { 100 DebugUIPlugin.errorDialog(fActivePart.getSite().getWorkbenchWindow().getShell(), ActionMessages.RunToLineAction_0, ActionMessages.RunToLineAction_1, e.getStatus()); } 102 } 103 } 104 107 public void selectionChanged(IAction action, ISelection selection) { 108 this.fAction = action; 109 update(); 110 } 111 112 115 public void update() { 116 if (fAction == null) { 117 return; 118 } 119 Runnable r = new Runnable () { 120 public void run() { 121 boolean enabled = false; 122 if (fPartTarget != null && fTargetElement != null) { 123 IWorkbenchPartSite site = fActivePart.getSite(); 124 if (site != null) { 125 ISelectionProvider selectionProvider = site.getSelectionProvider(); 126 if (selectionProvider != null) { 127 ISelection selection = selectionProvider.getSelection(); 128 enabled = fTargetElement.isSuspended() && fPartTarget.canRunToLine(fActivePart, selection, fTargetElement); 129 } 130 } 131 } 132 fAction.setEnabled(enabled); 133 } 134 }; 135 DebugUIPlugin.getStandardDisplay().asyncExec(r); 136 } 137 138 141 public void init(IAction action) { 142 this.fAction = action; 143 if (action != null) { 144 action.setText(ActionMessages.RunToLineActionDelegate_4); 145 action.setImageDescriptor(DebugUITools.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_RUN_TO_LINE)); 146 action.setDisabledImageDescriptor(DebugUITools.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_RUN_TO_LINE)); 147 } 148 } 149 152 public void runWithEvent(IAction action, Event event) { 153 run(action); 154 } 155 158 public void setActiveEditor(IAction action, IEditorPart targetEditor) { 159 init(action); 160 bindTo(targetEditor); 161 } 162 163 166 public void init(IViewPart view) { 167 bindTo(view); 168 } 169 170 175 private void bindTo(IWorkbenchPart part) { 176 IDebugContextManager manager = DebugUITools.getDebugContextManager(); 177 if (fActivePart != null && !fActivePart.equals(part)) { 178 manager.getContextService(fActivePart.getSite().getWorkbenchWindow()).removeDebugContextListener(fContextListener); 179 } 180 fPartTarget = null; 181 fActivePart = part; 182 if (part != null) { 183 IWorkbenchWindow workbenchWindow = part.getSite().getWorkbenchWindow(); 184 IDebugContextService service = manager.getContextService(workbenchWindow); 185 service.addDebugContextListener(fContextListener); 186 fPartTarget = (IRunToLineTarget) part.getAdapter(IRunToLineTarget.class); 187 if (fPartTarget == null) { 188 IAdapterManager adapterManager = Platform.getAdapterManager(); 189 if (adapterManager.hasAdapter(part, IRunToLineTarget.class.getName())) { 191 fPartTarget = (IRunToLineTarget) adapterManager.loadAdapter(part, IRunToLineTarget.class.getName()); 192 } 193 } 194 ISelection activeContext = service.getActiveContext(); 195 fContextListener.contextActivated(activeContext); 196 } 197 update(); 198 } 199 } 200 | Popular Tags |