Added library search. Allows searching for song titles containing input text.

This commit is contained in:
neviyn 2016-08-17 15:48:31 +01:00
parent e08eef404a
commit a8841b210b
2 changed files with 33 additions and 7 deletions

View File

@ -109,6 +109,11 @@
<version>4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.swinglabs.swingx</groupId>
<artifactId>swingx-all</artifactId>
<version>1.6.5-1</version>
</dependency>
</dependencies>
<repositories>

View File

@ -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<String> libraryDisplayType = new JComboBox<>();
private final JXTextField librarySearchField = new JXTextField();
final JTree libraryTree = new JTree();
private final Map<String, Runnable> 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<Song> 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 -> {
List<Song> 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);
y.getSongs().forEach(z -> addNodeToTreeModel(model, outerNode, new DefaultMutableTreeNode(z)));
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) {