Model built in background before being set as the view model, makes library loading much faster.

This commit is contained in:
neviyn 2016-02-24 14:42:08 +00:00
parent 9073242390
commit 1af5f72e1d
2 changed files with 72 additions and 43 deletions

View File

@ -3,7 +3,7 @@
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="20" y="20" width="500" height="400"/> <xy x="20" y="20" width="537" height="400"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
@ -63,26 +63,15 @@
</grid> </grid>
</children> </children>
</splitpane> </splitpane>
<component id="98c55" class="javax.swing.JSlider" binding="seekBar">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<inverted value="false"/>
<minorTickSpacing value="1"/>
<paintTicks value="false"/>
<snapToTicks value="false"/>
<value value="0"/>
<valueIsAdjusting value="false"/>
</properties>
</component>
<toolbar id="56df"> <toolbar id="56df">
<constraints> <constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"> <grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/> <preferred-size width="-1" height="20"/>
</grid> </grid>
</constraints> </constraints>
<properties/> <properties>
<floatable value="false"/>
</properties>
<border type="none"/> <border type="none"/>
<children> <children>
<component id="a01e9" class="javax.swing.JButton" binding="playButton"> <component id="a01e9" class="javax.swing.JButton" binding="playButton">
@ -105,8 +94,16 @@
<text value="&amp;Next"/> <text value="&amp;Next"/>
</properties> </properties>
</component> </component>
<component id="c8ecb" class="javax.swing.JSlider" binding="volumeSlider"> <grid id="9f75c" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints/> <constraints/>
<properties/>
<border type="none"/>
<children>
<component id="c8ecb" class="javax.swing.JSlider" binding="volumeSlider">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties> <properties>
<majorTickSpacing value="20"/> <majorTickSpacing value="20"/>
<minorTickSpacing value="10"/> <minorTickSpacing value="10"/>
@ -114,6 +111,16 @@
<value value="100"/> <value value="100"/>
</properties> </properties>
</component> </component>
<component id="9424d" class="javax.swing.JLabel" binding="volumeValue">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="100%"/>
</properties>
</component>
</children>
</grid>
</children> </children>
</toolbar> </toolbar>
<component id="e1626" class="javax.swing.JMenuBar" binding="menuBar"> <component id="e1626" class="javax.swing.JMenuBar" binding="menuBar">
@ -122,6 +129,19 @@
</constraints> </constraints>
<properties/> <properties/>
</component> </component>
<component id="98c55" class="javax.swing.JSlider" binding="seekBar">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<inverted value="false"/>
<minorTickSpacing value="1"/>
<paintTicks value="false"/>
<snapToTicks value="false"/>
<value value="0"/>
<valueIsAdjusting value="false"/>
</properties>
</component>
</children> </children>
</grid> </grid>
</form> </form>

View File

@ -25,6 +25,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
private JSlider volumeSlider; private JSlider volumeSlider;
private JMenuBar menuBar; private JMenuBar menuBar;
private JComboBox libraryDisplayType; private JComboBox libraryDisplayType;
private JLabel volumeValue;
private Player player = new Player(this); private Player player = new Player(this);
private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>());
@ -48,6 +49,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
playList.setModel(playlistTableModel); playList.setModel(playlistTableModel);
resetTree(); resetTree();
volumeSlider.setValue(player.getVolume()); volumeSlider.setValue(player.getVolume());
setVolumeValue(player.getVolume());
Thread seekBarUpdater = new Thread(() -> { Thread seekBarUpdater = new Thread(() -> {
boolean running = true; boolean running = true;
while (running) { while (running) {
@ -71,7 +73,10 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
libraryView.addMouseListener(mouseListener); libraryView.addMouseListener(mouseListener);
stopButton.addActionListener(e -> player.stop()); stopButton.addActionListener(e -> player.stop());
nextButton.addActionListener(e -> playNextSong()); 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()); playList.setComponentPopupMenu(createPlaylistPopup());
populateMenuBar(); populateMenuBar();
refreshLibrary(); refreshLibrary();
@ -90,9 +95,6 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
*/ */
private void resetTree() { private void resetTree() {
libraryView.removeAll(); 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. * @param libraryData Map of albums with a lists of associated songs.
*/ */
private void populateLibrary(Map<Album, List<Song>> libraryData) { private void populateLibrary(Map<Album, List<Song>> libraryData) {
resetTree(); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) libraryView.getModel().getRoot(); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot();
libraryData.forEach((k, v) -> { libraryData.forEach((k, v) -> {
DefaultMutableTreeNode albumNode = new DefaultMutableTreeNode(k); DefaultMutableTreeNode albumNode = new DefaultMutableTreeNode(k);
addNodeToTreeModel(parentNode, albumNode); addNodeToTreeModel(model, parentNode, albumNode);
new TreeSet<>(v).forEach(x -> addNodeToTreeModel(albumNode, new DefaultMutableTreeNode(x))); 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. * @param libraryData List of songs.
*/ */
private void populateLibrary(List<Song> libraryData) { private void populateLibrary(List<Song> libraryData) {
resetTree(); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) libraryView.getModel().getRoot(); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot();
libraryData.forEach(x -> addNodeToTreeModel(parentNode, new DefaultMutableTreeNode(x))); 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 parentNode Node that should be the parent of this node.
* @param node This node. * @param node This node.
*/ */
private void addNodeToTreeModel(DefaultMutableTreeNode parentNode, DefaultMutableTreeNode node) { private void addNodeToTreeModel(DefaultTreeModel model, DefaultMutableTreeNode parentNode, DefaultMutableTreeNode node) {
DefaultTreeModel libraryModel = (DefaultTreeModel) libraryView.getModel(); model.insertNodeInto(node, parentNode, parentNode.getChildCount());
libraryModel.insertNodeInto(node, parentNode, parentNode.getChildCount()); if (parentNode == model.getRoot()) {
if (parentNode == libraryModel.getRoot()) { model.nodeStructureChanged((TreeNode) model.getRoot());
libraryModel.nodeStructureChanged((TreeNode) libraryModel.getRoot());
} }
} }
@ -179,12 +181,15 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
} }
public void refreshLibrary(){ 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..."); DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("Refreshing Library...");
addNodeToTreeModel(parentNode, newNode); addNodeToTreeModel(model, parentNode, newNode);
libraryView.setModel(model);
Thread populateThread = new Thread(() -> { Thread populateThread = new Thread(() -> {
TreeMap treeMap = new TreeMap<>(Gateway.listAllSongsGroupedByAlbum().get()); TreeMap treeMap = new TreeMap<>(Gateway.listAllSongsGroupedByAlbum().get());
SwingUtilities.invokeLater(() -> populateLibrary(treeMap)); //SwingUtilities.invokeLater(() -> populateLibrary(treeMap));
populateLibrary(treeMap);
}); });
populateThread.start(); populateThread.start();
} }
@ -252,4 +257,8 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
return value; return value;
} }
private void setVolumeValue(int value){
volumeValue.setText(String.format("%d%%", value));
}
} }