Enable plugin's Actions only for project with specific type or SDK

Hello!

I'm looking for any way to enable\disable context-specific actions. I'm developing simple custom-language plugin and want to create something like "MyCustomLanguageProject". When user is inside it, popup menu on project tree will have a lot of options like "CreateMyCustomLanguageFile".
And I do not want to see them in another projects.

I think it will be some kind of option in project file or directory, or, it will be even better to bind to my custom SDK.

But haven't any idea, how to do it.

3 comments

There is no way to do so declaratively. You need to implement the AnAction.update() method and, in your implementation, check for your custom SDK (or anything else you might want to check) and call Presentation.setVisible(false) if your condition is not fulfilled.

0

Thank you!

So, I sholud do some runtime checks on every new action… It's sad.

0

When you install an action in your plugin.xml, it is application-wide. So you need to know what context it is being invoked under, so you will either have to check when it is invoked or before (or both).

There really isn't a great way to explain to the user "this menu item is only available when X" - from the context of the menu item. Maybe that would be a good feature. A help baloon or tooltip that explains why a Action's menu item is disabled.

Anyhow, here's a sample from my code:


public class GenerateLuaListingAction extends AnAction {     @Override     public void update(AnActionEvent e) {         e.getPresentation().setVisible(false);         e.getPresentation().setEnabled(false);         Project project = e.getData(LangDataKeys.PROJECT);         if (project != null) {             for (Module module : ModuleManager.getInstance(project).getModules()) {                 e.getPresentation().setVisible(true);                 Sdk luaSdk = LuaSdkType.findLuaSdk(module);                 if (luaSdk == null) continue;                 final String homePath = luaSdk.getHomePath();                 if (homePath == null) continue;                 if (LuaSdkType.getByteCodeCompilerExecutable(homePath).exists()) {                     e.getPresentation().setEnabled(true);                     break;                 }             }         }     }

0

Please sign in to leave a comment.