1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.ResourceBundle ; 14 15 import org.eclipse.jface.text.source.ISourceViewer; 16 import org.eclipse.swt.custom.StyledText; 17 18 24 public class RecenterAction extends TextEditorAction { 25 26 36 public RecenterAction(ResourceBundle bundle, String prefix, ITextEditor editor) { 37 super(bundle, prefix, editor); 38 } 39 40 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 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 |