1 11 12 package org.eclipse.ui.texteditor; 13 14 15 import java.util.ResourceBundle ; 16 17 import org.eclipse.swt.widgets.Composite; 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Shell; 20 21 import org.eclipse.jface.dialogs.IDialogSettings; 22 import org.eclipse.jface.dialogs.IInputValidator; 23 import org.eclipse.jface.dialogs.InputDialog; 24 import org.eclipse.jface.window.Window; 25 26 import org.eclipse.jface.text.BadLocationException; 27 import org.eclipse.jface.text.IDocument; 28 29 import org.eclipse.ui.IWorkbenchPage; 30 import org.eclipse.ui.internal.texteditor.NLSUtility; 31 import org.eclipse.ui.internal.texteditor.TextEditorPlugin; 32 33 34 52 public class GotoLineAction extends TextEditorAction { 53 54 59 class NumberValidator implements IInputValidator { 60 61 64 public String isValid(String input) { 65 66 if (input == null || input.length() == 0) 67 return " "; 69 try { 70 int i= Integer.parseInt(input); 71 if (i <= 0 || fLastLine < i) 72 return fBundle.getString(fPrefix + "dialog.invalid_range"); 74 } catch (NumberFormatException x) { 75 return fBundle.getString(fPrefix + "dialog.invalid_input"); } 77 78 return null; 79 } 80 } 81 82 88 static class GotoLineDialog extends InputDialog { 89 90 93 public GotoLineDialog(Shell parent, String title, String message, String initialValue, IInputValidator validator) { 94 super(parent, title, message, initialValue, validator); 95 } 96 97 100 protected Control createDialogArea(Composite parent) { 101 Control result= super.createDialogArea(parent); 102 getText().setFocus(); 103 applyDialogFont(result); 104 return result; 105 } 106 107 111 protected IDialogSettings getDialogBoundsSettings() { 112 String sectionName= getClass().getName() + "_dialogBounds"; IDialogSettings settings= TextEditorPlugin.getDefault().getDialogSettings(); 114 IDialogSettings section= settings.getSection(sectionName); 115 if (section == null) 116 section= settings.addNewSection(sectionName); 117 return section; 118 } 119 120 124 protected int getDialogBoundsStrategy() { 125 return DIALOG_PERSISTLOCATION; 126 } 127 } 128 129 130 private int fLastLine; 131 132 private ResourceBundle fBundle; 133 134 private String fPrefix; 135 136 147 public GotoLineAction(ResourceBundle bundle, String prefix, ITextEditor editor) { 148 super(bundle, prefix, editor); 149 fBundle= bundle; 150 fPrefix= prefix; 151 } 152 153 158 private void gotoLine(int line) { 159 160 ITextEditor editor= getTextEditor(); 161 162 IDocumentProvider provider= editor.getDocumentProvider(); 163 IDocument document= provider.getDocument(editor.getEditorInput()); 164 try { 165 166 int start= document.getLineOffset(line); 167 editor.selectAndReveal(start, 0); 168 169 IWorkbenchPage page= editor.getSite().getPage(); 170 page.activate(editor); 171 172 } catch (BadLocationException x) { 173 } 175 } 176 177 180 public void run() { 181 try { 182 183 ITextEditor editor= getTextEditor(); 184 185 if (editor == null) 186 return; 187 188 IDocumentProvider docProvider= editor.getDocumentProvider(); 189 if (docProvider == null) 190 return; 191 192 IDocument document= docProvider.getDocument(editor.getEditorInput()); 193 if (document == null) 194 return; 195 196 fLastLine= document.getLineOfOffset(document.getLength()) + 1; 197 198 String title= fBundle.getString(fPrefix + "dialog.title"); String message= NLSUtility.format(fBundle.getString(fPrefix + "dialog.message"), new Integer (fLastLine)); 201 GotoLineDialog d= new GotoLineDialog(editor.getSite().getShell(), title, message, "", new NumberValidator()); if (d.open() == Window.OK) { 203 try { 204 int line= Integer.parseInt(d.getValue()); 205 gotoLine(line - 1); 206 } catch (NumberFormatException x) { 207 } 208 } 209 210 } catch (BadLocationException x) { 211 } 212 } 213 } 214 | Popular Tags |