Skip to content

Commit

Permalink
Set codegen save dir to default if it's invalid (#772)
Browse files Browse the repository at this point in the history
* Set codegen save dir to default if it's invalid

Fixes #764

* Change logger level from info to warning
  • Loading branch information
SamCarlberg authored Jan 6, 2017
1 parent 7bfae24 commit b4c25eb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +35,9 @@
*/
public class CodeGenerationOptionsController {

private static final Logger logger =
Logger.getLogger(CodeGenerationOptionsController.class.getName());

@Inject
private SettingsProvider settingsProvider;
@Inject
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit b4c25eb

Please sign in to comment.