diff --git a/src/main/java/musicplayer/library/JTreeLibrary.java b/src/main/java/musicplayer/library/JTreeLibrary.java index 50f429a..6d875bf 100644 --- a/src/main/java/musicplayer/library/JTreeLibrary.java +++ b/src/main/java/musicplayer/library/JTreeLibrary.java @@ -42,8 +42,6 @@ public class JTreeLibrary extends ILibrary{ private final JXTextField librarySearchField = new JXTextField(); final JTree libraryTree = new JTree(); private final Map libraryDisplayVariants = createDisplayVariantMap(); - private final static Map albumArt = new ConcurrentHashMap<>(); - private ExecutorService albumArtCollectorPool; private final JPanel libraryPanel = new JPanel(); @@ -91,7 +89,6 @@ public class JTreeLibrary extends ILibrary{ otherContainer.add(libraryDisplayType); otherContainer.add(librarySearchField); libraryPanel.add(otherContainer, BorderLayout.NORTH); - getAlbumArt(); } /** @@ -229,7 +226,6 @@ public class JTreeLibrary extends ILibrary{ */ public void libraryUpdated() { libraryUpdating.set(false); - getAlbumArt(); refreshLibrary(); } @@ -267,25 +263,6 @@ public class JTreeLibrary extends ILibrary{ } } - private void getAlbumArt(){ - if(albumArtCollectorPool != null && !albumArtCollectorPool.isShutdown()) - albumArtCollectorPool.shutdownNow(); - albumArtCollectorPool = Executors.newFixedThreadPool(2); - Optional> dbQuery = database.listAllT(Album.class); - dbQuery.ifPresent(x -> x.forEach(y -> albumArtCollectorPool.submit(new Thread(() -> y.getAlbumArt().ifPresent - (art -> albumArt.put(y.getId(),art)))))); - // Only auto-refresh if we are actually showing albums - if(libraryDisplayType.getSelectedItem().toString().startsWith("Album")) { - new Thread(() -> { - try { - albumArtCollectorPool.awaitTermination(10, TimeUnit.SECONDS); - libraryTree.updateUI(); - } catch (InterruptedException ignored) { - } - }, "AlbumArtAutoRefresh").start(); - } - } - /** * Renderer for showing album art next to library album items. */ @@ -308,12 +285,7 @@ public class JTreeLibrary extends ILibrary{ label.setText(o.toString()); if (o instanceof Album) { Album album = (Album) o; - if(albumArt.containsKey(album.getId())){ - label.setIcon(albumArt.get(album.getId())); - } - else { - label.setIcon(missingIcon); - } + label.setIcon(album.getAlbumArt()); } } return label; diff --git a/src/main/java/musicplayer/model/Album.java b/src/main/java/musicplayer/model/Album.java index 3ddfcdc..02654ea 100644 --- a/src/main/java/musicplayer/model/Album.java +++ b/src/main/java/musicplayer/model/Album.java @@ -46,16 +46,19 @@ public class Album implements Comparable, IDBType, HasSongs { @Transient private static final int imageScaleToSize = 50; @Transient - private static final LoadingCache> albumArtCache = CacheBuilder.newBuilder() + private static final LoadingCache albumArtCache = CacheBuilder.newBuilder() .maximumSize(1000) .build( - new CacheLoader>() { + new CacheLoader() { @Override - public Optional load(Album album) throws Exception { + public ImageIcon load(Album album) throws Exception { return getAlbumArt(album); } } ); + @SuppressWarnings("ConstantConditions") + @Transient + private static final ImageIcon missingIcon = new ImageIcon(Album.class.getClassLoader().getResource("missing.gif")); protected Album() { } @@ -119,11 +122,11 @@ public class Album implements Comparable, IDBType, HasSongs { * @return Album art. */ @Transient - public Optional getAlbumArt(){ + public ImageIcon getAlbumArt(){ try { return albumArtCache.get(this); } catch (ExecutionException ignored) {} - return Optional.empty(); + return missingIcon; } /** @@ -132,23 +135,23 @@ public class Album implements Comparable, IDBType, HasSongs { * @return Album art. */ @Transient - private static Optional getAlbumArt(Album album){ + private static ImageIcon getAlbumArt(Album album){ Tag audioTags; File targetFile = album.songs.iterator().next().getSongFile(); Path baseDir = targetFile.getParentFile().toPath(); try { audioTags = AudioFileIO.read(targetFile).getTag(); if(audioTags != null && audioTags.getFirstArtwork() != null) { - return Optional.of(new ImageIcon(((Image)audioTags.getFirstArtwork().getImage()).getScaledInstance - (imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH))); + return new ImageIcon(((Image)audioTags.getFirstArtwork().getImage()).getScaledInstance + (imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH)); } } catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException ignored) { } try (DirectoryStream stream = Files.newDirectoryStream(baseDir, albumArtRegex)) { - return Optional.of(new ImageIcon(ImageIO.read(stream.iterator().next().toFile()).getScaledInstance - (imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH))); + return new ImageIcon(ImageIO.read(stream.iterator().next().toFile()).getScaledInstance + (imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH)); } catch (IOException | NoSuchElementException ignored) { } - return Optional.empty(); + return missingIcon; } }