Action Button enabled and visible when editor on specific file type.
Answered
i'm trying to make an action button only visible and enabled on "Dart programming language" files only.
and this is my update method:
@Override
public void update(@NotNull AnActionEvent e) {
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
assert psiFile != null;
this.getTemplatePresentation().setEnabledAndVisible(psiFile.getLanguage().isKindOf(DartLanguage.INSTANCE));
}
I tried to set it manually to false like this:
@Override
public void update(@NotNull AnActionEvent e) {
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
assert psiFile != null;
//this.getTemplatePresentation().setEnabledAndVisible(psiFile.getFileType().equals(DartFileType.INSTANCE));
this.getTemplatePresentation().setVisible(false);
this.getTemplatePresentation().setEnabled(false);
this.getTemplatePresentation().setEnabledAndVisible(false);
}
but the button is still showing:
so where could the problem be?
note: button (Reformat Code With 'Dart Format') is shown only when the editor is currently on a dart file, i tried to look into it, but it is using the exact same statement:
this.getTemplatePresentation().setEnabledAndVisible(psiFile.getFileType().equals(DartFileType.INSTANCE));
but with more complex conditions which are not my concern..
so what is wrong here?
Please sign in to leave a comment.
Hi,
You should modify AnActionEvent.presentation, not AnAction.templatePresentation.
Thanks you are right.