From 896e3091d9ba915f3765d239b45f0c803dfafa22 Mon Sep 17 00:00:00 2001 From: yezr <8996546+yezr@users.noreply.github.com> Date: Mon, 25 Nov 2024 13:53:24 -0500 Subject: [PATCH] rename userDirectory to configDirectory more details in exception when configDirectory missing always use user.dir as fallback --- .../network/brightspots/rcv/FileUtils.java | 23 ++++++++++--------- .../brightspots/rcv/GuiConfigController.java | 20 ++++++++-------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main/java/network/brightspots/rcv/FileUtils.java b/src/main/java/network/brightspots/rcv/FileUtils.java index cf2d23918..7f01070a2 100644 --- a/src/main/java/network/brightspots/rcv/FileUtils.java +++ b/src/main/java/network/brightspots/rcv/FileUtils.java @@ -31,29 +31,30 @@ final class FileUtils { // cache location for finding and creating user files and folders - private static String userDirectory = null; + private static String configDirectory = null; private FileUtils() {} - // return userDirectory if it exists + // return configDirectory if it exists // fallback to current working directory - static String getUserDirectory() { + static String getConfigDirectory() { String result; - if (userDirectory == null) { + if (configDirectory == null) { result = System.getProperty("user.dir"); - } else if (Files.isDirectory(new File(userDirectory).toPath())) { - result = userDirectory; + } else if (Files.isDirectory(new File(configDirectory).toPath())) { + result = configDirectory; } else { - Logger.warning("User directory %s does not exist. Using current working directory.", - userDirectory); - result = System.getProperty("user.home"); + Logger.info("Most recent .config load/save was done at path %s." + + " This path no longer exists. Falling back to current working directory.", + configDirectory); + result = System.getProperty("user.dir"); } return result; } - static void setUserDirectory(String userDirectory) { - FileUtils.userDirectory = userDirectory; + static void setConfigDirectory(String configDirectory) { + FileUtils.configDirectory = configDirectory; } static void createOutputDirectory(String dir) throws UnableToCreateDirectoryException { diff --git a/src/main/java/network/brightspots/rcv/GuiConfigController.java b/src/main/java/network/brightspots/rcv/GuiConfigController.java index c97ce6258..47cc65a9d 100644 --- a/src/main/java/network/brightspots/rcv/GuiConfigController.java +++ b/src/main/java/network/brightspots/rcv/GuiConfigController.java @@ -389,7 +389,7 @@ public void menuItemNewConfigClicked() { private void loadFile(File fileToLoad, boolean silentMode) { // set the user dir for future loads - FileUtils.setUserDirectory(fileToLoad.getParent()); + FileUtils.setConfigDirectory(fileToLoad.getParent()); // load and cache the config object GuiContext.getInstance() .setConfig(ContestConfig.loadContestConfig(fileToLoad.getAbsolutePath(), silentMode)); @@ -411,7 +411,7 @@ public void menuItemLoadConfigClicked() { if (checkForSaveAndContinue()) { FileChooser fc = new FileChooser(); if (selectedFile == null) { - fc.setInitialDirectory(new File(FileUtils.getUserDirectory())); + fc.setInitialDirectory(new File(FileUtils.getConfigDirectory())); } else { fc.setInitialDirectory(new File(selectedFile.getParent())); } @@ -432,7 +432,7 @@ public String getSelectedFilePath() { private File getSaveFile(Stage stage) { FileChooser fc = new FileChooser(); if (selectedFile == null) { - fc.setInitialDirectory(new File(FileUtils.getUserDirectory())); + fc.setInitialDirectory(new File(FileUtils.getConfigDirectory())); } else { fc.setInitialDirectory(new File(selectedFile.getParent())); fc.setInitialFileName(selectedFile.getName()); @@ -470,7 +470,7 @@ public String saveFile(Button fromButton, boolean useTemporaryFile) { private void saveFile(File fileToSave) { // set save file parent folder as the new default user folder - FileUtils.setUserDirectory(fileToSave.getParent()); + FileUtils.setConfigDirectory(fileToSave.getParent()); // create a rawConfig object from GUI content and serialize it as json JsonParser.writeToFile(fileToSave, createRawContestConfig()); // Reload to keep GUI fields updated in case invalid values are replaced during save process @@ -509,7 +509,7 @@ private void setGuiIsBusy(boolean isBusy) { public void menuItemValidateClicked() { setGuiIsBusy(true); ContestConfig config = - ContestConfig.loadContestConfig(createRawContestConfig(), FileUtils.getUserDirectory()); + ContestConfig.loadContestConfig(createRawContestConfig(), FileUtils.getConfigDirectory()); ValidatorService service = new ValidatorService(config); setUpAndStartService(service); } @@ -520,7 +520,7 @@ public void menuItemValidateClicked() { public void menuItemTabulateClicked() { setGuiIsBusy(true); ContestConfig config = - ContestConfig.loadContestConfig(createRawContestConfig(), FileUtils.getUserDirectory()); + ContestConfig.loadContestConfig(createRawContestConfig(), FileUtils.getConfigDirectory()); ValidatorService service = new ValidatorService(config); service.setOnSucceeded( event -> { @@ -658,7 +658,7 @@ public void menuItemExitClicked() { */ public void buttonOutputDirectoryClicked() { DirectoryChooser dc = new DirectoryChooser(); - dc.setInitialDirectory(new File(FileUtils.getUserDirectory())); + dc.setInitialDirectory(new File(FileUtils.getConfigDirectory())); dc.setTitle("Output Directory"); File outputDirectory = dc.showDialog(GuiContext.getInstance().getMainWindow()); if (outputDirectory != null) { @@ -675,7 +675,7 @@ public void buttonClearDatePickerContestDateClicked() { private List chooseFile(Provider provider, ExtensionFilter filter) { FileChooser fc = new FileChooser(); - fc.setInitialDirectory(new File(FileUtils.getUserDirectory())); + fc.setInitialDirectory(new File(FileUtils.getConfigDirectory())); fc.getExtensionFilters().add(filter); fc.setTitle("Select " + provider + " Cast Vote Record Files"); return fc.showOpenMultipleDialog(GuiContext.getInstance().getMainWindow()); @@ -696,7 +696,7 @@ public void buttonCvrFilePathClicked() { chooseFile(provider, new ExtensionFilter("CSV file(s)", "*.csv")); case DOMINION, HART -> { DirectoryChooser dc = new DirectoryChooser(); - dc.setInitialDirectory(new File(FileUtils.getUserDirectory())); + dc.setInitialDirectory(new File(FileUtils.getConfigDirectory())); dc.setTitle("Select " + provider + " Cast Vote Record Folder"); selectedDirectory = dc.showDialog(GuiContext.getInstance().getMainWindow()); } @@ -890,7 +890,7 @@ public void buttonDeleteCvrFileClicked() { public void buttonAutoLoadCandidatesClicked() { setGuiIsBusy(true); ContestConfig config = - ContestConfig.loadContestConfig(createRawContestConfig(), FileUtils.getUserDirectory()); + ContestConfig.loadContestConfig(createRawContestConfig(), FileUtils.getConfigDirectory()); AutoLoadCandidatesService service = new AutoLoadCandidatesService( config, tableViewCvrFiles.getItems(),