diff options
author | Sarika Sinha | 2017-02-28 04:11:00 +0000 |
---|---|---|
committer | Sarika Sinha | 2017-03-02 10:26:41 +0000 |
commit | a01efbceb2a8b9d62410cd7a6e2542417a64c815 (patch) | |
tree | 08670873e8ad7641f13b10be6c307df45e88129f | |
parent | 5875ab13fa7cbd511932f98efcdeb50447d49d99 (diff) | |
download | eclipse.platform.debug-a01efbceb2a8b9d62410cd7a6e2542417a64c815.tar.gz eclipse.platform.debug-a01efbceb2a8b9d62410cd7a6e2542417a64c815.tar.xz eclipse.platform.debug-a01efbceb2a8b9d62410cd7a6e2542417a64c815.zip |
Bug 498428 - "Run to Line" on Ctrl+Alt+Click in annotation/line numberY20170302-1000I20170302-2000
rulers
Change-Id: I67d5909551609a848794031ba0581921a880911c
4 files changed, 398 insertions, 2 deletions
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/ActionMessages.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/ActionMessages.java index fa785991b..622fcf206 100644 --- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/ActionMessages.java +++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/ActionMessages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2017 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -166,6 +166,8 @@ public class ActionMessages extends NLS { public static String RunToLineAction_0; public static String RunToLineAction_1; + public static String RunToLineAction_2; + public static String RunToLineAction_3; public static String RunToLineHandler_0; public static String RunToLineActionDelegate_4; diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/ActionMessages.properties b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/ActionMessages.properties index 7dbb8cbfc..721f34a81 100644 --- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/ActionMessages.properties +++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/actions/ActionMessages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2000, 2016 IBM Corporation and others. +# Copyright (c) 2000, 2017 IBM Corporation and others. # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 # which accompanies this distribution, and is available at @@ -139,6 +139,8 @@ DetailPaneMaxLengthAction_0=&Max Length... RunToLineAction_0=Error RunToLineAction_1=Run to line failed +RunToLineAction_2=Run to Line +RunToLineAction_3=Click RunToLineHandler_0=Run To Line RunToLineActionDelegate_4=Run to &Line diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/RulerRunToLineActionDelegate.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/RulerRunToLineActionDelegate.java new file mode 100644 index 000000000..afb7d952f --- /dev/null +++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/RulerRunToLineActionDelegate.java @@ -0,0 +1,129 @@ +/******************************************************************************* + * Copyright (c) 2017 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.debug.ui.actions; + +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.text.source.IVerticalRulerInfo; +import org.eclipse.swt.widgets.Event; +import org.eclipse.ui.IActionDelegate2; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.texteditor.AbstractRulerActionDelegate; +import org.eclipse.ui.texteditor.ITextEditor; + +/** + * A run to line action that can be contributed to a an editor or view. The + * action will perform the "run to line" operation for parts that provide an + * appropriate <code>IRunToLineTarget</code> adapter. + * <p> + * Following is example plug-in XML used to contribute this action to an editor. + * Note that the label attribute of this action is not displayed in the editor. + * Instead, the label of the created action is displayed. + * + * <pre> + * <extension point="org.eclipse.ui.editorActions"> + * <editorContribution + * targetID="example.editor" + * id="example.rulerActions"> + * <action + * label="Not Used" + * class="org.eclipse.debug.ui.actions.RulerRunToLineActionDelegate" + * style="push" + * actionID="RulerClick" + * id="example.doubleClickBreakpointAction"/> + * </editorContribution> + * </extension> + * </pre> + * </p> + * <p> + * This action can also be contributed to a vertical ruler context menu via the + * <code>popupMenus</code> extension point, by referencing the ruler's context + * menu identifier in the <code>targetID</code> attribute. + * + * <pre> + * <extension point="org.eclipse.ui.popupMenus"> + * <viewerContribution + * targetID="example.rulerContextMenuId" + * id="example.RulerPopupActions"> + * <action + * label="Run to Line" + * class="org.eclipse.debug.ui.actions.RulerRunToLineActionDelegate" + * menubarPath="additions" + * id="example.rulerContextMenu.runToLineAction"> + * </action> + * </viewerContribution> + * </pre> + * </p> + * <p> + * Clients may refer to this class as an action delegate in plug-in XML. + * </p> + * + * @since 3.12 + * @noextend This class is not intended to be subclassed by clients. + * @noinstantiate This class is not intended to be instantiated by clients. + */ +public class RulerRunToLineActionDelegate extends AbstractRulerActionDelegate implements IActionDelegate2 { + + private IEditorPart fEditor = null; + private RunToLineAction fDelegate = null; + + /* (non-Javadoc) + * @see org.eclipse.ui.texteditor.AbstractRulerActionDelegate#createAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.source.IVerticalRulerInfo) + */ + @Override + protected IAction createAction(ITextEditor editor, IVerticalRulerInfo rulerInfo) { + fDelegate = new RunToLineAction(editor, null, rulerInfo); + return fDelegate; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart) + */ + @Override + public void setActiveEditor(IAction callerAction, IEditorPart targetEditor) { + if (fEditor != null) { + if (fDelegate != null) { + fDelegate.dispose(); + fDelegate = null; + } + } + fEditor = targetEditor; + super.setActiveEditor(callerAction, targetEditor); + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction) + */ + @Override + public void init(IAction action) { + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IActionDelegate2#dispose() + */ + @Override + public void dispose() { + if (fDelegate != null) { + fDelegate.dispose(); + } + fDelegate = null; + fEditor = null; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event) + */ + @Override + public void runWithEvent(IAction action, Event event) { + if(fDelegate != null) { + fDelegate.runWithEvent(event); + } + } +} diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/RunToLineAction.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/RunToLineAction.java new file mode 100644 index 000000000..4d0d16bfb --- /dev/null +++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/actions/RunToLineAction.java @@ -0,0 +1,263 @@ +/******************************************************************************* + * Copyright (c) 2017 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Wind River Systems - added support for IToggleBreakpointsTargetFactory + *******************************************************************************/ +package org.eclipse.debug.ui.actions; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdapterManager; +import org.eclipse.core.runtime.Platform; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.model.ISuspendResume; +import org.eclipse.debug.internal.ui.DebugUIPlugin; +import org.eclipse.debug.internal.ui.actions.ActionMessages; +import org.eclipse.debug.ui.DebugUITools; +import org.eclipse.debug.ui.contexts.DebugContextEvent; +import org.eclipse.debug.ui.contexts.IDebugContextListener; +import org.eclipse.debug.ui.contexts.IDebugContextManager; +import org.eclipse.debug.ui.contexts.IDebugContextService; +import org.eclipse.jface.action.Action; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.TextSelection; +import org.eclipse.jface.text.source.IVerticalRulerInfo; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.MenuItem; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchPartSite; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.texteditor.IDocumentProvider; +import org.eclipse.ui.texteditor.ITextEditor; +import org.eclipse.ui.texteditor.IUpdate; + +/** + * Action to run to line in a vertical ruler of a workbench part containing a + * document. The part must provide an <code>IRunToLineTarget</code> adapter and + * <code>ISuspendResume</code> adapter. + * <p> + * Clients may instantiate this class. + * </p> + * + * @since 3.12 + * @see org.eclipse.debug.ui.actions.RulerToggleBreakpointActionDelegate + * @noextend This class is not intended to be subclassed by clients. + */ +public class RunToLineAction extends Action implements IUpdate { + + private IWorkbenchPart fActivePart = null; + private IRunToLineTarget fPartTarget = null; + private DebugContextListener fContextListener = new DebugContextListener(); + private ISuspendResume fTargetElement = null; + private IDocument fDocument; + private IVerticalRulerInfo fRulerInfo; + + class DebugContextListener implements IDebugContextListener { + + protected void contextActivated(ISelection selection) { + fTargetElement = null; + if (selection instanceof IStructuredSelection) { + IStructuredSelection ss = (IStructuredSelection) selection; + if (ss.size() == 1) { + fTargetElement = (ISuspendResume) DebugPlugin.getAdapter(ss.getFirstElement(), ISuspendResume.class); + } + } + update(); + } + + @Override + public void debugContextChanged(DebugContextEvent event) { + contextActivated(event.getContext()); + } + + } + + /** + * Constructs a new action to toggle a breakpoint in the given + * part containing the given document and ruler. + * + * @param part the part in which to toggle the breakpoint - provides + * an <code>IToggleBreakpointsTarget</code> adapter + * @param document the document breakpoints are being set in or + * <code>null</code> when the document should be derived from the + * given part + * @param rulerInfo specifies location the user has double-clicked + */ + public RunToLineAction(IWorkbenchPart part, IDocument document, IVerticalRulerInfo rulerInfo) { + super(ActionMessages.RunToLineAction_2 + '\t' + DebugUIPlugin.formatKeyBindingString(SWT.MOD1 + SWT.MOD3, ActionMessages.RunToLineAction_3)); + fActivePart = part; + fDocument = document; + fRulerInfo = rulerInfo; + initializeListeners(); + } + + /* + * (non-Javadoc) + * @see org.eclipse.jface.action.IAction#run() + */ + @Override + public void run() { + doIt(); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event) + */ + @Override + public void runWithEvent(Event event) { + if (event.widget instanceof MenuItem) { + doIt(); + } else { + // Even comes here on RulerClick, perform Run to Line if Ctrl Alt + // Click was used + if ((event.stateMask & SWT.MOD1) > 0 && (event.stateMask & SWT.MOD3) > 0) { + doIt(); + } + } + } + + void doIt() { + if (fPartTarget != null && fTargetElement != null) { + try { + IDocument document= getDocument(); + if (document != null) { + int line = fRulerInfo.getLineOfLastMouseButtonActivity(); + if (line > -1) { + ITextSelection selection = getTextSelection(document, line); + fPartTarget.runToLine(fActivePart, selection, fTargetElement); + } + } + } catch (CoreException e) { + DebugUIPlugin.errorDialog(fActivePart.getSite().getWorkbenchWindow().getShell(), ActionMessages.RunToLineAction_0, ActionMessages.RunToLineAction_1, e.getStatus()); // + } catch (BadLocationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + + /** + * Disposes this action. Clients must call this method when + * this action is no longer needed. + */ + public void dispose() { + IDebugContextManager manager = DebugUITools.getDebugContextManager(); + if (fActivePart != null) { + manager.getContextService(fActivePart.getSite().getWorkbenchWindow()).removeDebugContextListener(fContextListener); + } + fActivePart = null; + fTargetElement = null; + fPartTarget = null; + + } + + + + /* (non-Javadoc) + * @see org.eclipse.ui.texteditor.IUpdate#update() + */ + @Override + public void update() { + Runnable r = new Runnable() { + @Override + public void run() { + setEnabled(isTargetEnabled()); + } + }; + DebugUIPlugin.getStandardDisplay().asyncExec(r); + } + + private boolean isTargetEnabled() { + boolean enabled = false; + if (fPartTarget != null && fTargetElement != null) { + IWorkbenchPartSite site = fActivePart.getSite(); + if (site != null) { + ISelectionProvider selectionProvider = site.getSelectionProvider(); + if (selectionProvider != null) { + ISelection selection = selectionProvider.getSelection(); + enabled = fTargetElement.isSuspended() && fPartTarget.canRunToLine(fActivePart, selection, fTargetElement); + } + } + } + return enabled; + } + + private void initializeListeners() { + IDebugContextManager manager = DebugUITools.getDebugContextManager(); + IWorkbenchWindow workbenchWindow = fActivePart.getSite().getWorkbenchWindow(); + IDebugContextService service = manager.getContextService(workbenchWindow); + service.addDebugContextListener(fContextListener); + fPartTarget = fActivePart.getAdapter(IRunToLineTarget.class); + if (fPartTarget == null) { + IAdapterManager adapterManager = Platform.getAdapterManager(); + if (adapterManager.hasAdapter(fActivePart, IRunToLineTarget.class.getName())) { + fPartTarget = (IRunToLineTarget) adapterManager.loadAdapter(fActivePart, IRunToLineTarget.class.getName()); + } + } + ISelection activeContext = service.getActiveContext(); + fContextListener.contextActivated(activeContext); + + } + + /** + * Returns the document on which this action operates. + * + * @return the document or <code>null</code> if none + */ + + private IDocument getDocument() { + if (fDocument != null) { + return fDocument; + } + if (fActivePart instanceof ITextEditor) { + ITextEditor editor = (ITextEditor) fActivePart; + IDocumentProvider provider = editor.getDocumentProvider(); + if (provider != null) { + return provider.getDocument(editor.getEditorInput()); + } + } + IDocument doc = fActivePart.getAdapter(IDocument.class); + if (doc != null) { + return doc; + } + return null; + } + + /** + * Determines the text selection for the ruler action. If clicking on the + * ruler inside the highlighted text, return the text selection for the + * highlighted text. Otherwise, return a text selection representing the + * start of the line. + * + * @param document The IDocument backing the Editor. + * @param line The line clicked on in the ruler. + * @return An ITextSelection as described. + * @throws BadLocationException If underlying operations throw. + */ + private ITextSelection getTextSelection(IDocument document, int line) throws BadLocationException { + IRegion region = document.getLineInformation(line); + ITextSelection textSelection = new TextSelection(document, region.getOffset(), 0); + ISelectionProvider provider = fActivePart.getSite().getSelectionProvider(); + if (provider != null) { + ISelection selection = provider.getSelection(); + if (selection instanceof ITextSelection && ((ITextSelection) selection).getStartLine() <= line && ((ITextSelection) selection).getEndLine() >= line) { + textSelection = (ITextSelection) selection; + } + } + return textSelection; + } + +} |