我创建了一个带有参数的新命令的eclipse-rcp项目的plugin.xml。
I created an eclipse-rcp's project's plugin.xml with a new command with a parameter.
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.demomands.click"); //get the parameter iparam = cmd.getParameter("org.ipiel.demomands.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);
我试过这个例子传递参数和它的工作。
I tried this example to pass parameters and it worked.
这个例子中的问题只能传递String类型的参数。 (因为参数化)
The issue in this example that I could pass only parameters of type String. ( because Parameterization )
我想传递哈希映射的参数,一般来说传递任何对象。
I want to pass parameter of hash map and in general to pass any object.
我试过这个代码
IServiceLocator serviceLocator = PlatformUI.getWorkbench(); ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class); ExecutionEvent executionEvent = new ExecutionEvent(cmd, paramArray, null, null); cmd.executeWithChecks(executionEvent);但它没有工作,参数没有移动(它是null)
but it didn't work the parameters didn't move ( it was null)
你可以帮助将对象作为参数移动到命令中吗?
Could you please help to to move object as parameter in command ?
推荐答案这会让我感到困惑,为我的第一个答案添加另一个解决方案,我将提供另一个解决方案。 我给出的选择是A使用执行事件的选定对象(检查它包含很多信息)B)可以使用AbstractSourceProvider,以便将对象传递给应用程序上下文。
Since it would get confusing to add another solution to my first answer, I'll provide another one for a second solution. The choices I gave were " 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."
A)可以在您的处理程序中使用,如果您的对象是像树一样的结构化对象的选择:
A) can be used in your Handler if your object is the selection of a Structured Object like a Tree:
MyObject p = (MyObject) ((IStructuredSelection) HandlerUtil.getCurrentSelection(event)).getFirstElement();B)源提供者的使用有点棘手。主要的想法是,您将对象添加到应用程序上下文中。从我在阅读此博客之后设置的项目中,Eclipse 3.x的重要片段(注意:它是在德国,它提供的示例不起作用):在您的plugin.xml中添加:
B) The usage of a Source provider is a bit more tricky. The main idea is, that you add your object to the application context. The important snippets for Eclipse 3.x from a project that I set up after I read this blog (note: it is in german and the example it provides doesn't work): In your plugin.xml add:
<extension point="org.eclipse.ui.services"> <sourceProvider provider="com.voo.example.sourceprovider.PersonSourceProvider"> <variable name="com.voo.example.sourceprovider.currentPerson" priorityLevel="activePartId"> </variable> </sourceProvider>
设置自己的SourceProvider。调用getCurrentState,您可以获取该SourceProvider的变量(您的 Person 对象):
Set up your own SourceProvider. Calling the "getCurrentState" you can get the variable (your Person object in this case) of that SourceProvider:
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); }}
在您的视图中,您注册到SourceProvider并将对象设置为要传输到您的处理程序的对象。
In your View you register to the SourceProvider and set the Object to the object you want to transfer to your Handler.
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); } }); }在你的处理程序中,你可以调用PersonSourceProvider#getCurrentState获取对象
And in your Handler you can just call the PersonSourceProvider#getCurrentState to get your Objects back.
这种方法的优点是可以在任何地方使用Objectd。例如。您甚至可以设置一个PropertyTester以根据当前选定的对象启用/禁用UI元素。
Advantage of this method is, that you can use the Objectd anywhere you want. E.g. you can even set up a PropertyTester to enable/disable UI elements according to the currently selected Object.
更多推荐
如何在命令中传递对象参数?
发布评论