Skip to content

Commit

Permalink
Feat #115: Restored FileTypeIconUtil using Executor and runReadAction
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanj committed Jun 25, 2024
1 parent b875a01 commit 20c6751
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devoxx.genie.ui.component;

import com.devoxx.genie.ui.listener.FileRemoveListener;
import com.devoxx.genie.ui.util.FileTypeIconUtil;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
Expand Down Expand Up @@ -38,8 +39,8 @@ public FileEntryComponent(Project project,

setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

// Icon fileTypeIcon = FileTypeIconUtil.getFileTypeIcon(project, virtualFile);
JButton fileNameButton = new JButton(virtualFile.getName());
Icon fileTypeIcon = FileTypeIconUtil.getFileTypeIcon(project, virtualFile);
JButton fileNameButton = new JButton(virtualFile.getName(), fileTypeIcon);

JButton fileNameBtn = createButton(fileNameButton);
fileNameBtn.addActionListener(e -> openFileWithSelectedCode(project, virtualFile));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.devoxx.genie.ui.panel;

import com.devoxx.genie.service.FileListManager;
import com.devoxx.genie.ui.renderer.FileListCellRenderer;
import com.devoxx.genie.ui.util.FileTypeIconUtil;
import com.intellij.ide.util.gotoByName.GotoFileModel;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ReadAction;
Expand Down Expand Up @@ -99,7 +99,10 @@ private static void debounceSearch(Project project,
debounceTimer.get().start();
}

private static void searchFiles(Project project, String searchText, DefaultListModel<VirtualFile> listModel, JBList<VirtualFile> resultList) {
private static void searchFiles(Project project,
String searchText,
DefaultListModel<VirtualFile> listModel,
JBList<VirtualFile> resultList) {
new Task.Backgroundable(project, "Searching Files", true) {
private final List<VirtualFile> foundFiles = new ArrayList<>();

Expand Down Expand Up @@ -155,4 +158,26 @@ private static void addSelectedFile(@NotNull JBList<VirtualFile> resultList) {
FileListManager.getInstance().addFile(selectedFile);
}
}

private static class FileListCellRenderer extends DefaultListCellRenderer {
private final Project project;

public FileListCellRenderer(Project project) {
this.project = project;
}

@Override
public Component getListCellRendererComponent(JList<?> list,
Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

if (value instanceof VirtualFile file) {
label.setIcon(FileTypeIconUtil.getFileTypeIcon(project, file));
label.setText(file.getName());
}

return label;
}
}
}
69 changes: 35 additions & 34 deletions src/main/java/com/devoxx/genie/ui/util/FileTypeIconUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,59 @@

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.PsiManager;
import com.intellij.util.concurrency.AppExecutorUtil;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static com.devoxx.genie.ui.util.DevoxxGenieIcons.*;

/**
* Utility class for getting the icon for a file type
*
* @link <a href="https://jetbrains.design/intellij/resources/icons_list">IDEA icons</a>
*/
public class FileTypeIconUtil {

private FileTypeIconUtil() {
}

/**
* Returns the icon for the file type, currently only Java files are supported
*
* @param project The current project
* @param virtualFile The file
* @return The icon
*/
public static Icon getFileTypeIcon(Project project, VirtualFile virtualFile) {
final Icon[] icon = new Icon[1];
ApplicationManager.getApplication().invokeAndWait(() -> {
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (psiFile instanceof PsiJavaFile javaFile) {
PsiClass[] psiClasses = javaFile.getClasses();
if (psiClasses.length > 0) {
// Assuming the file contains only one top-level class
PsiClass psiClass = psiClasses[0];
if (psiClass.isInterface()) {
icon[0] = InterfaceIcon;
} else if (psiClass.isEnum()) {
icon[0] = EnumIcon;
} else {
icon[0] = ClassIcon;
}
}
} else {
if (virtualFile.getFileType().getName().equals("UNKNOWN")) {
icon[0] = CodeSnippetIcon;
Future<Icon> iconFuture = AppExecutorUtil.getAppExecutorService().submit(() ->
ApplicationManager.getApplication().runReadAction((Computable<Icon>) () -> {
Icon interfaceIcon = getIcon(project, virtualFile);
if (interfaceIcon != null) return interfaceIcon;
return virtualFile.getFileType().getName().equals("UNKNOWN") ? CodeSnippetIcon : ClassIcon;
}));

try {
return iconFuture.get(100, TimeUnit.MILLISECONDS); // Adjust timeout as needed
} catch (InterruptedException | ExecutionException | TimeoutException e) {
// Log the error if needed
return ClassIcon; // Return a default icon in case of any error
}
}

private static @Nullable Icon getIcon(Project project, VirtualFile virtualFile) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (psiFile instanceof PsiJavaFile javaFile) {
PsiClass[] psiClasses = javaFile.getClasses();
if (psiClasses.length > 0) {
PsiClass psiClass = psiClasses[0];
if (psiClass.isInterface()) {
return InterfaceIcon;
} else if (psiClass.isEnum()) {
return EnumIcon;
} else {
icon[0] = ClassIcon;
return ClassIcon;
}
}
});
return icon[0];
}
return null;
}
}

0 comments on commit 20c6751

Please sign in to comment.