From 4cb3cbb491ec5c1e119efd3847929bd41adbcd84 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Mon, 26 Oct 2020 14:12:14 +0300 Subject: [PATCH] Resolve #79 --- src/main/java/nsusbloader/AppPreferences.java | 6 ++-- .../Controllers/FrontController.java | 14 +++----- .../Controllers/NxdtController.java | 8 ++--- .../Controllers/SplitMergeController.java | 14 ++++---- src/main/java/nsusbloader/FilesHelper.java | 32 +++++++++++++++++++ 5 files changed, 50 insertions(+), 24 deletions(-) create mode 100644 src/main/java/nsusbloader/FilesHelper.java diff --git a/src/main/java/nsusbloader/AppPreferences.java b/src/main/java/nsusbloader/AppPreferences.java index a2e6828..9da2df5 100644 --- a/src/main/java/nsusbloader/AppPreferences.java +++ b/src/main/java/nsusbloader/AppPreferences.java @@ -61,7 +61,7 @@ public class AppPreferences { public void setNsIp(String ip){preferences.put("NSIP", ip);} public String getNsIp(){return preferences.get("NSIP", "192.168.1.42");} - public String getRecent(){ return preferences.get("RECENT", System.getProperty("user.home")); } + public String getRecent(){ return FilesHelper.getRealFolder(preferences.get("RECENT", System.getProperty("user.home"))); } public void setRecent(String path){ preferences.put("RECENT", path); } //------------ SETTINGS ------------------// @@ -124,12 +124,12 @@ public class AppPreferences { public int getSplitMergeType(){ return preferences.getInt("SM_TYPE", 0); } public void setSplitMergeType(int value){ preferences.putInt("SM_TYPE", value); } - public String getSplitMergeRecent(){ return preferences.get("SM_RECENT", System.getProperty("user.home")); } + public String getSplitMergeRecent(){ return FilesHelper.getRealFolder(preferences.get("SM_RECENT", System.getProperty("user.home"))); } public void setSplitMergeRecent(String value){ preferences.put("SM_RECENT", value); } // RCM // public String getRecentRcm(int num){ return preferences.get(String.format("RCM_%02d", num), ""); } public void setRecentRcm(int num, String value){ preferences.put(String.format("RCM_%02d", num), value); } // NXDT // - public String getNXDTSaveToLocation(){ return preferences.get("nxdt_saveto", System.getProperty("user.home")); } + public String getNXDTSaveToLocation(){ return FilesHelper.getRealFolder(preferences.get("nxdt_saveto", System.getProperty("user.home"))); } public void setNXDTSaveToLocation(String value){ preferences.put("nxdt_saveto", value); } } diff --git a/src/main/java/nsusbloader/Controllers/FrontController.java b/src/main/java/nsusbloader/Controllers/FrontController.java index 3790f40..0b2f12e 100644 --- a/src/main/java/nsusbloader/Controllers/FrontController.java +++ b/src/main/java/nsusbloader/Controllers/FrontController.java @@ -32,6 +32,7 @@ import javafx.stage.FileChooser; import nsusbloader.AppPreferences; import nsusbloader.COM.NET.NETCommunications; import nsusbloader.COM.USB.UsbCommunications; +import nsusbloader.FilesHelper; import nsusbloader.MediatorControl; import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.NSLDataTypes.EModule; @@ -193,11 +194,7 @@ public class FrontController implements Initializable { FileChooser fileChooser = new FileChooser(); fileChooser.setTitle(resourceBundle.getString("btn_OpenFile")); - File validator = new File(previouslyOpenedPath); - if (validator.exists() && validator.isDirectory()) - fileChooser.setInitialDirectory(validator); - else - fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); + fileChooser.setInitialDirectory(new File(FilesHelper.getRealFolder(previouslyOpenedPath))); if (getSelectedProtocol().equals("TinFoil") && MediatorControl.getInstance().getContoller().getSettingsCtrlr().getTinfoilSettings().isXciNszXczSupport()) fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP/XCI/NSZ/XCZ", "*.nsp", "*.xci", "*.nsz", "*.xcz")); @@ -223,11 +220,8 @@ public class FrontController implements Initializable { DirectoryChooser dirChooser = new DirectoryChooser(); dirChooser.setTitle(resourceBundle.getString("btn_OpenFile")); - File validator = new File(previouslyOpenedPath); - if (validator.exists() && validator.isDirectory()) - dirChooser.setInitialDirectory(validator); - else - dirChooser.setInitialDirectory(new File(System.getProperty("user.home"))); + String saveToLocation = FilesHelper.getRealFolder(previouslyOpenedPath); + dirChooser.setInitialDirectory(new File(saveToLocation)); splitFile = dirChooser.showDialog(usbNetPane.getScene().getWindow()); diff --git a/src/main/java/nsusbloader/Controllers/NxdtController.java b/src/main/java/nsusbloader/Controllers/NxdtController.java index 7d696ec..c824cc9 100644 --- a/src/main/java/nsusbloader/Controllers/NxdtController.java +++ b/src/main/java/nsusbloader/Controllers/NxdtController.java @@ -25,6 +25,7 @@ import javafx.scene.control.Label; import javafx.scene.layout.Region; import javafx.stage.DirectoryChooser; import nsusbloader.AppPreferences; +import nsusbloader.FilesHelper; import nsusbloader.MediatorControl; import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.NSLDataTypes.EModule; @@ -52,11 +53,8 @@ public class NxdtController implements Initializable { public void initialize(URL url, ResourceBundle resourceBundle) { this.rb = resourceBundle; - File saveToValidator = new File(AppPreferences.getInstance().getNXDTSaveToLocation()); - if (saveToValidator.exists()) - saveToLocationLbl.setText(saveToValidator.getAbsolutePath()); - else - saveToLocationLbl.setText(System.getProperty("user.home")); + String saveToLocation = AppPreferences.getInstance().getNXDTSaveToLocation(); + saveToLocationLbl.setText(saveToLocation); btnDumpStopImage = new Region(); btnDumpStopImage.getStyleClass().add("regionDump"); diff --git a/src/main/java/nsusbloader/Controllers/SplitMergeController.java b/src/main/java/nsusbloader/Controllers/SplitMergeController.java index 46be04f..2611e33 100644 --- a/src/main/java/nsusbloader/Controllers/SplitMergeController.java +++ b/src/main/java/nsusbloader/Controllers/SplitMergeController.java @@ -20,7 +20,6 @@ package nsusbloader.Controllers; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.Node; import javafx.scene.control.*; import javafx.scene.input.DragEvent; import javafx.scene.input.TransferMode; @@ -29,6 +28,7 @@ import javafx.scene.layout.VBox; import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import nsusbloader.AppPreferences; +import nsusbloader.FilesHelper; import nsusbloader.MediatorControl; import nsusbloader.ModelControllers.CancellableRunnable; import nsusbloader.NSLDataTypes.EModule; @@ -97,10 +97,13 @@ public class SplitMergeController implements Initializable { saveToPathLbl.setText(AppPreferences.getInstance().getSplitMergeRecent()); changeSaveToBtn.setOnAction((actionEvent -> { - DirectoryChooser dc = new DirectoryChooser(); - dc.setTitle(resourceBundle.getString("tabSplMrg_Btn_SelectFolder")); - dc.setInitialDirectory(new File(saveToPathLbl.getText())); - File saveToDir = dc.showDialog(changeSaveToBtn.getScene().getWindow()); + DirectoryChooser directoryChooser = new DirectoryChooser(); + directoryChooser.setTitle(resourceBundle.getString("tabSplMrg_Btn_SelectFolder")); + + String saveToLocation = FilesHelper.getRealFolder(saveToPathLbl.getText()); + directoryChooser.setInitialDirectory(new File(saveToLocation)); + + File saveToDir = directoryChooser.showDialog(changeSaveToBtn.getScene().getWindow()); if (saveToDir != null) saveToPathLbl.setText(saveToDir.getAbsolutePath()); })); @@ -227,7 +230,6 @@ public class SplitMergeController implements Initializable { * */ @FXML private void handleDrop(DragEvent event) { - Node sourceNode = (Node) event.getSource(); File fileDrpd = event.getDragboard().getFiles().get(0); if (fileDrpd.isDirectory()) diff --git a/src/main/java/nsusbloader/FilesHelper.java b/src/main/java/nsusbloader/FilesHelper.java new file mode 100644 index 0000000..5267ada --- /dev/null +++ b/src/main/java/nsusbloader/FilesHelper.java @@ -0,0 +1,32 @@ +/* + Copyright 2019-2020 Dmitry Isaenko + + This file is part of NS-USBloader. + + NS-USBloader is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + NS-USBloader is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with NS-USBloader. If not, see . +*/ +package nsusbloader; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class FilesHelper { + public static String getRealFolder(String path){ + Path splitMergePath = Paths.get(path); + if (Files.notExists(splitMergePath) || Files.isRegularFile(splitMergePath)) + return System.getProperty("user.home"); + return path; + } +}