java 如何在 Eclipse RCP 中的视图之间进行通信?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2227701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:09:48  来源:igfitidea点击:

How to communicate between views in Eclipse RCP?

javaeclipse-rcp

提问by geejay

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

在 Eclipse RCP 中,我正在使用 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.

但是这样我就没有对视图的引用。因此我不知道如何告诉 ViewA 更新 ViewB。

What's the best pattern to use here?

在这里使用的最佳模式是什么?

回答by DJ.

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

除了上面提到的 VonC 之外,ISourceProviderListener如果您需要的更改不是由选择触发,您也可以使用。

  • Have ViewBimplements ISourceProviderListener
  • Create an implementation of ISourceProviderand register it in the services
  • Have ViewAget the ISourceProviderand update it to trigger the changes in ViewB
  • ViewB工具ISourceProviderListener
  • 创建一个实现ISourceProvider并将其注册到服务中
  • ViewA获取ISourceProvider并更新它以触发更改ViewB

Read the documentation on those interfaces along with IServiceLocatorand ISourceProviderServiceto get better idea how it all plays out.

阅读有关这些接口的文档,IServiceLocatorISourceProviderService更好地了解它是如何发挥作用的。

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

您还可以查看此 Lars Vogel 的教程,其中包含一些如何使用ISourceProvider

回答by VonC

You have the different communication paradigm summarize in the IBM article

您在IBM 文章中总结了不同的通信范式

  • To make a view capable of listening to selection changes, a view must implement the ISelectionListenerinterface and must register itself with the workbench page
  • Using the IAdaptableinterface: A class that implements IAdaptablehas the capability to dynamically return certain types of adapters that can then be used to retrieve further information.
  • property change listener paradigm
  • 要使视图能够侦听选择更改,视图必须实现ISelectionListener接口并且必须向工作台页面注册自己
  • 使用IAdaptable接口:实现的类IAdaptable能够动态返回某些类型的适配器,然后可以使用这些适配器来检索更多信息。
  • 属性更改侦听器范例

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.

使用 UI 选择的一种更智能的方法是将使用者视图注册为特定视图部件的侦听器。正如您在下面的示例中看到的,在注册选择侦听器期间,源视图部分的视图 ID 作为参数被提及。

  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 JFaceTableViewerand adds it as a selection provider to the workbench site. This code enables any UI selection changes in the TableViewerto propagate to the page and finally to the interested consumer views.

Listing 2. Setting up a selection provider

这种方法将消除对消费者视图的冗余回调,否则如果该视图被注册为非特定侦听器,则会发生这种情况。

清单 2 中的代码片段显示了createPartControl()创建JFaceTableViewer并将其作为选择提供程序添加到工作台站点的视图的方法。此代码使 中的任何 UI 选择更改TableViewer传播到页面,并最终传播到感兴趣的消费者视图。

清单 2. 设置选择提供程序

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)

您会在eclipse3.5RCP 教程中找到类似的方法(2010 年 2 月 4 日更新)

回答by OSezer

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.

EvenBroker (e4) 实现:使用事件代理在视图和插件之间发送消息(字符串)。

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 
}