Refreshing File Icons
I wrote an IconProvider which draws an overlay for some Java files and it is working in most cases. The user can enable/disable these icons by a setting in preferences. If the user changes this option, I need to refresh the icons but it does not work.
Here a small example how it looks:

I want to refesh the icons in project view and the tab headers of the open editors. This is what I tried:
ProjectView.getInstance(project).refresh();
AbstractProjectViewPane pane = ProjectView.getInstance(project).getCurrentProjectViewPane();
pane.updateFromRoot(true);
for(VirtualFile file : FileEditorManager.getInstance(project).getOpenFiles()) {
file.refresh(false, true);
FileEditorManagerEx.getInstanceEx(project).updateFilePresentation(file);
}
I see from log outputs that the Icon Provider is called and it returns the correct icon, but the view is not updating. I guess the icons are cached somewhere. After I type one character in the editor, the icons are refreshed as expected.
This is the simplified code of my icon provider (it simply adds an overlay over all icons of java files, if the setting is enabled):
public class IconProv extends IconProvider {
@Override
public @Nullable Icon getIcon(@NotNull PsiElement psiElement, int flags) {
if (Main.enable) {
if (psiElement instanceof PsiJavaFile) {
PsiJavaFile file = (PsiJavaFile) psiElement;
for(PsiClass cl : file.getClasses()) {
System.out.println("File: " + psiElement);
return handleClass(flags, cl);
}
}
if (psiElement instanceof PsiClass) {
System.out.println("Class: " + psiElement);
PsiClass psiClass = (PsiClass) psiElement;
return handleClass(flags, psiClass);
}
}
System.out.println("NONE: " + psiElement);
return null;
}
@NotNull
private LayeredIcon handleClass(int flags, PsiClass psiClass) {
LayeredIcon icon = new LayeredIcon(2);
icon.setIcon(PsiClassImplUtil.getClassIcon(flags, psiClass), 0, 0, 0);
icon.setIcon(IconLoader.getIcon("target.png", IconProv.class), 1, 0, 0);
return icon;
}
}
For simplicity of the example, I store the value of the setting in the static field "Main.enable"
Here the registration of the provider:
<iconProvider implementation="IconProv" order="first"/>
I uploaded a small samle project:
Upload id: 2021_08_05_Gtr6ApWiUK28g7iF (file: IconTest.zip)
Just use menu entries "Enable" and "Disable" under main entry "MyPlugin" to toggle the setting.
What do I have to do to refresh all icons in project view and in the tab headers? Am I doing something wrong or is it a bug?
请先登录再写评论。
From where do you call your refresh code? is it invoked in EDT?
It is called from a menu action. IntelliJ shows "AWT-EventQueue-0" as thread.
To be really sure, I now wrapped the call into
but with the same effect.
Sorry for delay, after some research it seems the current API does not work as expected for your case unfortunately..