I added a control in the toolbar which extends WorkbenchWindowControlContribution
and implements ISelectionProvider
. This control contains a Combo and when I change the selection I want to notify another view. Because it's not a ViewPart I can't call getSite().setSelectionProvider
directly. I tried to set it like this:
PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().setSelectionProvider(MyControl.this); });
If I add this to the protected Control createControl(Composite parent)
method the getActivePart would always be null. I also tried registering it when the Combo's value changes for the first time, but that way my other View wasn't notified about the change event.
Here is the ISelectionProvider methods:
@Override public void addSelectionChangedListener(ISelectionChangedListener listener) { listeners.add(listener); } @Override public ISelection getSelection() { return new StructuredSelection(comboBox.getText()); } @Override public void removeSelectionChangedListener(ISelectionChangedListener listener) { listeners.remove(listener); } @Override public void setSelection(ISelection selection) { for (ISelectionChangedListener listener : listeners) { listener.selectionChanged(new SelectionChangedEvent(this, selection)); } }
The View that should be getting notified implements ISelectionListener
. I register it in the public void createPartControl(Composite parent)
with getSite().getPage().addSelectionListener(this);
And here is the override of the SelectionChanged too:
@Override public void selectionChanged(IWorkbenchPart part, ISelection selection) { if (part instanceof MyControl) { String selectedProtocol = ((StructuredSelection) selection).getFirstElement().toString(); } }
Can it be done?
Thanks!
0 comments:
Post a Comment