01: /*******************************************************************************
02: * Copyright (c) 2006, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.texteditor;
11:
12: import java.util.ResourceBundle;
13:
14: import org.eclipse.jface.text.source.ISourceViewer;
15: import org.eclipse.swt.custom.StyledText;
16:
17: /**
18: * An action to handle emacs-like recenter.
19: * This function scrolls the selected window to put the cursor at the middle of the screen.
20: *
21: * @since 3.3
22: */
23: public class RecenterAction extends TextEditorAction {
24:
25: /**
26: * Creates a new action for the given text editor. The action configures its
27: * visual representation from the given resource bundle.
28: *
29: * @param bundle the resource bundle
30: * @param prefix a prefix to be prepended to the various resource keys
31: * (described in <code>ResourceAction</code> constructor), or
32: * <code>null</code> if none
33: * @param editor the text editor
34: */
35: public RecenterAction(ResourceBundle bundle, String prefix,
36: 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)
49: .getSourceViewer();
50: if (viewer == null)
51: return;
52:
53: StyledText st = viewer.getTextWidget();
54: if (st == null)
55: return;
56:
57: // compute the number of lines displayed
58: int height = st.getClientArea().height;
59: int lineHeight = st.getLineHeight();
60:
61: int caretOffset = st.getCaretOffset();
62: int caretLine = st.getLineAtOffset(caretOffset);
63:
64: int topLine = Math.max(0,
65: (caretLine - (height / (lineHeight * 2))));
66: st.setTopIndex(topLine);
67: }
68: }
|