Determine if VirtualFile is in source path or add "new" action

Actually, I have a new action that I want to show up in the "new" sub-menu when right-clicking somewhere in the source path in the project view, like Class and Interface. But I don't want it to show up when clicking out of the source path, the same way that Class and Interface do. I've tried checking for visibility of the action "NewClass," but it's always true:

AnAction anAction = ActionManager.getInstance().getAction("NewClass");
LOG.info("anAction.getTemplatePresentation().isVisible() = " + anAction.getTemplatePresentation().isVisible());
LOG.info("anAction.isEnabledInModalContext() = " + anAction.isEnabledInModalContext());

Always prints:
anAction.getTemplatePresentation().isVisible() = true
anAction.isEnabledInModalContext() = false

regardless of where I click.

So to hack around this, I get the VirtualFile that was selected when the right-click happened, and I want to check if it's in the source path, but I'm not finding a way to do that.

FWIW, here's the line in plugin.xml that adds my action to the menu:
<add-to-group group-id="NewGroup" anchor="after" relative-to-action="NewClass"/>

Any thoughts would be appreciated.

--Erik

4 comments
Comment actions Permalink

ProjectRootManger.getInstance(project).getFileIndex() returns ProjectFileIndex
object with lot of useful methods.
In your case desired condition would look like fileIndex.isInSource(vFile)
&& !fileIndex.isInLibrarySource(vFile).

-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"

Actually, I have a new action that I want to show up in the "new"
sub-menu when right-clicking somewhere in the source path in the
project view, like Class and Interface. But I don't want it to show up
when clicking out of the source path, the same way that Class and
Interface do. I've tried checking for visibility of the action
"NewClass," but it's always true:

AnAction anAction = ActionManager.getInstance().getAction("NewClass");

LOG.info("anAction.getTemplatePresentation().isVisible() = " +
anAction.getTemplatePresentation().isVisible());

LOG.info("anAction.isEnabledInModalContext() = " +
anAction.isEnabledInModalContext());

Always prints: anAction.getTemplatePresentation().isVisible() = true
anAction.isEnabledInModalContext() = false

regardless of where I click.

So to hack around this, I get the VirtualFile that was selected when
the right-click happened, and I want to check if it's in the source
path, but I'm not finding a way to do that.

FWIW, here's the line in plugin.xml that adds my action to the menu:
<add-to-group group-id="NewGroup" anchor="after"
relative-to-action="NewClass"/>

Any thoughts would be appreciated.

--Erik




0
Comment actions Permalink

Great, that worked great (using ProjectRootManager, you had a typo). I'm now detecting where the click is happening, how do I make my action not show up? I thought it was something like:
this.getTemplatePresentation().setVisible(false);
this.getTemplatePresentation().setEnabled(false);

in the action's update(), but that doesn't seem to be doing anything. Do I need to just remove it from the parent DefaultActionGroup? I started implementing it this way and it got pretty ugly pretty fast.

Also, is there a not too difficult way to create a new class/interface, add an extends/implements, add a method or two, and write it out to the appropriate file? I see PsiDirectory.createClass/Interface, and PsiClass.add(). Can I just create and add the extends/implements/fields/methods using those methods? (I can figure out how to do this if it can be done like this). And how do I write out to the file? This stuff isn't that big a deal as I could just write Strings to a file or use Velocity or some such.

Thanks again.
--Erik

0
Comment actions Permalink

Erik wrote:

Great, that worked great (using ProjectRootManager, you had a typo). I'm now detecting where the click is happening, how do I make my action not show up? I thought it was something like:
this.getTemplatePresentation().setVisible(false);
this.getTemplatePresentation().setEnabled(false);


I thought it was getPresentation()?

Ciao,
Gordon

--
Gordon Tyler (Software Developer)
Quest Software <http://www.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: (416) 933-5046 | Fax: (416) 933-5001

0
Comment actions Permalink

I thought it was too, but there is no AnAction.getPresentation(). However, event.getPresentation().setVisible() does do what I want, thank you.

0

Please sign in to leave a comment.