Skip to content

Commit

Permalink
Various changes, especially related to the history manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed May 30, 2024
1 parent 91c84cd commit 9d4ed88
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dlsc.gemsfx.HistoryButton;
import com.dlsc.gemsfx.Spacer;
import com.dlsc.gemsfx.util.HistoryManager;
import com.dlsc.gemsfx.util.InMemoryHistoryManager;
import com.dlsc.gemsfx.util.PreferencesHistoryManager;
import com.dlsc.gemsfx.util.StringHistoryManager;
import javafx.application.Application;
Expand Down Expand Up @@ -36,32 +37,24 @@ public void start(Stage primaryStage) throws Exception {
TabPane tabPane = new TabPane();
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
tabPane.getTabs().addAll(
new Tab("Basic", basicDemo()),
new Tab("Advanced", advancedDemo()),
new Tab("Other", otherDemo())
new Tab("In-Memory History Manager", inMemoryDemmo()),
new Tab("String History Manager", stringHistoryDemo()),
new Tab("Preferences History Manager)", prefsDemo())
);

primaryStage.setScene(new Scene(tabPane, 800, 600));
primaryStage.setTitle("History Manager Demo");
primaryStage.show();
}

private Node basicDemo() {
private Node inMemoryDemmo() {
TextField textField = new TextField();

HistoryButton<String> historyButton = new HistoryButton<>(textField);

// Tips: We can set the delimiter and preferencesKey when creating, otherwise use the default value.
// StringHistoryManager historyManager = new StringHistoryManager(";", "history-records",
// Preferences.userNodeForPackage(HistoryManagerApp.class).node("simpleTextField"));

StringHistoryManager historyManager = new StringHistoryManager();

// Tips: If we want to persist the history after the application restarts, we need to set the preferences.
// historyManager.setPreferences(Preferences.userNodeForPackage(HistoryManagerApp.class).node("simpleTextField"));

// Tips: If we want to enable the history function, we need to set the history manager.
InMemoryHistoryManager<String> historyManager = new InMemoryHistoryManager<>();
historyButton.setHistoryManager(historyManager);

historyButton.setOnItemSelected(item -> {
historyButton.hidePopup();
textField.setText(item);
Expand Down Expand Up @@ -92,19 +85,14 @@ private Node basicDemo() {
/**
* Creates a text field with a history button.
*/
private Node advancedDemo() {
private Node stringHistoryDemo() {
TextField textField = new TextField();

StringHistoryManager historyManager = new StringHistoryManager();
// Tips: You can set the delimiter and preferencesKey when creating, otherwise use the default value.
// PreferencesHistoryManager historyManager = new PreferencesHistoryManager(";", "save-items");

// Tips: If you want to persist the history after the application restarts, Please set the preferences.
historyManager.setPreferences(Preferences.userNodeForPackage(HistoryManagerApp.class).node("textField"));
// Optional: Set the maximum history size.default is 30.
StringHistoryManager historyManager = new StringHistoryManager(Preferences.userNodeForPackage(HistoryManagerApp.class), "advanced-demo");
historyManager.setMaxHistorySize(10);

// Optional: if the history is empty, set some default values
if (historyManager.getAll().isEmpty()) {
if (historyManager.getAllUnmodifiable().isEmpty()) {
historyManager.set(List.of("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"));
}

Expand All @@ -128,7 +116,7 @@ private Node advancedDemo() {
historyButton.hidePopup();
});
clearAll.managedProperty().bind(clearAll.visibleProperty());
clearAll.visibleProperty().bind(Bindings.isNotEmpty(historyManager.getAll()));
clearAll.visibleProperty().bind(Bindings.isNotEmpty(historyManager.getAllUnmodifiable()));

leftBox.getChildren().addAll(group, new Spacer(), clearAll);
leftBox.setAlignment(Pos.CENTER);
Expand Down Expand Up @@ -162,7 +150,7 @@ private Node advancedDemo() {
/**
* Creates a list view with a history button.
*/
private Node otherDemo() {
private Node prefsDemo() {
ListView<Student> listView = new ListView<>();
listView.getItems().addAll(
new Student("John", 90),
Expand All @@ -177,29 +165,29 @@ private Node otherDemo() {
new Student("David", 83)
);

PreferencesHistoryManager<Student> historyManager = new PreferencesHistoryManager<>(
new StringConverter<>() {
@Override
public String toString(Student object) {
return object.name() + " : " + object.score();
}

@Override
public Student fromString(String string) {
String[] parts = string.split(" : ");
return new Student(parts[0], Integer.parseInt(parts[1]));
}
}
);
StringConverter<Student> converter = new StringConverter<>() {
@Override
public String toString(Student object) {
return object.name() + " : " + object.score();
}

@Override
public Student fromString(String string) {
String[] parts = string.split(" : ");
return new Student(parts[0], Integer.parseInt(parts[1]));
}
};

Preferences preferences = Preferences.userNodeForPackage(HistoryManagerApp.class);

PreferencesHistoryManager<Student> historyManager = new PreferencesHistoryManager<>(preferences, "list", converter);

listView.setOnMouseClicked(e -> {
if (e.getClickCount() == 2) {
historyManager.add(listView.getSelectionModel().getSelectedItem());
}
});

historyManager.setPreferences(Preferences.userNodeForPackage(HistoryManagerApp.class).node("list"));

HistoryButton<Student> historyButton = new HistoryButton<>();
historyButton.setHistoryManager(historyManager);
historyButton.setText("History");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public void start(Stage primaryStage) throws Exception {
enableHistoryBox.selectedProperty().addListener((obs, oldVal, newVal) -> {
if (newVal) {
if (field.getHistoryManager() == null) {
historyManager = new StringHistoryManager(Preferences.userNodeForPackage(SearchFieldApp.class).node("field"));
historyManager = new StringHistoryManager(Preferences.userNodeForPackage(SearchFieldApp.class), "search-field-id");
// Optional: Set the maximum history size. default is 30.
historyManager.setMaxHistorySize(20);
// Optional: If the history items is empty, we can set a default history list.
if (historyManager.getAll().isEmpty()) {
if (historyManager.getAllUnmodifiable().isEmpty()) {
historyManager.set(List.of("United Kingdom", "Switzerland"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void start(Stage primaryStage) throws Exception {
enableHistoryBox.selectedProperty().addListener((obs, oldVal, newVal) -> {
if (newVal) {
if (stringHistoryManager == null) {
Preferences preferences = Preferences.userNodeForPackage(SearchTextFieldApp.class).node("field1");
stringHistoryManager = new StringHistoryManager(preferences);
Preferences preferences = Preferences.userNodeForPackage(SearchTextFieldApp.class);
stringHistoryManager = new StringHistoryManager(preferences, "search-text-field-id");
}
field.setHistoryManager(stringHistoryManager);
} else {
Expand Down Expand Up @@ -86,7 +86,7 @@ public void start(Stage primaryStage) throws Exception {

Button removeHistoryButton = new Button("Remove First History Item");
removeHistoryButton.setMaxWidth(Double.MAX_VALUE);
removeHistoryButton.setOnAction(e -> Optional.ofNullable(field.getHistoryManager()).ifPresent(historyManager -> historyManager.remove(historyManager.getAll().get(0))));
removeHistoryButton.setOnAction(e -> Optional.ofNullable(field.getHistoryManager()).ifPresent(historyManager -> historyManager.remove(historyManager.getAllUnmodifiable().get(0))));

Button clearButton = new Button("Clear History");
clearButton.setMaxWidth(Double.MAX_VALUE);
Expand Down
7 changes: 4 additions & 3 deletions gemsfx/src/main/java/com/dlsc/gemsfx/HistoryButton.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dlsc.gemsfx;

import com.dlsc.gemsfx.util.HistoryManager;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.css.PseudoClass;
Expand Down Expand Up @@ -392,15 +393,15 @@ private ListView<T> createListView() {

HistoryManager<T> historyManager = getHistoryManager();
if (historyManager != null) {
Bindings.bindContent(listView.getItems(), historyManager.getAll());
Bindings.bindContent(listView.getItems(), historyManager.getAllUnmodifiable());
}

historyManagerProperty().addListener((observable, oldManager, newManager) -> {
if (oldManager != null) {
Bindings.unbindContent(listView.getItems(), oldManager.getAll());
Bindings.unbindContent(listView.getItems(), oldManager.getAllUnmodifiable());
}
if (newManager != null) {
Bindings.bindContent(listView.getItems(), newManager.getAll());
Bindings.bindContent(listView.getItems(), newManager.getAllUnmodifiable());
}
});

Expand Down
Loading

0 comments on commit 9d4ed88

Please sign in to comment.