Skip to content

Commit

Permalink
Add history filtering and fix stylesheet reference issue
Browse files Browse the repository at this point in the history
Introduced filter functionality in HistoryManager to allow only the values passing the filter to be saved in history. In addition, corrected the misdirected reference to the user agent stylesheet in SearchTextField.
  • Loading branch information
leewyatt committed May 18, 2024
1 parent 5520446 commit a86c264
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gemsfx/src/main/java/com/dlsc/gemsfx/SearchTextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private StackPane createRightNode() {

@Override
public String getUserAgentStylesheet() {
return Objects.requireNonNull(SearchTextField2.class.getResource("search-text-field.css")).toExternalForm();
return Objects.requireNonNull(SearchTextField.class.getResource("search-text-field.css")).toExternalForm();
}

private ObjectProperty<Node> historyPlaceholder = new SimpleObjectProperty<>(this, "historyPlaceholder");
Expand Down
24 changes: 24 additions & 0 deletions gemsfx/src/main/java/com/dlsc/gemsfx/util/HistoryManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.dlsc.gemsfx.util;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.collections.ObservableList;

import java.util.List;
import java.util.function.Predicate;

/**
* The HistoryManager interface defines the standard operations to manage history storage
Expand Down Expand Up @@ -79,4 +81,26 @@ public interface HistoryManager<T> {
*/
void setMaxHistorySize(int maxHistorySize);

/**
* Returns the property that holds the filter used when adding items to the history.
* Only items that pass the filter will be added to the history.
*
* @return the property containing the filter
*/
ObjectProperty<Predicate<T>> filterProperty();

/**
* Sets a filter to be used when adding items to the history. Only items that pass the
* filter will be added to the history.
*
* @param filter The filter to apply.
*/
void setFilter(Predicate<T> filter);

/**
* Gets the current filter used for adding items to the history.
*
* @return The current filter.
*/
Predicate<T> getFilter();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -127,7 +128,7 @@ public final void set(List<T> history) {
* @param item the item to add
*/
public final void add(T item) {
if (item != null) {
if (item != null && getFilter().test(item)) {
history.remove(item);
history.add(0, item);
trimHistory();
Expand Down Expand Up @@ -203,6 +204,26 @@ public final void setMaxHistorySize(int maxHistorySize) {
maxHistorySizeProperty().set(maxHistorySize);
}

private final ObjectProperty<Predicate<T>> filter = new SimpleObjectProperty<>(this, "filter", it -> true);

/**
* Returns the property object for the filter used when adding items to the history.
* Only items that pass the filter will be added to the history.
*
* @return the property object for the filter
*/
public final ObjectProperty<Predicate<T>> filterProperty() {
return filter;
}

public final Predicate<T> getFilter() {
return filter.get();
}

public final void setFilter(Predicate<T> filter) {
this.filter.set(filter);
}

private final ObjectProperty<Preferences> preferences = new SimpleObjectProperty<>(this, "preferences");

/**
Expand Down Expand Up @@ -242,7 +263,7 @@ private void trimHistory() {
* @return the converted unique list of strings
*/
private List<T> convertToUniqueList(List<T> history) {
return history.stream().distinct().filter(Objects::nonNull).limit(Math.max(0, getMaxHistorySize())).toList();
return history.stream().distinct().filter(Objects::nonNull).filter(getFilter()).limit(Math.max(0, getMaxHistorySize())).toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dlsc.gemsfx.util;

import javafx.util.StringConverter;
import org.apache.commons.lang3.StringUtils;

/**
* Manages a history of string records using the Java Preferences API. This class specializes
Expand Down Expand Up @@ -43,10 +44,12 @@ public String fromString(String string) {

public StringHistoryManager() {
super(DEFAULT_STRING_CONVERTER);
setFilter(StringUtils::isNotEmpty);
}

public StringHistoryManager(String delimiter, String preferencesKey) {
super(delimiter, preferencesKey, DEFAULT_STRING_CONVERTER);
setFilter(StringUtils::isNotEmpty);
}

}

0 comments on commit a86c264

Please sign in to comment.