如何在命令中传递对象参数?
I created an eclipse-rcp's project's plugin.xml with a new command with a parameter.
我创建了一个eclipse-rcp项目的插件。带有参数的新命令的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);
I tried this example to pass parameters and it worked.
我试过这个例子来传递参数,它起作用了。
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.
我想要传递哈希映射的参数,并在一般情况下传递任何对象。
I tried this code
我试着这段代码
IServiceLocator serviceLocator = PlatformUI.getWorkbench();
ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);
ExecutionEvent executionEvent = new ExecutionEvent(cmd, paramArray, null, null);
cmd.executeWithChecks(executionEvent);
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 ?
你能帮忙把物体作为参数移动到命令里吗?
3 个解决方案
#1
3
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)使用“执行事件”的选择对象(检查它,它包含许多信息)。您可以使用AbstractSourceProvider,这样您就可以将您的对象传递给应用程序上下文。
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) 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:
B)源提供程序的使用有点棘手。主要思想是,将对象添加到应用程序上下文。Eclipse 3的重要片段。我在阅读完这个博客后建立的一个项目(注意:它是德语的,它提供的例子不适用):在你的插件中。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>
Set up your own SourceProvider. Calling the "getCurrentState" you can get the variable (your Person object in this case) of that SourceProvider:
设置自己的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);
}
}
}
In your View you register to the SourceProvider and set the Object to the object you want to transfer to your Handler.
在您的视图中,您向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);
}
});
}
And in your Handler you can just call the PersonSourceProvider#getCurrentState to get your Objects back.
在您的处理程序中,您可以调用PersonSourceProvider#getCurrentState来将对象返回。
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.
这种方法的优点是,您可以在任何需要的地方使用Objectd。您甚至可以根据当前选择的对象设置PropertyTester来启用/禁用UI元素。
更多相关文章
- JavaScript 面向对象编程,严格过程的标准化编程法,目前为止最好的
- JAVA复习3 Java类和对象
- java对象判断是否为空工具类
- 如何在Javascript中解析URL查询参数?(复制)
- Java-控制台传递参数
- 【阿里云】Java面向对象开发课程笔记(十六)——抽象类