Search code examples
javaeclipsextextxtend

Calling Xtext generator from an Eclipse View


I'm planning to generate code by calling the generator from a button located in an Eclipse View, similar to this approach:

https://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/,

but instead of calling the generator from the context menu I want to call it with a click on a button, like the following code shows:

    button1.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            // Call GenerationHandler here
        }

        ...
    });

The Problem is, I can't simply call the execute-Method in the GenerationHandler as shown below:

public class GenerationHandler extends AbstractHandler implements IHandler {

@Inject private IGenerator generator; 
@Inject private Provider<EclipseResourceFileSystemAccess> fileAccessProvider;

@Override
public Object execute(ExecutionEvent event) throws ExecutionException { 
    ...
    return null;
}

@Override
public boolean isEnabled() {
    return true;
}
}

And I'm not sure how to make an Handler, that I can access form the button.So the question is, is there a way to access the Handler from the SelectionListener widgetSelected() method?

Thanks


Solution

  • Use the IHandlerService to execute the command that the handler handles:

    IHandlerService handlerService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(IHandlerService.class);
    
    handlerService.executeCommand("your command id", null);