【问题标题】:How to update the changes in two different eclipse views programmatically如何以编程方式更新两个不同 Eclipse 视图中的更改
【发布时间】:2018-04-29 14:18:54
【问题描述】:

我在 RCP 中创建了两个不同的视图。现在我想将在一个 Eclipse 视图中所做的更改自动反映在另一个视图中。

我对 RCP 插件开发完全陌生。谁能帮我编写实现上述要求的代码?

【问题讨论】:

  • @howlger:嗨,我不应该使用 e4 实现。如果您有任何其他想法,请告诉我
  • e4 表示 5 年前推出的 Eclipse 4.x。但当然,您可以在不使用 Eclipse 平台的任何东西的情况下实现它。

标签: eclipse eclipse-plugin eclipse-rcp jface


【解决方案1】:

我假设您使用 Eclipse 3 平台,据我所知,有三种连接工作台部件的方法(我的参考文献是“Eclipse Rich Client Platform”第二版 p255):

1) 要向其他部分公开结构更改,请使用选择服务:

要注册部分更改,请使用 ISelectionProvider,例如在您的第一个视图中:

public void createPartControl(Composite parent) {
  int style = SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL;
  viewer = new TableViewer(parent, style);
  getSite().setSelectionProvider(viewer);
  ...
}

要监听更改(不要忘记在不再需要时取消注册监听器)使用 ISelectionListener,例如在您的第二个视图中:

public class ChaptersView extends ViewPart {
  private TableViewer viewer;
  ISelectionListener listener = new ISelectionListener() {
     public void selectionChanged(IWorkbenchPart part, ISelection sel) {
        if (!(sel instanceof IStructuredSelection))
           return;
        IStructuredSelection ss = (IStructuredSelection) sel;
        Object o = ss.getFirstElement();
        if (o instanceof Book)
           viewer.setInput(ss.size()==1 ? o : null);
     }
  };
  public void createPartControl(Composite parent) {
     getSite().getPage().addSelectionListener(listener);
  }
  public void dispose() {
     getSite().getPage().removeSelectionListener(listener);
  }
}

这些例子来自here

2) 要监听关闭、打开或隐藏等部分事件,请使用部分监听器:

您必须在某处实现 IPartListener2,然后使用活动工作台窗口的活动页面将此侦听器添加到部件侦听器:

workbenchWindow().getActivePage().addPartListener(IPartListener2);

不要忘记在不再需要时取消注册监听器:

workbenchWindow().getActivePage().removePartListener(IPartListener2);

3) 您可以实现自己的通信机制,但它可能会在各部分之间引入过强的耦合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多