问题
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:
IHandlerService handlerService = (IHandlerService) viewer.getSite().getService(IHandlerService.class);
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
handlerService.executeCommand(cmdID, null);
Both of them gives me NullPointerException
.
回答1:
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);
回答2:
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
来源:https://stackoverflow.com/questions/12724511/how-to-get-an-ihandlerservice-object-in-eclipse-4-rcp