mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 21:48:22 +02:00
read and edit csv preferences
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user