Bind saved HTML and replaceable suggestion catalogues toward the view
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package de.neitzel.fx.control;
|
||||
|
||||
import java.util.Locale;
|
||||
import javafx.beans.InvalidationListener;
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ListChangeListener;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.collections.WeakListChangeListener;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.control.TextField;
|
||||
@@ -16,6 +20,18 @@ import javafx.stage.Popup;
|
||||
public final class AutoCompleteTextField extends TextField {
|
||||
private static final int ROW_HEIGHT = 26;
|
||||
private final ObservableList<String> suggestions = FXCollections.observableArrayList();
|
||||
private final ObjectProperty<ObservableList<String>> suggestionsSource =
|
||||
new SimpleObjectProperty<>(this, "suggestionsSource") {
|
||||
@Override public void unbind() {
|
||||
if (isBound()) {
|
||||
super.unbind();
|
||||
set(null);
|
||||
}
|
||||
}
|
||||
};
|
||||
private final ListChangeListener<String> contentChanged = change -> updateSuggestions();
|
||||
private final WeakListChangeListener<String> weakContentChanged = new WeakListChangeListener<>(contentChanged);
|
||||
private ObservableList<String> observedSuggestions;
|
||||
private final ListView<String> choices = new ListView<>();
|
||||
private final Popup popup = new Popup();
|
||||
private boolean accepting;
|
||||
@@ -29,9 +45,12 @@ public final class AutoCompleteTextField extends TextField {
|
||||
var value = choices.getSelectionModel().getSelectedItem();
|
||||
if (event.getButton() == MouseButton.PRIMARY && value != null) accept(value);
|
||||
});
|
||||
suggestions.addListener((ListChangeListener<String>) change -> updateSuggestions());
|
||||
suggestionsSource.addListener((InvalidationListener) ignored -> observeSuggestions());
|
||||
textProperty().addListener((property, before, after) -> updateSuggestions());
|
||||
sceneProperty().addListener((property, before, after) -> popup.hide());
|
||||
sceneProperty().addListener((property, before, after) -> {
|
||||
popup.hide();
|
||||
observeSuggestions();
|
||||
});
|
||||
disabledProperty().addListener((property, before, disabled) -> {
|
||||
if (disabled) popup.hide();
|
||||
});
|
||||
@@ -42,6 +61,21 @@ public final class AutoCompleteTextField extends TextField {
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
/** Optional live catalogue. Without a source the fixed FXML suggestions are used. */
|
||||
public ObjectProperty<ObservableList<String>> suggestionsSourceProperty() { return suggestionsSource; }
|
||||
public ObservableList<String> getSuggestionsSource() { return suggestionsSource.get(); }
|
||||
public void setSuggestionsSource(ObservableList<String> value) { suggestionsSource.set(value); }
|
||||
|
||||
private void observeSuggestions() {
|
||||
var source = getSuggestionsSource();
|
||||
var next = getScene() == null ? null : source == null ? suggestions : source;
|
||||
if (observedSuggestions == next) return;
|
||||
if (observedSuggestions != null) observedSuggestions.removeListener(weakContentChanged);
|
||||
observedSuggestions = next;
|
||||
if (next != null) next.addListener(weakContentChanged);
|
||||
updateSuggestions();
|
||||
}
|
||||
|
||||
private void handleKey(KeyEvent event) {
|
||||
var selection = choices.getSelectionModel();
|
||||
if (event.getCode() == KeyCode.DOWN || event.getCode() == KeyCode.UP && popup.isShowing()) {
|
||||
@@ -67,11 +101,11 @@ public final class AutoCompleteTextField extends TextField {
|
||||
}
|
||||
|
||||
private void updateSuggestions() {
|
||||
if (accepting || !isFocused() || isDisabled() || getScene() == null
|
||||
if (observedSuggestions == null || accepting || !isFocused() || isDisabled() || getScene() == null
|
||||
|| getScene().getWindow() == null || !getScene().getWindow().isShowing()) return;
|
||||
String prefix = getText() == null ? "" : getText().strip().toLowerCase(Locale.ROOT);
|
||||
var matches = prefix.isEmpty() ? java.util.List.<String>of()
|
||||
: suggestions.stream().filter(value -> value != null && !value.isBlank())
|
||||
: observedSuggestions.stream().filter(value -> value != null && !value.isBlank())
|
||||
.filter(value -> value.toLowerCase(Locale.ROOT).startsWith(prefix)).distinct().limit(10).toList();
|
||||
choices.getItems().setAll(matches);
|
||||
choices.getSelectionModel().clearSelection();
|
||||
|
||||
@@ -34,6 +34,7 @@ public final class HtmlViewer extends BorderPane implements AutoCloseable {
|
||||
private final StringProperty mediaReloadText = new SimpleStringProperty(this, "mediaReloadText", "Reload external media");
|
||||
private final StringProperty mediaTooltipText = new SimpleStringProperty(this, "mediaTooltipText", "Loads external images for this viewer only and contacts their servers.");
|
||||
private final StringProperty noExternalMediaText = new SimpleStringProperty(this, "noExternalMediaText", "No resolvable external image addresses found.");
|
||||
private final StringProperty html = new SimpleStringProperty(this, "html");
|
||||
private HtmlDocument document;
|
||||
private Task<HtmlDocument> pending;
|
||||
private long generation;
|
||||
@@ -41,6 +42,11 @@ public final class HtmlViewer extends BorderPane implements AutoCloseable {
|
||||
|
||||
public HtmlViewer() {
|
||||
getStyleClass().add("html-viewer");
|
||||
html.addListener((observable, before, value) -> {
|
||||
if (closed) return;
|
||||
if (value == null || value.isBlank()) clear();
|
||||
else loadHtml(value);
|
||||
});
|
||||
browser.setContextMenuEnabled(false);
|
||||
browser.getEngine().setJavaScriptEnabled(false);
|
||||
loading.bind(preparing.or(browser.getEngine().getLoadWorker().runningProperty()));
|
||||
@@ -153,6 +159,7 @@ public final class HtmlViewer extends BorderPane implements AutoCloseable {
|
||||
@Override public void close() {
|
||||
requireFxThread();
|
||||
if (closed) return;
|
||||
html.unbind();
|
||||
clear();
|
||||
closed = true;
|
||||
}
|
||||
@@ -166,6 +173,11 @@ public final class HtmlViewer extends BorderPane implements AutoCloseable {
|
||||
if (!Platform.isFxApplicationThread()) throw new IllegalStateException("HtmlViewer requires the JavaFX application thread.");
|
||||
}
|
||||
|
||||
/** Local source text. Imperative display operations do not publish values to this input. */
|
||||
public StringProperty htmlProperty() { return html; }
|
||||
public String getHtml() { return html.get(); }
|
||||
public void setHtml(String value) { requireOpen(); html.set(value); }
|
||||
|
||||
public ReadOnlyBooleanProperty loadingProperty() { return loading.getReadOnlyProperty(); }
|
||||
public boolean isLoading() { return loading.get(); }
|
||||
public ReadOnlyObjectProperty<Throwable> errorProperty() { return error.getReadOnlyProperty(); }
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javafx.beans.InvalidationListener;
|
||||
import javafx.beans.property.Property;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.collections.ObservableList;
|
||||
@@ -58,7 +59,8 @@ final class DirectBindingApplier {
|
||||
return;
|
||||
}
|
||||
if (attribute.getValue() instanceof ObservableList<?> values) {
|
||||
assignList(controlProperty, values, lifetime);
|
||||
if ("view".equals(binding.getDirection())) alignTowardView(controlProperty, attribute, lifetime);
|
||||
else assignList(controlProperty, values, lifetime);
|
||||
return;
|
||||
}
|
||||
String converterName = binding.getConverter();
|
||||
@@ -551,8 +553,17 @@ final class DirectBindingApplier {
|
||||
private static void alignTowardView(Property<?> controlProperty, ObservableValue<?> attribute,
|
||||
ViewLifetime lifetime) {
|
||||
Property<Object> control = (Property<Object>) controlProperty;
|
||||
Object previous = control.getValue();
|
||||
control.setValue(attribute.getValue());
|
||||
lifetime.listen(attribute, (observable, oldValue, newValue) -> control.setValue(newValue));
|
||||
// ChangeListeners may suppress a new list whose elements equal the previous list.
|
||||
InvalidationListener follow = ignored -> control.setValue(attribute.getValue());
|
||||
attribute.addListener(follow);
|
||||
lifetime.own(() -> {
|
||||
attribute.removeListener(follow);
|
||||
if ((previous instanceof ObservableList<?> || control.getValue() instanceof ObservableList<?>)
|
||||
&& control.getValue() == attribute.getValue())
|
||||
control.setValue(previous);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user