Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Edit entities with context menu via parameterized commands
Edit entities with context menu via parameterized commands [message #1038186] Wed, 10 April 2013 14:19 Go to next message
Andreas Kühne is currently offline 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 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph Keimel
Messages: 367
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

Re: Edit entities with context menu via parameterized commands [message #1039483 is a reply to message #1039447] Fri, 12 April 2013 08:08 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Why don't you propagate and then inject the specific domain object? DI is type sensitive so you would only get the method called if it is of the right type and would skip those 3.x style type checks.

Re: Edit entities with context menu via parameterized commands [message #1039484 is a reply to message #1039447] Fri, 12 April 2013 08:08 Go to previous messageGo to next message
Andreas Kühne is currently offline Andreas Kühne
Messages: 2
Registered: April 2013
Junior Member
Hi Christoph,

thanks for your help, this worked for me Smile

Regards, Andreas
Re: Edit entities with context menu via parameterized commands [message #1039492 is a reply to message #1039483] Fri, 12 April 2013 08:16 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph Keimel
Messages: 367
Registered: December 2010
Location: Germany
Senior Member
Sopot Cela wrote on Fri, 12 April 2013 10:08
Why don't you propagate and then inject the specific domain object? DI is type sensitive so you would only get the method called if it is of the right type and would skip those 3.x style type checks.

I find it more flexible to work with ISelection:
- handle multiple selection (could be of different types)
- use the adapter framework
Re: Edit entities with context menu via parameterized commands [message #1039499 is a reply to message #1039483] Fri, 12 April 2013 08:28 Go to previous message
Christoph Keimel is currently offline Christoph Keimel
Messages: 367
Registered: December 2010
Location: Germany
Senior Member
Sopot Cela wrote on Fri, 12 April 2013 10:08
Why don't you propagate and then inject the specific domain object? DI is type sensitive so you would only get the method called if it is of the right type and would skip those 3.x style type checks.

Sorry ... now I get it. You probably mean:
@CanExecute
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection) {
	if (selection == null) {
		return false;
	} else if (selection.isEmpty()) {
		return false;
	}
	... test that selection has the right type ...
}
Previous Topic:EModelService can't find MMenuElement's
Next Topic:IStatusLineManger
Goto Forum:
  


Current Time: Mon Oct 20 22:44:06 GMT 2014

Powered by FUDForum. Page generated in 0.04431 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software