feat: add directory selectors for AI test toolchain

This commit is contained in:
张家训
2026-08-05 11:19:32 +08:00
parent c21a3c43a1
commit 8db8976bed
2 changed files with 103 additions and 0 deletions
@@ -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<Window> ownerSupplier;
private final TextField pathField = new TextField();
private final Button chooseButton = new Button("选择目录");
public DirectoryPathSelector(String label, Supplier<Window> 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()));
}
}
@@ -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()));
}
}