diff --git a/fx/docs/bars.md b/fx/docs/bars.md index b47dd1f..4665379 100644 --- a/fx/docs/bars.md +++ b/fx/docs/bars.md @@ -5,9 +5,12 @@ Use JavaFX's `ToolBar` for toolbar content and NeitzelLib's ordinary application Nodes. They work in Java and ordinary FXML without a DockingManager, ViewLoader, ViewModel or initialized injection context. -The toolbar deliberately reuses JavaFX's existing public control: its item -list, orientation, overflow handling, accessibility and CSS already provide -the required behavior. There is no second library toolbar API to learn. +Use JavaFX's `ToolBar` directly for permanent bars. `ToolBarPane` hosts a +horizontal JavaFX toolbar and adds an optional × at its left edge, outside +the overflow menu. Set `dismissible` to show the button. Clicking it hides +the pane and calls `onDismiss` if supplied. Bind `managedProperty()` to +`visibleProperty()` when a hidden pane should release its layout space. +Show the pane again with `setVisible(true)`. Its items are retained. ## Java diff --git a/fx/docs/fxml-controls.md b/fx/docs/fxml-controls.md index 9fa6f79..914e9d6 100644 --- a/fx/docs/fxml-controls.md +++ b/fx/docs/fxml-controls.md @@ -9,9 +9,12 @@ context. The optional `ViewLoader` applies NeitzelLib binding declarations. | `de.neitzel.fx.view.DataGrid` | Columns, typed attributes, widths, filters, grouping, page size and setup identity | Bind `items` or `source`; explicitly load after configuration, close with the view | | `de.neitzel.fx.view.Table` | `collection`, `row`, `Column` declarations | Existing ViewLoader collection/Component rows and `selection` binding | | `de.neitzel.fx.control.StatusBar` | `leftItems`, `rightItems` containing ordinary Nodes | Bind the properties of the contained controls | +| `de.neitzel.fx.control.AutoCompleteTextField` | Standard TextField attributes and a `suggestions` list of strings | Type a prefix, use arrow keys and Enter or select with the mouse. Selecting emits the normal text-field action | +| `de.neitzel.fx.control.TagChip` | `text`, `removable` (false by default), `onRemove` | The action handler owns removing the tag from its model | | `de.neitzel.fx.docking.DockPane` | `initialLayout`, `DockLayout`, group/split definitions | Attach a DockingManager to open application-owned content | | `de.neitzel.fx.control.NonNegativeIntegerSpinner` | `minimum`, `maximum`, `initialValue` constructor attributes | Bind writable `number`, which shares the value factory's property | | `javafx.scene.control.ToolBar` | Standard `items` and `orientation` | Standard JavaFX control, deliberately reused by the library | +| `de.neitzel.fx.control.ToolBarPane` | `items`, `dismissible`, `dismissText`, `onDismiss` | Uses a JavaFX ToolBar and keeps the optional dismissal button outside overflow | Supporting objects such as coordinators, sources, setup stores, popup hosts, result renderers, theme managers and tray integrations are application/runtime diff --git a/fx/src/main/java/de/neitzel/fx/control/AutoCompleteTextField.java b/fx/src/main/java/de/neitzel/fx/control/AutoCompleteTextField.java new file mode 100644 index 0000000..a249a6f --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/control/AutoCompleteTextField.java @@ -0,0 +1,99 @@ +package de.neitzel.fx.control; + +import java.util.Locale; +import javafx.collections.FXCollections; +import javafx.collections.ListChangeListener; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.scene.control.ListView; +import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; +import javafx.scene.input.MouseButton; +import javafx.stage.Popup; + +/** A free-form field offering case-insensitive prefix matches from an observable list. */ +public final class AutoCompleteTextField extends TextField { + private static final int ROW_HEIGHT = 26; + private final ObservableList suggestions = FXCollections.observableArrayList(); + private final ListView choices = new ListView<>(); + private final Popup popup = new Popup(); + private boolean accepting; + + public AutoCompleteTextField() { + popup.setAutoHide(true); + popup.getContent().add(choices); + choices.setFocusTraversable(false); + choices.setFixedCellSize(ROW_HEIGHT); + choices.setOnMouseClicked(event -> { + var value = choices.getSelectionModel().getSelectedItem(); + if (event.getButton() == MouseButton.PRIMARY && value != null) accept(value); + }); + suggestions.addListener((ListChangeListener) change -> updateSuggestions()); + textProperty().addListener((property, before, after) -> updateSuggestions()); + sceneProperty().addListener((property, before, after) -> popup.hide()); + disabledProperty().addListener((property, before, disabled) -> { + if (disabled) popup.hide(); + }); + addEventFilter(KeyEvent.KEY_PRESSED, this::handleKey); + } + + public ObservableList getSuggestions() { + return suggestions; + } + + private void handleKey(KeyEvent event) { + var selection = choices.getSelectionModel(); + if (event.getCode() == KeyCode.DOWN || event.getCode() == KeyCode.UP && popup.isShowing()) { + if (!popup.isShowing()) updateSuggestions(); + if (!popup.isShowing()) return; + if (event.getCode() == KeyCode.DOWN) selection.selectNext(); + else selection.selectPrevious(); + choices.scrollTo(selection.getSelectedIndex()); + event.consume(); + } else if (popup.isShowing() && event.getCode() == KeyCode.ESCAPE) { + popup.hide(); + event.consume(); + } else if (popup.isShowing() && event.getCode() == KeyCode.ENTER) { + var value = selection.getSelectedItem(); + popup.hide(); + if (value != null) { + accept(value); + event.consume(); + } + } else if (event.getCode() == KeyCode.TAB) { + popup.hide(); + } + } + + private void updateSuggestions() { + if (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.of() + : suggestions.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(); + if (matches.isEmpty()) { + popup.hide(); + return; + } + var bounds = localToScreen(getBoundsInLocal()); + if (bounds == null) return; + choices.setPrefSize(Math.max(170, bounds.getWidth()), ROW_HEIGHT * matches.size() + 2); + if (!popup.isShowing()) popup.show(this, bounds.getMinX(), bounds.getMaxY()); + } + + private void accept(String value) { + accepting = true; + try { + popup.hide(); + setText(value); + positionCaret(value.length()); + fireEvent(new ActionEvent()); + } finally { + accepting = false; + } + } +} diff --git a/fx/src/main/java/de/neitzel/fx/control/TagChip.java b/fx/src/main/java/de/neitzel/fx/control/TagChip.java new file mode 100644 index 0000000..167a896 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/control/TagChip.java @@ -0,0 +1,45 @@ +package de.neitzel.fx.control; + +import javafx.beans.binding.Bindings; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.StringProperty; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.geometry.Pos; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.layout.HBox; + +/** A rounded tag label with an optional remove action. */ +public final class TagChip extends HBox { + private final Label label = new Label(); + private final Button remove = new Button("×"); + private final BooleanProperty removable = new SimpleBooleanProperty(this, "removable"); + + public TagChip() { + getStyleClass().add("tag-chip"); + getStylesheets().add(TagChip.class.getResource("tag-chip.css").toExternalForm()); + setAlignment(Pos.CENTER_LEFT); + setSpacing(3); + remove.getStyleClass().add("tag-chip-remove"); + remove.accessibleTextProperty().bind(Bindings.concat("Remove ", label.textProperty())); + remove.visibleProperty().bind(removable); + remove.managedProperty().bind(removable); + getChildren().addAll(label, remove); + } + + public TagChip(String text) { + this(); + setText(text); + } + + public StringProperty textProperty() { return label.textProperty(); } + public String getText() { return label.getText(); } + public void setText(String value) { label.setText(value); } + public BooleanProperty removableProperty() { return removable; } + public boolean isRemovable() { return removable.get(); } + public void setRemovable(boolean value) { removable.set(value); } + public EventHandler getOnRemove() { return remove.getOnAction(); } + public void setOnRemove(EventHandler handler) { remove.setOnAction(handler); } +} diff --git a/fx/src/main/java/de/neitzel/fx/control/ToolBarPane.java b/fx/src/main/java/de/neitzel/fx/control/ToolBarPane.java new file mode 100644 index 0000000..46a9f49 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/control/ToolBarPane.java @@ -0,0 +1,58 @@ +package de.neitzel.fx.control; + +import javafx.beans.DefaultProperty; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Node; +import javafx.scene.control.Button; +import javafx.scene.control.ToolBar; +import javafx.scene.layout.BorderPane; + +/** A horizontal JavaFX toolbar with an optional dismissal button at its leading edge. */ +@DefaultProperty("items") +public final class ToolBarPane extends BorderPane { + private final ToolBar toolbar = new ToolBar(); + private final Button dismiss = new Button("×"); + private final BooleanProperty dismissible = new SimpleBooleanProperty(this, "dismissible"); + private final ObjectProperty> onDismiss = + new SimpleObjectProperty<>(this, "onDismiss"); + + public ToolBarPane() { + getStyleClass().add("tool-bar-pane"); + setCenter(toolbar); + dismiss.getStyleClass().add("tool-bar-dismiss"); + dismiss.setAccessibleText("Close toolbar"); + BorderPane.setAlignment(dismiss, Pos.CENTER_LEFT); + BorderPane.setMargin(dismiss, new Insets(0, 3, 0, 3)); + dismiss.visibleProperty().bind(dismissible); + dismiss.managedProperty().bind(dismissible); + dismissible.addListener((property, old, enabled) -> setLeft(enabled ? dismiss : null)); + dismiss.setOnAction(event -> { + setVisible(false); + var handler = getOnDismiss(); + if (handler != null) handler.handle(event); + }); + } + + public ObservableList getItems() { + return toolbar.getItems(); + } + + public BooleanProperty dismissibleProperty() { return dismissible; } + public boolean isDismissible() { return dismissible.get(); } + public void setDismissible(boolean value) { dismissible.set(value); } + + public ObjectProperty> onDismissProperty() { return onDismiss; } + public EventHandler getOnDismiss() { return onDismiss.get(); } + public void setOnDismiss(EventHandler value) { onDismiss.set(value); } + + public String getDismissText() { return dismiss.getAccessibleText(); } + public void setDismissText(String value) { dismiss.setAccessibleText(value); } +} diff --git a/fx/src/main/resources/de/neitzel/fx/control/tag-chip.css b/fx/src/main/resources/de/neitzel/fx/control/tag-chip.css new file mode 100644 index 0000000..0672c6f --- /dev/null +++ b/fx/src/main/resources/de/neitzel/fx/control/tag-chip.css @@ -0,0 +1,16 @@ +.tag-chip { + -fx-background-color: derive(-fx-base, 12%); + -fx-background-radius: 14; + -fx-border-color: -fx-outer-border; + -fx-border-radius: 14; + -fx-padding: 2 8 2 8; +} +.tag-chip .tag-chip-remove { + -fx-background-color: transparent; + -fx-padding: 0 1 0 3; +} +.tag-chip .tag-chip-remove:hover, +.tag-chip .tag-chip-remove:focused { + -fx-background-color: -fx-outer-border; + -fx-background-radius: 10; +} diff --git a/fx/src/test/java/de/neitzel/fx/view/DataGridFxmlTest.java b/fx/src/test/java/de/neitzel/fx/view/DataGridFxmlTest.java index 39f13a8..b28c01c 100644 --- a/fx/src/test/java/de/neitzel/fx/view/DataGridFxmlTest.java +++ b/fx/src/test/java/de/neitzel/fx/view/DataGridFxmlTest.java @@ -55,6 +55,69 @@ class DataGridFxmlTest { """; + @Test + void loadsReusableTagAndCompletionControls() throws Exception { + DataGridTest.fx(() -> { + String xml = """ + + + + + + + + + """; + try { + javafx.scene.layout.VBox box = new FXMLLoader().load( + new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))); + var field = (de.neitzel.fx.control.AutoCompleteTextField) box.getChildren().getFirst(); + assertEquals(List.of("Archiv"), field.getSuggestions()); + var tag = (de.neitzel.fx.control.TagChip) box.getChildren().getLast(); + assertEquals("Archiv", tag.getText()); + var remove = (javafx.scene.control.Button) tag.getChildren().getLast(); + var clicked = new AtomicBoolean(); + tag.setOnRemove(event -> clicked.set(true)); + remove.fire(); + assertTrue(clicked.get()); + tag.setRemovable(false); + assertFalse(remove.isVisible()); + } catch (IOException failure) { + throw new UncheckedIOException(failure); + } + }); + } + + @Test + void dismissibleToolbarRetainsItsItemsWhenReopened() throws Exception { + DataGridTest.fx(() -> { + String markup = """ + + + + """; + try { + de.neitzel.fx.control.ToolBarPane toolbar = new FXMLLoader().load( + new ByteArrayInputStream(markup.getBytes(StandardCharsets.UTF_8))); + var item = toolbar.getItems().getFirst(); + var dismiss = (javafx.scene.control.Button) toolbar.getLeft(); + assertEquals("Schließen", dismiss.getAccessibleText()); + var dismissed = new AtomicBoolean(); + toolbar.setOnDismiss(event -> dismissed.set(true)); + dismiss.fire(); + assertTrue(dismissed.get()); + assertFalse(toolbar.isVisible()); + toolbar.setVisible(true); + assertSame(item, toolbar.getItems().getFirst()); + toolbar.setDismissible(false); + assertNull(toolbar.getLeft()); + } catch (IOException failure) { + throw new UncheckedIOException(failure); + } + }); + } + @SuppressWarnings("unchecked") private static DataGrid load(String fxml, Map namespace) throws IOException {