Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Correct enabled/disabled widget status at startup?(How to get menu and toolbar items to show the right enabled status at startup.)
Correct enabled/disabled widget status at startup? [message #1245477] Thu, 13 February 2014 21:54 Go to next message
R Shapiro is currently offline R ShapiroFriend
Messages: 386
Registered: June 2011
Senior Member
Currently I rely on isEnabled() to decide whether or not any given Command is enabled at any given time.
This works in general but not at startup. In that context the all my menu and tool-bar widgets show as enabled when in fact they're not. Once the user attempts to run one of these disabled commands that look enabled, the command of course fails and Eclipse shows the dialog saying "The chosen operation is not enabled". From that point on the widgets show the enabled status correctly.

In short everything works just as I would expect, except that the initial visible state of the widgets does not correspond to the status as returned from isEnabled().

How can I fix this? Even an ugly hack will do.

Note that I'm using Commands, not Actions.
Re: Correct enabled/disabled widget status at startup? [message #1257815 is a reply to message #1245477] Wed, 26 February 2014 20:35 Go to previous messageGo to next message
Erika Redmark is currently offline Erika RedmarkFriend
Messages: 22
Registered: January 2013
Junior Member
I'm not sure exactly how you have things set up, but would you be able to explicitly setEnabled(state) each command right before the menu appears? Perhaps the states are set initially without syncing with the UI.
Re: Correct enabled/disabled widget status at startup? [message #1258538 is a reply to message #1257815] Thu, 27 February 2014 13:39 Go to previous messageGo to next message
R Shapiro is currently offline R ShapiroFriend
Messages: 386
Registered: June 2011
Senior Member
Thanks for the reply.

I have an isEnabled() method on my handler base class that depends on selection and works fine in general: the menu items enable and disable exactly when I want them to. The problem is only at startup. All the menu items are initially enabled, which is not what I want. I want them to start out disabled. Of course isEnabled() itself is not useful here, since it's only run when the operation is executed -- too late for initialization.

The setEnabled() method for org.eclipse.core.commands.AbstractHandler doesn't take an enabled/disabled state parameter, it takes an 'evaluation context' Object. I played around with this some but never got it to do anything useful with respect to the initial enabled/disabled state. I think this is also only invoked on execution, like isEnabled(), so too late for setting the initial state.


If there's a hook that will allow me to set the enabled state "right before the menu appears", I don't know where it is. How would I do that?

[Updated on: Thu, 27 February 2014 14:00]

Report message to a moderator

Re: Correct enabled/disabled widget status at startup? [message #1258894 is a reply to message #1258538] Thu, 27 February 2014 21:49 Go to previous messageGo to next message
Erika Redmark is currently offline Erika RedmarkFriend
Messages: 22
Registered: January 2013
Junior Member
Unfortunately, all my code uses actions, and there is a clear point of entry when the view loads up for me to run setEnabled, so I don't know an off-my-head solution.

The logic which determines whether or not things are enabled/disabled, is that part of the 'enabledWhen' or 'activeWhen' parts of the command when you declare your command's extension point? Or are you trying to do so programatically somewhere else? Where exactly does your logic reside that sets enabled states after initialisation; is it only in the isEnabled overriden methods for your custom handler? If that's the case, I think you may need to add an 'enabledWhen' to the extension point to re-use that code and it might make it work on initialisation.

Disclaimer: I'm not an Eclipse developer so I don't know for sure.

[Updated on: Thu, 27 February 2014 21:55]

Report message to a moderator

Re: Correct enabled/disabled widget status at startup? [message #1259589 is a reply to message #1258894] Fri, 28 February 2014 15:14 Go to previous messageGo to next message
R Shapiro is currently offline R ShapiroFriend
Messages: 386
Registered: June 2011
Senior Member
Quote:
Unfortunately, all my code uses actions, and there is a clear point of entry when the view loads up for me to run setEnabled, so I don't know an off-my-head solution.


I started with Actions, but eventually the deprecation warnings got to me and I switched to Commands. Having gone through that conversion I'm hesitant to go back.

In the Commands API, at least as I'm using it, commands are defined by classes that implement org.eclipse.core.commands.IHandler. The menu/toolbar then consists of items each of which specifies a default handler class. All my handler classes extend a base class of my own, which in turn extends the core handler class AbstractHandler. I use this pattern because the same enabled/disabled check is used for every command -- either they're all enabled or all disabled, depending on the current selection.


Quote:
The logic which determines whether or not things are enabled/disabled, is that part of the 'enabledWhen' or 'activeWhen' parts of the command when you declare your command's extension point? Or are you trying to do so programatically somewhere else?


I don't see any extension points like the ones you mention. The only mechanism I can find is the isEnabled() method in IHandler. It's a simple and clean API and it works fine once the state has been initialized properly. But that's where I'm stuck -- initialization. IHandler also has an isHandled() method but it has the same initialization issues as isEnbled().


Quote:
Where exactly does your logic reside that sets enabled states after initialisation; is it only in the isEnabled overriden methods for your custom handler? If that's the case, I think you may need to add an 'enabledWhen' to the extension point to re-use that code and it might make it work on initialisation.


I know I need add something but unfortunately I don't see these extension points.



Re: Correct enabled/disabled widget status at startup? [message #1259811 is a reply to message #1259589] Fri, 28 February 2014 20:36 Go to previous messageGo to next message
Erika Redmark is currently offline Erika RedmarkFriend
Messages: 22
Registered: January 2013
Junior Member
I'm developing something for Eclipse Helios and up... in this environment Actions are not deprecated. If they are for 4.x development then I wasn't aware until now actually.

The best example I can find of what I mean is Vogella's tutorial, http://www.vogella.com/tutorials/EclipseCommandsAdvanced/article.html#commands_restrictions There is a warning above stating that the section is for Eclipse 3.x only, and the warning gives a link to a different tutorial which may contain what you are looking for. The pattern you talk of sounds valid to me; Unfortunately, I don't know Eclipse API well enough either, especially 4.x, so I'm afraid I can't help any further on why they aren't initialised properly. Sorry.

[Updated on: Fri, 28 February 2014 20:37]

Report message to a moderator

Re: Correct enabled/disabled widget status at startup? [message #1260608 is a reply to message #1259811] Sat, 01 March 2014 17:47 Go to previous message
R Shapiro is currently offline R ShapiroFriend
Messages: 386
Registered: June 2011
Senior Member
I hadn't seen the tutorial before. Thanks for the pointer.

My plugin has no views of its own, it only has a command menu. So I'm not sure the sample code in the tutorial will apply as is. But I can at least get at 'enabled when' now. Possibly the 'test' variant will work if I can figure out how supply my own test.
Previous Topic:NoClassDefFoundError for imported package
Next Topic:Adding Link/RadioButtons/Labels to Eclipse Plugin Preferences Page
Goto Forum:
  


Current Time: Thu Apr 25 08:19:29 GMT 2024

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

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

Back to the top