Showing an action for a single file in the project explorer view only

Answered

I'm trying to implement an AnActionEvent in my plugin that should show up in the context menu of the project explorer view for the file "pom.xml" only. So I overwrote the update function.

@Override
public void update(@NotNull AnActionEvent event) {
    super.update(event);
    String psiFileName = event.getData(CommonDataKeys.PSI_FILE).getName();
    event.getPresentation().setEnabledAndVisible(psiFileName.equals("pom.xml"));
}

But I get NullPointerExceptions because event.getData(CommonDataKeys.PSI_FILE) returns null occasionally.

I found out that this is the case if I call the context menu on directories or packages in the project explorer view. So I changed the code:

@Override
public void update(@NotNull AnActionEvent event) {
    super.update(event);
    PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE);
    if (psiFile != null && !psiFile.isDirectory()) {
        event.getPresentation().setEnabledAndVisible(psiFile.getName().equals("pom.xml"));
    }
}

Now the action appears in the context menues of packages and directories as well. But why? This is not what I intended. What am I missing here?

2 comments
Comment actions Permalink

Marc, you're setting action's presentation to be visible only when the condition is satisfied. It never gets hidden. Try with:

boolean visible = psiFile != null && !psiFile.isDirectory() && psiFile.getName().equals("pom.xml")
event.getPresentation().setEnabledAndVisible(visible);
0
Comment actions Permalink

Ah I see. Thanks a lot!

0

Please sign in to leave a comment.