Skip to content

Commit

Permalink
Refactor preferences usage in SearchTextField
Browse files Browse the repository at this point in the history
SearchTextField's preferences usage has been refactored. Before, preferences were being accessed by storing an ID and using a Boolean property to decide if the history should be stored. Now, this information is accessed directly by storing a Preferences object. This change simplifies the process of handling preferences and makes the code more concise.
  • Loading branch information
dlemmermann committed May 10, 2024
1 parent 3083135 commit fa2be51
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.time.LocalTime;
import java.util.List;
import java.util.prefs.Preferences;

public class SearchTextFieldApp extends Application {

Expand All @@ -27,13 +28,10 @@ public class SearchTextFieldApp extends Application {
public void start(Stage primaryStage) throws Exception {

field1 = new SearchTextField();
field1.setStoringHistory(true);
field1.setPreferencesId("standard-field");
field1.setCellFactory(param -> new RemovableListCell<>((listView, item) -> field1.removeHistory(item)));
field1.setPreferences(Preferences.userNodeForPackage(SearchTextFieldApp.class).node("field1"));

SearchTextField field2 = new SearchTextField(true);
field2.setStoringHistory(true);
field2.setPreferencesId("round-field");
field2.setPreferences(Preferences.userNodeForPackage(SearchTextFieldApp.class).node("field2"));

Label label = new Label("Max History Size:");
Spinner<Integer> maxHistorySizeSpinner = new Spinner<>(5, 50, 10, 5);
Expand Down
65 changes: 16 additions & 49 deletions gemsfx/src/main/java/com/dlsc/gemsfx/SearchTextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class SearchTextField extends CustomTextField {
private static final int DEFAULT_MAX_HISTORY_SIZE = 30;
private static final boolean ENABLE_HISTORY_POPUP = true;
private static final boolean DEFAULT_ADD_HISTORY_ON_ENTER = true;

private static final PseudoClass DISABLED_POPUP_PSEUDO_CLASS = PseudoClass.getPseudoClass("disabled-popup");
private static final PseudoClass HISTORY_POPUP_SHOWING_PSEUDO_CLASS = PseudoClass.getPseudoClass("history-popup-showing");

Expand Down Expand Up @@ -90,25 +91,21 @@ public SearchTextField(boolean round) {
addEventHandlers();
addPropertyListeners();

setCellFactory(param -> new RemovableListCell<>((listView, item) -> removeHistory(item)));

getUnmodifiableHistory().addListener((Observable it) -> {
if (isStoringHistory()) {
String id = getPreferencesId();
if (StringUtils.isNotBlank(id)) {
storeHistory();
} else {
throw new UnsupportedOperationException("Cannot store history, the preferences ID is empty");
}
if (getPreferences() != null) {
storeHistory();
}
});

InvalidationListener loadHistoryListener = it -> {
if (isStoringHistory()) {
if (getPreferences() != null) {
loadHistory();
}
};

storingHistoryProperty().addListener(loadHistoryListener);
preferencesIdProperty().addListener(loadHistoryListener);
preferencesProperty().addListener(loadHistoryListener);
}

private void storeHistory() {
Expand All @@ -125,14 +122,6 @@ private void loadHistory() {
}
}

private Preferences getPreferences() {
String preferencesId = getPreferencesId();
if (StringUtils.isNotBlank(preferencesId)) {
return Preferences.userNodeForPackage(SearchTextField.class).node(preferencesId);
}
return null;
}

private void addEventHandlers() {
// On Action event, add the text to the history
addEventHandler(ActionEvent.ANY, e -> {
Expand Down Expand Up @@ -454,45 +443,23 @@ public final ReadOnlyBooleanProperty historyPopupShowingProperty() {
return historyPopupShowing.getReadOnlyProperty();
}

private final StringProperty preferencesId = new SimpleStringProperty(this, "preferencesId");

public final String getPreferencesId() {
return preferencesId.get();
}
private final ObjectProperty<Preferences> preferences = new SimpleObjectProperty<>(this, "preferences");

/**
* Stores an ID for the field used for persisting the search history.
* Stores a preferences object that will be used for persisting the search history of the field.
*
* @return the preferences id for the field
* @return the preferences used for persisting the search history
*/
public final StringProperty preferencesIdProperty() {
return preferencesId;
public final ObjectProperty<Preferences> preferencesProperty() {
return preferences;
}

public final void setPreferencesId(String id) {
this.preferencesId.set(id);
}

// storing size

private final BooleanProperty storingHistory = new SimpleBooleanProperty(this, "storingHistory", false);

public final boolean isStoringHistory() {
return storingHistory.get();
}

/**
* Determines if the field's search history will be stored in the user preferences, so that the
* items will appear automatically next time the application is run.
*
* @return true if the search history of the field will be persisted
*/
public final BooleanProperty storingHistoryProperty() {
return storingHistory;
public final Preferences getPreferences() {
return preferences.get();
}

public final void setStoringHistory(boolean storingHistory) {
this.storingHistory.set(storingHistory);
public final void setPreferences(Preferences preferences) {
this.preferences.set(preferences);
}

/**
Expand Down

0 comments on commit fa2be51

Please sign in to comment.