(searchText,
this.defaultOrdering, this.caseSensitive);
final var 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.
*
* @param e focus event
* @since 2019-04-13
* @since v0.2.0
*/
private void searchBoxFocusGained(final FocusEvent e) {
this.searchBoxFocused = true;
if (this.searchBoxEmpty) {
this.searchBox.setText("");
this.searchBox.setForeground(Color.BLACK);
}
}
/**
* Runs whenever the search box loses focus.
*
* @param e focus event
* @since 2019-04-13
* @since v0.2.0
*/
private void searchBoxFocusLost(final FocusEvent e) {
this.searchBoxFocused = false;
if (this.searchBoxEmpty) {
this.searchBox.setText(EMPTY_TEXT);
this.searchBox.setForeground(EMPTY_FOREGROUND);
}
}
/**
* Runs whenever the text in the search box is changed.
*
* Reapplies the search filter, and custom filters.
*
*
* @since 2019-04-14
* @since v0.2.0
*/
private void searchBoxTextChanged() {
if (this.searchBoxFocused) {
this.searchBoxEmpty = this.searchBox.getText().equals("");
}
final var searchText = this.searchBoxEmpty ? ""
: this.searchBox.getText();
final var comparator = new FilterComparator(searchText,
this.defaultOrdering, this.caseSensitive);
final var searchFilter = this.getSearchFilter(searchText);
// initialize list with items that match the filter then sort
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);
}
/**
* Resets the search box list's contents to the provided items, removing any
* old items
*
* @param newItems new items to put in list
* @since 2021-05-22
* @since v0.3.0
*/
public void setItems(Collection extends E> newItems) {
this.itemsToFilter.clear();
this.itemsToFilter.addAll(newItems);
this.reapplyFilter();
}
/**
* Manually updates the search box's item list.
*
* @since 2020-08-27
* @since v0.3.0
*/
public void updateList() {
this.searchBoxTextChanged();
}
}