Question

I have a RCP application with some views and an ActionBarAdvisor that creates a toolbar for the application. The toolbar is initialized like this:

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);

    ...

}

The purpose of this toolbar item is to switch the color of the values of a JFace table. The user can switch the coloring in the table on or off by pressing the button. But what is the best way to tell the table that coloring should be enabled/disabled? At the moment I've done it this way:

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");
        }

    } 
...

When pressing the button, I get the view that contains the table. Within this view class is a method 'SetColoringValues' that informs the label providers for the table columns that the button was pressed and updates the table. This works but I'm not sure if it is the best solution...

My first idea was to simple add listeners to the button. After pressing the button, the listeners will be informed. But this seems not to work because I don't get the ActionBarAdvisor object, were the toolbar was created. There seems to be no getter method for the toolbar that is part of the workbench window.

What is the best solution for this problem?

Was it helpful?

Solution

1) you should better use the Eclipse RCP commands framework (extension points *.commands, *.handlers, *.menu) instead of implementing this the way you do. It may look cumbersome at the first glance but in fact it is much more structured and straightforward.

2) the way you get an instance of the view is totally wrong: you rely upon the fact that the view has index of 3 in the page which is never warranted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top