Drawing Hands by M.C. Escher (1948)

Jeff Mesnil


Eclipse’s ISetSelectionTarget interface

On July 3rd, 2006 in eclipse

I just found a new Interface in Eclipse which I found quite handy.

Let’s say that you have an action which creates a new resource and you want to notify views that they have to show this resource.

The key interface is ISetSelectionTarget. If your views respond to this interface (by implementation or adaptation), they can then receive a selectReveal(ISelection selection) message.

For example, one of my view implements ISetSelectionTarget and defines the method:

    public void selectReveal(ISelection selection) {
       StructuredSelection ssel = convertSelection(selection);
       if (!ssel.isEmpty()) {
          viewer.getControl().setRedraw(false);
          viewer.setSelection(ssel, true);
          viewer.getControl().setRedraw(true);
       }
    }

How do you send this selectReveal() message from your action?
An example is found in the JDT, in the org.eclipse.ui.wizards.newresource.BasicNewResourceWizard class:

  public static void selectAndReveal(IResource resource, IWorkbenchWindow window) {
    // validate the input
    if (window == null || resource == null)
        return;
    IWorkbenchPage page = window.getActivePage();
    if (page == null)
        return;

    // get all the view and editor parts
    List parts = new ArrayList();
    IWorkbenchPartReference refs[] = page.getViewReferences();
    for (int i = 0; i < refs.length; i++) {
        IWorkbenchPart part = refs[i].getPart(false);
        if (part != null)
            parts.add(part);
    }
    refs = page.getEditorReferences();
    for (int i = 0; i < refs.length; i++) {
        if (refs[i].getPart(false) != null)
            parts.add(refs[i].getPart(false));
    }

    final ISelection selection = new StructuredSelection(resource);
    Iterator itr = parts.iterator();
    while (itr.hasNext()) {
        IWorkbenchPart part = (IWorkbenchPart) itr.next();

        // get the part's ISetSelectionTarget implementation
        ISetSelectionTarget target = null;
        if (part instanceof ISetSelectionTarget)
            target = (ISetSelectionTarget) part;
        else
            target = (ISetSelectionTarget) part.getAdapter(ISetSelectionTarget.class);

        if (target != null) {
            // select and reveal resource
            final ISetSelectionTarget finalTarget = target;
            window.getShell().getDisplay().asyncExec(new Runnable() {
                public void run() {
                    finalTarget.selectReveal(selection);
                }
            });
        }
    }
  }

However, your code does not need to be that generic. If you already know which view may be interested to be notified, you can use

IViewPart part = page.findView(viewId):

to retrieve the interested IViewPart and after checking that it will respond to ISetSelectionTarget (either by implementation or adaptation), you can send it a selectReveal() message directly.

4 Responses to “Eclipse’s ISetSelectionTarget interface”

  1. chip Says:

    ei jeff, got a problem with the getActivePage() command… id like to ask if it will still work if you have many pages in one workbenchwindow? i know that there is only one active page on a workbench at a particular time. but will this command be able to select that page amongst the other inactive pages in the window? and a follow up question: will the “getActiveWorkbenchWindow()” also work if you have many workbenches windows opened? thanx….

    really need the solution asap

    —chip—

  2. jmesnil Says:

    Hi Chip,

    I’m not sure to follow you. getActivePage() will return the page which is
    active for the current IWorkbenchWindow (there can be only one).
    If your IWWindow has more than one page, you can iterate on them
    by calling IWWindow.getPages().
    However once you found the one you want, you may make it active by calling
    IWWindow.setActivePage(IWPage) (though I never tried it since I’ve never had
    more than one IWPage in my apps).

    The same should go if you have severalIWWindow. By calling IWorkbench.getWorkbenchWindows(),
    you can iterate on the IWWindows until you find the right one.

    Hope it’ll help you

  3. chip Says:

    thanx for the last comment…. helped me a lot… now, im currently working with multiple workbenches. how would i let “workbench A” respond to an action like a mouse clik in “workbench b?”? example: i clicked on a button in “workbench b”, “workbench a” should show an object in its viewer… id like to ask if this is possible?? i mean having multiple workbenches react with each other?? can you help me with this?? its cracking me up… hehehe and can you recommend me a good eclipse book?? hehehe currently working with eclipse v3.2…. thnx again…

    —chip—

  4. jmesnil Says:

    hi chip,

    your question is out of my competences: I never tried such things.

    Maybe you could try something like that in your MouseListener

    for each window in IWorkbench.getWorkbenchWindows() {
        page = window.getActivePage()
        part = part.findView("your.view.id")
        target = (ISetSelectionTarget) part;
        target.selectReveal(selection)
    

    This should select & reveal the view regardless of the IWWindow it belongs to.
    If you’re sure your view is in one and only one IWWindow, it should be enough

    As for Eclipse books, there are 2 must haves I used daily:
    Contributing to Eclipse
    Eclipse RCP

    hope it helps,
    jeff