diff --git a/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java b/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java index 31f0220..68b60fd 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java @@ -49,6 +49,7 @@ import ninja.javafx.smartcsv.fx.table.model.CSVModel; import ninja.javafx.smartcsv.fx.table.model.CSVRow; import ninja.javafx.smartcsv.fx.table.model.CSVValue; import ninja.javafx.smartcsv.preferences.PreferencesFileReader; +import ninja.javafx.smartcsv.preferences.PreferencesFileWriter; import ninja.javafx.smartcsv.validation.ValidationError; import ninja.javafx.smartcsv.validation.ValidationFileReader; import org.springframework.beans.factory.annotation.Autowired; @@ -58,7 +59,6 @@ import org.supercsv.prefs.CsvPreference; import java.io.File; import java.io.IOException; -import java.net.URISyntaxException; import java.net.URL; import java.util.Optional; import java.util.ResourceBundle; @@ -73,6 +73,16 @@ import static javafx.application.Platform.runLater; @Component public class SmartCSVController extends FXMLController { + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // constants + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + private static final File PREFERENCES_FILE = new File(System.getProperty("user.home") + + File.separator + + ".SmartCSV.fx" + + File.separator + "" + + "preferences.json"); + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // injections //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -80,6 +90,9 @@ public class SmartCSVController extends FXMLController { @Autowired private PreferencesFileReader preferencesLoader; + @Autowired + private PreferencesFileWriter preferencesWriter; + @Autowired private CSVFileReader csvLoader; @@ -207,7 +220,9 @@ public class SmartCSVController extends FXMLController { Optional result = alert.showAndWait(); if (result.get() == ButtonType.OK){ - setCsvPreference(preferencesController.getCsvPreference()); + CsvPreference csvPreference = preferencesController.getCsvPreference(); + setCsvPreference(csvPreference); + saveCsvPreferences(csvPreference); } } @@ -234,15 +249,37 @@ public class SmartCSVController extends FXMLController { private void loadCsvPreferences() { try { - File preferencesFile = new File( - getClass().getResource("/ninja/javafx/smartcsv/fx/preferences/preferences.json").toURI()); - preferencesLoader.read(preferencesFile); + if (PREFERENCES_FILE.exists()) { + preferencesLoader.read(PREFERENCES_FILE); + } setCsvPreference(preferencesLoader.getCSVpreference()); - } catch (IOException | URISyntaxException e) { + } catch (IOException e) { e.printStackTrace(); } } + private void saveCsvPreferences(CsvPreference csvPreference) { + try { + createPreferenceFile(); + preferencesWriter.saveFile(PREFERENCES_FILE, csvPreference); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void createPreferenceFile() throws IOException { + if (!PREFERENCES_FILE.exists()) { + createPreferencesFileFolder(); + PREFERENCES_FILE.createNewFile(); + } + } + + private void createPreferencesFileFolder() { + if (!PREFERENCES_FILE.getParentFile().exists()) { + PREFERENCES_FILE.getParentFile().mkdir(); + } + } + private void setCsvPreference(CsvPreference csvPreference) { csvLoader.setCsvPreference(csvPreference); csvFileWriter.setCsvPreference(csvPreference); diff --git a/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileReader.java b/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileReader.java index 8487413..bae0290 100644 --- a/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileReader.java +++ b/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileReader.java @@ -32,9 +32,6 @@ import ninja.javafx.smartcsv.FileReader; import org.springframework.stereotype.Service; import org.supercsv.prefs.CsvPreference; import org.supercsv.quote.AlwaysQuoteMode; -import org.supercsv.quote.ColumnQuoteMode; -import org.supercsv.quote.NormalQuoteMode; -import org.supercsv.quote.QuoteMode; import java.io.File; import java.io.IOException; diff --git a/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileWriter.java b/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileWriter.java new file mode 100644 index 0000000..516705f --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileWriter.java @@ -0,0 +1,61 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +package ninja.javafx.smartcsv.preferences; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigRenderOptions; +import org.springframework.stereotype.Service; +import org.supercsv.prefs.CsvPreference; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static java.nio.file.Files.write; + +/** + * Save preferences to configuration file + */ +@Service +public class PreferencesFileWriter { + + public void saveFile(File file, CsvPreference csvPreference) throws IOException { + Map preferences = new HashMap<>(); + preferences.put("quoteChar", Character.toString(csvPreference.getQuoteChar())); + preferences.put("delimiterChar", Character.toString((char)csvPreference.getDelimiterChar())); + preferences.put("endOfLineSymbols", csvPreference.getEndOfLineSymbols()); + preferences.put("surroundingSpacesNeedQuotes", csvPreference.isSurroundingSpacesNeedQuotes()); + preferences.put("ignoreEmptyLines", csvPreference.isIgnoreEmptyLines()); + preferences.put("quoteMode", QuoteModeHelper.getQuoteModeName(csvPreference.getQuoteMode())); + + Config config = ConfigFactory.parseMap(preferences); + write(file.toPath(), config.root().render(ConfigRenderOptions.concise()).getBytes()); + } + +}