Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat #115: Restored FileTypeIconUtil using Executor and runReadAction #117

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}