From 045742a8e6452b521518f88646009fc4152b2b1d Mon Sep 17 00:00:00 2001 From: hizumiaoba <56146205+hizumiaoba@users.noreply.github.com> Date: Sun, 14 Apr 2024 14:29:38 +0900 Subject: [PATCH] fix: improve file selecting logic (#33) * extract logic for opening file dialog * fix platform issue * fix typo * delete error popup logic for aborting the operation --- .../mctimemachine/MainController.java | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/main/java/io/github/hizumiaoba/mctimemachine/MainController.java b/src/main/java/io/github/hizumiaoba/mctimemachine/MainController.java index cc7994b..87ed001 100644 --- a/src/main/java/io/github/hizumiaoba/mctimemachine/MainController.java +++ b/src/main/java/io/github/hizumiaoba/mctimemachine/MainController.java @@ -30,7 +30,6 @@ import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory; import javafx.scene.control.TabPane; import javafx.scene.control.TextField; -import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -301,47 +300,50 @@ void onOpenSavesFolderBtnClick() { @FXML void onSelectBackupSavingFolderBtnClick() { - DirectoryChooser dc = new DirectoryChooser(); - dc.setTitle("バックアップを保存するフォルダを選択してください。"); - dc.setInitialDirectory(new File(System.getProperty("user.home"))); - log.debug("awaiting for the user to select the backup saving folder."); - File f = dc.showDialog(null); + File f = openFileChooser("バックアップを保存するフォルダを選択してください。", + new File(System.getProperty("user.home"))); if (f == null) { - log.debug("Got nothing."); return; } - log.debug("Got the folder: {}", f.getAbsolutePath()); - backupSavingFolderPathField.setText(f.getAbsolutePath()); + assignPath(backupSavingFolderPathField, f); } @FXML - void onSelectLauncherExeBtnClick() { - FileChooser fc = new FileChooser(); - fc.setTitle("ランチャーの実行ファイルを選択してください。"); - fc.setInitialDirectory(new File(System.getProperty("user.home"))); - log.debug("awaiting for the user to select the executable file."); - File f = fc.showOpenDialog(null); + void onSelectSavesFolderBtnClick() { + File f = openFileChooser("\".minecraft/saves\"フォルダを選択してください。", + new File(System.getProperty("user.home"))); if (f == null) { - log.debug("Got nothing."); return; } - log.debug("Got the file: {}", f.getAbsolutePath()); - launcherExePathField.setText(f.getAbsolutePath()); + assignPath(savesFolderPathField, f); } @FXML - void onSelectSavesFolderBtnClick() { - DirectoryChooser dc = new DirectoryChooser(); - dc.setTitle("\".minecraft/saves\"フォルダを選択してください。"); - dc.setInitialDirectory(new File(System.getProperty("user.home"))); - log.debug("awaiting for the user to select the saves folder."); - File f = dc.showDialog(null); - if (f == null) { - log.debug("Got nothing."); - return; - } - log.debug("Got the folder: {}", f.getAbsolutePath()); - savesFolderPathField.setText(f.getAbsolutePath()); + void onSelectLauncherExeBtnClick() { + File f = openFileChooser("ランチャーの実行ファイルを選択してください。", + new File(System.getProperty("user.home"))); + if (f == null) { + return; + } + assignPath(launcherExePathField, f); + } + + private File openFileChooser(String title, File initialDir) { + FileChooser fc = new FileChooser(); + fc.setTitle(title); + fc.setInitialDirectory(initialDir); + log.debug("awaiting for the user to select the file."); + File f = fc.showOpenDialog(null); + if (f == null) { + log.debug("Got nothing."); + return null; + } + return f; + } + + private void assignPath(TextField injected, File target) { + log.debug("injecting the path: {}", target.getAbsolutePath()); + injected.setText(target.getAbsolutePath()); } @FXML