From 8db8976bed54ec483a108b00e813d4ce1ed44c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AE=B6=E8=AE=AD?= Date: Wed, 5 Aug 2026 11:19:32 +0800 Subject: [PATCH] feat: add directory selectors for AI test toolchain --- .../biz/aitest/ui/DirectoryPathSelector.java | 67 +++++++++++++++++++ .../aitest/ui/DirectoryPathSelectorTest.java | 36 ++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/main/java/com/example/workutils/biz/aitest/ui/DirectoryPathSelector.java create mode 100644 src/test/java/com/example/workutils/biz/aitest/ui/DirectoryPathSelectorTest.java diff --git a/src/main/java/com/example/workutils/biz/aitest/ui/DirectoryPathSelector.java b/src/main/java/com/example/workutils/biz/aitest/ui/DirectoryPathSelector.java new file mode 100644 index 0000000..7443b7e --- /dev/null +++ b/src/main/java/com/example/workutils/biz/aitest/ui/DirectoryPathSelector.java @@ -0,0 +1,67 @@ +package com.example.workutils.biz.aitest.ui; + +import javafx.geometry.Pos; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.stage.DirectoryChooser; +import javafx.stage.Window; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.function.Supplier; + +public class DirectoryPathSelector extends HBox { + private final String label; + private final Supplier ownerSupplier; + private final TextField pathField = new TextField(); + private final Button chooseButton = new Button("选择目录"); + + public DirectoryPathSelector(String label, Supplier ownerSupplier) { + super(8); + this.label = label; + this.ownerSupplier = ownerSupplier; + pathField.setEditable(false); + pathField.setPromptText("请选择" + label); + pathField.setAccessibleText(label); + chooseButton.setAccessibleText("选择 " + label + " 目录"); + chooseButton.setOnAction(event -> chooseDirectory()); + setAlignment(Pos.CENTER_LEFT); + HBox.setHgrow(pathField, Priority.ALWAYS); + getChildren().addAll(pathField, chooseButton); + } + + public Path getPath() { + return pathField.getText().isBlank() ? Path.of("") : Path.of(pathField.getText()); + } + + public void setPath(Path path) { + pathField.setText(path == null || path.toString().isBlank() ? "" : path.toString()); + } + + public TextField pathField() { + return pathField; + } + + public Button chooseButton() { + return chooseButton; + } + + void applySelection(File selected) { + if (selected != null) { + setPath(selected.toPath()); + } + } + + private void chooseDirectory() { + DirectoryChooser chooser = new DirectoryChooser(); + chooser.setTitle("选择 " + label + " 目录"); + Path current = getPath(); + if (!current.toString().isBlank() && Files.isDirectory(current)) { + chooser.setInitialDirectory(current.toFile()); + } + applySelection(chooser.showDialog(ownerSupplier.get())); + } +} diff --git a/src/test/java/com/example/workutils/biz/aitest/ui/DirectoryPathSelectorTest.java b/src/test/java/com/example/workutils/biz/aitest/ui/DirectoryPathSelectorTest.java new file mode 100644 index 0000000..c070c9e --- /dev/null +++ b/src/test/java/com/example/workutils/biz/aitest/ui/DirectoryPathSelectorTest.java @@ -0,0 +1,36 @@ +package com.example.workutils.biz.aitest.ui; + +import com.example.workutils.system.ui.FxTestSupport; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +class DirectoryPathSelectorTest { + + @Test + void pathIsReadOnlyAndCancelKeepsExistingValue() throws Exception { + DirectoryPathSelector selector = FxTestSupport.fx( + () -> new DirectoryPathSelector("JDK 8 Home", () -> null)); + FxTestSupport.fx(() -> selector.setPath(Path.of("C:/jdk8"))); + FxTestSupport.fx(() -> selector.applySelection(null)); + + assertFalse(FxTestSupport.fx(() -> selector.pathField().isEditable())); + assertEquals(Path.of("C:/jdk8"), FxTestSupport.fx(selector::getPath)); + } + + @Test + void selectedDirectoryUpdatesValue() throws Exception { + DirectoryPathSelector selector = FxTestSupport.fx( + () -> new DirectoryPathSelector("Maven Home", () -> null)); + + FxTestSupport.fx(() -> selector.applySelection(new File("C:/maven"))); + + assertEquals(Path.of("C:/maven"), FxTestSupport.fx(selector::getPath)); + assertEquals("选择 Maven Home 目录", FxTestSupport.fx( + () -> selector.chooseButton().getAccessibleText())); + } +}