read and edit csv preferences

This commit is contained in:
Andreas Billmann
2016-01-11 09:03:22 +01:00
parent 5762354d19
commit 789c5012a4
14 changed files with 391 additions and 12 deletions

View File

@@ -0,0 +1,45 @@
/*
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.csv;
import org.supercsv.prefs.CsvPreference;
/**
*
*/
public class CSVConfigurable {
protected CsvPreference csvPreference;
public CSVConfigurable() {
csvPreference = CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
}
public void setCsvPreference(CsvPreference csvPreference) {
this.csvPreference = csvPreference;
}
}

View File

@@ -32,7 +32,6 @@ import ninja.javafx.smartcsv.fx.table.model.CSVRow;
import org.springframework.stereotype.Service;
import org.supercsv.io.CsvMapReader;
import org.supercsv.io.ICsvMapReader;
import org.supercsv.prefs.CsvPreference;
import java.io.File;
import java.io.IOException;
@@ -42,7 +41,7 @@ import java.util.Map;
* reads the csv file and stores the values in csv model
*/
@Service
public class CSVFileReader implements FileReader {
public class CSVFileReader extends CSVConfigurable implements FileReader {
private CSVModel model;
@@ -51,7 +50,7 @@ public class CSVFileReader implements FileReader {
ICsvMapReader mapReader = null;
try {
mapReader = new CsvMapReader(new java.io.FileReader(file.getAbsoluteFile()), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
mapReader = new CsvMapReader(new java.io.FileReader(file.getAbsoluteFile()), csvPreference);
model = new CSVModel(file.getAbsolutePath());
// the header columns are used as the keys to the Map

View File

@@ -31,8 +31,6 @@ import ninja.javafx.smartcsv.fx.table.model.CSVRow;
import org.springframework.stereotype.Service;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.io.ICsvMapWriter;
import org.supercsv.prefs.CsvPreference;
import org.supercsv.quote.AlwaysQuoteMode;
import java.io.FileWriter;
import java.io.IOException;
@@ -44,16 +42,12 @@ import static java.util.stream.Collectors.toMap;
* filewriter for the csv
*/
@Service
public class CSVFileWriter {
public class CSVFileWriter extends CSVConfigurable {
public void saveFile(CSVModel model) throws IOException {
ICsvMapWriter mapWriter = null;
try {
CsvPreference preference = new CsvPreference.
Builder(CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE).
useQuoteMode(new AlwaysQuoteMode())
.build();
mapWriter = new CsvMapWriter(new FileWriter(model.getFilepath()), preference);
mapWriter = new CsvMapWriter(new FileWriter(model.getFilepath()), csvPreference);
mapWriter.writeHeader(model.getHeader());
for(CSVRow row: model.getRows()) {

View File

@@ -42,18 +42,23 @@ import ninja.javafx.smartcsv.csv.CSVFileReader;
import ninja.javafx.smartcsv.csv.CSVFileWriter;
import ninja.javafx.smartcsv.fx.about.AboutController;
import ninja.javafx.smartcsv.fx.list.ValidationErrorListCell;
import ninja.javafx.smartcsv.fx.preferences.PreferencesController;
import ninja.javafx.smartcsv.fx.table.ObservableMapValueFactory;
import ninja.javafx.smartcsv.fx.table.ValidationCellFactory;
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.validation.ValidationError;
import ninja.javafx.smartcsv.validation.ValidationFileReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
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;
@@ -72,6 +77,9 @@ public class SmartCSVController extends FXMLController {
// injections
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Autowired
private PreferencesFileReader preferencesLoader;
@Autowired
private CSVFileReader csvLoader;
@@ -84,6 +92,9 @@ public class SmartCSVController extends FXMLController {
@Autowired
private AboutController aboutController;
@Autowired
private PreferencesController preferencesController;
@FXML
private BorderPane applicationPane;
@@ -130,9 +141,12 @@ public class SmartCSVController extends FXMLController {
errorList.getSelectionModel().selectedItemProperty().addListener(observable -> scrollToError());
fileChanged.addListener(observable -> setStateName());
setStateName();
initCsvPreferences();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// setter
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -184,6 +198,19 @@ public class SmartCSVController extends FXMLController {
alert.showAndWait();
}
@FXML
public void preferences(ActionEvent actionEvent) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Preferences");
alert.setHeaderText("Preferences");
alert.getDialogPane().setContent(preferencesController.getView());
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
setCsvPreference(preferencesController.getCsvPreference());
}
}
public boolean canExit() {
boolean canExit = true;
if (model != null && fileChanged.get()) {
@@ -205,6 +232,24 @@ public class SmartCSVController extends FXMLController {
// private methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void initCsvPreferences() {
try {
File preferencesFile = new File(
getClass().getResource("/ninja/javafx/smartcsv/fx/preferences/preferences.json").toURI());
preferencesLoader.read(preferencesFile);
CsvPreference csvPreference = preferencesLoader.getCSVpreference();
setCsvPreference(csvPreference);
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}
private void setCsvPreference(CsvPreference csvPreference) {
csvLoader.setCsvPreference(csvPreference);
csvFileWriter.setCsvPreference(csvPreference);
preferencesController.setCsvPreference(csvPreference);
}
private void loadFile(FileReader fileReader, String filterText, String filter, String title, Label fileLabel) {
final FileChooser fileChooser = new FileChooser();

View File

@@ -0,0 +1,95 @@
/*
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.fx.preferences;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import ninja.javafx.smartcsv.fx.FXMLController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.supercsv.prefs.CsvPreference;
import java.net.URL;
import java.util.ResourceBundle;
import static ninja.javafx.smartcsv.preferences.QuoteModeHelper.getQuoteMode;
import static ninja.javafx.smartcsv.preferences.QuoteModeHelper.getQuoteModeName;
/**
* controller for preferences
*/
@Component
public class PreferencesController extends FXMLController {
@FXML
private TextField quoteChar;
@FXML
private TextField delimiterChar;
@FXML
private CheckBox surroundingSpacesNeedQuotes;
@FXML
private CheckBox ignoreEmptyLines;
@FXML
private ComboBox<String> quoteMode;
private String endOfLineSymbols;
@Value("${fxml.smartcvs.preferences.view}")
@Override
public void setFxmlFilePath(String filePath) {
this.fxmlFilePath = filePath;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
quoteMode.getItems().addAll("normal", "always", "column");
}
public void setCsvPreference(CsvPreference csvPreference) {
quoteChar.setText(Character.toString(csvPreference.getQuoteChar()));
delimiterChar.setText(Character.toString((char)csvPreference.getDelimiterChar()));
surroundingSpacesNeedQuotes.setSelected(csvPreference.isSurroundingSpacesNeedQuotes());
ignoreEmptyLines.setSelected(csvPreference.isIgnoreEmptyLines());
quoteMode.getSelectionModel().select(getQuoteModeName(csvPreference.getQuoteMode()));
endOfLineSymbols = csvPreference.getEndOfLineSymbols();
}
public CsvPreference getCsvPreference() {
return new CsvPreference.Builder(quoteChar.getText().charAt(0), delimiterChar.getText().charAt(0), endOfLineSymbols)
.useQuoteMode(getQuoteMode(quoteMode.getSelectionModel().getSelectedItem()))
.surroundingSpacesNeedQuotes(surroundingSpacesNeedQuotes.isSelected())
.ignoreEmptyLines(ignoreEmptyLines.isSelected())
.build();
}
}

View File

@@ -0,0 +1,84 @@
/*
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 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;
import static ninja.javafx.smartcsv.preferences.QuoteModeHelper.getQuoteMode;
/**
* file reader for the preferences
*/
@Service
public class PreferencesFileReader implements FileReader {
private Config config;
private CsvPreference csvPreference;
public PreferencesFileReader() {
csvPreference = new CsvPreference.
Builder(CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE).
useQuoteMode(new AlwaysQuoteMode()).build();
}
@Override
public void read(File filename) throws IOException {
config = ConfigFactory.parseFile(filename);
if (config != null) {
char quoteChar = config.getString("quoteChar").charAt(0);
char delimiterChar = config.getString("delimiterChar").charAt(0);
String endOfLineSymbols = config.getString("endOfLineSymbols");
boolean surroundingSpacesNeedQuotes = config.getBoolean("surroundingSpacesNeedQuotes");
boolean ignoreEmptyLines = config.getBoolean("ignoreEmptyLines");
String quoteMode = config.getString("quoteMode");
csvPreference = new CsvPreference.Builder(quoteChar, delimiterChar, endOfLineSymbols)
.useQuoteMode(getQuoteMode(quoteMode))
.surroundingSpacesNeedQuotes(surroundingSpacesNeedQuotes)
.ignoreEmptyLines(ignoreEmptyLines)
.build();
}
}
public CsvPreference getCSVpreference() {
return csvPreference;
}
}

View File

@@ -0,0 +1,56 @@
/*
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 org.supercsv.quote.AlwaysQuoteMode;
import org.supercsv.quote.ColumnQuoteMode;
import org.supercsv.quote.NormalQuoteMode;
import org.supercsv.quote.QuoteMode;
/**
* Helper class for mapping quote quoteModeName
*/
public class QuoteModeHelper {
public static QuoteMode getQuoteMode(String quoteModeName) {
switch (quoteModeName) {
case "always": return new AlwaysQuoteMode();
case "column": return new ColumnQuoteMode();
default: return new NormalQuoteMode();
}
}
public static String getQuoteModeName(QuoteMode quoteMode) {
if (quoteMode instanceof AlwaysQuoteMode) {
return "always";
} else if (quoteMode instanceof ColumnQuoteMode) {
return "column";
} else {
return "normal";
}
}
}