开发者

How to communicate between views in Eclipse RCP?

开发者 https://www.devze.com 2022-12-19 19:55 出处:网络
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 开发者_Stack

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 开发者_StackOverflow中文版update ViewB.

What's the best pattern to use here?


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


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)


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 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消