Java – eclipse RCP – add a listener immediately after creating the view

Greetings to stackovlowians,

I am developing an eclipse RCP application and must add the selectionlistener to the Project Explorer view immediately after creation

I've realized that I can't do this in the activator of my contribution plug-in, but in order to pass the platform UI Getworkbench() gets selectionservice getActiveWorkbenchWindow(). Getselectionservice () I must have an active workbench window (this is null when calling activator start()

So my question: when can I get the selectionservice to create a Project Explorer view and see it, but the user has not been able to "press any button"?

Any comments and suggestions are appreciated!

Solution

If you really want to track user selections from startup without a UI (such as a view) that can register iselectionlistener at creation time, you can use the startup hook

Eclipse provides the extension point org eclipse. ui. startup. It accepts an implementation interface, org eclipse. ui. Class of istartup It will be called after UI is created, so ISelectionService is already available, and 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");
    }
}

Note 07000 because it forces OSGi to activate your bundles very early (so do all dependent bundles!) And slow down the system startup speed So please make sure your bag is neat Minimize bundle dependency Sometimes you need to move the startup hook code into a separate package to achieve this

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>