有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java工具栏从eclipse编辑器中的自定义悬停中消失

我正在为Eclipse开发一个编辑器插件,它可以处理我自己的脚本语言。在编辑器中,我有一个鼠标悬停,显示鼠标光标下元素的简短信息。 现在,我正在尝试在悬停的底部创建一个工具栏,并在那里放置一个按钮,该按钮将在线打开更详细的描述

我已经基于answer to that question编写了我的代码。该按钮是可见的,并且在单击时起作用。 但是,在我将鼠标移到悬停位置后不久,它就消失了。为什么会发生这种情况?我该如何防止

以下是我的代码的相关部分:

@Override
public IInformationControlCreator getHoverControlCreator() {
    return new IInformationControlCreator() {
        @Override
        public IInformationControl createInformationControl(final Shell parent) {
            ToolBarManager tbm = new ToolBarManager(SWT.FLAT);
            DefaultInformationControl defaultInformationControl = new DefaultInformationControl(parent, tbm);
            Action action = new Action() {
                @Override
                public void run() {
                    MessageDialog.openInformation(parent, "omg", "It works.");
                }
            };
            action.setText("123 test 321");
            Bundle bundle = FrameworkUtil.getBundle(this.getClass());
            URL url = FileLocator.find(bundle, new Path("icons/test.gif"), null);
            action.setImageDescriptor(ImageDescriptor.createFromURL(url));
            tbm.add(action);
            tbm.update(true);
            return defaultInformationControl;
        }
    };
}

共 (1) 个答案

  1. # 1 楼答案

    使用DefaultInformationControl(父项,tbm)创建悬停时,工具栏可见。然而,当你把鼠标移到悬停上时,它就会获得焦点。然后调用DefaultInformationControl中的方法getInformationPresenterControlCreator()

    它看起来像(源代码):

    public IInformationControlCreator getInformationPresenterControlCreator() {
        return new IInformationControlCreator() {
            /*
             * @see org.eclipse.jface.text.IInformationControlCreator#createInformationControl(org.eclipse.swt.widgets.Shell)
             */
            public IInformationControl createInformationControl(Shell parent) {
                return new DefaultInformationControl(parent,
                        (ToolBarManager) null, fPresenter);
            }
        };
    }
    

    看看回程线。它会使工具栏管理器无效。这就是它消失的原因

    快速解决方案可能是创建一个扩展DefaultInformationControl的新类,然后在重写中进行扩展

    @Override
    public IInformationControlCreator getInformationPresenterControlCreator() {
        return new YourOwnInformationControlCreator();
    }
    

    这样你就可以通过正确的工具栏管理器