No longer unnecessarily storing search value.

This commit is contained in:
neviyn 2016-08-17 16:01:49 +01:00
parent a8841b210b
commit b91729004b

View File

@ -37,7 +37,6 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
private final JXTextField librarySearchField = new JXTextField(); private final JXTextField librarySearchField = new JXTextField();
final JTree libraryTree = new JTree(); final JTree libraryTree = new JTree();
private final Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap(); 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. * @return Map of display types for the library paired code to populate the library with data in the correct format.
@ -72,10 +71,7 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
} }
}); });
librarySearchField.setPrompt("Search... (Press Enter)"); librarySearchField.setPrompt("Search... (Press Enter)");
librarySearchField.addActionListener(e ->{ librarySearchField.addActionListener(e -> refreshLibrary());
filterValue = librarySearchField.getText();
refreshLibrary();
});
JPanel otherContainer = new JPanel(); JPanel otherContainer = new JPanel();
otherContainer.setLayout(new BoxLayout(otherContainer, BoxLayout.PAGE_AXIS)); otherContainer.setLayout(new BoxLayout(otherContainer, BoxLayout.PAGE_AXIS));
@ -117,7 +113,7 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
dbQuery.ifPresent(x -> { dbQuery.ifPresent(x -> {
DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode()); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot();
List<Song> x1 = x.parallelStream().filter(song -> song.getTitle().contains(filterValue)).collect(Collectors.toList()); List<Song> x1 = x.parallelStream().filter(song -> song.getTitle().contains(librarySearchField.getText())).collect(Collectors.toList());
Collections.sort(x1); Collections.sort(x1);
x1.forEach(y -> addNodeToTreeModel(model, parentNode, new DefaultMutableTreeNode(y))); x1.forEach(y -> addNodeToTreeModel(model, parentNode, new DefaultMutableTreeNode(y)));
libraryTree.setModel(model); libraryTree.setModel(model);
@ -139,7 +135,7 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot(); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) model.getRoot();
Collections.sort(x); Collections.sort(x);
x.forEach(y -> { x.forEach(y -> {
List<Song> filteredSongs = y.getSongs().parallelStream().filter(song -> song.getTitle().contains(filterValue)).collect(Collectors.toList()); List<Song> filteredSongs = y.getSongs().parallelStream().filter(song -> song.getTitle().contains(librarySearchField.getText())).collect(Collectors.toList());
if(!filteredSongs.isEmpty()) { if(!filteredSongs.isEmpty()) {
DefaultMutableTreeNode outerNode = new DefaultMutableTreeNode(y); DefaultMutableTreeNode outerNode = new DefaultMutableTreeNode(y);
addNodeToTreeModel(model, parentNode, outerNode); addNodeToTreeModel(model, parentNode, outerNode);
@ -241,7 +237,7 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
playlist.addSong((Song) selectedItem); playlist.addSong((Song) selectedItem);
} else if (selectedItem instanceof HasSongs) { } else if (selectedItem instanceof HasSongs) {
((HasSongs) selectedItem).getSongs().stream().filter(song -> song.getTitle().contains ((HasSongs) selectedItem).getSongs().stream().filter(song -> song.getTitle().contains
(filterValue)).forEach(playlist::addSong); (librarySearchField.getText())).forEach(playlist::addSong);
} }
} }
} catch (NullPointerException ignored) { } catch (NullPointerException ignored) {