e4 handler 传递自定义对象

2017/07/12 10:04
阅读数 26

e3,e4的commmand默认传递参数均为字符串String类型,不支持自定义类型,但是可以通过把对象转成字符串的形式传递过去后再重新转回对象。

The Parameterized Command does only accept Strings. Here is an example for smaller objects:

Disclaimer: this is for Eclipse 3.x. I am not using Eclipse 4.x a lot, so you might have to adapt there in case you need it.

Create a Pluginproject (com.voo.example.commandparameter.advanced) with a View (com.voo.example.commandparameter.advanced.view) , a Command (com.voo.example.commandparameter.advanced.sysoCommand) with menu entry and Handler(com.voo.example.commandparameter.advanced.sysoCommand), and a universal Object (MyTestObject).

The Command needs a Parameter and a Parametertype in the plugin.xml, that gets passed to it:

<extension
     point="org.eclipse.ui.commands">
  <command
        id="com.voo.example.commandparameter.advanced.sysoCommand"
        name="SysoCommand">
     <commandParameter
           id="myObject"
           name="object"
           optional="true"
           typeId="com.voo.example.commandparameter.advanced.testType">
     </commandParameter>
  </command>
  <commandParameterType
        id="com.voo.example.commandparameter.advanced.testType"
        type="com.voo.example.commandparameter.advanced.MyTestObject">
  </commandParameterType>

In the Object you set atrtibutes like name and street and define a convertToString method like that:

public String convertToString() {
    return getName() +",,,"+ getStreet();
}

(you can override the toString method, too. I just used that method to set weired delimiters to the returned String) And in a Class MyParamterConverter you can transfer it back:

public class MyParameterConverter extends AbstractParameterValueConverter {

public MyParameterConverter() {

}

@Override
public String convertToString(Object parameterValue)
        throws ParameterValueConversionException {
    return parameterValue.toString();
}

/**
 * This will always create a new object. Just keep that in mind 
 * if you're trying to work with the objects.
 */
@Override
public Object convertToObject(String parameterValue)
        throws ParameterValueConversionException {

    //Split the String to get the attributes back
    String delimiter =",,,";
    String[] split = parameterValue.split(delimiter);
    String name = split[0];
    String street = split [1];

    return new MyTestObject(name, street);
}
}

Now you can call the command with a buttonclick in your view, for example:

    btnGo.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {

            MyTestObject testObject = new MyTestObject(textName.getText(),textStreet.getText());

            ICommandService cS = (ICommandService)getSite().getService(ICommandService.class);
            IHandlerService hS = (IHandlerService)getSite().getService(IHandlerService.class);

            Command sysoComm = cS.getCommand("com.voo.example.commandparameter.advanced.sysoCommand");


            HashMap<String, String> params = new HashMap<String, String>();
            params.put("myObject", testObject.convertToString());

            ParameterizedCommand pC = ParameterizedCommand.generateCommand(sysoComm, params);

            try {
                hS.executeCommand(pC, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

And the Handler can transform the passed parameters back :

public class MyObjectHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException { 
    String param1 = event.getParameter("myObject");
    MyParameterConverter converter = new MyParameterConverter();
    Object convertToObject = null;

    try {
        convertToObject = converter.convertToObject(param1);
    } catch (ParameterValueConversionException e) {
        e.printStackTrace();
    }

    if (convertToObject instanceof MyTestObject) {
        MyTestObject to = (MyTestObject) convertToObject;
        System.out.println(to.toString());
    }

    return null;
}
}

This should work for most smaller sized objects that do not change while you pass them. If you need to pass bigger objects, you will have two choices: A) use the selected object of the "Execution Event" (examine that, it contains a lot of infos). B) you can use AbstractSourceProvider, so you can pass your object to the application context.

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部