【问题标题】:How to communicate between a RCP workbench toolbar and RCP views如何在 RCP 工作台工具栏和 RCP 视图之间进行通信
【发布时间】:2013-12-10 21:42:52
【问题描述】:

我有一个带有一些视图的 RCP 应用程序和一个为应用程序创建工具栏的 ActionBarAdvisor。工具栏的初始化如下:

public class ApplicationActionBarAdvisor extends ActionBarAdvisor 

...

protected void fillCoolBar(ICoolBarManager coolBar) 
{
    // Create new Toolbar
    IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());

    // Adds toolbar to perspective
    coolBar.add(toolbar);

    ...

    ActionContributionItem compareFilesCI = new ActionContributionItem(compareFiles);
    compareFilesCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
    toolbar.add(compareFilesCI);

    ...

}

此工具栏项的目的是切换 JFace 表的值的颜色。用户可以通过按下按钮来打开或关闭表格中的颜色。 但是告诉表格应该启用/禁用着色的最佳方法是什么?目前我已经这样做了:

public class ActionCompareFiles extends Action implements ISelectionListener, ActionFactory.IWorkbenchAction
{
...
public void run()
    {
        // Check if the button is enabled. When it is enabled, comparison should be performed 
        try
        {
            // Get the label values view 
            final ViewFileValues labelViewer = (ViewFileValues) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences()[3].getView(true);

            // Refresh the view
            labelViewer.setColoringValues(this.isChecked());
        }
        catch (Exception e)
        {
            System.out.println("Exception when searching the values view");
        }

    } 
...

按下按钮时,我会看到包含表格的视图。在这个视图类中有一个方法“SetColoringValues”,它通知表格列的标签提供者按钮被按下并更新表格。 这可行,但我不确定它是否是最好的解决方案......

我的第一个想法是简单地向按钮添加侦听器。按下按钮后,将通知听众。但这似乎不起作用,因为我没有得到 ActionBarAdvisor 对象,是 工具栏已创建。作为工作台窗口一部分的工具栏似乎没有 getter 方法。

这个问题的最佳解决方案是什么?

【问题讨论】:

    标签: java eclipse eclipse-rcp rcp


    【解决方案1】:

    1) 您最好使用 Eclipse RCP 命令框架(扩展点 *.commands、*.handlers、*.menu),而不是按照您的方式实现。乍一看可能看起来很麻烦,但实际上它更加结构化和简单。

    2) 获取视图实例的方式是完全错误的:您依赖于视图在页面中的索引为 3 的事实,而这从来没有得到保证。

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 1970-01-01
      • 2013-11-04
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多