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?

2 comments
Comment actions Permalink

Hi,

You should modify AnActionEvent.presentation, not AnAction.templatePresentation.

1
Comment actions Permalink

Thanks you are right.

0

Please sign in to leave a comment.