Disabling action in update method of another action

This is a plugin so Java actions are available too.
I would like to hide the "New -> Package" action in the context menu depending upon where I right clicked in the project structure.

I need to add this action because I need to use a different handler rather than CreateDirectoryOrPackageHandler.java

<action id="MyNewDir" class="com.my.ij.actions.MyCreateDirectoryOrPackageAction">
<add-to-group group-id="NewGroup" anchor="after" relative-to-action="NewFile"/>
</action>

Since this action is not overridable, I cannot add this with id="NewDir" as that id already exists.

Now both the Java and my "New -> Package" option is available in the context menu.

To hide the Java option, I am (on some condition) trying to hide the action in this way -

AnAction javaPackageAction = ActionManager.getInstance().getAction("NewDir");

Presentation javaPresentation = javaPackageAction.getTemplatePresentation();

javaPresentation.setVisible(false);
javaPresentation.setEnabled(false);

But it is not working. The option is still there. I see 2 options for "packages"

I know it may not be the ideal way. But is there another way? Should the above code work? If yes, what am I missing here?

I have used the similar approach to hide the "Generated Constructor" option for Java in my custom language (My custome language extends Java so it is depenedent

5 comments

This doesn't work because actions usually call 'setEnabled' in their own 'update' methods, and this will overwrite changes made in templatePresentation. It isn't possible to disable an action externally if it doesn't provide a special API for that. It's possible to completely remove an action via ActionManager#unregisterAction method though, but it may cause problems if some other code depends on the presence of that action.

What do you want to change in 'New Package's behavior? Probably we can provide a special extension point for that.

0

Java 'New package' shows an error in the dialog of "Create new package" if the package name is a strong keyword.

In our plugin, we allow strong keywords as intermediate package names (just not the root package), so we do not want to show that error message if a package with strong keyword name (for ex. "if") is being created under another package (for ex. test)

 

So a new CreateDirectoryOrPackageHandler.java needs to be used for different error message conditions.

That would be great if you could provide a special extension point.

 

P.S.

The same problem occurred with 'Generate Constructor' action. In that case, I was able to disable visibility of "GenerateConstructor" action using the same approach and add my own generate constructor action with a different id. 

0

Please note that "Not a valid package name" is not an error, it's just a warning, you can still press 'OK' and it'll create a package. The full warning text is "Not a valid package name, it will not be possible to create a Java class inside"now, and I think it's right to show such a warning. You're reusing Java source roots and Java modules for your plugin, so users may create regular Java classes in your project and it look strange that you won't be able to create a Java class in such packages.

0

Yes, it is a warning, but we would not like to show the warning either.

We have another source root 'gsrc' like 'src' where we do not allow to create Java classes.

 

0

If you have special source roots for your files and don't allow to create Java classes under it why not create a special root type for it? In that case you won't need to disable Java 'New Class' and 'New Package' actions, also Java compiler won't process files under such roots.

0

Please sign in to leave a comment.