środa, 31 marca 2010

How to work with Selection Service and make your WorkbenchPart more cooperative

The whole power of Eclipse platform comes from its cooperation features among of all it part. We used to see all views and editors work together seamlessly. But when comes to develop our own view or editor we found our part not so eager cooperate with environment. Many of the users found this as big disadvantage of your contribution and will not feel comfortable using it. To avoid you part an alien please let me share some common issues I faced during my advantage with Eclipse plug-in development.


1). Make your view open to cooperate with other parts and share own selection events.

Assuming your XXViewer implements ISelectionProvider interface (common to all objects that provide a selection).

public interface ISelectionProvider {
    public void addSelectionChangedListener(ISelectionChangedListener listener);
    public ISelection getSelection();
    public void removeSelectionChangedListener(ISelectionChangedListener listener);
    public void setSelection(ISelection selection);
}

You should set your view (more precisely your XXViewer) as a Selection Provider. Thus any other part listening on selection service can hear your selection events.

public void createPartControl(Composite parent) {
  viewer = new XXViewer(..);
  getSite().setSelectionProvider(viewer);
}

2). Make your view be aware of the others selection events. Don't let your contribution to be ignorant - listen others.

To do so you need to register your workbench part with the selection service.

private ISelectionListener listener = new ISelectionListener() {
  public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
   doSomeActionHere(IWorkbenchPart sourcepart, ISelection selection);
  }
 };

Also please remember about removing listener when part's dispose. It's really important!!

public void dispose() {
 getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(listener);
 super.dispose();
}

Ok, now you can be absolutely sure that your contribution will be full-rights member of Eclipse community.

Brak komentarzy:

Prześlij komentarz