Commands Part 4: Misc items ...

How do I know about execution of a command?

You can use the IExecutionListener:

ICommandService service = (ICommandService) serviceLocator.getService(ICommandService.class);
service.addExecutionListener(new IExecutionListener() {

    public void notHandled(String commandId, NotHandledException exception) {
    }

    public void postExecuteFailure(String commandId, ExecutionException exception) {
    }

    public void postExecuteSuccess(String commandId, Object returnValue) {
    }

    public void preExecute(String commandId, ExecutionEvent event) {
    }
});

The IExecutionListener is merely an observer of the command execution so it can neither veto on it nor make any changes to the event.

How do I get the active Window/Shell in my handler code?

Use HandlerUtil. Its a handy class which gives you access to most common variables like active window, active shell, context ids, active part, current selection,  etc.

Shell shell = HandlerUtil.getActiveShell(event);
ISelection selection = HandlerUtil.getCurrentSelection(event);

All of the get* methods will have a get*Checked variants. The first ones will return you null if the variable is not available. The checked ones will throw you an exception if the variable is null. If you have your own variable, you can use the getVariable() method to get it.

Can I programmatically execute a Command?

Yes. But don't simple call command.execute(), use the IHandlerService to execute a command:

IHandlerService handlerService = (IHandlerService) serviceLocator.getService(IHandlerService.class);
handlerService.executeCommand(commandId, null);

Can I programmatically create and manipulate a Command?

Yes! You can use ICommandService to create a new command and then associate a handler thru IHandlerService:

ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);
Command command = commandService.getCommand("my.new.undefined.command");
command.define("New Command", "This is created Programatically!", 
    commandService.getCategory("org.eclipse.ui.category.window"));

IHandlerService handlerService = (IHandlerService) serviceLocator.getService(IHandlerService.class);
handlerService.activateHandler(command.getId(), new AbstractHandler() {

    public Object execute(ExecutionEvent event) throws ExecutionException {
        System.out.println("Command executed !");
        return null;
    }
});

 

See also:

Part 1: Actions Vs Commands
Part 2: Selection and Enablement of Handlers
Part 3: Parameters for Commands
Part 5: ISourceProvider & dynamically updating Commands
Part 6: 'toggle' & 'radio' style menu contribution




Tired of unwanted and irrelevant results in Google? Try Eclipse Search, the Customized Seach Engine that gives you only relevant results - Powered by Google! It has plugins for Eclipse and Firefox/IE



Posted in Labels: , , , |

3 comments:

  1. Anonymous Says:

    Hi Prakash,
    your HOWTO for the commands is realy good. So now I have changed many actions in my own programm to commands.
    But I have one question, how can I activate / make visible an command in an special perspektive for example "open file" to that i can open an file editor.
    But in other perspectives you dont see this command?

    Greetings
    Heiko

  2. Charles Says:

    I've enjoyed this tutorial but there is one element of the new command framework that you have not covered. I've also searched other resources and have not found my answer. I have also experimented with code and have not solved my problem. This makes me conclude that the answer is so obvious I can't see it or that there is no answer. Perhaps you can help distinguish.

    Using the command framework I have not been able to add, using only plugin.xml entries, an ability to add toolbar menu items to a specific page of of a pagebook view. The Eclipse PDE implements, in code, various contributing content outline pages for the outline view. When no editor is open the outline view has no toolbar items. Depending on the file opened for edit, the icons in the views toolbar change to suit the file. Can that be done for a new contributing page using only command framework entries? I also want to implement the same behavior in my own pagebook views.

  3. David Pérez Says:

    Your tutorial is really good.
    I'm using more commands than before.

    But I think commands (or its handlers) cannot have a checked state. This is a limitation.