diff options
author | Dani Megert | 2010-05-18 10:43:31 +0000 |
---|---|---|
committer | Dani Megert | 2010-05-18 10:43:31 +0000 |
commit | 095534d3bf82d370393ff65424c720101cc2f4cc (patch) | |
tree | 2494a3cc5e365dc55e3b573ad2972a5aa65cfc03 | |
parent | 8954f26949d4e371e0efdc0019d0c7209c7ab53a (diff) | |
download | eclipse.platform.text-095534d3bf82d370393ff65424c720101cc2f4cc.tar.gz eclipse.platform.text-095534d3bf82d370393ff65424c720101cc2f4cc.tar.xz eclipse.platform.text-095534d3bf82d370393ff65424c720101cc2f4cc.zip |
Fixed bug 236241: [navigation] 'Go to Line' dialog should be pre-filled with current linev20100518-0800
-rw-r--r-- | org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/GotoLineAction.java | 123 |
1 files changed, 76 insertions, 47 deletions
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/GotoLineAction.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/GotoLineAction.java index bb3943d3b6a..a2ed256c030 100644 --- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/GotoLineAction.java +++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/GotoLineAction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2010 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 @@ -8,21 +8,26 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ - package org.eclipse.ui.texteditor; - import java.util.ResourceBundle; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Shell; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; + import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.jface.dialogs.InputDialog; +import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.window.Window; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextSelection; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.internal.texteditor.NLSUtility; @@ -30,30 +35,30 @@ import org.eclipse.ui.internal.texteditor.TextEditorPlugin; /** - * Action for jumping to a particular line in the editor's text viewer. - * The user is requested to enter the line number into an input dialog. - * The action is initially associated with a text editor via the constructor, - * but that can be subsequently changed using <code>setEditor</code>. + * Action for jumping to a particular line in the editor's text viewer. The user is requested to + * enter the line number into an input dialog. The action is initially associated with a text editor + * via the constructor, but that can be subsequently changed using <code>setEditor</code>. * <p> - * The following keys, prepended by the given option prefix, - * are used for retrieving resources from the given bundle: + * The following keys, prepended by the given option prefix, are used for retrieving resources from + * the given bundle: * <ul> - * <li><code>"dialog.invalid_range"</code> - to indicate an invalid line number</li> - * <li><code>"dialog.invalid_input"</code> - to indicate an invalid line number format</li> - * <li><code>"dialog.title"</code> - the input dialog's title</li> - * <li><code>"dialog.message"</code> - the input dialog's message</li> - * </ul></p> + * <li><code>"dialog.invalid_range"</code> - to indicate an invalid line number</li> + * <li><code>"dialog.invalid_input"</code> - to indicate an invalid line number format</li> + * <li><code>"dialog.title"</code> - the input dialog's title</li> + * <li><code>"dialog.message"</code> - the input dialog's message</li> + * </ul> + * </p> * <p> * This class may be instantiated; it is not intended to be subclassed. * </p> + * * @noextend This class is not intended to be subclassed by clients. */ public class GotoLineAction extends TextEditorAction { /** - * Validates whether the text found in the input field of the - * dialog forms a valid line number. A number is valid if it is - * one to which can be jumped. + * Validates whether the text found in the input field of the dialog forms a valid line number. + * A number is valid if it is one to which can be jumped. */ class NumberValidator implements IInputValidator { @@ -79,9 +84,8 @@ public class GotoLineAction extends TextEditorAction { } /** - * Standard input dialog with custom dialog bounds - * strategy and settings. - * + * Standard input dialog with custom dialog bounds strategy and settings. + * * @since 2.0 */ static class GotoLineDialog extends InputDialog { @@ -117,20 +121,21 @@ public class GotoLineAction extends TextEditorAction { /** The biggest valid line number of the presented document */ private int fLastLine; + /** This action's resource bundle */ private ResourceBundle fBundle; + /** This action's prefix used for accessing the resource bundle */ private String fPrefix; /** - * Creates a new action for the given text editor. The action configures its - * visual representation from the given resource bundle. - * + * Creates a new action for the given text editor. The action configures its visual + * representation from the given resource bundle. + * * @param bundle the resource bundle - * @param prefix a prefix to be prepended to the various resource keys - * (described in <code>ResourceAction</code> constructor), or - * <code>null</code> if none + * @param prefix a prefix to be prepended to the various resource keys (described in + * <code>ResourceAction</code> constructor), or <code>null</code> if none * @param editor the text editor * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor) */ @@ -156,7 +161,7 @@ public class GotoLineAction extends TextEditorAction { /** * Jumps to the given line. - * + * * @param line the line to jump to */ private void gotoLine(int line) { @@ -182,36 +187,60 @@ public class GotoLineAction extends TextEditorAction { * @see Action#run() */ public void run() { - try { - - ITextEditor editor= getTextEditor(); + ITextEditor editor= getTextEditor(); - if (editor == null) - return; + if (editor == null) + return; - IDocumentProvider docProvider= editor.getDocumentProvider(); - if (docProvider == null) - return; + IDocumentProvider docProvider= editor.getDocumentProvider(); + if (docProvider == null) + return; - IDocument document= docProvider.getDocument(editor.getEditorInput()); - if (document == null) - return; + IDocument document= docProvider.getDocument(editor.getEditorInput()); + if (document == null) + return; + try { fLastLine= document.getLineOfOffset(document.getLength()) + 1; + } catch (BadLocationException ex) { + IStatus status= new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, "Go to Line failed", ex); //$NON-NLS-1$ + TextEditorPlugin.getDefault().getLog().log(status); + return; + } - String title= fBundle.getString(fPrefix + "dialog.title"); //$NON-NLS-1$ - String message= NLSUtility.format(fBundle.getString(fPrefix + "dialog.message"), new Integer(fLastLine)); //$NON-NLS-1$ - - GotoLineDialog d= new GotoLineDialog(editor.getSite().getShell(), title, message, "", new NumberValidator()); //$NON-NLS-1$ - if (d.open() == Window.OK) { + String title= fBundle.getString(fPrefix + "dialog.title"); //$NON-NLS-1$ + String message= NLSUtility.format(fBundle.getString(fPrefix + "dialog.message"), new Integer(fLastLine)); //$NON-NLS-1$ + + String currentLineStr= ""; //$NON-NLS-1$ + ISelection selection= editor.getSelectionProvider().getSelection(); + if (selection instanceof ITextSelection) { + ITextSelection textSelection= (ITextSelection)selection; + Control textWidget= (Control)editor.getAdapter(Control.class); + boolean caretAtStartOfSelection= false; + if (textWidget instanceof StyledText) + caretAtStartOfSelection= ((StyledText)textWidget).getSelection().x == ((StyledText)textWidget).getCaretOffset(); + int currentLine; + if (caretAtStartOfSelection) + currentLine= textSelection.getStartLine(); + else { + int endOffset= textSelection.getOffset() + textSelection.getLength(); try { - int line= Integer.parseInt(d.getValue()); - gotoLine(line - 1); - } catch (NumberFormatException x) { + currentLine= document.getLineOfOffset(endOffset); + } catch (BadLocationException ex) { + currentLine= -1; } } + if (currentLine > -1) + currentLineStr= Integer.toString(currentLine + 1); + } - } catch (BadLocationException x) { + GotoLineDialog d= new GotoLineDialog(editor.getSite().getShell(), title, message, currentLineStr, new NumberValidator()); + if (d.open() == Window.OK) { + try { + int line= Integer.parseInt(d.getValue()); + gotoLine(line - 1); + } catch (NumberFormatException x) { + } } } } |