diff --git a/pom.xml b/pom.xml index 67b7411..e30ddd1 100644 --- a/pom.xml +++ b/pom.xml @@ -109,6 +109,11 @@ 4.0 compile + + org.swinglabs.swingx + swingx-all + 1.6.5-1 + diff --git a/src/main/java/musicplayer/library/JTreeLibrary.java b/src/main/java/musicplayer/library/JTreeLibrary.java index 88eb429..872b84c 100644 --- a/src/main/java/musicplayer/library/JTreeLibrary.java +++ b/src/main/java/musicplayer/library/JTreeLibrary.java @@ -9,6 +9,7 @@ import musicplayer.model.Artist; import musicplayer.model.HasSongs; import musicplayer.model.Song; import musicplayer.playlist.IPlaylist; +import org.jdesktop.swingx.JXTextField; import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; @@ -33,8 +34,10 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private final AtomicBoolean libraryUpdating = new AtomicBoolean(false); private final JComboBox libraryDisplayType = new JComboBox<>(); + private final JXTextField librarySearchField = new JXTextField(); final JTree libraryTree = new JTree(); private final Map libraryDisplayVariants = createDisplayVariantMap(); + private String filterValue = ""; /** * @return Map of display types for the library paired code to populate the library with data in the correct format. @@ -68,7 +71,17 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt refreshLibrary(); } }); - this.add(libraryDisplayType, BorderLayout.NORTH); + librarySearchField.setPrompt("Search... (Press Enter)"); + librarySearchField.addActionListener(e ->{ + filterValue = librarySearchField.getText(); + refreshLibrary(); + }); + + JPanel otherContainer = new JPanel(); + otherContainer.setLayout(new BoxLayout(otherContainer, BoxLayout.PAGE_AXIS)); + otherContainer.add(libraryDisplayType); + otherContainer.add(librarySearchField); + this.add(otherContainer, BorderLayout.NORTH); } /** @@ -104,8 +117,9 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt dbQuery.ifPresent(x -> { DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); - Collections.sort(x); - x.forEach(y -> addNodeToTreeModel(model, parentNode, new DefaultMutableTreeNode(y))); + List x1 = x.parallelStream().filter(song -> song.getTitle().contains(filterValue)).collect(Collectors.toList()); + Collections.sort(x1); + x1.forEach(y -> addNodeToTreeModel(model, parentNode, new DefaultMutableTreeNode(y))); libraryTree.setModel(model); }); if (!dbQuery.isPresent()) @@ -125,9 +139,12 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); Collections.sort(x); x.forEach(y -> { - DefaultMutableTreeNode outerNode = new DefaultMutableTreeNode(y); - addNodeToTreeModel(model, parentNode, outerNode); - y.getSongs().forEach(z -> addNodeToTreeModel(model, outerNode, new DefaultMutableTreeNode(z))); + List filteredSongs = y.getSongs().parallelStream().filter(song -> song.getTitle().contains(filterValue)).collect(Collectors.toList()); + if(!filteredSongs.isEmpty()) { + DefaultMutableTreeNode outerNode = new DefaultMutableTreeNode(y); + addNodeToTreeModel(model, parentNode, outerNode); + filteredSongs.forEach(z -> addNodeToTreeModel(model, outerNode, new DefaultMutableTreeNode(z))); + } }); libraryTree.setModel(model); }); @@ -172,6 +189,9 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt if (libraryTree.getModel() == model) { showEmpty(); } + else if(libraryTree.getModel().getChildCount(libraryTree.getModel().getRoot()) == 0){ + showEmpty(); + } }, "libraryRefresh"); populateThread.start(); } @@ -220,7 +240,8 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt if (selectedItem instanceof Song) { playlist.addSong((Song) selectedItem); } else if (selectedItem instanceof HasSongs) { - ((HasSongs) selectedItem).getSongs().forEach(playlist::addSong); + ((HasSongs) selectedItem).getSongs().stream().filter(song -> song.getTitle().contains + (filterValue)).forEach(playlist::addSong); } } } catch (NullPointerException ignored) {