Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to communicate between views in Eclipse RCP?

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?

like image 833
geejay Avatar asked Feb 09 '10 08:02

geejay


People also ask

How to create RCP application in Eclipse?

To create an RCP application, go to the File menu and select New > Project where you will be presented with the new project wizard. From here choose Plug-in Project. The next screen allows you to assign a name to your plugin. Usually, plug-in name follow Java's package naming conventions.

What is RCP in Eclipse?

The Rich Client Platform (RCP) is an exciting new way to build Java applications that can compete with native applications on any platform. This tutorial is designed to get you started building RCP applications quickly.


3 Answers

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

like image 84
DJ. Avatar answered Oct 04 '22 21:10

DJ.


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)

like image 31
VonC Avatar answered Oct 04 '22 22:10

VonC


There are different ways for view and plugin communications: eventbroker, listener etc..

EvenBroker (e4) Implementation: Use eventbroker to send message (string) between views and plugins.

Sender Side:

@Inject
private IEventBroker eventBroker; 
private static final String STATUS ="status";
eventBroker.send(STATUS, "status test message..");

Receiver Side:

@Inject
private IEventBroker eventBroker; 
private static final String STATUS ="status";
@Inject @Optional
public void  getEvent(@UIEventTopic(STATUS) String message) {
    ... //call method 
}
like image 32
OSezer Avatar answered Oct 04 '22 22:10

OSezer