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">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
<xy x="20" y="20" width="537" height="400"/>
</constraints>
<properties/>
<border type="none"/>
@ -63,26 +63,15 @@
</grid>
</children>
</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">
<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">
<preferred-size width="-1" height="20"/>
</grid>
</constraints>
<properties/>
<properties>
<floatable value="false"/>
</properties>
<border type="none"/>
<children>
<component id="a01e9" class="javax.swing.JButton" binding="playButton">
@ -105,15 +94,33 @@
<text value="&amp;Next"/>
</properties>
</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/>
<properties>
<majorTickSpacing value="20"/>
<minorTickSpacing value="10"/>
<paintTicks value="true"/>
<value value="100"/>
</properties>
</component>
<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>
<majorTickSpacing value="20"/>
<minorTickSpacing value="10"/>
<paintTicks value="true"/>
<value value="100"/>
</properties>
</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>
</toolbar>
<component id="e1626" class="javax.swing.JMenuBar" binding="menuBar">
@ -122,6 +129,19 @@
</constraints>
<properties/>
</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>
</grid>
</form>

View File

@ -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<Album, List<Song>> 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<Song> 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));
}
}