From 54b9f00faba367e1ef325c9d0bc75a848fadb906 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sun, 14 Apr 2019 15:39:38 -0400 Subject: The unit and prefix viewers now use SearchBoxList. --- .../unitConverter/converterGUI/SearchBoxList.java | 77 +++++++++++++++++++--- 1 file changed, 67 insertions(+), 10 deletions(-) (limited to 'src/org/unitConverter/converterGUI/SearchBoxList.java') diff --git a/src/org/unitConverter/converterGUI/SearchBoxList.java b/src/org/unitConverter/converterGUI/SearchBoxList.java index 7d3b748..35cc347 100644 --- a/src/org/unitConverter/converterGUI/SearchBoxList.java +++ b/src/org/unitConverter/converterGUI/SearchBoxList.java @@ -22,6 +22,7 @@ import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.function.Predicate; import javax.swing.JList; @@ -61,16 +62,25 @@ final class SearchBoxList extends JPanel { // event. private boolean searchBoxFocused = false; - private Predicate searchFilter = o -> true; + private Predicate customSearchFilter = o -> true; + private final Comparator defaultOrdering; + private final boolean caseSensitive; + + public SearchBoxList(final Collection itemsToFilter) { + this(itemsToFilter, null, false); + } /** * Creates the {@code SearchBoxList}. * * @since 2019-04-13 */ - public SearchBoxList(final Collection itemsToFilter) { + public SearchBoxList(final Collection itemsToFilter, final Comparator defaultOrdering, + final boolean caseSensitive) { super(new BorderLayout(), true); this.itemsToFilter = itemsToFilter; + this.defaultOrdering = defaultOrdering; + this.caseSensitive = caseSensitive; // create the components this.listModel = new DelegateListModel<>(new ArrayList<>(itemsToFilter)); @@ -108,7 +118,7 @@ final class SearchBoxList extends JPanel { * @since 2019-04-13 */ public void addSearchFilter(final Predicate filter) { - this.searchFilter = this.searchFilter.and(filter); + this.customSearchFilter = this.customSearchFilter.and(filter); } /** @@ -117,7 +127,44 @@ final class SearchBoxList extends JPanel { * @since 2019-04-13 */ public void clearSearchFilters() { - this.searchFilter = o -> true; + this.customSearchFilter = o -> true; + } + + /** + * @return this component's search box component + * @since 2019-04-14 + */ + 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 sensitive setting + * @since 2019-04-14 + */ + private Predicate getSearchFilter(final String searchText) { + if (this.caseSensitive) + return string -> string.contains(searchText); + else + return string -> string.toLowerCase().contains(searchText.toLowerCase()); + } + + /** + * @return this component's list component + * @since 2019-04-14 + */ + public final JList getSearchList() { + return this.searchItems; + } + + /** + * @return index selected in item list + * @since 2019-04-14 + */ + public int getSelectedIndex() { + return this.searchItems.getSelectedIndex(); } /** @@ -135,17 +182,18 @@ final class SearchBoxList extends JPanel { */ public void reapplyFilter() { final String searchText = this.searchBoxEmpty ? "" : this.searchBox.getText(); - final FilterComparator comparator = new FilterComparator(searchText); + final FilterComparator comparator = new FilterComparator(searchText, this.defaultOrdering, this.caseSensitive); + final Predicate searchFilter = this.getSearchFilter(searchText); this.listModel.clear(); this.itemsToFilter.forEach(string -> { - if (string.toLowerCase().contains(searchText.toLowerCase())) { + if (searchFilter.test(string)) { this.listModel.add(string); } }); // applies the custom filters - this.listModel.removeIf(this.searchFilter.negate()); + this.listModel.removeIf(this.customSearchFilter.negate()); // sorts the remaining items this.listModel.sort(comparator); @@ -181,23 +229,32 @@ final class SearchBoxList extends JPanel { } } + /** + * Runs whenever the text in the search box is changed. + *

+ * Reapplies the search filter, and custom filters. + *

+ * + * @since 2019-04-14 + */ private void searchBoxTextChanged() { if (this.searchBoxFocused) { this.searchBoxEmpty = this.searchBox.getText().equals(""); } final String searchText = this.searchBoxEmpty ? "" : this.searchBox.getText(); - final FilterComparator comparator = new FilterComparator(searchText); + final FilterComparator comparator = new FilterComparator(searchText, this.defaultOrdering, this.caseSensitive); + final Predicate searchFilter = this.getSearchFilter(searchText); // initialize list with items that match the filter then sort this.listModel.clear(); this.itemsToFilter.forEach(string -> { - if (string.toLowerCase().contains(searchText.toLowerCase())) { + if (searchFilter.test(string)) { this.listModel.add(string); } }); // applies the custom filters - this.listModel.removeIf(this.searchFilter.negate()); + this.listModel.removeIf(this.customSearchFilter.negate()); // sorts the remaining items this.listModel.sort(comparator); -- cgit v1.2.3