HandlerUtil for isEnabled of handler [message #1834917] |
Fri, 20 November 2020 08:46  |
Eclipse User |
|
|
|
Hello everyone,
I'm facing quite simple situation. I'm having the action handler (org.eclipse.core.commands.IHandler2, in particular the org.eclipse.core.commands.AbstractHandler). This handler does something with the currently opened editor.
Thus, I need to obtain the currently opened editor. This can be done simply in the execute method by the org.eclipse.ui.handlers.HandlerUtil class:
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
FooEditor fooEditor = (FooEditor) editor;
// ...
}
But I need to acess the current editor in the isEnabled method as well (the action is enabled only under the specific conditions including both the internal state of my business logic and also the current state (and selection) of the current editor):
@Override
public boolean isEnabled() {
IEditorPart editor = HandlerUtil.getActiveEditor(HERE); // <- ???
FooEditor fooEditor = (FooEditor) editor;
// ...
}
This is not possible, since the HandlerUtil class requires the instance of the ExecutionEvent to be passed.
Thus, is there any preffered way, how to obtain the current editor instance? All I can think about is:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()
which is quite ugly. And I'm not sure whether it would be even working in some edge cases, like when there is no window open.
|
|
|
Re: HandlerUtil for isEnabled of handler [message #1849336 is a reply to message #1834917] |
Wed, 12 January 2022 17:19  |
Eclipse User |
|
|
|
Yes, this is right. There are other ways but this is fine.
> which is quite ugly.
meh
> And I'm not sure whether it would be even working in some edge cases, like when there is no window open.
If there is no window open how could your handler work? You could build in some null checks if it really bothers you.
|
|
|
Powered by
FUDForum. Page generated in 0.43572 seconds