Edit entities with context menu via parameterized commands [message #1038186] |
Wed, 10 April 2013 14:19 |
Andreas Kühne Messages: 2 Registered: April 2013 |
Junior Member |
|
|
Hello everybody,
I have the following use case: I have JFace tree with some nodes which represents my structured set of data. Now I want to edit these entities by right-clicking one node and selecting the "Edit" item from the context menu, which opens a separate dialog to edit the entity.
My idea to achieve this was using a parameterized command and an appropriate handler class like the following, but this didn't work for me. As you can see, I changed the String type of the parameter to any arbitrary Object, but I didn't figure out how to assign a parameter of the general object type to a command.
@Execute
public void execute(@Named("com.example.e4.rcp.todo.commandparameter.input") Object myObject) {
System.out.println(param);
}
I currently haven't figured out how this works in a "beautiful way", so I constructed an ugly work-around: Every time the selection of my JFace tree changes, I use the event broker to post the selected entity. Any handler which subscribes to this post initializes itself with this entity. Now when the command is executed by clicking on a menu item, the corresponding (and already initialized) handler is executed.
private Person person;
@Inject
private IEventBroker broker;
@Inject
@Optional
public void initialize(@UIEventTopic("person_changed") Person person) {
this.person = person;
}
@CanExecute
public boolean canExecute() {
if (person!= null) {
return true;
}
return false;
}
@Execute
public void execute(Shell parent) {...}
Can anybody provide me a more beautiful way to achieve this functionality wihout using the event broker?
Thanks in advance
Regards, Andreas
|
|
|
Re: Edit entities with context menu via parameterized commands [message #1039447 is a reply to message #1038186] |
Fri, 12 April 2013 07:17 |
Christoph Keimel Messages: 482 Registered: December 2010 Location: Germany |
Senior Member |
|
|
Hi Andreas
you could use the ESelectionService to register the selection in your tree and then use the current selection in your handler.
Assuming you are using a JFace TreeViewer, the code to register the selection could look like this:
treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
selectionService.setSelection(event.getSelection());
}
});
Your handler can then use this selection:
@CanExecute
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
if (selection == null) {
return false;
} else if (selection instanceof IStructuredSelection) {
IStructuredSelection structured = (IStructuredSelection)selection;
if (selection.isEmpty())
return false;
... test that selection has the right type ...
} else {
return false;
}
}
@Execute
public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection) {
... open the edit dialog ...
}
Greetings
Christoph
[Updated on: Fri, 12 April 2013 07:18] Report message to a moderator
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01899 seconds