diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2024-03-24 13:25:22 -0500 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2024-03-24 13:25:22 -0500 |
commit | ed53492243ecad8d975401a97f5b634328ad2c71 (patch) | |
tree | 8a744f46320710355a02c9b2c371602ce69aefec /src/main/java/sevenUnitsGUI/SearchBoxList.java | |
parent | c878761f737c90fc3fa1caedd48e2ee01637108f (diff) | |
parent | 91d51c3c49c4c0877483220ac0f12db4efab8f60 (diff) |
Release version 0.5.0 (merge into stable)
Diffstat (limited to 'src/main/java/sevenUnitsGUI/SearchBoxList.java')
-rw-r--r-- | src/main/java/sevenUnitsGUI/SearchBoxList.java | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/src/main/java/sevenUnitsGUI/SearchBoxList.java b/src/main/java/sevenUnitsGUI/SearchBoxList.java index 9b41601..8fba459 100644 --- a/src/main/java/sevenUnitsGUI/SearchBoxList.java +++ b/src/main/java/sevenUnitsGUI/SearchBoxList.java @@ -40,13 +40,13 @@ import javax.swing.JTextField; * @since v0.2.0 */ final class SearchBoxList<E> extends JPanel { - + /** * @since 2019-04-13 * @since v0.2.0 */ private static final long serialVersionUID = 6226930279415983433L; - + /** * The text to place in an empty search box. * @@ -54,7 +54,7 @@ final class SearchBoxList<E> extends JPanel { * @since v0.2.0 */ private static final String EMPTY_TEXT = "Search..."; - + /** * The color to use for an empty foreground. * @@ -62,24 +62,24 @@ final class SearchBoxList<E> extends JPanel { * @since v0.2.0 */ private static final Color EMPTY_FOREGROUND = new Color(192, 192, 192); - + // the components private final Collection<E> itemsToFilter; private final DelegateListModel<E> listModel; private final JTextField searchBox; private final JList<E> searchItems; - + private boolean searchBoxEmpty = true; - + // I need to do this because, for some reason, Swing is auto-focusing my // search box without triggering a focus // event. private boolean searchBoxFocused = false; - + private Predicate<E> customSearchFilter = o -> true; private final Comparator<E> defaultOrdering; private final boolean caseSensitive; - + /** * Creates an empty SearchBoxList * @@ -88,7 +88,7 @@ final class SearchBoxList<E> extends JPanel { public SearchBoxList() { this(List.of(), null, false); } - + /** * Creates the {@code SearchBoxList}. * @@ -98,7 +98,7 @@ final class SearchBoxList<E> extends JPanel { public SearchBoxList(final Collection<E> itemsToFilter) { this(itemsToFilter, null, false); } - + /** * Creates the {@code SearchBoxList}. * @@ -116,35 +116,35 @@ final class SearchBoxList<E> extends JPanel { this.itemsToFilter = new ArrayList<>(itemsToFilter); this.defaultOrdering = defaultOrdering; this.caseSensitive = caseSensitive; - + // create the components this.listModel = new DelegateListModel<>(new ArrayList<>(itemsToFilter)); this.searchItems = new JList<>(this.listModel); - + this.searchBox = new JTextField(EMPTY_TEXT); this.searchBox.setForeground(EMPTY_FOREGROUND); - + // add them to the panel this.add(this.searchBox, BorderLayout.PAGE_START); this.add(new JScrollPane(this.searchItems), BorderLayout.CENTER); - + // set up the search box this.searchBox.addFocusListener(new FocusListener() { @Override public void focusGained(final FocusEvent e) { SearchBoxList.this.searchBoxFocusGained(e); } - + @Override public void focusLost(final FocusEvent e) { SearchBoxList.this.searchBoxFocusLost(e); } }); - + this.searchBox.addCaretListener(e -> this.searchBoxTextChanged()); this.searchBoxEmpty = true; } - + /** * Adds an additional filter for searching. * @@ -155,7 +155,7 @@ final class SearchBoxList<E> extends JPanel { public void addSearchFilter(final Predicate<E> filter) { this.customSearchFilter = this.customSearchFilter.and(filter); } - + /** * Resets the search filter. * @@ -165,7 +165,7 @@ final class SearchBoxList<E> extends JPanel { public void clearSearchFilters() { this.customSearchFilter = o -> true; } - + /** * @return items available in search list, including items that are hidden by * the search filter @@ -174,7 +174,7 @@ final class SearchBoxList<E> extends JPanel { public Collection<E> getItems() { return Collections.unmodifiableCollection(this.itemsToFilter); } - + /** * @return this component's search box component * @since 2019-04-14 @@ -183,7 +183,7 @@ final class SearchBoxList<E> extends JPanel { public final JTextField getSearchBox() { return this.searchBox; } - + /** * @param searchText text to search for * @return a filter that filters out that text, based on this list's case @@ -198,7 +198,7 @@ final class SearchBoxList<E> extends JPanel { return item -> item.toString().toLowerCase() .contains(searchText.toLowerCase()); } - + /** * @return this component's list component * @since 2019-04-14 @@ -207,7 +207,7 @@ final class SearchBoxList<E> extends JPanel { public final JList<E> getSearchList() { return this.searchItems; } - + /** * @return index selected in item list, -1 if no selection * @since 2019-04-14 @@ -216,7 +216,7 @@ final class SearchBoxList<E> extends JPanel { public int getSelectedIndex() { return this.searchItems.getSelectedIndex(); } - + /** * @return value selected in item list * @since 2019-04-13 @@ -225,7 +225,7 @@ final class SearchBoxList<E> extends JPanel { public Optional<E> getSelectedValue() { return Optional.ofNullable(this.searchItems.getSelectedValue()); } - + /** * Re-applies the filters. * @@ -238,21 +238,21 @@ final class SearchBoxList<E> extends JPanel { final FilterComparator<E> comparator = new FilterComparator<>(searchText, this.defaultOrdering, this.caseSensitive); final Predicate<E> searchFilter = this.getSearchFilter(searchText); - + this.listModel.clear(); this.itemsToFilter.forEach(item -> { if (searchFilter.test(item)) { this.listModel.add(item); } }); - + // applies the custom filters this.listModel.removeIf(this.customSearchFilter.negate()); - + // sorts the remaining items this.listModel.sort(comparator); } - + /** * Runs whenever the search box gains focus. * @@ -267,7 +267,7 @@ final class SearchBoxList<E> extends JPanel { this.searchBox.setForeground(Color.BLACK); } } - + /** * Runs whenever the search box loses focus. * @@ -282,7 +282,7 @@ final class SearchBoxList<E> extends JPanel { this.searchBox.setForeground(EMPTY_FOREGROUND); } } - + /** * Runs whenever the text in the search box is changed. * <p> @@ -301,7 +301,7 @@ final class SearchBoxList<E> extends JPanel { final FilterComparator<E> comparator = new FilterComparator<>(searchText, this.defaultOrdering, this.caseSensitive); final Predicate<E> searchFilter = this.getSearchFilter(searchText); - + // initialize list with items that match the filter then sort this.listModel.clear(); this.itemsToFilter.forEach(string -> { @@ -309,14 +309,14 @@ final class SearchBoxList<E> extends JPanel { this.listModel.add(string); } }); - + // applies the custom filters this.listModel.removeIf(this.customSearchFilter.negate()); - + // sorts the remaining items this.listModel.sort(comparator); } - + /** * Resets the search box list's contents to the provided items, removing any * old items @@ -329,7 +329,7 @@ final class SearchBoxList<E> extends JPanel { this.itemsToFilter.addAll(newItems); this.reapplyFilter(); } - + /** * Manually updates the search box's item list. * |