Enabling/Disabling a menu item

I have a menu item defined in plugin.xml with a action id, group-id, class.

I need to enable/disable it at certain events but I am not able to get its reference unless the user clicks on the menu item itself.

How can this be done?

7 comments
Comment actions Permalink

Edit : I do not want to store its reference in a static variable in the menu item class itself.

0
Comment actions Permalink

You can use ActionManager.getInstance().getAction("MyActionId") to get the action or use AnActionEvent.getPresentation().setVisible()/setEnabled() in AnAction.update() call to hide or disable action depending on some condition.

0
Comment actions Permalink

ActionManager.getInstance().getAction("MyActionId") - this will give me the AnAction reference. How to get the presentation associated with it?

0
Comment actions Permalink

AnAction can have multiple presentations (ex: being added to multiple toolbars simultaneously). They can be accessed with AnActionEvent.getPresentation() in update()/actionPerformed() actions.

You can modify presentation defaults using AnAction.getTemplatePresentation().

 

Could you clarify, why do you need it?

0
Comment actions Permalink

I need to enable the current presentation associated with one AnAction based on project closing. 

I am able to do it using update()  but that does not seem the most suitable way to do it as per my problem. 

 

So I was thinking of getting the presentation in the project listener and enabling it. 

0
Comment actions Permalink

> but that does not seem the most suitable way to do it as per my problem. 

Why do you think so? If the issue is with "too slow" reaction time, ActionToolbar.updateActionsImmediately() might help.

Is it possible to check currently opened projects during update(), instead of registering listener?

0
Comment actions Permalink

The thing is my problem is to edit the presentation on project closing but I am working with it in update()  which does not really have any relation with project opening or closing. It works when the parent menu or the menu itself is clicked. Secondly it is run repeatedly which is not my requirement. 

 

So basically its not about slow reaction time,  its just that the solution is not in line with the use case. 

0

Please sign in to leave a comment.