saving preferences to file

This commit is contained in:
Andreas Billmann
2016-01-11 11:14:25 +01:00
parent 6348c018df
commit c87267a0df
3 changed files with 104 additions and 9 deletions

View File

@@ -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.CSVRow;
import ninja.javafx.smartcsv.fx.table.model.CSVValue; import ninja.javafx.smartcsv.fx.table.model.CSVValue;
import ninja.javafx.smartcsv.preferences.PreferencesFileReader; import ninja.javafx.smartcsv.preferences.PreferencesFileReader;
import ninja.javafx.smartcsv.preferences.PreferencesFileWriter;
import ninja.javafx.smartcsv.validation.ValidationError; import ninja.javafx.smartcsv.validation.ValidationError;
import ninja.javafx.smartcsv.validation.ValidationFileReader; import ninja.javafx.smartcsv.validation.ValidationFileReader;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -58,7 +59,6 @@ import org.supercsv.prefs.CsvPreference;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.Optional; import java.util.Optional;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@@ -73,6 +73,16 @@ import static javafx.application.Platform.runLater;
@Component @Component
public class SmartCSVController extends FXMLController { 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 // injections
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -80,6 +90,9 @@ public class SmartCSVController extends FXMLController {
@Autowired @Autowired
private PreferencesFileReader preferencesLoader; private PreferencesFileReader preferencesLoader;
@Autowired
private PreferencesFileWriter preferencesWriter;
@Autowired @Autowired
private CSVFileReader csvLoader; private CSVFileReader csvLoader;
@@ -207,7 +220,9 @@ public class SmartCSVController extends FXMLController {
Optional<ButtonType> result = alert.showAndWait(); Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){ 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() { private void loadCsvPreferences() {
try { try {
File preferencesFile = new File( if (PREFERENCES_FILE.exists()) {
getClass().getResource("/ninja/javafx/smartcsv/fx/preferences/preferences.json").toURI()); preferencesLoader.read(PREFERENCES_FILE);
preferencesLoader.read(preferencesFile); }
setCsvPreference(preferencesLoader.getCSVpreference()); setCsvPreference(preferencesLoader.getCSVpreference());
} catch (IOException | URISyntaxException e) { } catch (IOException e) {
e.printStackTrace(); 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) { private void setCsvPreference(CsvPreference csvPreference) {
csvLoader.setCsvPreference(csvPreference); csvLoader.setCsvPreference(csvPreference);
csvFileWriter.setCsvPreference(csvPreference); csvFileWriter.setCsvPreference(csvPreference);

View File

@@ -32,9 +32,6 @@ import ninja.javafx.smartcsv.FileReader;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.supercsv.prefs.CsvPreference; import org.supercsv.prefs.CsvPreference;
import org.supercsv.quote.AlwaysQuoteMode; 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.File;
import java.io.IOException; import java.io.IOException;

View File

@@ -0,0 +1,61 @@
/*
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015 javafx.ninja <info@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<String, Object> 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());
}
}