KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.texteditor;
12
13 import java.util.ResourceBundle JavaDoc;
14
15 import org.eclipse.jface.text.source.ISourceViewer;
16 import org.eclipse.swt.custom.StyledText;
17
18 /**
19  * An action to handle emacs-like recenter.
20  * This function scrolls the selected window to put the cursor at the middle of the screen.
21  *
22  * @since 3.3
23  */

24 public class RecenterAction extends TextEditorAction {
25     
26     /**
27      * Creates a new action for the given text editor. The action configures its
28      * visual representation from the given resource bundle.
29      *
30      * @param bundle the resource bundle
31      * @param prefix a prefix to be prepended to the various resource keys
32      * (described in <code>ResourceAction</code> constructor), or
33      * <code>null</code> if none
34      * @param editor the text editor
35      */

36     public RecenterAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor) {
37         super(bundle, prefix, editor);
38     }
39
40     /*
41      * @see IAction#run()
42      */

43     public void run() {
44         ITextEditor editor= getTextEditor();
45         if (!(editor instanceof AbstractTextEditor))
46             return;
47
48         ISourceViewer viewer= ((AbstractTextEditor)editor).getSourceViewer();
49         if (viewer == null)
50             return;
51
52         StyledText st= viewer.getTextWidget();
53         if (st == null)
54             return;
55
56         // compute the number of lines displayed
57
int height= st.getClientArea().height;
58         int lineHeight= st.getLineHeight();
59
60         int caretOffset= st.getCaretOffset();
61         int caretLine= st.getLineAtOffset(caretOffset);
62
63         int topLine= Math.max(0, (caretLine - (height / (lineHeight * 2))));
64         st.setTopIndex(topLine);
65     }
66 }
67
Popular Tags