简体   繁体   中英

How to get an IHandlerService object in Eclipse 4 RCP?

I am working on a test project and followed Vogella's RCP tutorial . After that I made some changes on it, eg. created a JFace TreeView . Now I want that if the user double clicks on a TreeView element it opens up another Part . I have the command for it but, I do not know how to call it. If you look at the tutorial you may notice it only uses Parts, not Views and, I do not have an Application.java class that starts the workbench. Therefore the following methods do not work for me:

  1. IHandlerService handlerService = (IHandlerService) viewer.getSite().getService(IHandlerService.class);
  2. IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class); handlerService.executeCommand(cmdID, null);

Both of them gives me NullPointerException .

According to Lars Vogel, the recommended approach would be to inject the services as is sketched below (this works for me). The only snag is that the package org.eclipse.e4.core.commands seems to be inaccessible according to the plugin rules (maybe not exported yet).

But this works:

import org.eclipse.e4.core.commands.ECommandService;
import org.eclipse.e4.core.commands.EHandlerService;

public class MyPart{
public static final String S_CMD_MY_COMMAND_ID = "my.command";

@Inject ECommandService commandService;
@Inject EHandlerService service;

@PostConstruct
public void createComposite(Composite parent) {
    parent.setLayout(new GridLayout(2, false));
            ....
    Button btnMyButton = new Button(parent, SWT.NONE);
    btnMyButton.addSelectionListener(new SelectionAdapter() {
        @SuppressWarnings({ "restriction" })
        @Override
        public void widgetSelected(SelectionEvent e) {
            try {
                Command command = commandService.getCommand( S_CMD_MY_COMMAND_ID );
                if( !command.isDefined() )
                    return;
                ParameterizedCommand myCommand = commandService.createCommand(S_CMD_MY_COMMAND_ID, null);
                service.activateHandler(S_CMD_MY_COMMAND_ID, new MyCommandHandler());
                if( !service.canExecute(myCommand ))
                    return;
                service.executeHandler( myCommand );
            } catch (Exception ex) {
                throw new RuntimeException("command with id \"my command id\" not found");
            }           
        }
    });

    ....
}

The handler would be implemented as follows (not included to the appropriate command plugin extension, unless you also want it to implement IDefaultHandler for compatibility):

public class MyCommandHandler{

@Execute
public Object execute() throws ExecutionException {
    System.out.println("Hello");
    return null;
}

@CanExecute
public boolean canExecute() {
    return true;
}
}  

for details, see: http://www.vogella.com/articles/Eclipse4Services/article.html

https://groups.google.com/forum/#!topic/vogella/n5ztV-8GmkU

How about this old standby:

Command command = ((ICommandService)getSite().getService(ICommandService.class)).getCommand(commandId);
...
final Event trigger = new Event();
ExecutionEvent executionEvent = ((IHandlerService)getSite().getService(IHandlerService.class)).createExecutionEvent(command, trigger);
command.executeWithChecks(executionEvent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM