diff --git a/README.md b/README.md index 371adbc..959972b 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,12 @@ The [complete quick start](fx/docs/quick-start.md) includes local installation, Build with Java 25. Maven wrapper version is 3.9.11; the project requires Maven 3.9.11 or later. Surefire fails the build if tests fail. +## Changes in 1.0.3 + +- Show or hide DataGrid columns from the header checklist or context menu while retaining column order and widths. +- Observe column visibility changes from applications that store their own view preferences. +- Use reusable action icons with tooltips and accessible names in JavaFX controls. + ## Changes in 1.0.2 - Display saved HTML and SingleFile archives offline with [HtmlViewer](fx/docs/html-viewer.md). Each viewer can grant external image access for its current document. @@ -93,7 +99,7 @@ Build with Java 25. Maven wrapper version is 3.9.11; the project requires Maven ## Use a release from Maven -To use release `1.0.2`, add the Gitea registry to the consuming project's POM and use the release +To use release `1.0.3`, add the Gitea registry to the consuming project's POM and use the release version for the module you need: ```xml @@ -108,7 +114,7 @@ version for the module you need: de.neitzel.lib fx - 1.0.2 + 1.0.3 ``` diff --git a/core/pom.xml b/core/pom.xml index be68022..5b1d9ca 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -7,7 +7,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 core diff --git a/data-grid-jdbc/pom.xml b/data-grid-jdbc/pom.xml index d114006..32eb9fd 100644 --- a/data-grid-jdbc/pom.xml +++ b/data-grid-jdbc/pom.xml @@ -1,5 +1,5 @@ -4.0.0de.neitzel.libneitzellib1.0.2 +4.0.0de.neitzel.libneitzellib1.0.3 data-grid-jdbcOptional mapped JDBC grid source de.neitzel.libdata-grid${project.version} com.h2databaseh22.5.252test diff --git a/data-grid/pom.xml b/data-grid/pom.xml index ff7459e..a775e80 100644 --- a/data-grid/pom.xml +++ b/data-grid/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - de.neitzel.libneitzellib1.0.2 + de.neitzel.libneitzellib1.0.3 data-grid Toolkit-independent filter, query and grid setup contracts diff --git a/encryption/pom.xml b/encryption/pom.xml index e731280..38e73df 100644 --- a/encryption/pom.xml +++ b/encryption/pom.xml @@ -7,7 +7,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 encryption diff --git a/fx-example/README.md b/fx-example/README.md index e2e2873..334c045 100644 --- a/fx-example/README.md +++ b/fx-example/README.md @@ -30,12 +30,12 @@ From the NeitzelLib root with JDK 25: ./mvnw -f fx-example/pom.xml javafx:run ``` -The first command installs local `1.0.2` artifacts; the second runs only the application module. Do not combine `javafx:run` with `-am`, since the parent has no JavaFX launcher. See the [quick start](../fx/docs/quick-start.md) for test reports. +The first command installs local `1.0.3` artifacts; the second runs only the application module. Do not combine `javafx:run` with `-am`, since the parent has no JavaFX launcher. See the [quick start](../fx/docs/quick-start.md) for test reports. The packaged alternative, after `package` or `install`, is: ```bash -java -jar fx-example/target/fx-example-1.0.2.jar +java -jar fx-example/target/fx-example-1.0.3.jar ``` Keep the adjacent `target/libs` directory with the JAR: the manifest refers to those copied runtime dependencies. A desktop session is required. This example has no `module-info.java`; it is a classpath application. diff --git a/fx-example/docs/product-grid.md b/fx-example/docs/product-grid.md index 65d3535..313a388 100644 --- a/fx-example/docs/product-grid.md +++ b/fx-example/docs/product-grid.md @@ -6,7 +6,7 @@ From the repository root with JDK 25, build without installing runtime artifacts ```sh ./mvnw -pl fx-example -am package -DskipTests -java -cp 'fx-example/target/fx-example-1.0.2.jar:fx-example/target/libs/*' de.neitzel.fx.example.grid.ProductGridMain +java -cp 'fx-example/target/fx-example-1.0.3.jar:fx-example/target/libs/*' de.neitzel.fx.example.grid.ProductGridMain ``` The default catalog has 10,000 products. Both backends use identical stable IDs, names, categories, quantities and DECIMAL(12,2) prices. Fixtures include duplicate sort values, nulls, empty text, mixed case, Unicode, apostrophes and literal SQL wildcard characters. One `ProductCatalog` service maintains original text and Locale.ROOT comparison keys together on insertion/reset. Mutations run on application workers and explicitly refresh the grid; browsing is read-only. diff --git a/fx-example/docs/theme-gallery.md b/fx-example/docs/theme-gallery.md index f1caabd..d309220 100644 --- a/fx-example/docs/theme-gallery.md +++ b/fx-example/docs/theme-gallery.md @@ -5,7 +5,7 @@ After the reactor `verify` or `package` build, from the repository root (no local artifact install required): ```sh -java -cp 'fx-example/target/fx-example-1.0.2.jar:fx-example/target/libs/*' \ +java -cp 'fx-example/target/fx-example-1.0.3.jar:fx-example/target/libs/*' \ de.neitzel.fx.example.theme.ThemeGalleryMain ``` diff --git a/fx-example/pom.xml b/fx-example/pom.xml index 2e11643..38f5e35 100644 --- a/fx-example/pom.xml +++ b/fx-example/pom.xml @@ -7,7 +7,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 fx-example diff --git a/fx/README.md b/fx/README.md index 153fe71..f5597db 100644 --- a/fx/README.md +++ b/fx/README.md @@ -56,7 +56,7 @@ These are the versions configured by this source snapshot, not a promise of comp | --- | --- | --- | | JDK | Java 25 | Select a matching JDK for compilation and launch | | Maven | Minimum 3.9.11; wrapper 3.9.11 | Use the wrapper or an installed Maven | -| Library | `de.neitzel.lib:fx:1.0.2` | Use the Gitea Maven registry or install the local reactor | +| Library | `de.neitzel.lib:fx:1.0.3` | Use the Gitea Maven registry or install the local reactor | | Internal modules | `fx` directly depends on `core`, `injection` and toolkit-independent `data-grid` | Resolved transitively through Maven | | JavaFX | 27.0.1: base, graphics, controls, FXML and web | Resolved transitively; deployment still needs platform-native artifacts | | Saved HTML | jsoup 1.23.2, ph-css 8.2.3, TwelveMonkeys WebP 3.15.3 | Resolved transitively by `fx`; no separate application setup | diff --git a/fx/docs/data-grid-layout.md b/fx/docs/data-grid-layout.md index 92801c8..305712e 100644 --- a/fx/docs/data-grid-layout.md +++ b/fx/docs/data-grid-layout.md @@ -47,9 +47,22 @@ Drag a column's right boundary to preview a bounded width; release commits that column and Escape restores its previous sizing intent. Drag a header label to move a column, or into the grouping area to add a chip. Insertion feedback and fixed-position constraints apply; invalid/outside drops cancel without sorting. -Grouped columns remain visible. Drag chips to reorder levels, and use × to remove -a grouping level. A header click still sorts and the separate filter icon opens -its editor. The Grid setup menu offers keyboard-operable Column settings. +Drag chips to reorder grouping levels, and use × to remove a grouping level. +A header click sorts and the separate filter icon opens its editor. The Grid +setup menu offers keyboard-operable Column settings. + +The arrow at the far right of the header opens a checklist of all columns. +Select a column to toggle its visibility. A column header's context menu offers +ascending and descending sorting, clearing that column's sort, filtering and +grouping where configured, resetting the column width, and hiding the column. +The arrow remains available when every column is hidden. + +Applications can use `setColumnVisible(id, visible)`, `isColumnVisible(id)`, +and `columnVisibilityRevisionProperty()` to observe changes from either the UI or code. +Visibility applies to the open grid, including later pages. Hiding a column +preserves its order, width, filters, sorting, and grouping, and leaves existing +row components and drafts in place. Saved setups currently store the column +order and widths. Each group has a disclosure triangle. Expansion follows the typed ordered path, not its label or page number. Parent collapse preserves child choices. Current diff --git a/fx/docs/quick-start.md b/fx/docs/quick-start.md index 769eb2f..ce1ac65 100644 --- a/fx/docs/quick-start.md +++ b/fx/docs/quick-start.md @@ -1,13 +1,13 @@ # Your first JavaFX application -This walkthrough creates a complete Maven project with one editable name and a label that follows it. It uses Java 25, JavaFX 27.0.1 and NeitzelLib 1.0.2. The source build below uses the matching release tag. +This walkthrough creates a complete Maven project with one editable name and a label that follows it. It uses Java 25, JavaFX 27.0.1 and NeitzelLib 1.0.3. The source build below uses the matching release tag. ## Prepare the library Install a JDK 25. Use the library's Maven wrapper (3.9.11), or a separate Maven installation version 3.9.11 or newer. Check `java -version` and `./mvnw -version` before building; on macOS, select the JDK with `export JAVA_HOME=$(/usr/libexec/java_home -v 25)` if necessary. ```bash -git clone --branch v1.0.2 https://gitea.neitzel.de/konrad/NeitzelLib.git +git clone --branch v1.0.3 https://gitea.neitzel.de/konrad/NeitzelLib.git cd NeitzelLib ./mvnw -pl fx-example -am install ./mvnw -f fx-example/pom.xml javafx:run @@ -66,7 +66,7 @@ hello-fx/ de.neitzel.lib fx - 1.0.2 + 1.0.3 org.slf4j @@ -190,7 +190,7 @@ For a named-module application, see the [module and lifecycle notes](architectur | Symptom | Check | | --- | --- | -| Maven cannot resolve `de.neitzel.lib:fx:1.0.2` or its parent | Install the library reactor first; use the same Maven local repository for both projects. For direct downloads, see the [Maven repository setup](../../README.md#use-a-release-from-maven). | +| Maven cannot resolve `de.neitzel.lib:fx:1.0.3` or its parent | Install the library reactor first; use the same Maven local repository for both projects. For direct downloads, see the [Maven repository setup](../../README.md#use-a-release-from-maven). | | `No plugin found for prefix 'javafx'` | Invoke the example POM directly, or use the complete application POM above. Do not invoke the goal on the library reactor parent. | | JavaFX runtime/native library failure | Check the JDK version, architecture and JavaFX dependencies actually resolved; avoid mixing native artifacts from different platforms or JavaFX versions. | | `getResource(...)` returns null | The FXML belongs under `src/main/resources` in the matching package; verify it was copied to `target/classes`. Resource names are case-sensitive. | diff --git a/fx/pom.xml b/fx/pom.xml index 90b5a10..aad068a 100644 --- a/fx/pom.xml +++ b/fx/pom.xml @@ -7,7 +7,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 fx diff --git a/fx/src/main/java/de/neitzel/fx/control/ActionIcon.java b/fx/src/main/java/de/neitzel/fx/control/ActionIcon.java new file mode 100644 index 0000000..ee16fd2 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/control/ActionIcon.java @@ -0,0 +1,32 @@ +package de.neitzel.fx.control; + +import javafx.scene.control.ButtonBase; +import javafx.scene.control.ContentDisplay; +import javafx.scene.control.Tooltip; +import javafx.scene.layout.StackPane; +import javafx.scene.shape.SVGPath; + +/** A button graphic that follows the control's text color and exposes a tooltip and accessible name. */ +public final class ActionIcon { + private ActionIcon() {} + + /** Installs an outlined icon drawn on a 16 by 16 coordinate grid. */ + public static void install(ButtonBase button, String description, String path) { + var icon = new SVGPath(); + icon.setContent(path); + icon.setFill(null); + icon.strokeProperty().bind(button.textFillProperty()); + icon.setStrokeWidth(1.5); + icon.setStrokeLineCap(javafx.scene.shape.StrokeLineCap.ROUND); + icon.setStrokeLineJoin(javafx.scene.shape.StrokeLineJoin.ROUND); + var graphic = new StackPane(icon); + graphic.setMinSize(18, 18); + graphic.setPrefSize(18, 18); + graphic.setMaxSize(18, 18); + graphic.setMouseTransparent(true); + button.setGraphic(graphic); + button.setContentDisplay(ContentDisplay.GRAPHIC_ONLY); + button.setAccessibleText(description); + button.setTooltip(new Tooltip(description)); + } +} diff --git a/fx/src/main/java/de/neitzel/fx/view/DataGrid.java b/fx/src/main/java/de/neitzel/fx/view/DataGrid.java index b5f36d7..c9ac51c 100644 --- a/fx/src/main/java/de/neitzel/fx/view/DataGrid.java +++ b/fx/src/main/java/de/neitzel/fx/view/DataGrid.java @@ -33,8 +33,8 @@ public final class DataGrid extends VBox implements AutoCloseable { previous = pagingButton("Previous page", "M2 7 L14 0 V14 Z"), next = pagingButton("Next page", "M2 0 L14 7 L2 14 Z"), last = pagingButton("Last page", "M0 0 L12 7 L0 14 Z M14 0 H16 V14 H14 Z"), - retry = new Button("Retry"), - refresh = new Button("Refresh"); + retry = actionButton("Retry", "M2 8 A6 6 0 1 1 5 13 M2 3 V8 H7"), + refresh = actionButton("Refresh", "M14 7 A6 6 0 1 0 13 11 M14 2 V7 H9"); private final ComboBox pageSize = new ComboBox<>(); private GridQuery initial; private List columnDefinitions; @@ -59,6 +59,8 @@ public final class DataGrid extends VBox implements AutoCloseable { private final Label activeFilters = new Label(); private final Label interactionStatus = new Label(); private final MenuButton setupMenu = new MenuButton("Grid setup"); + private final MenuButton columnMenu = new MenuButton(); + private final ReadOnlyLongWrapper columnVisibilityRevision = new ReadOnlyLongWrapper(); private final ReadOnlyLongWrapper committedRevision = new ReadOnlyLongWrapper(); private List defaultGrouping = List.of(); private GridSetup developerDefaults; @@ -68,6 +70,12 @@ public final class DataGrid extends VBox implements AutoCloseable { private GridPersistenceAttachment attachment; private GridColumnSettings columnSettings; + private static Button actionButton(String description, String path) { + var button = new Button(); + de.neitzel.fx.control.ActionIcon.install(button, description, path); + return button; + } + private static Button pagingButton(String name, String path) { var button = new Button(); var icon = new SVGPath(); @@ -272,7 +280,12 @@ public final class DataGrid extends VBox implements AutoCloseable { List.of(), new PageRequest.Paged( 0, Math.min(50, source.capabilities().maximumPageSize()))); - renderer = table.openResults(columns, rowFactory); + renderer = table.openResults(columns, rowFactory, columnMenu); + columnMenu.getStyleClass().add("data-grid-column-menu"); + columnMenu.setAccessibleText("Column visibility"); + columnMenu.setTooltip(new Tooltip("Column visibility")); + columnMenu.setOnShowing(event -> updateColumnMenu()); + renderer.setHeaderMenuFactory(this::headerMenu); renderer.setSelectionMode(getSelectionMode()); selectedKeys.bind(renderer.selectedKeysProperty()); selectedElements.bind(renderer.selectedElementsProperty()); @@ -343,9 +356,12 @@ public final class DataGrid extends VBox implements AutoCloseable { controls.getChildren().add(setupMenu); interactionStatus.setWrapText(true); interactionStatus.getStyleClass().add("data-grid-interaction-status"); + de.neitzel.fx.control.ActionIcon.install(setupMenu, "Grid setup", + "M2 4 H14 M2 8 H14 M2 12 H14 M5 2 V6 M11 6 V10 M7 10 V14"); var css = DataGrid.class.getResource("data-grid.css"); if (css != null) getStylesheets().add(css.toExternalForm()); - Button clearActiveFilters = new Button("Clear filters"); + Button clearActiveFilters = actionButton("Clear filters", + "M1 2 H15 L9 8 V14 L6 12 V8 Z M11 10 L15 14 M15 10 L11 14"); clearActiveFilters.setOnAction(e -> setFilters(List.of())); filterSummary.getChildren().addAll(activeFilters, clearActiveFilters); filterSummary.setVisible(false); @@ -417,6 +433,66 @@ public final class DataGrid extends VBox implements AutoCloseable { renderer.setColumnOrder(ids); } + /** Returns whether this grid currently displays the column. */ + public boolean isColumnVisible(String id) { + check(); + return renderer.isColumnVisible(id); + } + + /** Shows or hides a column without fetching rows or changing the saved grid setup. */ + public void setColumnVisible(String id, boolean visible) { + if (visible == isColumnVisible(id)) return; + closeFilter(); + renderer.setColumnVisible(id, visible); + columnVisibilityRevision.set(columnVisibilityRevision.get() + 1); + } + + /** Observes effective visibility changes, which are independent of saved grid setups. */ + public ReadOnlyLongProperty columnVisibilityRevisionProperty() { + return columnVisibilityRevision.getReadOnlyProperty(); + } + + private void updateColumnMenu() { + columnMenu.getItems().setAll(renderer.columnOrder().stream().map(id -> { + CheckMenuItem item = new CheckMenuItem(renderer.columnHeader(id)); + item.setSelected(isColumnVisible(id)); + item.setOnAction(e -> setColumnVisible(id, item.isSelected())); + return item; + }).toList()); + } + + private ContextMenu headerMenu(String id, HBox cell) { + var menu = new ContextMenu( + menuItem("Sort ascending", () -> setSort(new SortKey(id, SortKey.Direction.ASCENDING))), + menuItem("Sort descending", () -> setSort(new SortKey(id, SortKey.Direction.DESCENDING)))); + if (query().sort().stream().anyMatch(key -> key.columnId().equals(id))) + menu.getItems().add(menuItem("Clear sort", () -> setSort(null))); + if (filterColumns.containsKey(id)) { + var filterButton = cell.lookup(".filter-button"); + menu.getItems().add(menuItem("Filter…", () -> openFilter(id, filterButton))); + } + if (renderer.canGroup(id)) { + boolean grouped = renderer.groupingColumns().contains(id); + menu.getItems().add(menuItem(grouped ? "Remove grouping" : "Group by column", () -> { + var nextGroups = new ArrayList<>(renderer.groupingColumns()); + if (grouped) nextGroups.remove(id); + else if (!nextGroups.contains(id)) nextGroups.add(id); + setGroupingColumns(nextGroups); + })); + } + menu.getItems().addAll( + menuItem("Reset column width", () -> setColumnWidth(id, new ColumnWidth.Default())), + new SeparatorMenuItem(), + menuItem("Hide column", () -> setColumnVisible(id, false))); + return menu; + } + + private static MenuItem menuItem(String text, Runnable action) { + var item = new MenuItem(text); + item.setOnAction(e -> action.run()); + return item; + } + public double setColumnWidth(String id, ColumnWidth width) { check(); closeFilter(); @@ -1252,6 +1328,7 @@ public final class DataGrid extends VBox implements AutoCloseable { } if (attachment != null) attachment.close(); closed = true; + columnMenu.hide(); closeFilter(); declaration.close(); if (coordinator != null) coordinator.close(); diff --git a/fx/src/main/java/de/neitzel/fx/view/ResultColumnSeparators.java b/fx/src/main/java/de/neitzel/fx/view/ResultColumnSeparators.java index 5fde1d5..ca3840c 100644 --- a/fx/src/main/java/de/neitzel/fx/view/ResultColumnSeparators.java +++ b/fx/src/main/java/de/neitzel/fx/view/ResultColumnSeparators.java @@ -25,6 +25,10 @@ final class ResultColumnSeparators extends Group { return decoration; } + boolean matchesColumnCount(int columns) { + return getChildren().size() == Math.max(0, columns - 1); + } + void position(double[] widths) { double x = 0; for (int i = 0; i < getChildren().size(); i++) { diff --git a/fx/src/main/java/de/neitzel/fx/view/ResultHeaderGestures.java b/fx/src/main/java/de/neitzel/fx/view/ResultHeaderGestures.java index a2e9901..0906814 100644 --- a/fx/src/main/java/de/neitzel/fx/view/ResultHeaderGestures.java +++ b/fx/src/main/java/de/neitzel/fx/view/ResultHeaderGestures.java @@ -343,13 +343,10 @@ final class ResultHeaderGestures implements AutoCloseable { headerViewport = pane; break; } - if (scroll == null - || headerViewport == null - || Double.isNaN(pointerX) - || !headerViewport - .localToScene(headerViewport.getBoundsInLocal()) - .contains(pointerX, pointerY)) return; - var bounds = scroll.localToScene(scroll.getViewportBounds()); + if (scroll == null || headerViewport == null || Double.isNaN(pointerX)) + return; + var bounds = headerViewport.localToScene(headerViewport.getBoundsInLocal()); + if (!bounds.contains(pointerX, pointerY)) return; double delta = pointerX < bounds.getMinX() + 30 ? -.012 diff --git a/fx/src/main/java/de/neitzel/fx/view/ResultScrollAlignment.java b/fx/src/main/java/de/neitzel/fx/view/ResultScrollAlignment.java index 17d75f6..29ed12f 100644 --- a/fx/src/main/java/de/neitzel/fx/view/ResultScrollAlignment.java +++ b/fx/src/main/java/de/neitzel/fx/view/ResultScrollAlignment.java @@ -2,6 +2,8 @@ package de.neitzel.fx.view; import javafx.beans.binding.Bindings; import javafx.beans.binding.DoubleBinding; +import javafx.beans.binding.ObjectBinding; +import javafx.geometry.Insets; import javafx.scene.control.ScrollPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; @@ -9,29 +11,43 @@ import javafx.scene.layout.VBox; /** Makes equal normalized scroll values represent equal pixel offsets in queried results. */ final class ResultScrollAlignment implements AutoCloseable { private final HBox header; + private final VBox body; private final double originalMinimum; + private final Insets originalPadding; + private final ObjectBinding bodyPadding; private final DoubleBinding minimum; + private final DoubleBinding viewportDifference; - ResultScrollAlignment(HBox header, VBox body, ScrollPane headerScroll, ScrollPane bodyScroll) { + ResultScrollAlignment( + HBox header, VBox body, ScrollPane headerScroll, ScrollPane bodyScroll, + boolean hasHeaderAccessory) { this.header = header; + this.body = body; originalMinimum = header.getMinWidth(); + originalPadding = body.getPadding(); + viewportDifference = Bindings.createDoubleBinding( + () -> headerScroll.getViewportBounds().getWidth() + - bodyScroll.getViewportBounds().getWidth(), + headerScroll.viewportBoundsProperty(), bodyScroll.viewportBoundsProperty()); + // The fixed header menu can make the header viewport narrower. Add trailing body space + // so the last column can scroll fully into view in both viewports. + bodyPadding = hasHeaderAccessory ? Bindings.createObjectBinding( + () -> new Insets(originalPadding.getTop(), + originalPadding.getRight() + Math.max(0, -viewportDifference.get()), + originalPadding.getBottom(), originalPadding.getLeft()), + viewportDifference) : null; + if (bodyPadding != null) body.paddingProperty().bind(bodyPadding); // A vertical body scrollbar narrows its viewport. Trailing row actions can also // make its content wider than the column headings. Reserve blank header space // so both scrollable extents match; the existing two-way hvalue binding then // moves every mapped field and heading by the same number of pixels. minimum = Bindings.createDoubleBinding( - () -> - body.getLayoutBounds().getWidth() - + Math.max( - 0, - headerScroll.getViewportBounds().getWidth() - - bodyScroll - .getViewportBounds() - .getWidth()), - body.layoutBoundsProperty(), - headerScroll.viewportBoundsProperty(), - bodyScroll.viewportBoundsProperty()); + () -> body.getLayoutBounds().getWidth() + + (hasHeaderAccessory + ? viewportDifference.get() + : Math.max(0, viewportDifference.get())), + body.layoutBoundsProperty(), viewportDifference); header.minWidthProperty().bind(minimum); } @@ -40,5 +56,11 @@ final class ResultScrollAlignment implements AutoCloseable { header.minWidthProperty().unbind(); header.setMinWidth(originalMinimum); minimum.dispose(); + if (bodyPadding != null) { + body.paddingProperty().unbind(); + body.setPadding(originalPadding); + bodyPadding.dispose(); + } + viewportDifference.dispose(); } } diff --git a/fx/src/main/java/de/neitzel/fx/view/Table.java b/fx/src/main/java/de/neitzel/fx/view/Table.java index 3b466f7..f425270 100644 --- a/fx/src/main/java/de/neitzel/fx/view/Table.java +++ b/fx/src/main/java/de/neitzel/fx/view/Table.java @@ -246,13 +246,26 @@ public class Table extends VBox { */ public TableResults openResults(List definitions, java.util.function.Function factory) { + return openResults(definitions, factory, null); + } + + TableResults openResults(List definitions, + java.util.function.Function factory, Region headerAccessory) { if (!javafx.application.Platform.isFxApplicationThread()) { throw new IllegalStateException("Use the JavaFX application thread"); } if (opened || resultMode) { throw new IllegalStateException("Table is already open"); } - TableResults renderer = new TableResults<>(header, body, List.copyOf(definitions), factory, headerScroll, scroll); + TableResults renderer = new TableResults<>( + header, body, List.copyOf(definitions), factory, headerScroll, scroll, headerAccessory); + if (headerAccessory != null) { + getChildren().remove(headerScroll); + headerScroll.setMinWidth(0); + HBox.setHgrow(headerScroll, Priority.ALWAYS); + headerAccessory.setMaxHeight(Double.MAX_VALUE); + getChildren().addFirst(new HBox(headerScroll, headerAccessory)); + } // DataGrid grows this Table; let its viewport consume that height as well. VBox.setVgrow(scroll, Priority.ALWAYS); resultMode = true; diff --git a/fx/src/main/java/de/neitzel/fx/view/TableResults.java b/fx/src/main/java/de/neitzel/fx/view/TableResults.java index 9659fe6..ff8646f 100644 --- a/fx/src/main/java/de/neitzel/fx/view/TableResults.java +++ b/fx/src/main/java/de/neitzel/fx/view/TableResults.java @@ -8,8 +8,10 @@ import javafx.collections.*; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.control.Button; +import javafx.scene.control.ContextMenu; import javafx.scene.control.Label; import javafx.scene.control.Tooltip; +import javafx.geometry.Side; import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; import javafx.scene.input.KeyEvent; @@ -20,6 +22,7 @@ import javafx.scene.layout.VBox; import javafx.scene.shape.SVGPath; import java.util.*; +import java.util.function.BiFunction; import java.util.function.Function; /** @@ -31,10 +34,12 @@ import java.util.function.Function; public final class TableResults implements AutoCloseable { private final HBox header; private final VBox body; + private final Region headerAccessory; private final Function factory; private final Map definitions = new LinkedHashMap<>(); private final Map widths = new LinkedHashMap<>(); private List order; + private final Set hiddenColumns = new HashSet<>(); private List> entries = List.of(); private final ReadOnlyObjectWrapper selectedKey = new ReadOnlyObjectWrapper<>(); private final ReadOnlyObjectWrapper selectedElement = new ReadOnlyObjectWrapper<>(); @@ -56,6 +61,8 @@ public final class TableResults implements AutoCloseable { private List sortKeys = List.of(); private java.util.function.BiConsumer filterRequest = (id, node) -> {}; private java.util.function.Consumer sortRequest = id -> {}; + private BiFunction headerMenuFactory; + private ContextMenu headerMenu; private List> groupColumns = List.of(); private List grouping = List.of(); @@ -79,10 +86,12 @@ public final class TableResults implements AutoCloseable { List columns, Function factory, javafx.scene.control.ScrollPane headerScroll, - javafx.scene.control.ScrollPane bodyScroll) { + javafx.scene.control.ScrollPane bodyScroll, + Region headerAccessory) { requireFx(); this.header = header; this.body = body; + this.headerAccessory = headerAccessory; this.factory = Objects.requireNonNull(factory); gestures = new ResultHeaderGestures(this, header, body); if (columns.isEmpty()) @@ -94,7 +103,8 @@ public final class TableResults implements AutoCloseable { } order = List.copyOf(definitions.keySet()); validateOrder(order); - scrollAlignment = new ResultScrollAlignment(header, body, headerScroll, bodyScroll); + scrollAlignment = new ResultScrollAlignment( + header, body, headerScroll, bodyScroll, headerAccessory != null); drawHeader(); body.setOnMouseClicked(e -> { if (e.getTarget() == body && e.getButton() == MouseButton.PRIMARY) select(null); @@ -105,7 +115,7 @@ public final class TableResults implements AutoCloseable { checkOpen(); columnSeparators = true; drawHeader(); - entries.forEach(e -> layoutRow(e.view())); + applyWidths(widths); } public ReadOnlyObjectProperty selectedKeyProperty() { @@ -143,6 +153,27 @@ public final class TableResults implements AutoCloseable { return Map.copyOf(widths); } + boolean isColumnVisible(String id) { + checkOpen(); + if (!definitions.containsKey(id)) throw new IllegalArgumentException("Unknown column: " + id); + return !hiddenColumns.contains(id); + } + + void setColumnVisible(String id, boolean visible) { + if (visible == isColumnVisible(id)) return; + Node focus = header.getScene() == null ? null : header.getScene().getFocusOwner(); + boolean headerFocused = focus != null && descendant(header, focus); + if (visible) hiddenColumns.remove(id); + else hiddenColumns.add(id); + drawHeader(); + applyWidths(widths); + if (headerFocused && headerAccessory != null) headerAccessory.requestFocus(); + } + + private List visibleOrder(List ordering) { + return ordering.stream().filter(id -> !hiddenColumns.contains(id)).toList(); + } + /** Marks retained rows unavailable without disabling header/query controls. */ public void setRowsInteractive(boolean interactive) { checkOpen(); @@ -157,6 +188,11 @@ public final class TableResults implements AutoCloseable { sortRequest = Objects.requireNonNull(handler); } + void setHeaderMenuFactory(BiFunction factory) { + checkOpen(); + headerMenuFactory = Objects.requireNonNull(factory); + } + /** Opt-in filter actions; clicking the icon never issues a sort request. */ public void setFilterActions( Set columns, java.util.function.BiConsumer handler) { @@ -196,8 +232,12 @@ public final class TableResults implements AutoCloseable { } } + String columnHeader(String id) { + return definitions.get(id).header(); + } + private String headerText(String id) { - return definitions.get(id).header() + return columnHeader(id) + sortKeys.stream() .filter(k -> k.columnId().equals(id)) .map(k -> k.direction() == SortKey.Direction.ASCENDING ? " ↑" : " ↓") @@ -337,7 +377,8 @@ public final class TableResults implements AutoCloseable { Math.clamp(e.value(), c.minimumWidth(), c.maximumWidth())) : width); } - var nextHeader = headerNodes(nextOrder, preparedWidths); + var visibleOrder = visibleOrder(nextOrder); + var nextHeader = headerNodes(visibleOrder, preparedWidths); result = List.copyOf(result); Set keys = new HashSet<>(); for (var row : result) @@ -365,7 +406,7 @@ public final class TableResults implements AutoCloseable { next.add(new Entry<>(row, view, click, key)); validateRow(view); view.root().getStyleClass().add("result-row"); - layoutRow(view, nextOrder, preparedWidths); + layoutRow(view, visibleOrder, preparedWidths); view.root().addEventHandler(MouseEvent.MOUSE_CLICKED, click); view.root().setFocusTraversable(true); view.root().addEventHandler(KeyEvent.KEY_PRESSED, key); @@ -379,7 +420,7 @@ public final class TableResults implements AutoCloseable { order = nextOrder; widths.clear(); widths.putAll(preparedWidths); - header.getChildren().setAll(nextHeader); + replaceHeader(nextHeader); captured = nextCaptured; groupTree = nextTree; grouping = nextGrouping.columns(); @@ -396,8 +437,7 @@ public final class TableResults implements AutoCloseable { validateOrder(ids); order = ids; drawHeader(); - entries.forEach(e -> layoutRow(e.view())); - resizeGroupHeadings(); + applyWidths(widths); layoutCommitted.run(); } @@ -455,7 +495,15 @@ public final class TableResults implements AutoCloseable { } private void drawHeader() { - header.getChildren().setAll(headerNodes(order, widths)); + replaceHeader(headerNodes(visibleOrder(order), widths)); + } + + private void replaceHeader(List cells) { + if (headerMenu != null) { + headerMenu.hide(); + headerMenu = null; + } + header.getChildren().setAll(cells); } private List headerNodes(List ordering, Map sizes) { @@ -501,6 +549,14 @@ public final class TableResults implements AutoCloseable { if (columnSeparators && !id.equals(ordering.getLast())) cell.getChildren().add(ResultColumnSeparators.forHeader(cell)); gestures.header(cell, label, handle, id, () -> sortRequest.accept(id)); + cell.setOnContextMenuRequested(event -> { + if (headerMenuFactory == null) return; + if (headerMenu != null) headerMenu.hide(); + headerMenu = headerMenuFactory.apply(id, cell); + if (event.isKeyboardTrigger()) headerMenu.show(cell, Side.BOTTOM, 0, 0); + else headerMenu.show(cell, event.getScreenX(), event.getScreenY()); + event.consume(); + }); size(cell, width(id, sizes)); nodes.add(cell); } @@ -513,10 +569,6 @@ public final class TableResults implements AutoCloseable { region.setMaxWidth(width); } - private void layoutRow(ResultRow view) { - layoutRow(view, order, widths); - } - private void layoutRow(ResultRow view, List ordering, Map sizes) { Set fields = Collections.newSetFromMap(new IdentityHashMap<>()); fields.addAll(view.fields().values()); @@ -532,7 +584,7 @@ public final class TableResults implements AutoCloseable { else if (!fields.contains(child)) children.add(child); } if (columnSeparators) { - if (separators == null) + if (separators == null || !separators.matchesColumnCount(ordering.size())) separators = new ResultColumnSeparators(view.root(), ordering.size()); separators.position(ordering.stream().mapToDouble(id -> width(id, sizes)).toArray()); children.add(separators); @@ -543,7 +595,10 @@ public final class TableResults implements AutoCloseable { view.root().getScene() == null ? null : view.root().getScene().getFocusOwner(); boolean restore = focus != null && descendant(view.root(), focus); view.root().getChildren().setAll(children); - if (restore) focus.requestFocus(); + if (restore) { + if (descendant(view.root(), focus)) focus.requestFocus(); + else view.root().requestFocus(); + } } } @@ -762,6 +817,7 @@ public final class TableResults implements AutoCloseable { .getChildren() .getFirst() .requestFocus(); + else if (headerAccessory != null) headerAccessory.requestFocus(); } } resizeGroupHeadings(); @@ -862,8 +918,13 @@ public final class TableResults implements AutoCloseable { } private void resizeGroupHeadings() { - double total = order.stream().mapToDouble(this::width).sum(); - disclosure.values().forEach(button -> size(button, total)); + resizeGroupHeadings(visibleOrder(order), widths); + } + + private void resizeGroupHeadings(List visible, Map sizes) { + double total = visible.stream().mapToDouble(id -> width(id, sizes)).sum(); + disclosure.values().forEach( + button -> size(button, visible.isEmpty() ? Region.USE_COMPUTED_SIZE : total)); } void previewWidth(String id, ColumnWidth value) { @@ -875,9 +936,9 @@ public final class TableResults implements AutoCloseable { private void applyWidths(Map sizes) { for (Node node : header.getChildren()) size((Region) node, width((String) node.getUserData(), sizes)); - entries.forEach(e -> layoutRow(e.view(), order, sizes)); - double total = order.stream().mapToDouble(id -> width(id, sizes)).sum(); - disclosure.values().forEach(button -> size(button, total)); + var visible = visibleOrder(order); + entries.forEach(e -> layoutRow(e.view(), visible, sizes)); + resizeGroupHeadings(visible, sizes); } private void release(List> retired) { @@ -913,7 +974,7 @@ public final class TableResults implements AutoCloseable { var old = entries; entries = List.of(); body.getChildren().clear(); - header.getChildren().clear(); + replaceHeader(List.of()); body.setOnMouseClicked(null); selectedKey.set(null); selectedElement.set(null); diff --git a/fx/src/main/resources/de/neitzel/fx/view/data-grid.css b/fx/src/main/resources/de/neitzel/fx/view/data-grid.css index d0b29f1..10bd71b 100644 --- a/fx/src/main/resources/de/neitzel/fx/view/data-grid.css +++ b/fx/src/main/resources/de/neitzel/fx/view/data-grid.css @@ -2,6 +2,9 @@ .grouping-chip { -fx-padding: 4 8; -fx-alignment: center-left; -fx-background-color: #e8eff7; -fx-border-color: #8a9eb5; -fx-cursor: open-hand; } .data-grid .column-cell { -fx-alignment: center-left; -fx-min-height: 32; } .data-grid .column-header { -fx-padding: 5 6; } +.data-grid .data-grid-column-menu, .data-grid .data-grid-column-menu > .label { -fx-padding: 0; } +.data-grid .data-grid-column-menu > .arrow-button { -fx-padding: 0 8; } +.neitzel-theme .data-grid .data-grid-column-menu:focused { -fx-border-color: -nl-focus; } .data-grid .column-resize:hover { -fx-background-color: #3776b6; } .group-heading { -fx-background-color: #e8eff7; -fx-text-fill: #182b3b; -fx-border-color: transparent transparent #b4c4d5 transparent; -fx-background-radius: 0; -fx-alignment: center-left; } .group-heading:focused { -fx-border-color: #3776b6; -fx-border-width: 2; } diff --git a/fx/src/test/java/de/neitzel/fx/view/DataGridGroupingTest.java b/fx/src/test/java/de/neitzel/fx/view/DataGridGroupingTest.java index 8501e57..a3caf90 100644 --- a/fx/src/test/java/de/neitzel/fx/view/DataGridGroupingTest.java +++ b/fx/src/test/java/de/neitzel/fx/view/DataGridGroupingTest.java @@ -5,9 +5,13 @@ import static org.junit.jupiter.api.Assertions.*; import de.neitzel.grid.*; import javafx.application.Platform; +import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.*; +import javafx.scene.input.ContextMenuEvent; import javafx.scene.layout.*; +import javafx.stage.Stage; +import javafx.stage.Window; import org.junit.jupiter.api.*; @@ -95,6 +99,137 @@ class DataGridGroupingTest { return new GroupPath(List.of(new GroupPath.Segment("type", new FilterValue.Text(type)))); } + @Test + void headerMenusChangeVisibilityWithoutReplacingRowsOrLosingLayout() throws Exception { + var reads = new AtomicInteger(); + var disposed = new AtomicInteger(); + Map fields = new HashMap<>(); + var grid = fx(() -> grid(reads, disposed, fields)); + var stage = fx(Stage::new); + try { + fx(grid::loadAsync).toCompletableFuture().get(5, TimeUnit.SECONDS); + fx(() -> { + stage.setScene(grid.getScene()); + stage.show(); + grid.setExpanded(path("A"), true); + grid.setColumnOrder(List.of("status", "type")); + grid.setColumnWidth("type", new ColumnWidth.Explicit(235)); + var editor = fields.get(1L); + editor.setText("unfinished draft"); + var row = (HBox) editor.getParent(); + assertSame(editor, row.getChildren().get(1)); + var menu = (MenuButton) grid.lookup(".data-grid-column-menu"); + var header = (HBox) grid.lookup(".column-header-row"); + long visibility = grid.columnVisibilityRevisionProperty().get(); + var context = showColumnContextMenu(header.getChildren().get(1), false); + context.getItems().getLast().fire(); + assertEquals(++visibility, grid.columnVisibilityRevisionProperty().get()); + grid.setColumnVisible("type", false); + assertEquals(visibility, grid.columnVisibilityRevisionProperty().get()); + assertFalse(context.isShowing()); + assertFalse(grid.isColumnVisible("type")); + assertNull(editor.getParent()); + assertEquals(1, header.getChildren().size()); + assertEquals(List.of("type"), grid.groupingColumns()); + + menu.show(); + assertTrue(menu.isShowing()); + var status = (CheckMenuItem) menu.getItems().getFirst(); + var type = (CheckMenuItem) menu.getItems().getLast(); + assertEquals("Status", status.getText()); + assertTrue(status.isSelected()); + assertEquals("Type", type.getText()); + assertFalse(type.isSelected()); + status.setSelected(false); + status.fire(); + menu.hide(); + assertTrue(header.getChildren().isEmpty()); + assertTrue(menu.isVisible()); + menu.show(); + type = (CheckMenuItem) menu.getItems().getLast(); + type.setSelected(true); + type.fire(); + menu.hide(); + assertEquals(visibility + 2, grid.columnVisibilityRevisionProperty().get()); + assertSame(editor, row.getChildren().getFirst()); + assertEquals(235, editor.getPrefWidth()); + assertEquals("unfinished draft", editor.getText()); + assertEquals(0, disposed.get()); + assertEquals(1, reads.get()); + return null; + }); + fx(() -> { + var header = (HBox) grid.lookup(".column-header-row"); + var context = showColumnContextMenu(header.getChildren().getFirst(), true); + context.getItems().stream() + .filter(item -> "Sort descending".equals(item.getText())) + .findFirst().orElseThrow().fire(); + context.hide(); + return null; + }); + DataGridTest.drain(); + fx(() -> { + assertEquals(List.of(new SortKey("type", SortKey.Direction.DESCENDING)), + grid.stateProperty().get().displayed().sort()); + assertFalse(grid.isColumnVisible("status")); + assertTrue(grid.isColumnVisible("type")); + assertEquals(1, ((HBox) grid.lookup(".column-header-row")).getChildren().size()); + assertEquals(2, reads.get()); + return null; + }); + } finally { + fx(() -> { + grid.close(); + stage.close(); + return null; + }); + } + } + + private static ContextMenu showColumnContextMenu(Node cell, boolean fromKeyboard) { + var point = cell.localToScreen(0, 0); + cell.fireEvent(new ContextMenuEvent(ContextMenuEvent.CONTEXT_MENU_REQUESTED, + 0, 0, point.getX(), point.getY(), fromKeyboard, null)); + return Window.getWindows().stream() + .filter(ContextMenu.class::isInstance).map(ContextMenu.class::cast) + .filter(ContextMenu::isShowing).findFirst().orElseThrow(); + } + + @Test + void selectorStaysFixedAndHeadersAlignAtTheRightScrollEdge() throws Exception { + var fields = new HashMap(); + var grid = fx(() -> grid(new AtomicInteger(), new AtomicInteger(), fields)); + try { + fx(grid::loadAsync).toCompletableFuture().get(5, TimeUnit.SECONDS); + fx(() -> { + grid.setExpanded(path("A"), true); + grid.setColumnWidth("type", new ColumnWidth.Explicit(400)); + grid.setColumnWidth("status", new ColumnWidth.Explicit(400)); + var root = grid.getScene().getRoot(); + root.resize(640, 500); + root.layout(); + var menu = grid.lookup(".data-grid-column-menu"); + double menuX = menu.localToScene(0, 0).getX(); + var scroll = (ScrollPane) grid.lookup(".table-scroll"); + var header = (HBox) grid.lookup(".column-header-row"); + scroll.setHvalue(1); + root.layout(); + assertEquals(menuX, menu.localToScene(0, 0).getX(), .5); + var type = fields.get(1L); + assertEquals(type.localToScene(0, 0).getX(), + header.getChildren().getFirst().localToScene(0, 0).getX(), 1); + var last = header.getChildren().getLast(); + assertEquals(menuX, last.localToScene(last.getBoundsInLocal()).getMaxX(), 1); + return null; + }); + } finally { + fx(() -> { + grid.close(); + return null; + }); + } + } + @Test void presentationChangesPreserveComponentsDraftsAndHiddenSelectionWithoutQueries() throws Exception { diff --git a/fx/src/test/java/de/neitzel/fx/view/DataGridNativeInteractionTest.java b/fx/src/test/java/de/neitzel/fx/view/DataGridNativeInteractionTest.java index d2fd155..843dc6b 100644 --- a/fx/src/test/java/de/neitzel/fx/view/DataGridNativeInteractionTest.java +++ b/fx/src/test/java/de/neitzel/fx/view/DataGridNativeInteractionTest.java @@ -326,7 +326,10 @@ class DataGridNativeInteractionTest { ScrollPane scroll = fx(() -> (ScrollPane) grid.lookup(".table-scroll")); var original = order(); begin(fx(() -> label("name"))); - Bounds viewport = fx(() -> scroll.localToScreen(scroll.getViewportBounds())); + Bounds viewport = fx(() -> { + var headerScroll = grid.lookup(".table-header-scroll"); + return headerScroll.localToScreen(headerScroll.getBoundsInLocal()); + }); move(new Point2D(viewport.getMaxX() - 10, point(fx(() -> header("type")), .5, .5).getY())); waitFor(() -> scroll.getHvalue() > .1, "Native edge hover scrolls"); capture("edge-scroll"); diff --git a/gson/pom.xml b/gson/pom.xml index 63bb6f3..1b12422 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -7,7 +7,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 gson diff --git a/injection/README.md b/injection/README.md index 80a8004..cfd5bbc 100644 --- a/injection/README.md +++ b/injection/README.md @@ -8,13 +8,13 @@ The worked example is the person desk in `fx-example`, package `de.neitzel.fx.ex ## Quick start -Depend on the module. It brings Reflections 0.10.2 and `jakarta.inject-api` 2.0.1 (plus shared logging dependencies). The parent artifact is `de.neitzel.lib:neitzellib:1.0.2`. Java 25. +Depend on the module. It brings Reflections 0.10.2 and `jakarta.inject-api` 2.0.1 (plus shared logging dependencies). The parent artifact is `de.neitzel.lib:neitzellib:1.0.3`. Java 25. ```xml de.neitzel.lib injection - 1.0.2 + 1.0.3 ``` diff --git a/injection/pom.xml b/injection/pom.xml index 9a5b200..be7ac89 100644 --- a/injection/pom.xml +++ b/injection/pom.xml @@ -7,7 +7,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 injection diff --git a/log4j/pom.xml b/log4j/pom.xml index 8c14246..53224d3 100644 --- a/log4j/pom.xml +++ b/log4j/pom.xml @@ -7,7 +7,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 log4j diff --git a/net/pom.xml b/net/pom.xml index e76d228..354a179 100644 --- a/net/pom.xml +++ b/net/pom.xml @@ -7,7 +7,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 net diff --git a/pom.xml b/pom.xml index f0d62b6..dc0ef93 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 pom diff --git a/quarkus/pom.xml b/quarkus/pom.xml index 90c36f4..9d76e8d 100644 --- a/quarkus/pom.xml +++ b/quarkus/pom.xml @@ -7,7 +7,7 @@ de.neitzel.lib neitzellib - 1.0.2 + 1.0.3 quarkus