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.

August 15th, 2006 at 6:58
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—
August 18th, 2006 at 10:45
Hi Chip,
I’m not sure to follow you.
getActivePage()will return the page which isactive for the current
IWorkbenchWindow(there can be only one).If your
IWWindowhas more than one page, you can iterate on themby 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 hadmore than one
IWPagein my apps).The same should go if you have several
IWWindow. By callingIWorkbench.getWorkbenchWindows(),you can iterate on the
IWWindowsuntil you find the right one.Hope it’ll help you
August 22nd, 2006 at 4:05
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—
August 22nd, 2006 at 12:47
hi chip,
your question is out of my competences: I never tried such things.
Maybe you could try something like that in your
MouseListenerThis should select & reveal the view regardless of the
IWWindowit belongs to.If you’re sure your view is in one and only one
IWWindow, it should be enoughAs for Eclipse books, there are 2 must haves I used daily:
Contributing to Eclipse
Eclipse RCP
hope it helps,
jeff