1 11 package org.eclipse.ui.console.actions; 12 13 14 import com.ibm.icu.text.MessageFormat; 15 16 import org.eclipse.jface.dialogs.IInputValidator; 17 import org.eclipse.jface.dialogs.InputDialog; 18 import org.eclipse.jface.text.BadLocationException; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.ITextViewer; 21 import org.eclipse.jface.window.Window; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.ui.console.ConsolePlugin; 25 import org.eclipse.ui.internal.console.ConsoleMessages; 26 27 35 public class TextViewerGotoLineAction extends TextViewerAction { 36 37 42 class NumberValidator implements IInputValidator { 43 44 public String isValid(String input) { 45 try { 46 int i= Integer.parseInt(input); 47 if (i <= 0 || fLastLine < i) 48 return ConsoleMessages.TextViewerGotoLineAction_Line_number_out_of_range_1; 49 50 } catch (NumberFormatException x) { 51 return ConsoleMessages.TextViewerGotoLineAction_Not_a_number_2; 52 } 53 54 return null; 55 } 56 } 57 58 protected int fLastLine; 59 protected ITextViewer fTextViewer; 60 61 64 public TextViewerGotoLineAction(ITextViewer viewer) { 65 super(viewer, -1); 66 fTextViewer= viewer; 67 setText(ConsoleMessages.TextViewerGotoLineAction_Go_to__Line____Ctrl_L_4); 68 setToolTipText(ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1); 69 setDescription(ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1); 70 } 71 72 75 public void update() { 76 } 77 78 81 protected void gotoLine(int line) { 82 83 IDocument document= fTextViewer.getDocument(); 84 try { 85 int start= document.getLineOffset(line); 86 int length= document.getLineLength(line); 87 fTextViewer.getTextWidget().setSelection(start, start + length); 88 fTextViewer.revealRange(start, length); 89 } catch (BadLocationException x) { 90 ConsolePlugin.errorDialog(fTextViewer.getTextWidget().getShell(), ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1, ConsoleMessages.TextViewerGotoLineAction_Exceptions_occurred_attempt_to_go_to_line_2, x); } 92 } 93 94 97 public void run() { 98 try { 99 Point selection= fTextViewer.getTextWidget().getSelection(); 100 IDocument document= fTextViewer.getDocument(); 101 fLastLine= document.getLineOfOffset(document.getLength()) + 1; 102 int startLine= selection == null ? 1 : fTextViewer.getTextWidget().getLineAtOffset(selection.x) + 1; 103 String title= ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1; 104 String message= MessageFormat.format(ConsoleMessages.TextViewerGotoLineAction_Enter_line_number__8, new Object [] {new Integer (fLastLine)}); 105 String value= Integer.toString(startLine); 106 Shell activeShell= fTextViewer.getTextWidget().getShell(); 107 InputDialog d= new InputDialog(activeShell, title, message, value, new NumberValidator()); 108 if (d.open() == Window.OK) { 109 try { 110 int line= Integer.parseInt(d.getValue()); 111 gotoLine(line - 1); 112 } catch (NumberFormatException x) { 113 ConsolePlugin.errorDialog(activeShell, ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1, ConsoleMessages.TextViewerGotoLineAction_Exceptions_occurred_attempt_to_go_to_line_2, x); } 115 } 116 } catch (BadLocationException x) { 117 ConsolePlugin.errorDialog(fTextViewer.getTextWidget().getShell(), ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1, ConsoleMessages.TextViewerGotoLineAction_Exceptions_occurred_attempt_to_go_to_line_2, x); return; 119 } 120 } 121 } 122 | Popular Tags |