Skip to content

Commit

Permalink
Refactor code and add history on focus lost
Browse files Browse the repository at this point in the history
Renamed the property 'addHistoryOnEnter' to 'addingItemToHistoryOnEnter' for clarity. Also, added a new property 'addingItemToHistoryOnFocusLost' to handle text addition to history when the search text field loses focus. Updated the associated demo app to reflect these changes.
  • Loading branch information
dlemmermann committed May 10, 2024
1 parent 1e696f8 commit be5fd62
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.dlsc.gemsfx.demo;

import com.dlsc.gemsfx.RemovableListCell;
import com.dlsc.gemsfx.SearchTextField;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
Expand Down Expand Up @@ -47,8 +46,8 @@ public void start(Stage primaryStage) throws Exception {

CheckBox addHistoryOnActionBox = new CheckBox("Add History on Enter");
addHistoryOnActionBox.setSelected(true);
field1.addHistoryOnEnterProperty().bind(addHistoryOnActionBox.selectedProperty());
field2.addHistoryOnEnterProperty().bind(addHistoryOnActionBox.selectedProperty());
field1.addingItemToHistoryOnEnterProperty().bind(addHistoryOnActionBox.selectedProperty());
field2.addingItemToHistoryOnEnterProperty().bind(addHistoryOnActionBox.selectedProperty());

Button setHistoryButton = new Button("Set History");
setHistoryButton.setMaxWidth(Double.MAX_VALUE);
Expand Down
59 changes: 46 additions & 13 deletions gemsfx/src/main/java/com/dlsc/gemsfx/SearchTextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* such as a history of search terms, an optional history popup, and custom icons for search and clear operations.
* <p>
* By default, when the user presses the "enter" key (triggering the onAction event), the text is added to the history.
* This behavior can be disabled by setting the {@link #addHistoryOnEnterProperty()}.
* This behavior can be disabled by setting the {@link #addingItemToHistoryOnEnterProperty()}.
* <br>
* Additionally, history can be manually added based on user actions, such as after typing text and selecting an item
* from a ListView or TableView that displays results, or through other interactions, by calling the {@link #addHistory}
Expand All @@ -55,7 +55,8 @@ 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 boolean DEFAULT_ADDING_ITEM_TO_HISTORY_ON_ENTER = true;
private static final boolean DEFAULT_ADDING_ITEM_TO_HISTORY_ON_FOCUS_LOST = 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 @@ -99,6 +100,12 @@ public SearchTextField(boolean round) {

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

focusedProperty().addListener(it -> {
if (!isFocused() && isAddingItemToHistoryOnFocusLost()) {
addHistory(getText());
}
});

getUnmodifiableHistory().addListener((Observable it) -> {
if (getPreferences() != null) {
storeHistory();
Expand Down Expand Up @@ -135,7 +142,7 @@ private void loadHistory() {
private void addEventHandlers() {
// On Action event, add the text to the history
addEventHandler(ActionEvent.ANY, e -> {
if (getAddHistoryOnEnter()) {
if (isAddingItemToHistoryOnEnter()) {
addHistory(getText());
}
});
Expand Down Expand Up @@ -405,26 +412,52 @@ public final void setEnableHistoryPopup(boolean enableHistoryPopup) {
enableHistoryPopupProperty().set(enableHistoryPopup);
}

private BooleanProperty addHistoryOnEnter;
// add on enter

private BooleanProperty addingItemToHistoryOnEnter;

/**
* Indicates whether the text of the text field should be added to the history when the user presses the Enter key.
* Determines whether the text of the text field should be added to the history when the user presses the Enter key.
*
* @return true if the text should be added to the history on Enter, false otherwise
*/
public final BooleanProperty addHistoryOnEnterProperty() {
if (addHistoryOnEnter == null) {
addHistoryOnEnter = new SimpleBooleanProperty(this, "addHistoryOnEnter", DEFAULT_ADD_HISTORY_ON_ENTER);
public final BooleanProperty addingItemToHistoryOnEnterProperty() {
if (addingItemToHistoryOnEnter == null) {
addingItemToHistoryOnEnter = new SimpleBooleanProperty(this, "addingItemToHistoryOnEnter", DEFAULT_ADDING_ITEM_TO_HISTORY_ON_ENTER);
}
return addingItemToHistoryOnEnter;
}

public final boolean isAddingItemToHistoryOnEnter() {
return addingItemToHistoryOnEnter == null ? DEFAULT_ADDING_ITEM_TO_HISTORY_ON_ENTER : addingItemToHistoryOnEnter.get();
}

public final void setAddingItemToHistoryOnEnter(boolean addingItemToHistoryOnEnter) {
addingItemToHistoryOnEnterProperty().set(addingItemToHistoryOnEnter);
}

// add on focus lost

private BooleanProperty addingItemToHistoryOnFocusLost;

/**
* Determines whether the text of the text field should be added to the history when the field losses its focus.
*
* @return true if the text should be added to the history on focus lost, false otherwise
*/
public final BooleanProperty addingItemToHistoryOnFocusLostProperty() {
if (addingItemToHistoryOnFocusLost == null) {
addingItemToHistoryOnFocusLost = new SimpleBooleanProperty(this, "addingItemToHistoryOnFocusLost", DEFAULT_ADDING_ITEM_TO_HISTORY_ON_FOCUS_LOST);
}
return addHistoryOnEnter;
return addingItemToHistoryOnFocusLost;
}

public final boolean getAddHistoryOnEnter() {
return addHistoryOnEnter == null ? DEFAULT_ADD_HISTORY_ON_ENTER : addHistoryOnEnter.get();
public final boolean isAddingItemToHistoryOnFocusLost() {
return addingItemToHistoryOnFocusLost == null ? DEFAULT_ADDING_ITEM_TO_HISTORY_ON_FOCUS_LOST : addingItemToHistoryOnFocusLost.get();
}

public final void setAddHistoryOnEnter(boolean addHistoryOnEnter) {
addHistoryOnEnterProperty().set(addHistoryOnEnter);
public final void setAddingItemToHistoryOnFocusLost(boolean addingItemToHistoryOnFocusLost) {
addingItemToHistoryOnFocusLostProperty().set(addingItemToHistoryOnFocusLost);
}

private final ReadOnlyBooleanWrapper historyPopupShowing = new ReadOnlyBooleanWrapper(this, "historyPopupShowing") {
Expand Down

0 comments on commit be5fd62

Please sign in to comment.