0

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. 
Ardeshana Milan
  • 373
  • 5
  • 23
  • How are you creating the Controller class? Injection is not done on classes created using `new`. – greg-449 Dec 01 '15 at 12:30
  • Hi greg, I am connecting my fxml to controller class (java) as explained here in link : https://blogs.oracle.com/jmxetc/entry/connecting_scenebuilder_edited_fxml_to. And I am using FxmlLoader to load the fxml in view using postconstruct(Pane composite) in a e4 view. – Ardeshana Milan Dec 01 '15 at 13:11

1 Answers1

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Hi Greg, Thanks first. That worked for `execute()` but i have to call `execute(EPartService partService,MApplication application,EModelService modelService)`. Do you know how i can get all these in my control class? – Ardeshana Milan Dec 02 '15 at 10:33
  • Sorry I don't understand. All those things can be injected. – greg-449 Dec 02 '15 at 10:38
  • From Controller Class i need to call Handler's execute(EPartService partService,MApplication application,EModelService modelService) method which is defined in question. I am getting `NPE` while i inject `EPartService` , `MApplication` and `EModelService `. So is there any way to call this handler manually from that controller class. – Ardeshana Milan Dec 02 '15 at 11:40
  • I think you need to add your new code to the question because I still don't follow what you are now doing. – greg-449 Dec 02 '15 at 11:46
  • please check now. Thanks – Ardeshana Milan Dec 02 '15 at 12:03
  • You should not run a Handler like that. The code using `ParameterizedCommand` is the correct way to run a handler. The ContextInjectionFactory.inject` call should be in the code using `FxmlLoader` to create the Controller class so that the Controller class is injected. – greg-449 Dec 02 '15 at 12:42
  • Hi greg, I am able to execute command now using.`ECommandService` and `EHandlerService` . Like said here http://stackoverflow.com/a/17405293/2691625. – Ardeshana Milan Dec 03 '15 at 06:57