summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/converterGUI/SearchBoxList.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/unitConverter/converterGUI/SearchBoxList.java')
-rw-r--r--src/org/unitConverter/converterGUI/SearchBoxList.java77
1 files changed, 67 insertions, 10 deletions
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<String> searchFilter = o -> true;
+ private Predicate<String> customSearchFilter = o -> true;
+ private final Comparator<String> defaultOrdering;
+ private final boolean caseSensitive;
+
+ public SearchBoxList(final Collection<String> itemsToFilter) {
+ this(itemsToFilter, null, false);
+ }
/**
* Creates the {@code SearchBoxList}.
*
* @since 2019-04-13
*/
- public SearchBoxList(final Collection<String> itemsToFilter) {
+ public SearchBoxList(final Collection<String> itemsToFilter, final Comparator<String> 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<String> 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<String> 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<String> 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<String> 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.
+ * <p>
+ * Reapplies the search filter, and custom filters.
+ * </p>
+ *
+ * @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<String> 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);