From 9666bf5a0aeda1db73999a12357d341e8f6205d4 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 28 Mar 2017 14:15:05 +0100 Subject: [PATCH] Basic implementation of album art grid view. --- .../java/musicplayer/swingui/PlayerGUI.java | 11 +++ .../swingui/albumview/AlbumViewer.java | 70 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/main/java/musicplayer/swingui/albumview/AlbumViewer.java diff --git a/src/main/java/musicplayer/swingui/PlayerGUI.java b/src/main/java/musicplayer/swingui/PlayerGUI.java index 73dbd7c..4da2416 100644 --- a/src/main/java/musicplayer/swingui/PlayerGUI.java +++ b/src/main/java/musicplayer/swingui/PlayerGUI.java @@ -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 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; } diff --git a/src/main/java/musicplayer/swingui/albumview/AlbumViewer.java b/src/main/java/musicplayer/swingui/albumview/AlbumViewer.java new file mode 100644 index 0000000..427dedf --- /dev/null +++ b/src/main/java/musicplayer/swingui/albumview/AlbumViewer.java @@ -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 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 createListArea(){ + DefaultListModel listModel = new DefaultListModel<>(); + database.listAllT(Album.class).ifPresent(data -> data.forEach(listModel::addElement)); + JList 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{ + + 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 list, Album value, int index, boolean isSelected, boolean cellHasFocus) { + iconLabel.setIcon(value.getAlbumArt()); + textLabel.setText(value.getName()); + return this; + } + } +}