Basic implementation of album art grid view.

This commit is contained in:
neviyn 2017-03-28 14:15:05 +01:00
parent 2a2bb28a26
commit 9666bf5a0a
2 changed files with 81 additions and 0 deletions

View File

@ -1,12 +1,15 @@
package musicplayer.swingui;
import com.google.inject.Inject;
import com.google.inject.Provider;
import musicplayer.StartPlayingException;
import musicplayer.callbacks.PlayerCallbackInterface;
import musicplayer.db.IDatabase;
import musicplayer.library.ILibrary;
import musicplayer.library.JTreeLibrary;
import musicplayer.player.IPlayer;
import musicplayer.playlist.IPlaylist;
import musicplayer.swingui.albumview.AlbumViewer;
import musicplayer.util.ConfigManager;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
@ -34,6 +37,8 @@ import java.util.stream.Collectors;
* Swing based music player UI.
*/
public class PlayerGUI extends JPanel implements PlayerCallbackInterface {
@Inject
private Provider<IDatabase> databaseProvider;
private final IPlaylist playlist;
private JSlider seekBar;
private final IPlayer player;
@ -269,6 +274,12 @@ public class PlayerGUI extends JPanel implements PlayerCallbackInterface {
helpMenu.add(menuItem);
menuBar.add(helpMenu);
menuItem = new JMenuItem("Album Tiles");
menuItem.addActionListener(e ->
new AlbumViewer(databaseProvider.get())
);
menuBar.add(menuItem);
return menuBar;
}

View File

@ -0,0 +1,70 @@
package musicplayer.swingui.albumview;
import musicplayer.db.IDatabase;
import musicplayer.model.Album;
import javax.swing.*;
import java.awt.*;
/**
* Window that shows a tiled view of all albums in the users' library.
*/
public class AlbumViewer {
private static final Dimension maxTileSize = new Dimension(80, 80);
private final IDatabase database;
/**
* Window that shows a tiled view of all albums in the users' library.
* @param database User song database
*/
public AlbumViewer(IDatabase database){
this.database = database;
JFrame frame = new JFrame("Album View");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout());
JList<Album> albumJList = createListArea();
frame.getContentPane().add(new JScrollPane(albumJList));
frame.setSize(800, 600);
frame.setVisible(true);
}
/**
* @return A JList containing all albums in the user database with art and name.
*/
private JList<Album> createListArea(){
DefaultListModel<Album> listModel = new DefaultListModel<>();
database.listAllT(Album.class).ifPresent(data -> data.forEach(listModel::addElement));
JList<Album> list = new JList<>(listModel);
list.setCellRenderer(new AlbumViewRenderer());
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);
return list;
}
private class AlbumViewRenderer extends JPanel implements ListCellRenderer<Album>{
private final JLabel textLabel = new JLabel();
private final JLabel iconLabel = new JLabel();
public AlbumViewRenderer(){
setOpaque(true);
setMinimumSize(maxTileSize);
setPreferredSize(maxTileSize);
setMaximumSize(maxTileSize);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setBorder(BorderFactory.createLineBorder(Color.black));
add(Box.createVerticalGlue()); // Vertical alignment
add(iconLabel); // Album art
add(textLabel); // Album name
add(Box.createVerticalGlue()); // Vertical alignment
}
@Override
public Component getListCellRendererComponent(JList<? extends Album> list, Album value, int index, boolean isSelected, boolean cellHasFocus) {
iconLabel.setIcon(value.getAlbumArt());
textLabel.setText(value.getName());
return this;
}
}
}