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.