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?
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.
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.
Besides what VonC has mentioned above, you can also use ISourceProviderListener
if the changes you need are not triggered by selection.
ViewB
implements ISourceProviderListener
ISourceProvider
and register it in the servicesViewA
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
ISelectionListener
interface and must register itself with the workbench pageIAdaptable
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.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 aJFace
TableViewer
and adds it as a selection provider to the workbench site. This code enables any UI selection changes in theTableViewer
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With