From 80f28f80ddcd7dc736af4043af46d7099ebf8f35 Mon Sep 17 00:00:00 2001 From: weisj Date: Wed, 15 Apr 2020 13:24:47 +0200 Subject: [PATCH] Added icons for text/image MIME types. --- .../ui/filechooser/DarkFileChooserUI.java | 80 +++++++++++++++++++ .../darklaf/ui/filechooser/DarkFilePane.java | 1 - .../properties/ui/fileChooser.properties | 2 + 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUI.java index cdccbf1c..b30bd269 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUI.java @@ -34,25 +34,49 @@ import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; +import javax.swing.filechooser.FileSystemView; +import javax.swing.filechooser.FileView; import javax.swing.plaf.ComponentUI; import java.awt.*; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; public class DarkFileChooserUI extends DarkFileChooserUIBridge { public static final String KEY_VIEW_TYPE = "viewType"; + private static final String MIME_TEXT = "text/"; + private static final String MIME_IMAGE = "image/"; public DarkFileChooserUI(final JFileChooser b) { super(b); } protected AlignedLabel filesOfTypeLabel; + protected BasicFileView fileView; + protected Icon textFileIcon; + protected Icon imageFileIcon; public static ComponentUI createUI(final JComponent c) { return new DarkFileChooserUI((JFileChooser) c); } + @Override + protected void installIcons(final JFileChooser fc) { + super.installIcons(fc); + textFileIcon = UIManager.getIcon("FileView.textFileIcon"); + imageFileIcon = UIManager.getIcon("FileView.imageFileIcon"); + } + + @Override + protected void uninstallIcons(final JFileChooser fc) { + super.uninstallIcons(fc); + textFileIcon = null; + imageFileIcon = null; + } + @Override public void installComponents(final JFileChooser fc) { fc.setBorder(new EmptyBorder(10, 10, 7, 10)); @@ -338,4 +362,60 @@ public class DarkFileChooserUI extends DarkFileChooserUIBridge { topMargin = 5; } } + + public void clearIconCache() { + getFileView().clearIconCache(); + } + + public BasicFileView getFileView() { + if (fileView == null) fileView = createFileView(); + return fileView; + } + + @Override + public FileView getFileView(final JFileChooser fc) { + return getFileView(); + } + + protected BasicFileView createFileView() { + return new DarkFileView(); + } + + protected class DarkFileView extends BasicFileView { + + public Icon getIcon(final File f) { + Icon icon = getCachedIcon(f); + if (icon != null) { + return icon; + } + icon = fileIcon; + if (f != null) { + FileSystemView fsv = getFileChooser().getFileSystemView(); + + if (fsv.isFloppyDrive(f)) { + icon = floppyDriveIcon; + } else if (fsv.isDrive(f)) { + icon = hardDriveIcon; + } else if (fsv.isComputerNode(f)) { + icon = computerIcon; + } else if (f.isDirectory()) { + icon = directoryIcon; + } else { + try { + String mimeType = Files.probeContentType(f.toPath()); + if (mimeType == null) mimeType = ""; + if (mimeType.startsWith(MIME_IMAGE)) { + icon = imageFileIcon; + } else if (mimeType.startsWith(MIME_TEXT)) { + icon = textFileIcon; + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + cacheIcon(f, icon); + return icon; + } + } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePane.java b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePane.java index ba001adc..e5df48c4 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePane.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePane.java @@ -47,7 +47,6 @@ import java.util.function.Supplier; public class DarkFilePane extends DarkFilePaneUIBridge { - public DarkFilePane(final FileChooserUIAccessor fileChooserUIAccessor) { super(fileChooserUIAccessor); } diff --git a/core/src/main/resources/com/github/weisj/darklaf/properties/ui/fileChooser.properties b/core/src/main/resources/com/github/weisj/darklaf/properties/ui/fileChooser.properties index 85b05736..c81e62e8 100644 --- a/core/src/main/resources/com/github/weisj/darklaf/properties/ui/fileChooser.properties +++ b/core/src/main/resources/com/github/weisj/darklaf/properties/ui/fileChooser.properties @@ -52,3 +52,5 @@ FileView.directoryIcon = files/folder.svg[themed] FileView.computerIcon = files/desktop.svg[themed] FileView.floppyDriveIcon = menu/save.svg[themed] FileView.hardDriveIcon = files/drive.svg[themed] +FileView.textFileIcon = files/text.svg[themed] +FileView.imageFileIcon = files/image.svg[themed]