Search code examples
eclipsee4efxclipse

Not able to inject CommandService to execute the command manually


I have handler (to create new parts) assigned to particular command, which is defined as below and working fine,
Handler class :

..
@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 ");
}     

I have one controller class to control the Fxml Objects (the click event on treeView). I want to execute that command manually from event handler treeview.
Controller Class :

@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();
                }

            }
        }
    });  

Here, Injected CommandService is null so not able to execute this parametric execute method of my handler class.
I believe there is another CommandService (org.eclipse.fx.core.command.CommandService) for e(fx)clipse but that is also comming null. I am using that as below
Controller Class :

@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);
}

[Edit] :
My aim is to open new Part in Partstack. So in first code snippet you can see how i am opening part in execute(..). Here, Is there any way to get partService , application and modelService using ContextInjectionFactory or IEclipseContext?
Controller Class :

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. 

Solution

  • 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.