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();
final JTree libraryTree = new JTree();
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();
@ -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<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.
*/
@ -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;

View File

@ -46,16 +46,19 @@ public class Album implements Comparable<Album>, IDBType, HasSongs {
@Transient
private static final int imageScaleToSize = 50;
@Transient
private static final LoadingCache<Album, Optional<ImageIcon>> albumArtCache = CacheBuilder.newBuilder()
private static final LoadingCache<Album, ImageIcon> albumArtCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.build(
new CacheLoader<Album, Optional<ImageIcon>>() {
new CacheLoader<Album, ImageIcon>() {
@Override
public Optional<ImageIcon> 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<Album>, IDBType, HasSongs {
* @return Album art.
*/
@Transient
public Optional<ImageIcon> 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<Album>, IDBType, HasSongs {
* @return Album art.
*/
@Transient
private static Optional<ImageIcon> 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<Path> 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;
}
}