From 1af5f72e1d02ec10774a79d933dbd48eec1de4e5 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Wed, 24 Feb 2016 14:42:08 +0000 Subject: [PATCH] Model built in background before being set as the view model, makes library loading much faster. --- src/main/java/musicplayer/PlayerGUI.form | 66 +++++++++++++++--------- src/main/java/musicplayer/PlayerGUI.java | 49 +++++++++++------- 2 files changed, 72 insertions(+), 43 deletions(-) diff --git a/src/main/java/musicplayer/PlayerGUI.form b/src/main/java/musicplayer/PlayerGUI.form index 04c97ea..b7a9585 100644 --- a/src/main/java/musicplayer/PlayerGUI.form +++ b/src/main/java/musicplayer/PlayerGUI.form @@ -3,7 +3,7 @@ - + @@ -63,26 +63,15 @@ - - - - - - - - - - - - - - + + + @@ -105,15 +94,33 @@ - + + - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + @@ -122,6 +129,19 @@ + + + + + + + + + + + + + diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index 224d350..edd931b 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -25,6 +25,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf private JSlider volumeSlider; private JMenuBar menuBar; private JComboBox libraryDisplayType; + private JLabel volumeValue; private Player player = new Player(this); private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); @@ -48,6 +49,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf playList.setModel(playlistTableModel); resetTree(); volumeSlider.setValue(player.getVolume()); + setVolumeValue(player.getVolume()); Thread seekBarUpdater = new Thread(() -> { boolean running = true; while (running) { @@ -71,7 +73,10 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf libraryView.addMouseListener(mouseListener); stopButton.addActionListener(e -> player.stop()); nextButton.addActionListener(e -> playNextSong()); - volumeSlider.addChangeListener(e -> player.setVolume(((JSlider)e.getSource()).getValue())); + volumeSlider.addChangeListener(e ->{ + player.setVolume(((JSlider)e.getSource()).getValue()); + setVolumeValue(player.getVolume()); + }); playList.setComponentPopupMenu(createPlaylistPopup()); populateMenuBar(); refreshLibrary(); @@ -90,9 +95,6 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf */ private void resetTree() { libraryView.removeAll(); - DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(); - DefaultTreeModel treeModel = new DefaultTreeModel(rootNode); - libraryView.setModel(treeModel); } /** @@ -100,13 +102,14 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf * @param libraryData Map of albums with a lists of associated songs. */ private void populateLibrary(Map> libraryData) { - resetTree(); - DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) libraryView.getModel().getRoot(); + DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); + DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); libraryData.forEach((k, v) -> { DefaultMutableTreeNode albumNode = new DefaultMutableTreeNode(k); - addNodeToTreeModel(parentNode, albumNode); - new TreeSet<>(v).forEach(x -> addNodeToTreeModel(albumNode, new DefaultMutableTreeNode(x))); + addNodeToTreeModel(model, parentNode, albumNode); + new TreeSet<>(v).forEach(x -> addNodeToTreeModel(model, albumNode, new DefaultMutableTreeNode(x))); }); + libraryView.setModel(model); } /** @@ -114,10 +117,10 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf * @param libraryData List of songs. */ private void populateLibrary(List libraryData) { - resetTree(); - DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) libraryView.getModel().getRoot(); - libraryData.forEach(x -> addNodeToTreeModel(parentNode, new DefaultMutableTreeNode(x))); - + DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); + DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); + libraryData.forEach(x -> addNodeToTreeModel(model, parentNode, new DefaultMutableTreeNode(x))); + libraryView.setModel(model); } /** @@ -125,11 +128,10 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf * @param parentNode Node that should be the parent of this node. * @param node This node. */ - private void addNodeToTreeModel(DefaultMutableTreeNode parentNode, DefaultMutableTreeNode node) { - DefaultTreeModel libraryModel = (DefaultTreeModel) libraryView.getModel(); - libraryModel.insertNodeInto(node, parentNode, parentNode.getChildCount()); - if (parentNode == libraryModel.getRoot()) { - libraryModel.nodeStructureChanged((TreeNode) libraryModel.getRoot()); + private void addNodeToTreeModel(DefaultTreeModel model, DefaultMutableTreeNode parentNode, DefaultMutableTreeNode node) { + model.insertNodeInto(node, parentNode, parentNode.getChildCount()); + if (parentNode == model.getRoot()) { + model.nodeStructureChanged((TreeNode) model.getRoot()); } } @@ -179,12 +181,15 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf } public void refreshLibrary(){ - DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) libraryView.getModel().getRoot(); + DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); + DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Refreshing Library..."); - addNodeToTreeModel(parentNode, newNode); + addNodeToTreeModel(model, parentNode, newNode); + libraryView.setModel(model); Thread populateThread = new Thread(() -> { TreeMap treeMap = new TreeMap<>(Gateway.listAllSongsGroupedByAlbum().get()); - SwingUtilities.invokeLater(() -> populateLibrary(treeMap)); + //SwingUtilities.invokeLater(() -> populateLibrary(treeMap)); + populateLibrary(treeMap); }); populateThread.start(); } @@ -252,4 +257,8 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf return value; } + + private void setVolumeValue(int value){ + volumeValue.setText(String.format("%d%%", value)); + } }