eclipse ui:设置滚动条,但编辑器不遵循 [英] eclipse ui: setting scrollbar but editor does not follow

查看:676
本文介绍了eclipse ui:设置滚动条,但编辑器不遵循的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Eclipse UI中,我想在编辑器中设置可见区域。换句话说,如果我的文件的行数大于我的编辑器可以显示的行数,那么我想指定第一个显示的行。我的方法是设置垂直滚动条的选择值。下面找到我的代码,从不相关的部分中删除。

In the Eclipse UI, I'd like to set the visible area in an editor. In other words, if the number of lines of my file is larger than the number of lines my editor can show then I want to specify the first shown line. My approach is to set the selection value of the vertical scroll bar. Below you find my code, stripped from irrelevant parts.

给定的代码正确设置滚动条(拇指正在跳跃)。问题是:编辑器中可见的区域不反映此值,它只保留在该位置。在下一次使用滚动条时,例如,在点击箭头以增加值(向下滚动)后,编辑器的可见区域跳转到正确的位置(即,由拇指指定的位置)。

The given code sets the scroll bar correctly (the thumb is jumping). The problem is: the area visible in the editor does not reflect this value, it just remains where it was. Upon the next use of the scrollbar, e.g., after clicking the arrow for incrementing the value (scrolling down), the visible area of the editor jumps to the correct position (i.e., the position specified by the thumb).

所以,我想,编辑器需要直接或通过监听器通知我设置的新的滚动条值。但是我找不到它的机制。

So, I guess, the editor needs to be notified about the new scroll bar value that I have set, either directly or via a listener. But I could not find the mechanism for it.

public static void update(IWorkbenchWindow w, int someValue)
{
    Display display = w.getWorkbench().getDisplay();
    Scrollable scrollable = (Scrollable) display.getFocusControl();
    ScrollBar vScrollBar = scrollable.getVerticalBar();
    vScrollBar.setSelection(someValue); // between minimal and maximal bound

    // missing call to notify the active editor about the scroll bar value change
} 


推荐答案

如您所见 ScrollBar.setSelection 不发送选择事件给你的听众。

As you have seen ScrollBar.setSelection does not send a selection event to its listeners.

你可能会更好地使用类似于文本编辑器使用的代码 Goto行代码:

You would probably be better using code similar to that used by the text editor Goto line code:

IWorkbenchWindow w = ....

IWorkbenchPage page = w.getActivePage();

IEditorPart editor = page.getActiveEditor();

// TODO check editor is actually a text editor
ITextEditorPart textEditor = (ITextEditorPart)editor;

IDocumentProvider provider = textEditor.getDocumentProvider();
IDocument document = provider.getDocument(textEditor.getEditorInput());

try {
  int start = document.getLineOffset(line);

   textEditor.selectAndReveal(start, 0);
} catch (BadLocationException x) {
    // ignore
}

部分来自 org.eclipse.ui.texteditor.GotoLineAction

您可以获得当前可见的行使用 getTopIndex()从您的编辑器 TextViewer SourceViewer 方法。

You can get the current visible line from your editor TextViewer or SourceViewer using the getTopIndex() method.

这篇关于eclipse ui:设置滚动条,但编辑器不遵循的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆