如何通过代码将参数传递给eclipse rcp应用程序中的命令?

时间:2013-11-22 12:55:02

标签: java eclipse eclipse-plugin eclipse-rcp

我使用以下代码运行通过org.eclipse.ui.commands添加的命令:

IServiceLocator serviceLocator = PlatformUI.getWorkbench();
ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);

try  {
        Command command = commandService.getCommand("my_command_id");
        command.executeWithChecks(new ExecutionEvent());

    } catch (ExecutionException ex1) {

    } catch (NotDefinedException ex2){

    } catch(NotEnabledException ex3){

    } catch(NotHandledException ex4){

    }


这工作正常,但我想将参数传递给此命令。我有接受参数的机制,但是如何通过代码传递它?

1 个答案:

答案 0 :(得分:1)

想通了!以下是做同样的事情:

IServiceLocator serviceLocator = PlatformUI.getWorkbench();
ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);

try  { 
        Command command = commandService.getCommand("my_command_id");
        Map<String, String> map = new HashMap<String, String>();
        map.put("param_name", "param_value");
        /*more parameter's can be added to Map and passed*/
        command.executeWithChecks(new ExecutionEvent(null, map, null, null));

    } catch (ExecutionException e1) {

    } catch (NotDefinedException e2){

    } catch(NotEnabledException e3){

    } catch(NotHandledException e4){

    }