Eclipse RCP - 在创建视图后立即添加监听器 [英] Eclipse RCP - add a Listener right after a View has been created

查看:540
本文介绍了Eclipse RCP - 在创建视图后立即添加监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Graoverings stackoverflowians,

Greetings fellow Stackoverflowians,

我正在开发一个Eclipse RCP应用程序,并且必须将 SelectionListener 添加到 Project Explorer视图创建后的那一刻。

I am developing an Eclipse RCP application, and must add a SelectionListener to the Project Explorer view the moment after it's created.

我意识到我无法在激活器中执行此操作我的贡献插件,而为了通过 PlatformUI.getWorkbench()获取 SelectionService .getActiveWorkbenchWindow()。getSelectionService()当调用 Activator start()时,我必须有一个活动的工作台窗口( null

I've realized that I cannot do this in the Activator of my contributing plug-in, whereas in order to get the SelectionService via PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService() I must have an active workbench window (which is null when the Activator start() is called)

所以我的问题:
我什么时候可以获得 SelectionService 以便项目资源管理器视图已创建且可见,但用户尚未能按任何按钮?

So my question: When can I get the SelectionService so that the Project Explorer view has been created and is visible, but the user has not yet been able to 'push any buttons'?

任何意见和建议表示赞赏!

Any opinions and suggestions are appreciated!

推荐答案

当你真的想跟踪启动时的用户选择时拥有一个可以在创建时注册 ISelectionListener 的UI(如视图),你可以使用一个启动钩子。

When you really want to track user selection from startup without having a UI (like a view) that can register an ISelectionListener on creation, you can us a startup hook.

Eclipse提供了扩展点 org.eclipse.ui.startup 。它接受一个实现接口 org.eclipse.ui.IStartup 的类。它将在UI创建后调用,因此 ISelectionService 已经可用:

Eclipse provides the extension point org.eclipse.ui.startup. It accepts a class that implements the interface org.eclipse.ui.IStartup. It will be called after the UI has been created, so the ISelectionService is already available then:

public class StartupHook implements IStartup, ISelectionListener {

    @Override
    public void earlyStartup() {
        final IWorkbench workbench = PlatformUI.getWorkbench();
        workbench.addWindowListener(new IWindowListener() {

            @Override
            public void windowOpened(IWorkbenchWindow window) {
                addSelectionListener(window);
            }

            @Override
            public void windowClosed(IWorkbenchWindow window) {
                removeSelectionListener(window);
            }
            /* ... */
        });

        workbench.getDisplay().asyncExec(new Runnable() {
            public void run() {
                for (IWorkbenchWindow window : workbench.getWorkbenchWindows()) {
                    addSelectionListener(window);
                }
            }
        });
    }

    private void addSelectionListener(IWorkbenchWindow window) {
        if (window != null) {
            window.getSelectionService().addSelectionListener("org.eclipse.ui.navigator.ProjectExplorer", this);
        }
    }

    private void removeSelectionListener(IWorkbenchWindow window) {
        if (window != null) {
            window.getSelectionService().removeSelectionListener("org.eclipse.ui.navigator.ProjectExplorer", this);
        }
    }

    @Override
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
        // TODO handle selection changes
        System.out.println("selection changed");
    }
}

请注意不鼓励使用此UI启动挂钩,因为它强制OSGi很早就激活你的捆绑(所以所有依赖捆绑也是如此!)并减慢系统启动速度。所以请确保你的包很整齐。将捆绑依赖性降至最低。有时需要将启动钩子代码移动到一个单独的包中以实现它。

Note that it is discouraged to use this UI startup hook as it forces OSGi to activate your bundle very early (and so all dependent bundles too!) and slows down system startup. So please make sure that your bundle is neat and slim. Reduce bundle dependencies to a minimum. Sometimes it is necessary move the startup hook code into a separate bundle to achieve that.

这篇关于Eclipse RCP - 在创建视图后立即添加监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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