如何在RCP工作台工具栏和RCP视图之间进行通信



我有一个带有一些视图的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方法。

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

1)您应该更好地使用Eclipse RCP命令框架(扩展点*.commands、*.handlers、*.menu),而不是像您那样实现它。乍一看可能很麻烦,但实际上它更结构化、更直观。

2) 获取视图实例的方式是完全错误的:您依赖于这样一个事实,即视图在页面中的索引为3,而这是不合理的。

最新更新