统一导出历史表格列宽

This commit is contained in:
张家训
2026-08-01 20:50:08 +08:00
parent 19f27f13c6
commit bd959825a3
2 changed files with 113 additions and 8 deletions
@@ -23,6 +23,13 @@ import java.util.function.Function;
public final class ExportHistoryPane extends VBox {
private static final double ACTION_WIDTH = 150;
private static final double INDEX_WIDTH = 64;
private static final double QUERY_WIDTH = 140;
private static final double FILE_MIN_WIDTH = 220;
private static final double COUNT_WIDTH = 88;
private static final double TIME_WIDTH = 180;
private final TableView<HistoryItem> table = new TableView<>();
public ExportHistoryPane(
@@ -64,12 +71,24 @@ public final class ExportHistoryPane extends VBox {
null, fontSize));
table.getStyleClass().add("export-history-table");
table.getColumns().add(actionColumn(commands, fontSize));
table.getColumns().add(textColumn("序号", HistoryItem::index));
table.getColumns().add(textColumn(queryColumnTitle, HistoryItem::query));
table.getColumns().add(fileColumn());
table.getColumns().add(textColumn(countColumnTitle, HistoryItem::count));
table.getColumns().add(textColumn("时间", HistoryItem::timestamp));
TableColumn<HistoryItem, Void> actionColumn = actionColumn(commands, fontSize);
TableColumn<HistoryItem, String> indexColumn = textColumn("序号", HistoryItem::index);
TableColumn<HistoryItem, String> queryColumn = textColumn(queryColumnTitle, HistoryItem::query);
TableColumn<HistoryItem, String> fileColumn = fileColumn();
TableColumn<HistoryItem, String> countColumn = textColumn(countColumnTitle, HistoryItem::count);
TableColumn<HistoryItem, String> timeColumn = textColumn("时间", HistoryItem::timestamp);
fixWidth(actionColumn, ACTION_WIDTH);
fixWidth(indexColumn, INDEX_WIDTH);
fixWidth(queryColumn, QUERY_WIDTH);
fileColumn.setMinWidth(FILE_MIN_WIDTH);
fileColumn.setMaxWidth(Double.MAX_VALUE);
fileColumn.setResizable(true);
fixWidth(countColumn, COUNT_WIDTH);
fixWidth(timeColumn, TIME_WIDTH);
table.getColumns().addAll(
actionColumn, indexColumn, queryColumn, fileColumn, countColumn, timeColumn);
VBox.setVgrow(table, Priority.ALWAYS);
getChildren().add(table);
@@ -79,6 +98,13 @@ public final class ExportHistoryPane extends VBox {
return table;
}
private void fixWidth(TableColumn<HistoryItem, ?> column, double width) {
column.setMinWidth(width);
column.setPrefWidth(width);
column.setMaxWidth(width);
column.setResizable(false);
}
private TableColumn<HistoryItem, String> textColumn(
String title, Function<HistoryItem, String> value) {
TableColumn<HistoryItem, String> column = new TableColumn<>(title);
@@ -106,8 +132,6 @@ public final class ExportHistoryPane extends VBox {
private TableColumn<HistoryItem, Void> actionColumn(
HistoryFileCommands commands, int fontSize) {
TableColumn<HistoryItem, Void> column = new TableColumn<>("操作");
column.setPrefWidth(150);
column.setResizable(false);
column.setCellFactory(ignored -> new TableCell<>() {
private final Button open = StyledButton.small(
"打开", Theme.base(fontSize - 1));
@@ -0,0 +1,81 @@
package com.example.workutils.system.ui.history;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import org.junit.jupiter.api.Test;
import java.util.List;
import static com.example.workutils.system.ui.FxTestSupport.fx;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ExportHistoryPaneLayoutTest {
@Test
void usesSixSemanticColumnsWithOnlyTheFileColumnResizable() throws Exception {
fx(() -> {
ExportHistoryPane pane = pane();
List<TableColumn<HistoryItem, ?>> columns = List.copyOf(pane.table().getColumns());
assertEquals(List.of("操作", "序号", "导出格式", "生成文件", "文件数", "时间"),
columns.stream().map(TableColumn::getText).toList());
assertEquals(6, columns.size());
assertFixed(columns.get(0), 150);
assertFixed(columns.get(1), 64);
assertFixed(columns.get(2), 140);
assertTrue(columns.get(3).isResizable());
assertEquals(220, columns.get(3).getMinWidth());
assertFixed(columns.get(4), 88);
assertFixed(columns.get(5), 180);
});
}
@Test
void wideningTheTableGivesAllAdditionalSpaceToTheFileColumn() throws Exception {
fx(() -> {
ExportHistoryPane pane = pane();
TableView<HistoryItem> table = pane.table();
StackPane root = new StackPane(pane);
Scene scene = new Scene(root, 900, 420);
root.applyCss();
root.layout();
List<Double> before = table.getColumns().stream()
.map(TableColumn::getWidth).toList();
scene.getWindow();
root.resize(1200, 420);
root.applyCss();
root.layout();
List<Double> after = table.getColumns().stream()
.map(TableColumn::getWidth).toList();
for (int index : List.of(0, 1, 2, 4, 5)) {
assertEquals(before.get(index), after.get(index), 0.5);
}
assertTrue(after.get(3) > before.get(3) + 250,
"the file column must absorb the widened table space");
});
}
private static ExportHistoryPane pane() {
HistoryFileCommands commands = new HistoryFileCommands(
ignored -> { }, ignored -> { }, ignored -> { }, ignored -> { });
HistoryItem item = new HistoryItem(
"1", "HTML", "D:/exports/api-document.html", "1", "2026-07-31 20:06:30");
return new ExportHistoryPane(
"导出格式", "文件数", FXCollections.observableArrayList(item), commands, 14);
}
private static void assertFixed(TableColumn<HistoryItem, ?> column, double width) {
assertFalse(column.isResizable());
assertEquals(width, column.getMinWidth());
assertEquals(width, column.getPrefWidth());
assertEquals(width, column.getMaxWidth());
}
}