Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3bfd31a43c045103aee2b93e5d153dbb2dfcb167 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
 * Copyright (c) 2006, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Daesung Ha <nberserk@gmail.com> - supports top and bottom scrolling - https://bugs.eclipse.org/bugs/show_bug.cgi?id=412267 
 *******************************************************************************/
package org.eclipse.ui.texteditor;

import java.util.ResourceBundle;

import org.eclipse.swt.custom.StyledText;

import org.eclipse.jface.text.source.ISourceViewer;

/**
 * An action to handle emacs-like recenter.
 * This function scrolls the selected window to put the cursor at the middle/top/bottom of the screen.
 *
 * @since 3.3
 */
public class RecenterAction extends TextEditorAction {
	private static final int RECENTER_MIDDLE= 0;
	private static final int RECENTER_TOP= 1;
	private static final int RECENTER_BOTTOM= 2;
	private static final int RECENTER_POS_SIZE= 3;
	private int fPrevOffset= -1;
	private int fDestPos;

	/**
	 * Creates a new action for the given text editor. The action configures its
	 * visual representation from the given resource bundle.
	 *
	 * @param bundle the resource bundle
	 * @param prefix a prefix to be prepended to the various resource keys
	 *   (described in <code>ResourceAction</code> constructor), or
	 *   <code>null</code> if none
	 * @param editor the text editor
	 */
	public RecenterAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
		super(bundle, prefix, editor);
	}

	/*
	 * @see IAction#run()
	 */
	public void run() {
		ITextEditor editor= getTextEditor();
		if (!(editor instanceof AbstractTextEditor))
			return;

		ISourceViewer viewer= ((AbstractTextEditor)editor).getSourceViewer();
		if (viewer == null)
			return;

		StyledText st= viewer.getTextWidget();
		if (st == null)
			return;

		// compute the number of lines displayed
		int height= st.getClientArea().height;
		int lineHeight= st.getLineHeight();
		int rowPerScreen= height / lineHeight;

		int caretOffset= st.getCaretOffset();
		int caretLine= st.getLineAtOffset(caretOffset);		
		if (caretOffset==fPrevOffset) { // if successive call in same position
			fDestPos++;
			fDestPos%= RECENTER_POS_SIZE;
		}else{
			fDestPos= RECENTER_MIDDLE;
		}
		fPrevOffset= caretOffset;
		
		int line= 0;
		switch (fDestPos) {
			case RECENTER_MIDDLE:
				line= Math.max(0, (caretLine - rowPerScreen / 2));
				break;
			case RECENTER_TOP:
				line= caretLine;
				break;
			case RECENTER_BOTTOM:
				line= Math.max(0, caretLine - rowPerScreen + 1);
				break;
			default:
				break;
		}		
		st.setTopIndex(line);
	}
}

Back to the top