Added library search. Allows searching for song titles containing input text.
This commit is contained in:
parent
e08eef404a
commit
a8841b210b
5
pom.xml
5
pom.xml
@ -109,6 +109,11 @@
|
|||||||
<version>4.0</version>
|
<version>4.0</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.swinglabs.swingx</groupId>
|
||||||
|
<artifactId>swingx-all</artifactId>
|
||||||
|
<version>1.6.5-1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
@ -9,6 +9,7 @@ import musicplayer.model.Artist;
|
|||||||
import musicplayer.model.HasSongs;
|
import musicplayer.model.HasSongs;
|
||||||
import musicplayer.model.Song;
|
import musicplayer.model.Song;
|
||||||
import musicplayer.playlist.IPlaylist;
|
import musicplayer.playlist.IPlaylist;
|
||||||
|
import org.jdesktop.swingx.JXTextField;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.tree.DefaultMutableTreeNode;
|
import javax.swing.tree.DefaultMutableTreeNode;
|
||||||
@ -33,8 +34,10 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
|
|||||||
static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode();
|
static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode();
|
||||||
private final AtomicBoolean libraryUpdating = new AtomicBoolean(false);
|
private final AtomicBoolean libraryUpdating = new AtomicBoolean(false);
|
||||||
private final JComboBox<String> libraryDisplayType = new JComboBox<>();
|
private final JComboBox<String> libraryDisplayType = new JComboBox<>();
|
||||||
|
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.
|
||||||
@ -68,7 +71,17 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
|
|||||||
refreshLibrary();
|
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 -> {
|
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();
|
||||||
Collections.sort(x);
|
List<Song> x1 = x.parallelStream().filter(song -> song.getTitle().contains(filterValue)).collect(Collectors.toList());
|
||||||
x.forEach(y -> addNodeToTreeModel(model, parentNode, new DefaultMutableTreeNode(y)));
|
Collections.sort(x1);
|
||||||
|
x1.forEach(y -> addNodeToTreeModel(model, parentNode, new DefaultMutableTreeNode(y)));
|
||||||
libraryTree.setModel(model);
|
libraryTree.setModel(model);
|
||||||
});
|
});
|
||||||
if (!dbQuery.isPresent())
|
if (!dbQuery.isPresent())
|
||||||
@ -125,9 +139,12 @@ 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());
|
||||||
|
if(!filteredSongs.isEmpty()) {
|
||||||
DefaultMutableTreeNode outerNode = new DefaultMutableTreeNode(y);
|
DefaultMutableTreeNode outerNode = new DefaultMutableTreeNode(y);
|
||||||
addNodeToTreeModel(model, parentNode, outerNode);
|
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);
|
libraryTree.setModel(model);
|
||||||
});
|
});
|
||||||
@ -172,6 +189,9 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
|
|||||||
if (libraryTree.getModel() == model) {
|
if (libraryTree.getModel() == model) {
|
||||||
showEmpty();
|
showEmpty();
|
||||||
}
|
}
|
||||||
|
else if(libraryTree.getModel().getChildCount(libraryTree.getModel().getRoot()) == 0){
|
||||||
|
showEmpty();
|
||||||
|
}
|
||||||
}, "libraryRefresh");
|
}, "libraryRefresh");
|
||||||
populateThread.start();
|
populateThread.start();
|
||||||
}
|
}
|
||||||
@ -220,7 +240,8 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
|
|||||||
if (selectedItem instanceof Song) {
|
if (selectedItem instanceof Song) {
|
||||||
playlist.addSong((Song) selectedItem);
|
playlist.addSong((Song) selectedItem);
|
||||||
} else if (selectedItem instanceof HasSongs) {
|
} 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) {
|
} catch (NullPointerException ignored) {
|
||||||
|
Loading…
Reference in New Issue
Block a user