Showing an action for a single file in the project explorer view only
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?
请先登录再写评论。
Marc, you're setting action's presentation to be visible only when the condition is satisfied. It never gets hidden. Try with:
Ah I see. Thanks a lot!