Question

My original RCP was started in 3.x and currently I am running it on Juno with the compatibility layer. I was looking into doing a soft migration so I have started to slowly change my practices. One of the things I am doing is to change my actions into commands.

I have a view (which is like a directory explorer) currently that adds actions to the toolbar and popup menu of the view. These actions call specific methods in the view, for example to go up one directory.

It was easy to do this by action because I just create my action in the View class itself and programmatically add them to the toolbar

IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
        mgr.add(upDirectory);
        mgr.add(refresh);
        mgr.add(changeRoot);

and the creation of the actions are called from the createPartControl()

upDirectory = new Action("Go up one directory") {
            public void run() {
                goUpOneDirectory();
            }
        };

where goUpOneDirectory() is a method in the view

If I want to convert this to a command, I want to be able to access this method of the view in my handler. So I tried the following,

private void createHandlers()
    {
        IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
IHandler upDirHandler = new AbstractHandler() {
            public Object execute(ExecutionEvent event)
                    throws ExecutionException {
                goUpOneDirectory();
                return null;
            }
        };
        handlerService.activateHandler("updir.id", upDirHandler);
    }

And createHandlers is called in the createPartControl, and the command is added via the plugin.xml to the toolbar of the view. The problem is that the moment my view is out of focus it disables the buttons in the toolbar for these commands.

I want them to remain enabled at all times. How can I do that?

I know that the isEnabled() returns true all the time so I am not sure why it happens. The activateHanlder is called once in createPartControl so I feel that it should remain active all the time.

Edit: Ok I just saw this ,

IHandlerService from the workbench part site is the part handler service. Any handlers activated through the part handlers service will only be active when that part is active. Any listeners added to the part handler service will be removed when the part is disposed, and any active handlers will be deactivated (but not disposed).

So how can I get this,

IHandlerService from the workbench is the global handler service. it provides no special activation scoping or lifecycle.

Was it helpful?

Solution

Sorry, I should have waited a bit longer before asking, I figured it out!

I changed the,

IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class); 

to

IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);

and it worked. I will leave the question in case it helps other people.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top