Moved missing album art detection to Album for global support rather than just in JTreeLibrary.

This commit is contained in:
neviyn 2017-03-28 03:11:23 +01:00
parent b9517d995a
commit b4a1ce3de1
2 changed files with 15 additions and 40 deletions

View File

@ -42,8 +42,6 @@ public class JTreeLibrary extends ILibrary{
private final JXTextField librarySearchField = new JXTextField(); private final JXTextField librarySearchField = new JXTextField();
final JTree libraryTree = new JTree(); final JTree libraryTree = new JTree();
private final Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap(); private final Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap();
private final static Map<Long, ImageIcon> albumArt = new ConcurrentHashMap<>();
private ExecutorService albumArtCollectorPool;
private final JPanel libraryPanel = new JPanel(); private final JPanel libraryPanel = new JPanel();
@ -91,7 +89,6 @@ public class JTreeLibrary extends ILibrary{
otherContainer.add(libraryDisplayType); otherContainer.add(libraryDisplayType);
otherContainer.add(librarySearchField); otherContainer.add(librarySearchField);
libraryPanel.add(otherContainer, BorderLayout.NORTH); libraryPanel.add(otherContainer, BorderLayout.NORTH);
getAlbumArt();
} }
/** /**
@ -229,7 +226,6 @@ public class JTreeLibrary extends ILibrary{
*/ */
public void libraryUpdated() { public void libraryUpdated() {
libraryUpdating.set(false); libraryUpdating.set(false);
getAlbumArt();
refreshLibrary(); 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<List<Album>> 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. * Renderer for showing album art next to library album items.
*/ */
@ -308,12 +285,7 @@ public class JTreeLibrary extends ILibrary{
label.setText(o.toString()); label.setText(o.toString());
if (o instanceof Album) { if (o instanceof Album) {
Album album = (Album) o; Album album = (Album) o;
if(albumArt.containsKey(album.getId())){ label.setIcon(album.getAlbumArt());
label.setIcon(albumArt.get(album.getId()));
}
else {
label.setIcon(missingIcon);
}
} }
} }
return label; return label;

View File

@ -46,16 +46,19 @@ public class Album implements Comparable<Album>, IDBType, HasSongs {
@Transient @Transient
private static final int imageScaleToSize = 50; private static final int imageScaleToSize = 50;
@Transient @Transient
private static final LoadingCache<Album, Optional<ImageIcon>> albumArtCache = CacheBuilder.newBuilder() private static final LoadingCache<Album, ImageIcon> albumArtCache = CacheBuilder.newBuilder()
.maximumSize(1000) .maximumSize(1000)
.build( .build(
new CacheLoader<Album, Optional<ImageIcon>>() { new CacheLoader<Album, ImageIcon>() {
@Override @Override
public Optional<ImageIcon> load(Album album) throws Exception { public ImageIcon load(Album album) throws Exception {
return getAlbumArt(album); return getAlbumArt(album);
} }
} }
); );
@SuppressWarnings("ConstantConditions")
@Transient
private static final ImageIcon missingIcon = new ImageIcon(Album.class.getClassLoader().getResource("missing.gif"));
protected Album() { protected Album() {
} }
@ -119,11 +122,11 @@ public class Album implements Comparable<Album>, IDBType, HasSongs {
* @return Album art. * @return Album art.
*/ */
@Transient @Transient
public Optional<ImageIcon> getAlbumArt(){ public ImageIcon getAlbumArt(){
try { try {
return albumArtCache.get(this); return albumArtCache.get(this);
} catch (ExecutionException ignored) {} } catch (ExecutionException ignored) {}
return Optional.empty(); return missingIcon;
} }
/** /**
@ -132,23 +135,23 @@ public class Album implements Comparable<Album>, IDBType, HasSongs {
* @return Album art. * @return Album art.
*/ */
@Transient @Transient
private static Optional<ImageIcon> getAlbumArt(Album album){ private static ImageIcon getAlbumArt(Album album){
Tag audioTags; Tag audioTags;
File targetFile = album.songs.iterator().next().getSongFile(); File targetFile = album.songs.iterator().next().getSongFile();
Path baseDir = targetFile.getParentFile().toPath(); Path baseDir = targetFile.getParentFile().toPath();
try { try {
audioTags = AudioFileIO.read(targetFile).getTag(); audioTags = AudioFileIO.read(targetFile).getTag();
if(audioTags != null && audioTags.getFirstArtwork() != null) { if(audioTags != null && audioTags.getFirstArtwork() != null) {
return Optional.of(new ImageIcon(((Image)audioTags.getFirstArtwork().getImage()).getScaledInstance return new ImageIcon(((Image)audioTags.getFirstArtwork().getImage()).getScaledInstance
(imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH))); (imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH));
} }
} catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException ignored) { } catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException ignored) {
} }
try (DirectoryStream<Path> stream = Files.newDirectoryStream(baseDir, albumArtRegex)) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(baseDir, albumArtRegex)) {
return Optional.of(new ImageIcon(ImageIO.read(stream.iterator().next().toFile()).getScaledInstance return new ImageIcon(ImageIO.read(stream.iterator().next().toFile()).getScaledInstance
(imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH))); (imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH));
} catch (IOException | NoSuchElementException ignored) { } catch (IOException | NoSuchElementException ignored) {
} }
return Optional.empty(); return missingIcon;
} }
} }