I'm working in Eclipse 3.4 on a new plugin. I am using the Command framework to implement most of the commands that are appropriate to the plugin that I am using. Thus, opening a view is a command in its own right.
There are times where I would like to invoke the command programmatically. For example, I have a wizard that contributes to the New wizards, and if a certain view is not open when I hit the performFinish in that wizard, I want the wizard to invoke the openView command that I have created.
Obviously, opening a view from the wizard is fairly easy - the code is the same as the code in the command for the most part - but the point with the command pattern is to make sure that all of the appropriate listeners for a command are properly advised. Thus, invoking the command will have all the handlers execute appropriately. I have read and worked through Chapter 6 and have not seen any example of doing this. I have also searched through the rest of the book for examlpes with not much luck.
The Eclipse documentation indicates that the way to do this is via the ICommandService which you can obtain by calling getService() on various objects. The following code succeeds in invoking the command:
- Code: Select all
ICommandService cmdService = (ICommandService) workbench.getService(ICommandService.class);
Command cmd = cmdService.getCommand("plugin.commands.openView");
ExecutionEvent event = new ExecutionEvent(cmd, new HashMap<String, String>(), this, null);
try {
cmd.executeWithChecks(event);
}
catch ...
The problem with this, though, is the creation of the ExecutionEvent object. HandlerUtil makes use of the ExecutionEvent object to determine the active workbench window. The problem is that my code does not provide this information and that is because I don't know how to provide it. As a result, HandlerUtil.getActiveWorkbenchWindow() throws an ExecutionException which has to be caught and dealt with in the handler.
The code in the handler now looks like this:
- Code: Select all
IWorkbenchWindow window = null;
try {
window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
}
catch (ExecutionException e) {
// Ignore... we expect this one. It means the event did not give us the detail we need.
}
if (window == null) {
window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null) {
return null;
}
}
Clearly, this is a workaround. I therefore have a couple of questions:
1) Am I barking up the wrong tree doing things this way?
2) Is there an easier way of doing this?
3) If not, where do I go to get the information on how to build the ExecutionEvent?