Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle config path renames #903

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/main/java/network/brightspots/rcv/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,31 @@
final class FileUtils {

// cache location for finding and creating user files and folders
private static String userDirectory = null;
private static String initialDirectory = null;

private FileUtils() {}

// return userDirectory if it exists
// fallback to current working directory
static String getUserDirectory() {
return userDirectory == null ? System.getProperty("user.dir") : userDirectory;
// When working with filesystem throughout RCTab
// remember the latest folder the user was working in
// fallback to current working directory if that hasn't been set
static String getInitialDirectory() {
String result;
if (initialDirectory == null) {
result = System.getProperty("user.dir");
} else if (Files.isDirectory(new File(initialDirectory).toPath())) {
result = initialDirectory;
} else {
Logger.info("Most recent .config load/save was done at path %s."
+ " This path no longer exists. Falling back to current working directory.",
initialDirectory);
result = System.getProperty("user.dir");
}

return result;
}

static void setUserDirectory(String userDirectory) {
FileUtils.userDirectory = userDirectory;
static void setInitialDirectory(String initialDirectory) {
FileUtils.initialDirectory = initialDirectory;
}

static void createOutputDirectory(String dir) throws UnableToCreateDirectoryException {
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/network/brightspots/rcv/GuiConfigController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.setInitialDirectory(fileToLoad.getParent());
// load and cache the config object
GuiContext.getInstance()
.setConfig(ContestConfig.loadContestConfig(fileToLoad.getAbsolutePath(), silentMode));
Expand All @@ -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.getInitialDirectory()));
} else {
fc.setInitialDirectory(new File(selectedFile.getParent()));
}
Expand All @@ -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.getInitialDirectory()));
} else {
fc.setInitialDirectory(new File(selectedFile.getParent()));
fc.setInitialFileName(selectedFile.getName());
Expand Down Expand Up @@ -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.setInitialDirectory(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
Expand Down Expand Up @@ -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.getInitialDirectory());
ValidatorService service = new ValidatorService(config);
setUpAndStartService(service);
}
Expand All @@ -519,8 +519,9 @@ public void menuItemValidateClicked() {
*/
public void menuItemTabulateClicked() {
setGuiIsBusy(true);
ContestConfig config =
ContestConfig.loadContestConfig(createRawContestConfig(), FileUtils.getUserDirectory());
ContestConfig config = ContestConfig.loadContestConfig(
createRawContestConfig(),
FileUtils.getInitialDirectory());
ValidatorService service = new ValidatorService(config);
service.setOnSucceeded(
event -> {
Expand Down Expand Up @@ -658,7 +659,7 @@ public void menuItemExitClicked() {
*/
public void buttonOutputDirectoryClicked() {
DirectoryChooser dc = new DirectoryChooser();
dc.setInitialDirectory(new File(FileUtils.getUserDirectory()));
dc.setInitialDirectory(new File(FileUtils.getInitialDirectory()));
dc.setTitle("Output Directory");
File outputDirectory = dc.showDialog(GuiContext.getInstance().getMainWindow());
if (outputDirectory != null) {
Expand All @@ -675,7 +676,7 @@ public void buttonClearDatePickerContestDateClicked() {

private List<File> chooseFile(Provider provider, ExtensionFilter filter) {
FileChooser fc = new FileChooser();
fc.setInitialDirectory(new File(FileUtils.getUserDirectory()));
fc.setInitialDirectory(new File(FileUtils.getInitialDirectory()));
fc.getExtensionFilters().add(filter);
fc.setTitle("Select " + provider + " Cast Vote Record Files");
return fc.showOpenMultipleDialog(GuiContext.getInstance().getMainWindow());
Expand All @@ -696,7 +697,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.getInitialDirectory()));
dc.setTitle("Select " + provider + " Cast Vote Record Folder");
selectedDirectory = dc.showDialog(GuiContext.getInstance().getMainWindow());
}
Expand Down Expand Up @@ -889,8 +890,9 @@ public void buttonDeleteCvrFileClicked() {
/** Action when "Auto-Load Candidates" button is clicked. */
public void buttonAutoLoadCandidatesClicked() {
setGuiIsBusy(true);
String dir = FileUtils.getInitialDirectory();
ContestConfig config =
ContestConfig.loadContestConfig(createRawContestConfig(), FileUtils.getUserDirectory());
ContestConfig.loadContestConfig(createRawContestConfig(), dir);
AutoLoadCandidatesService service = new AutoLoadCandidatesService(
config,
tableViewCvrFiles.getItems(),
Expand Down
Loading