KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > console > actions > TextViewerGotoLineAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
28  * Action to position a text viewer to a specific line.
29  * <p>
30  * Clients may instantiate this class; this class is not intended to
31  * be subclassed.
32  * </p>
33  * @since 3.0
34  */

35 public class TextViewerGotoLineAction extends TextViewerAction {
36
37     /**
38      * Validates whether the text found in the input field of the
39      * dialog forms a valid line number, i.e. one to which can be
40      * jumped.
41      */

42     class NumberValidator implements IInputValidator {
43
44         public String JavaDoc isValid(String JavaDoc 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 JavaDoc 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     /**
62      * Constructs a goto line action for the viewer using the provided resource bundle
63      */

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     /**
73      * @see TextViewerAction#update()
74      */

75     public void update() {
76     }
77
78     /**
79      * Jumps to the line.
80      */

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); //
91
}
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.action.IAction#run()
96      */

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 JavaDoc title= ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1;
104             String JavaDoc message= MessageFormat.format(ConsoleMessages.TextViewerGotoLineAction_Enter_line_number__8, new Object JavaDoc[] {new Integer JavaDoc(fLastLine)});
105             String JavaDoc 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 JavaDoc x) {
113                     ConsolePlugin.errorDialog(activeShell, ConsoleMessages.TextViewerGotoLineAction_Go_To_Line_1, ConsoleMessages.TextViewerGotoLineAction_Exceptions_occurred_attempt_to_go_to_line_2, x); //
114
}
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); //
118
return;
119         }
120     }
121 }
122
Popular Tags