Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » HandlerUtil for isEnabled of handler(There is extremelly usefull utility HandleUtil class. But what if I need its features in the handler's isEnabled method? )
HandlerUtil for isEnabled of handler [message #1834917] Fri, 20 November 2020 13:46 Go to next message
M. J. is currently offline M. J.Friend
Messages: 6
Registered: September 2018
Junior Member
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:

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

		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:

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 22:19 Go to previous message
Wim Jongman is currently offline Wim JongmanFriend
Messages: 493
Registered: July 2009
Senior Member

Yes, this is right. There are other ways but this is fine.

> 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.
If there is no window open how could your handler work? You could build in some null checks if it really bothers you.
Previous Topic:Building an Eclipse RCP application for Raspian aarch64 fails because of missing SWT classes
Next Topic:--add-opens in INI file not correctly used by launcher?
Goto Forum:

Current Time: Tue May 28 14:09:58 GMT 2024

Powered by FUDForum. Page generated in 0.03585 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top