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.java122
1 files changed, 66 insertions, 56 deletions
diff --git a/src/org/unitConverter/converterGUI/SearchBoxList.java b/src/org/unitConverter/converterGUI/SearchBoxList.java
index 1995466..10ef589 100644
--- a/src/org/unitConverter/converterGUI/SearchBoxList.java
+++ b/src/org/unitConverter/converterGUI/SearchBoxList.java
@@ -36,13 +36,13 @@ import javax.swing.JTextField;
* @since v0.2.0
*/
final class SearchBoxList 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.
*
@@ -50,7 +50,7 @@ final class SearchBoxList extends JPanel {
* @since v0.2.0
*/
private static final String EMPTY_TEXT = "Search...";
-
+
/**
* The color to use for an empty foreground.
*
@@ -58,94 +58,92 @@ final class SearchBoxList extends JPanel {
* @since v0.2.0
*/
private static final Color EMPTY_FOREGROUND = new Color(192, 192, 192);
-
+
// the components
private final Collection<String> itemsToFilter;
private final DelegateListModel<String> listModel;
private final JTextField searchBox;
private final JList<String> 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
+
+ // 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<String> customSearchFilter = o -> true;
private final Comparator<String> defaultOrdering;
private final boolean caseSensitive;
-
+
/**
* Creates the {@code SearchBoxList}.
*
- * @param itemsToFilter
- * items to put in the list
+ * @param itemsToFilter items to put in the list
* @since 2019-04-14
*/
public SearchBoxList(final Collection<String> itemsToFilter) {
this(itemsToFilter, null, false);
}
-
+
/**
* Creates the {@code SearchBoxList}.
*
- * @param itemsToFilter
- * items to put in the list
- * @param defaultOrdering
- * default ordering of items after filtration (null=Comparable)
- * @param caseSensitive
- * whether or not the filtration is case-sensitive
+ * @param itemsToFilter items to put in the list
+ * @param defaultOrdering default ordering of items after filtration
+ * (null=Comparable)
+ * @param caseSensitive whether or not the filtration is case-sensitive
*
* @since 2019-04-13
* @since v0.2.0
*/
- public SearchBoxList(final Collection<String> itemsToFilter, final Comparator<String> defaultOrdering,
+ 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));
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.
*
- * @param filter
- * filter to add.
+ * @param filter filter to add.
* @since 2019-04-13
* @since v0.2.0
*/
public void addSearchFilter(final Predicate<String> filter) {
this.customSearchFilter = this.customSearchFilter.and(filter);
}
-
+
/**
* Resets the search filter.
*
@@ -155,7 +153,7 @@ final class SearchBoxList extends JPanel {
public void clearSearchFilters() {
this.customSearchFilter = o -> true;
}
-
+
/**
* @return this component's search box component
* @since 2019-04-14
@@ -164,11 +162,11 @@ final class SearchBoxList 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 sensitive setting
+ * @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
* @since v0.2.0
*/
@@ -176,9 +174,10 @@ final class SearchBoxList extends JPanel {
if (this.caseSensitive)
return string -> string.contains(searchText);
else
- return string -> string.toLowerCase().contains(searchText.toLowerCase());
+ return string -> string.toLowerCase()
+ .contains(searchText.toLowerCase());
}
-
+
/**
* @return this component's list component
* @since 2019-04-14
@@ -187,7 +186,7 @@ final class SearchBoxList extends JPanel {
public final JList<String> getSearchList() {
return this.searchItems;
}
-
+
/**
* @return index selected in item list
* @since 2019-04-14
@@ -196,7 +195,7 @@ final class SearchBoxList extends JPanel {
public int getSelectedIndex() {
return this.searchItems.getSelectedIndex();
}
-
+
/**
* @return value selected in item list
* @since 2019-04-13
@@ -205,7 +204,7 @@ final class SearchBoxList extends JPanel {
public String getSelectedValue() {
return this.searchItems.getSelectedValue();
}
-
+
/**
* Re-applies the filters.
*
@@ -213,29 +212,30 @@ final class SearchBoxList extends JPanel {
* @since v0.2.0
*/
public void reapplyFilter() {
- final String searchText = this.searchBoxEmpty ? "" : this.searchBox.getText();
- final FilterComparator comparator = new FilterComparator(searchText, this.defaultOrdering, this.caseSensitive);
+ final String searchText = this.searchBoxEmpty ? ""
+ : this.searchBox.getText();
+ 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 (searchFilter.test(string)) {
this.listModel.add(string);
}
});
-
+
// 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.
*
- * @param e
- * focus event
+ * @param e focus event
* @since 2019-04-13
* @since v0.2.0
*/
@@ -246,12 +246,11 @@ final class SearchBoxList extends JPanel {
this.searchBox.setForeground(Color.BLACK);
}
}
-
+
/**
* Runs whenever the search box loses focus.
*
- * @param e
- * focus event
+ * @param e focus event
* @since 2019-04-13
* @since v0.2.0
*/
@@ -262,7 +261,7 @@ final class SearchBoxList extends JPanel {
this.searchBox.setForeground(EMPTY_FOREGROUND);
}
}
-
+
/**
* Runs whenever the text in the search box is changed.
* <p>
@@ -276,10 +275,12 @@ final class SearchBoxList extends JPanel {
if (this.searchBoxFocused) {
this.searchBoxEmpty = this.searchBox.getText().equals("");
}
- final String searchText = this.searchBoxEmpty ? "" : this.searchBox.getText();
- final FilterComparator comparator = new FilterComparator(searchText, this.defaultOrdering, this.caseSensitive);
+ final String searchText = this.searchBoxEmpty ? ""
+ : this.searchBox.getText();
+ 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 -> {
@@ -287,11 +288,20 @@ final class SearchBoxList extends JPanel {
this.listModel.add(string);
}
});
-
+
// applies the custom filters
this.listModel.removeIf(this.customSearchFilter.negate());
-
+
// sorts the remaining items
this.listModel.sort(comparator);
}
+
+ /**
+ * Manually updates the search box's item list.
+ *
+ * @since 2020-08-27
+ */
+ public void updateList() {
+ this.searchBoxTextChanged();
+ }
}