0

我将处理程序(用于创建新部件)分配给特定命令,该命令定义如下并且工作正常,
处理程序类:

..
@Execute
public void execute(EPartService partService,MApplication application,EModelService modelService) {
    MPart part = MBasicFactory.INSTANCE.createPart();
    part.setLabel("New Part");
    part.setContributionURI("platform:/plugin/com.my.First.app/src/com/my/first/Parts/EditorPart/EmptyPart");
    List<MPartStack> stacks = modelService.findElements(application, null,  MPartStack.class, null);
    stacks.get(2).getChildren().add(part);
    partService.showPart(part, PartState.ACTIVATE);
}

@Execute
public void execute() {
    //This Hello is getting printed.
    System.out.println("Hello ");
}     

我有一个控制器类来控制 Fxml 对象(treeView 上的单击事件)。我想从事件处理程序树视图手动执行该命令。
控制器类:

@Inject
org.eclipse.e4.core.commands.ECommandService commandService;
@Inject
org.eclipse.e4.core.commands.EHandlerService service;
..
..

locationTreeView.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            if (mouseEvent.getClickCount() == 2 && mouseEvent.getButton().equals(MouseButton.PRIMARY)
                    && locationTreeView.getSelectionModel().getSelectedItem().isLeaf()) {
                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 ShowImmobilizerPart());
                    if (!service.canExecute(myCommand))
                        return;
                    service.executeHandler(myCommand);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        }
    });  

在这里,Injected CommandService 为空,因此无法执行我的处理程序类的这个参数执行方法。
我相信还有另一个CommandServiceorg.eclipse.fx.core.command.CommandService),e(fx)clipse但这也是空的。我正在使用如下
控制器类:

@Inject org.eclipse.fx.core.command.CommandService commandService;
..
..
if (commandService.exists(S_CMD_MY_COMMAND_ID) && commandService.canExecute(S_CMD_MY_COMMAND_ID, null)) {
    commandService.execute(S_CMD_MY_COMMAND_ID, null);
}

[编辑]
我的目标是在 Partstack 中打开新的 Part。因此,在第一个代码片段中,您可以看到我如何在执行(..)中打开部分。在这里,有没有办法使用ContextInjectionFactoryor获得 partService 、 application 和 modelService IEclipseContext
控制器类

MyHandler myHandler = new MyHandler();
ContextInjectionFactory.inject(myHandler, iEclipseContext);
//myHandler.execute();  This is woroking if i define execute() method in handler Class.
myHandler.execute(partService,application,modelService); // This is not working as i am injecting this arguments at top of class. 
4

1 回答 1

1

Only classes created by Eclipse from descriptions in the e4xmi file are injected.

If you have a class created some other way you can do injection using:

ContextInjectionFactory.inject(object, context);

where object is the class instance to be injected and context is the IEclipseContext to use. Note that this will not inject the Constructor.

You can also use ContextInjectionFactory.make to create a class using injection.

于 2015-12-01T13:23:53.230 回答