如何在命令中传递对象参数?

use*_*697 5 java eclipse-plugin

我使用带有参数的新命令创建了 eclipse-rcp 项目的plugin.xml。

 ArrayList<parameterization> parameters = new ArrayList<parameterization>();
 IParameter iparam;

 //get the command from plugin.xml
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 ICommandService cmdService =     (ICommandService)window.getService(ICommandService.class);
 Command cmd = cmdService.getCommand("org.ipiel.demo.commands.click");

//get the parameter
iparam = cmd.getParameter("org.ipiel.demo.commands.click.paramenter1");
Parameterization params = new Parameterization(iparam, "commandValue");
parameters.add(params);

//build the parameterized command
 ParameterizedCommand pc = new ParameterizedCommand(cmd, parameters.toArray(new       Parameterization[parameters.size()]));

//execute the command
 IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
handlerService.executeCommand(pc, null);
Run Code Online (Sandbox Code Playgroud)

我尝试了这个例子来传递参数并且它有效。

本示例中的问题是我只能传递 String 类型的参数。(因为参数化)

我想传递哈希映射的参数,并且通常传递任何对象。

我试过这段代码

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




    ExecutionEvent executionEvent = new ExecutionEvent(cmd, paramArray, null, null);
    cmd.executeWithChecks(executionEvent);
Run Code Online (Sandbox Code Playgroud)

但它不起作用,参数没有移动(它为空)

您能帮忙将对象作为命令中的参数移动吗?

Cal*_*lon 5

由于在我的第一个答案中添加另一个解决方案会让人感到困惑,因此我将为第二个解决方案提供另一个解决方案。我给出的选择是“A)使用“执行事件”的选定对象(检查一下,它包含很多信息)。B)您可以使用 AbstractSourceProvider,这样您就可以将对象传递到应用程序上下文。”

如果您的对象是选择像树这样的结构化对象,则可以在您的处理程序中使用 A):

MyObject p = (MyObject) ((IStructuredSelection) HandlerUtil.getCurrentSelection(event)).getFirstElement();
Run Code Online (Sandbox Code Playgroud)

B) 源提供者的使用有点棘手。主要思想是将对象添加到应用程序上下文中。阅读此博客后我设置的项目中的 Eclipse 3.x 的重要片段(注意:它是德语,它提供的示例不起作用): 在您的 plugin.xml 中添加:

  <extension point="org.eclipse.ui.services">
  <sourceProvider
        provider="com.voo.example.sourceprovider.PersonSourceProvider">
     <variable
           name="com.voo.example.sourceprovider.currentPerson"
           priorityLevel="activePartId">
     </variable>
  </sourceProvider>
Run Code Online (Sandbox Code Playgroud)

设置您自己的 SourceProvider。调用“getCurrentState”,您可以获取该 SourceProvider 的变量(在本例中为您的Person对象):

public class PersonSourceProvider extends AbstractSourceProvider{

/** This is the variable that is used as reference to the SourceProvider
 */
public static final String PERSON_ID = "com.voo.example.sourceprovider.currentPerson";
private Person currentPerson;

public PersonSourceProvider() {

}

@Override
public void dispose() {
    currentPerson = null;
}

**/**
 * Used to get the Status of the source from the framework
 */
@Override
public Map<String, Person> getCurrentState() {
    Map<String, Person> personMap = new HashMap<String, Person>();
    personMap.put(PERSON_ID, currentPerson);
    return personMap;
}**

@Override
public String[] getProvidedSourceNames() {
    return new String[]{PERSON_ID};
}

public void personChanged(Person p){
    if (this.currentPerson != null && this.currentPerson.equals(p)){
        return;
    }

    this.currentPerson = p;
    fireSourceChanged(ISources.ACTIVE_PART_ID, PERSON_ID, this.currentPerson);
}
Run Code Online (Sandbox Code Playgroud)

}

在您的视图中,您注册到 SourceProvider 并将对象设置为您想要传输到处理程序的对象。

public void createPartControl(Composite parent) {

    viewer = new TreeViewer(parent);
    viewer.setLabelProvider(new ViewLabelProvider());
    viewer.setContentProvider(new ViewContentProvider());
    viewer.setInput(rootPerson);
    getSite().setSelectionProvider(viewer);
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            Person p = null;
            if (event.getSelection() instanceof TreeSelection) {
                TreeSelection selection = (TreeSelection) event.getSelection();
                if (selection.getFirstElement() instanceof Person) {
                    p = (Person) selection.getFirstElement();
                }
            }
            if (p==null) {
                return;
            }
            IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            ISourceProviderService service = (ISourceProviderService) window.getService(ISourceProviderService.class);
            PersonSourceProvider sourceProvider = (PersonSourceProvider) service.getSourceProvider(PersonSourceProvider.PERSON_ID);
            sourceProvider.personChanged(p);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在您的处理程序中,您只需调用 PersonSourceProvider#getCurrentState 即可取回您的对象。

这种方法的优点是,您可以在任何您想要的地方使用 Objectd。例如,您甚至可以设置 PropertyTester 以根据当前选定的对象启用/禁用 UI 元素。