RSS

Search Engine

Wednesday, June 16, 2010

Using parameters in commands

You can also define parameters in commands. Create project "de.vogella.rcp.commands.parameterfirst" using the "Hello RCP" template.

Create a command with the id "de.vogella.rcp.commands.parameterfirst.helloName" and a default handler "de.vogella.rcp.commands.parameterfirst.handler.HelloName".

Right click on your command, select New -> commandParameter

Use the following id for the parameter "de.vogella.rcp.commands.parameterfirst.commandParameter1"

In the handler you have to evaluate the parameter.

   
package de.vogella.rcp.commands.parameterfirst.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.handlers.HandlerUtil;

public class HelloName extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
String name = event
.getParameter("de.vogella.rcp.commands.parameterfirst.commandParameter1");
MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
"Hello", "Hello " + name);
return null;
}
}

Add this command to the menu. On the command, right click and select New -> Parameter

As name maintain the name of the parameter, pass as value the value you want to use in your handler.

Add the same command again to the menu and pass another parameter.

If you run your application and select the menu entry the value should be used in the message box.

0 comments:

Post a Comment