Reputation: 534
I have an editor in an RCP Program that contains a TableViewer
. The content of the TableViewer
can be updated as the result of an Action
in the editor. This is currently done by creating the new input and calling
tableViewer.setInput(updatedInput);
Unfortunately, this doesn't send a SelectionChangedEvent
until either the editor loses focus or a new selection is made in the table. This leads to trouble in a Command
which gets the current selection via HandlerUtil.getCurrentSelection(event)
-
ISelection oldSelection = tableViewer.getSelection();
Collection<Foo> newFoos = fooAction.createNewFoos();
tableViewer.setInput(newFoos);
...
...//call an action.
...//Inside the action:
Collection<Foo> selectedFoos = HandlerUtil.getCurrentSelection(event).toList();
//No good! We get the unchanged selection back!
I see two ways to solve this problem. Iterating through the input and adjusting the existing objects instead of calling setInput()
would probably work, but is not an option in my case. Instead, I want to trigger a SelectionChangedEvent
, which will get HandlerUtil
to update its currentSelection
variable. Currently, I get this to happen like this:
ISelection oldSelection = tableViewer.getSelection();
Collection<Foo> newFoos = fooAction.createNewFoos();
tableViewer.setInput(newFoos);
ISelection selection = tableViewer.getSelection();
tableViewer.setSelection(null);
tableViewer.setSelection(selection);
This works, as the selection changes, which forces an event to be fired off. But it's also an ugly hack, and even with a comment I think this could potentially confuse anyone looking at this chunk of code in the future.
So with all that out of the way, is there a proper way to get a TableViewer
to fire a SelectionChangedEvent
? Could I maybe get this to happen automatically with the proper ContentProvider
? Or can I somehow accomplish this by firing a property change from the view or editor?
Any tips would be greatly appreciated!
Upvotes: 0
Views: 114
Reputation: 111142
What you show is fine except that you should set the selection to empty rather than null with:
tableViewer.setSelection(StructuredSelection.EMPTY);
Upvotes: 1