简体   繁体   English

如何在Eclipse 4 RCP中获取IHandlerService对象?

[英]How to get an IHandlerService object in Eclipse 4 RCP?

I am working on a test project and followed Vogella's RCP tutorial . 我正在做一个测试项目,并遵循了Vogella的RCP教程 After that I made some changes on it, eg. 之后,我对其进行了一些更改,例如。 created a JFace TreeView . 创建了一个JFace TreeView Now I want that if the user double clicks on a TreeView element it opens up another Part . 现在,我希望如果用户双击TreeView元素,它将打开另一个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. 如果您看一下该教程,您可能会注意到它仅使用部件而不使用视图,并且我没有启动工作台的Application.java类。 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 . 他们两个都给了我NullPointerException

According to Lars Vogel, the recommended approach would be to inject the services as is sketched below (this works for me). 根据Lars Vogel的说法,推荐的方法是注入服务,如下图所示(这对我有用)。 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). 唯一的问题是,根据插件规则,似乎无法访问org.eclipse.e4.core.command包(可能尚未导出)。

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): 该处理程序将按以下方式实现(不包括在相应的命令插件扩展中,除非您还希望它实现IDefaultHandler以实现兼容性):

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 有关详细信息,请参见: http : //www.vogella.com/articles/Eclipse4Services/article.html

https://groups.google.com/forum/#!topic/vogella/n5ztV-8GmkU 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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