Programmatic editor Action / Command confusion [message #316762] |
Mon, 18 June 2007 09:21  |
Eclipse User |
|
|
|
Originally posted by: xuiterlinden.be-value.nl
Hi,
I build an editor for which I would like to have a few accelerator keys
for the actions in the editor's actionbar.
The actions are created programmatically in an editor contributor class.
For each action I set the ActionDefinitionId to correspond with the Id
of a command as created using the commands extension point.
For each command I created a keybinding.
As I understood, in the setActiveEditor I should register my actions
like this:
TestAction t = new TestAction();
toolBarManager.add(t);
getActionBars().setGlobalActionHandler(t.getId(), t);
getActionBars().updateActionBars();
But somehow I can't get it to work. I also tried the same thing with a
declarative action (with an actiondelegate), and this works fine, but
that's not the way I can do this. For my editor, I have to create the
actions at runtime since they are dynamic.
Anyone ?
Thanks,
Xander
|
|
|
|
Re: Programmatic editor Action / Command confusion [message #316765 is a reply to message #316764] |
Mon, 18 June 2007 10:21  |
Eclipse User |
|
|
|
Paul Webster wrote:
>
> Instead you use org.eclipse.jface.commands.ActionHandler:
>
> IHandlerService service
> = (IHandlerService)getSite().getService(IHandlerService.class) ;
> service.activateHandler(t.getActionDefinitionId(),
> new ActionHandler(t));
>
> This only needs to be done once, you shouldn't be creating your actions
> on every setActiveEditor(*) but instead redirecting your actions to the
> current editor on every setActiveEditor(*) call. You can activate your
> editor actions on your init(*).
Let me amend :-)
If you programmatically create actions in your editor, you use the form
above.
If you are programmatically creating the actions in the
EditorActionBarContributor, you need to use the workbench window to get
the IHandlerService. If appropriate you can leave your handlers active
all the time, or you can scope it to the correct active editor type:
IHandlerService service = (IHandlerService)getPage()
.getWorkbenchWindow().getService(IHandlerService.class);
service.activateHandler(t.getActionDefinitionId(), new ActionHandler(t),
new ActiveEditorTypeExpression("editor.id"));
Where ActiveEditorTypeExpression should look something like:
class ActiveEditorTypeExpression extends Expression {
private String editorId;
public ActiveEditorTypeExpression(String id) {
editorId = id;
}
public void collectExpressionInfo(ExpressionInfo info) {
info.addVariableNameAccess(ISources.ACTIVE_EDITOR_ID_NAME);
}
public EvaluationResult evaluate(IEvaluationContext context)
throws CoreException {
final Object variable
= context.getVariable(ISources.ACTIVE_EDITOR_ID_NAME);
if (equals(editorId, variable)) {
return EvaluationResult.TRUE;
}
return EvaluationResult.FALSE;
}
public boolean equals(Object obj) {
if (obj instanceof ActiveEditorTypeExpression) {
return equals(editorId,
((ActiveEditorTypeExpression)obj).editorId);
}
return false;
}
public int hashCode() {
return editorId.hashCode();
}
}
You also have to de-activate the actions and dispose the ActionHandlers
in the EditorActionBarContributor dispose() call.
Later,
PW
|
|
|
Powered by
FUDForum. Page generated in 0.05863 seconds