views:

538

answers:

2

In Eclipse RCP, I am creating views for the Perspective using IPageLayout.addView(...)

But this way I don't have a reference to the view. Therefore I don't know how I can tell ViewA to update ViewB.

What's the best pattern to use here?

+2  A: 

You have the different communication paradigm summarize in the IBM article

  • To make a view capable of listening to selection changes, a view must implement the ISelectionListener interface and must register itself with the workbench page
  • Using the IAdaptable interface: A class that implements IAdaptable has the capability to dynamically return certain types of adapters that can then be used to retrieve further information.
  • property change listener paradigm

Regarding the first approach, the article details:

A smarter way to consume UI selections is to register the consumer views as listeners to specific view parts. As you can see in the example below, the view ID of the source view part is mentioned as a parameter during registering a selection listener.

  getSite().getPage().addSelectionListener("SampleViewId",(ISelectionListener)this);

This approach will eliminate the redundant callbacks to the consumer view that would otherwise occur if that view were registered as a nonspecific listener.

The code snippet in Listing 2 shows the createPartControl() method of a view that creates a JFace TableViewer and adds it as a selection provider to the workbench site. This code enables any UI selection changes in the TableViewer to propagate to the page and finally to the interested consumer views.

Listing 2. Setting up a selection provider

public void createPartControl(Composite parent) {
    // Set up a JFace Viewer
    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.setContentProvider(new ViewContentProvider());
    viewer.setLabelProvider(new ViewLabelProvider());
    viewer.setSorter(new NameSorter());
    viewer.setInput(getViewSite());

    // ADD the JFace Viewer as a Selection Provider to the View site.
    getSite().setSelectionProvider(viewer);

}

You will find a similar approach in the RCP tutorial for eclipse3.5 (update February, 4th 2010)

VonC
+5  A: 

Besides what VonC has mentioned above, you can also use ISourceProviderListener if the changes you need are not triggered by selection.

  • Have ViewB implements ISourceProviderListener
  • Create an implementation of ISourceProvider and register it in the services
  • Have ViewA get the ISourceProvider and update it to trigger the changes in ViewB

Read the documentation on those interfaces along with IServiceLocator and ISourceProviderService to get better idea how it all plays out.

You can also see this Lars Vogel's tutorial which has some example how to use the ISourceProvider

DJ
`ISourceProviderListener`: I missed that one. Good solution too. +1. And that would now be "Besides what VonC has mentioned **below**" ;)
VonC