parasietje
parasietje

Reputation: 1539

Eclipse RCP Commands framework: I am lost

I have two separate views that both show multiple lists of values. Both views are open at the same time.

I have a generic command AddNewItemInList that I want to add to the Toolbar of both views. The command should add a new item to the list that currently has focus. The command should be disabled if the user has not yet selected a list.

I currently have used the following to execute the command.

public class MyViewPart extends ViewPart {
    [...]
    public Object getAdapter(Class clazz) {
       if(clazz.equals(List.class))
           return getListInFocus(); // can be null if no list in focus
       return null;
    }
}

public class AddNewItemInList extends AbstractHandler {
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        MyList list = HandlerUtil.getActivePart(event).getAdapter(MyList.class);
        list.add(new Item());
        return null;
    }
}

So far so good, but I have no clue how to enable or disable the Handler accordingly. I have read about the Eclipse Core Commands framework. I am very lost.

Answered questions: How can I create an expression to see if the getAdapter() method of the view does not return null? -> Answered below

How do I get the view for which this command was configured (and not the currently active view) ? -> I can add the viewId as a command parameter

Remaining questions: Should I configure two separate Handler instances in my plugin.xml (one for each view) ? -> The enabledWhen condition can only use the current evaluation context. It seems I cannot use any parameters provided by the command. This means a handler is either enabled or disabled depending on the state of the full workbench.

Upvotes: 3

Views: 530

Answers (2)

Alexey Romanov
Alexey Romanov

Reputation: 170713

How can I create an expression to see if the getAdapter() method of the view does not return null?

You can use custom property testers. It needs a lot of boilerplate and looks ugly, but it works.

EDIT: Actually, it's a lot simpler:

<with variable="activePart">
  <adapt type="com.foo.MyList"/>
</with>

Upvotes: 1

Grzegorz Gajos
Grzegorz Gajos

Reputation: 2363

Custom property testers have some issues and they are really ugly. They promise to do some improvements in version 4.0. You can try to implement something similar by yourself.

Upvotes: 1

Related Questions