capint
11/1/2015 - 6:49 PM

Eclipse >> UI >> Windows selection service

Eclipse >> UI >> Windows selection service

getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(l);
//Or
ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
private ISelectionListener mylistener = new ISelectionListener() {
        public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
        if (sourcepart != MyView.this &&
            selection instanceof IStructuredSelection) {
            doSomething(((IStructuredSelection) selection).toList());
            }
        }
    };
    
public void dispose() {
        ISelectionService s = getSite().getWorkbenchWindow().getSelectionService();
     s.removeSelectionListener(mylistener);
        super.dispose();
    }
>> Each workbench window has its own SELECTION SERVICE instance. 
>> The service keeps track of the SELECTION in the currently active part 
(SELECTION PROVIDER) and propagates selection changes to all registered LISTENERs.

>> SELECTION:
  For convenience there are default implementations for these interfaces:
    org.eclipse.jface.viewers.StructuredSelection
    org.eclipse.jface.viewers.TreeSelection
    org.eclipse.jface.text.TextSelection
    org.eclipse.jface.text.MarkSelection

>> SELECTION PROVIDER:
  All JFace viewers are so called selection providers. A selection provider
implements the interface ISelectionProvider
It is like this:
  public interface ISelectionProvider {
      public void addSelectionChangedListener(ISelectionChangedListener listener);
      public ISelection getSelection();
      public void removeSelectionChangedListener(
              ISelectionChangedListener listener);
      public void setSelection(ISelection selection);
  }

>> LISTENER:
  Views register an ISelectionListener to get notified when the window's 
current selection changes:
    void addSelectionListener(ISelectionListener listener)
    void removeSelectionListener(ISelectionListener listener)