From b4c25ebaf5fde38787d19094ebeba3ace474d06d Mon Sep 17 00:00:00 2001 From: Sam Carlberg Date: Fri, 6 Jan 2017 15:33:46 -0500 Subject: [PATCH] Set codegen save dir to default if it's invalid (#772) * Set codegen save dir to default if it's invalid Fixes #764 * Change logger level from info to warning --- .../CodeGenerationSettingsConverter.java | 13 +++++++++++++ .../core/settings/CodeGenerationSettings.java | 16 ++++++++++++++++ .../CodeGenerationOptionsController.java | 9 +++++++++ 3 files changed, 38 insertions(+) diff --git a/core/src/main/java/edu/wpi/grip/core/serialization/CodeGenerationSettingsConverter.java b/core/src/main/java/edu/wpi/grip/core/serialization/CodeGenerationSettingsConverter.java index b419617f14..9a5fb56b93 100644 --- a/core/src/main/java/edu/wpi/grip/core/serialization/CodeGenerationSettingsConverter.java +++ b/core/src/main/java/edu/wpi/grip/core/serialization/CodeGenerationSettingsConverter.java @@ -9,11 +9,18 @@ import com.thoughtworks.xstream.converters.reflection.ReflectionConverter; import com.thoughtworks.xstream.io.HierarchicalStreamReader; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.logging.Logger; + /** * XStream converter for {@link edu.wpi.grip.core.settings.CodeGenerationSettings}. */ public class CodeGenerationSettingsConverter extends ReflectionConverter { + private static final Logger logger = + Logger.getLogger(CodeGenerationSettingsConverter.class.getName()); + private final EventBus eventBus; @Inject @@ -28,6 +35,12 @@ public CodeGenerationSettingsConverter(Project project, EventBus eventBus) { public CodeGenerationSettings unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { CodeGenerationSettings settings = (CodeGenerationSettings) super.unmarshal(reader, context); + if (!Files.isDirectory(Paths.get(settings.getSaveDir()))) { + logger.warning("Save dir '" + settings.getSaveDir() + "' does not exist; using default"); + settings = CodeGenerationSettings.builder(settings) + .saveDir(CodeGenerationSettings.DEFAULT_SETTINGS.getSaveDir()) + .build(); + } eventBus.post(new CodeGenerationSettingsChangedEvent(settings)); return settings; } diff --git a/core/src/main/java/edu/wpi/grip/core/settings/CodeGenerationSettings.java b/core/src/main/java/edu/wpi/grip/core/settings/CodeGenerationSettings.java index 387a28224e..1f3626dbaa 100644 --- a/core/src/main/java/edu/wpi/grip/core/settings/CodeGenerationSettings.java +++ b/core/src/main/java/edu/wpi/grip/core/settings/CodeGenerationSettings.java @@ -106,6 +106,22 @@ public static Builder builder() { return new Builder(); } + /** + * Creates a builder with defaults from the given settings. + * + * @param defaultSettings the default settings for the builder to use + * @return a settings builder + */ + public static Builder builder(CodeGenerationSettings defaultSettings) { + return new Builder() + .language(defaultSettings.getLanguage()) + .className(defaultSettings.getClassName()) + .implementVisionPipeline(defaultSettings.shouldImplementWpilibPipeline()) + .saveDir(defaultSettings.getSaveDir()) + .packageName(defaultSettings.getPackageName()) + .moduleName(defaultSettings.getModuleName()); + } + public static final class Builder { private String language; diff --git a/ui/src/main/java/edu/wpi/grip/ui/codegeneration/CodeGenerationOptionsController.java b/ui/src/main/java/edu/wpi/grip/ui/codegeneration/CodeGenerationOptionsController.java index b853a60a13..cfad2cce0f 100644 --- a/ui/src/main/java/edu/wpi/grip/ui/codegeneration/CodeGenerationOptionsController.java +++ b/ui/src/main/java/edu/wpi/grip/ui/codegeneration/CodeGenerationOptionsController.java @@ -13,6 +13,8 @@ import org.bytedeco.javacpp.opencv_core.Mat; import java.io.File; +import java.nio.file.Files; +import java.util.logging.Logger; import javafx.application.Platform; import javafx.collections.FXCollections; @@ -33,6 +35,9 @@ */ public class CodeGenerationOptionsController { + private static final Logger logger = + Logger.getLogger(CodeGenerationOptionsController.class.getName()); + @Inject private SettingsProvider settingsProvider; @Inject @@ -188,6 +193,10 @@ private void loadPythonControls() { private void browseForSave() { DirectoryChooser dc = new DirectoryChooser(); File destDir = new File(settingsProvider.getCodeGenerationSettings().getSaveDir()); + if (!Files.isDirectory(destDir.toPath())) { + logger.warning("Loaded save directory does not exist, setting to default."); + destDir = new File(CodeGenerationSettings.DEFAULT_SETTINGS.getSaveDir()); + } dc.setInitialDirectory(destDir); dc.setTitle("Choose save location"); File save = dc.showDialog(optionsGrid.getScene().getWindow());