KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > GotoLineAction


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
12 package org.eclipse.ui.texteditor;
13
14
15 import java.util.ResourceBundle JavaDoc;
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 /**
35  * Action for jumping to a particular line in the editor's text viewer.
36  * The user is requested to enter the line number into an input dialog.
37  * The action is initially associated with a text editor via the constructor,
38  * but that can be subsequently changed using <code>setEditor</code>.
39  * <p>
40  * The following keys, prepended by the given option prefix,
41  * are used for retrieving resources from the given bundle:
42  * <ul>
43  * <li><code>"dialog.invalid_range"</code> - to indicate an invalid line number</li>
44  * <li><code>"dialog.invalid_input"</code> - to indicate an invalid line number format</li>
45  * <li><code>"dialog.title"</code> - the input dialog's title</li>
46  * <li><code>"dialog.message"</code> - the input dialog's message</li>
47  * </ul></p>
48  * <p>
49  * This class may be instantiated; it is not intended to be subclassed.
50  * </p>
51  */

52 public class GotoLineAction extends TextEditorAction {
53
54     /**
55      * Validates whether the text found in the input field of the
56      * dialog forms a valid line number. A number is valid if it is
57      * one to which can be jumped.
58      */

59     class NumberValidator implements IInputValidator {
60
61         /*
62          * @see IInputValidator#isValid(String)
63          */

64         public String JavaDoc isValid(String JavaDoc input) {
65
66             if (input == null || input.length() == 0)
67                 return " "; //$NON-NLS-1$
68

69             try {
70                 int i= Integer.parseInt(input);
71                 if (i <= 0 || fLastLine < i)
72                     return fBundle.getString(fPrefix + "dialog.invalid_range"); //$NON-NLS-1$
73

74             } catch (NumberFormatException JavaDoc x) {
75                 return fBundle.getString(fPrefix + "dialog.invalid_input"); //$NON-NLS-1$
76
}
77
78             return null;
79         }
80     }
81
82     /**
83      * Standard input dialog which additionally sets the focus to the
84      * text input field. Workaround for <code>InputDialog</code> issue.
85      * 1GIJZOO: ITPSRCEDIT:ALL - Gotodialog's edit field has no initial focus
86      * @since 2.0
87      */

88     static class GotoLineDialog extends InputDialog {
89
90         /*
91          * @see InputDialog#InputDialog(org.eclipse.swt.widgets.Shell, java.lang.String, java.lang.String, java.lang.String, org.eclipse.jface.dialogs.IInputValidator)
92          */

93         public GotoLineDialog(Shell parent, String JavaDoc title, String JavaDoc message, String JavaDoc initialValue, IInputValidator validator) {
94             super(parent, title, message, initialValue, validator);
95         }
96
97         /*
98          * @see InputDialog#createDialogArea(Composite)
99          */

100         protected Control createDialogArea(Composite parent) {
101             Control result= super.createDialogArea(parent);
102             getText().setFocus();
103             applyDialogFont(result);
104             return result;
105         }
106         
107         /*
108          * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
109          * @since 3.2
110          */

111         protected IDialogSettings getDialogBoundsSettings() {
112             String JavaDoc sectionName= getClass().getName() + "_dialogBounds"; //$NON-NLS-1$
113
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         /*
121          * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsStrategy()
122          * @since 3.2
123          */

124         protected int getDialogBoundsStrategy() {
125             return DIALOG_PERSISTLOCATION;
126         }
127     }
128
129     /** The biggest valid line number of the presented document */
130     private int fLastLine;
131     /** This action's resource bundle */
132     private ResourceBundle JavaDoc fBundle;
133     /** This action's prefix used for accessing the resource bundle */
134     private String JavaDoc fPrefix;
135
136     /**
137      * Creates a new action for the given text editor. The action configures its
138      * visual representation from the given resource bundle.
139      *
140      * @param bundle the resource bundle
141      * @param prefix a prefix to be prepended to the various resource keys
142      * (described in <code>ResourceAction</code> constructor), or
143      * <code>null</code> if none
144      * @param editor the text editor
145      * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
146      */

147     public GotoLineAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor) {
148         super(bundle, prefix, editor);
149         fBundle= bundle;
150         fPrefix= prefix;
151     }
152
153     /**
154      * Jumps to the given line.
155      *
156      * @param line the line to jump to
157      */

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             // ignore
174
}
175     }
176
177     /*
178      * @see Action#run()
179      */

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 JavaDoc title= fBundle.getString(fPrefix + "dialog.title"); //$NON-NLS-1$
199
String JavaDoc message= NLSUtility.format(fBundle.getString(fPrefix + "dialog.message"), new Integer JavaDoc(fLastLine)); //$NON-NLS-1$
200

201             GotoLineDialog d= new GotoLineDialog(editor.getSite().getShell(), title, message, "", new NumberValidator()); //$NON-NLS-1$
202
if (d.open() == Window.OK) {
203                 try {
204                     int line= Integer.parseInt(d.getValue());
205                     gotoLine(line - 1);
206                 } catch (NumberFormatException JavaDoc x) {
207                 }
208             }
209
210         } catch (BadLocationException x) {
211         }
212     }
213 }
214
Popular Tags