mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 21:48:22 +02:00
reorganized storage of models
This commit is contained in:
@@ -32,6 +32,7 @@ import java.io.IOException;
|
|||||||
/**
|
/**
|
||||||
* read some file
|
* read some file
|
||||||
*/
|
*/
|
||||||
public interface FileReader {
|
public interface FileReader<E> {
|
||||||
|
E getContent();
|
||||||
void read(File filename) throws IOException;
|
void read(File filename) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import java.io.IOException;
|
|||||||
/**
|
/**
|
||||||
* write some file
|
* write some file
|
||||||
*/
|
*/
|
||||||
public interface FileWriter {
|
public interface FileWriter<E> {
|
||||||
|
void setContent(E content);
|
||||||
void write(File filename) throws IOException;
|
void write(File filename) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,8 +40,7 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* reads the csv file and stores the values in csv model
|
* reads the csv file and stores the values in csv model
|
||||||
*/
|
*/
|
||||||
@Service
|
public class CSVFileReader extends CSVConfigurable implements FileReader<CSVModel> {
|
||||||
public class CSVFileReader extends CSVConfigurable implements FileReader {
|
|
||||||
|
|
||||||
private CSVModel model;
|
private CSVModel model;
|
||||||
|
|
||||||
@@ -72,7 +71,7 @@ public class CSVFileReader extends CSVConfigurable implements FileReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVModel getData() {
|
public CSVModel getContent() {
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,12 +42,11 @@ import static java.util.stream.Collectors.toMap;
|
|||||||
/**
|
/**
|
||||||
* filewriter for the csv
|
* filewriter for the csv
|
||||||
*/
|
*/
|
||||||
@Service
|
public class CSVFileWriter extends CSVConfigurable implements ninja.javafx.smartcsv.FileWriter<CSVModel> {
|
||||||
public class CSVFileWriter extends CSVConfigurable implements ninja.javafx.smartcsv.FileWriter {
|
|
||||||
|
|
||||||
private CSVModel model;
|
private CSVModel model;
|
||||||
|
|
||||||
public void setModel(CSVModel model) {
|
public void setContent(CSVModel model) {
|
||||||
this.model = model;
|
this.model = model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
78
src/main/java/ninja/javafx/smartcsv/files/FileStorage.java
Normal file
78
src/main/java/ninja/javafx/smartcsv/files/FileStorage.java
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package ninja.javafx.smartcsv.files;
|
||||||
|
|
||||||
|
import javafx.beans.property.BooleanProperty;
|
||||||
|
import javafx.beans.property.ObjectProperty;
|
||||||
|
import javafx.beans.property.SimpleBooleanProperty;
|
||||||
|
import javafx.beans.property.SimpleObjectProperty;
|
||||||
|
import ninja.javafx.smartcsv.FileReader;
|
||||||
|
import ninja.javafx.smartcsv.FileWriter;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class stores files and there state
|
||||||
|
* @author abi
|
||||||
|
*/
|
||||||
|
public class FileStorage<E> {
|
||||||
|
|
||||||
|
private FileReader<E> reader;
|
||||||
|
private FileWriter<E> writer;
|
||||||
|
|
||||||
|
public FileStorage(FileReader<E> reader, FileWriter<E> writer) {
|
||||||
|
this.reader = reader;
|
||||||
|
this.writer = writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private BooleanProperty fileChanged = new SimpleBooleanProperty(true);
|
||||||
|
private ObjectProperty<File> file = new SimpleObjectProperty<>();
|
||||||
|
private ObjectProperty<E> content = new SimpleObjectProperty<E>();
|
||||||
|
|
||||||
|
public boolean isFileChanged() {
|
||||||
|
return fileChanged.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanProperty fileChangedProperty() {
|
||||||
|
return fileChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFileChanged(boolean fileChanged) {
|
||||||
|
this.fileChanged.set(fileChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getFile() {
|
||||||
|
return file.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectProperty<File> fileProperty() {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFile(File file) {
|
||||||
|
this.file.set(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public E getContent() {
|
||||||
|
return content.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectProperty<E> contentProperty() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(E content) {
|
||||||
|
this.content.set(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load() throws IOException {
|
||||||
|
reader.read(file.get());
|
||||||
|
setContent(reader.getContent());
|
||||||
|
setFileChanged(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save() throws IOException {
|
||||||
|
writer.setContent(content.get());
|
||||||
|
writer.write(file.get());
|
||||||
|
setFileChanged(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,10 +26,6 @@
|
|||||||
|
|
||||||
package ninja.javafx.smartcsv.fx;
|
package ninja.javafx.smartcsv.fx;
|
||||||
|
|
||||||
import javafx.beans.property.BooleanProperty;
|
|
||||||
import javafx.beans.property.ObjectProperty;
|
|
||||||
import javafx.beans.property.SimpleBooleanProperty;
|
|
||||||
import javafx.beans.property.SimpleObjectProperty;
|
|
||||||
import javafx.collections.ListChangeListener;
|
import javafx.collections.ListChangeListener;
|
||||||
import javafx.collections.WeakListChangeListener;
|
import javafx.collections.WeakListChangeListener;
|
||||||
import javafx.concurrent.WorkerStateEvent;
|
import javafx.concurrent.WorkerStateEvent;
|
||||||
@@ -41,10 +37,9 @@ import javafx.scene.control.*;
|
|||||||
import javafx.scene.layout.AnchorPane;
|
import javafx.scene.layout.AnchorPane;
|
||||||
import javafx.scene.layout.BorderPane;
|
import javafx.scene.layout.BorderPane;
|
||||||
import javafx.stage.FileChooser;
|
import javafx.stage.FileChooser;
|
||||||
import ninja.javafx.smartcsv.FileReader;
|
|
||||||
import ninja.javafx.smartcsv.FileWriter;
|
|
||||||
import ninja.javafx.smartcsv.csv.CSVFileReader;
|
import ninja.javafx.smartcsv.csv.CSVFileReader;
|
||||||
import ninja.javafx.smartcsv.csv.CSVFileWriter;
|
import ninja.javafx.smartcsv.csv.CSVFileWriter;
|
||||||
|
import ninja.javafx.smartcsv.files.FileStorage;
|
||||||
import ninja.javafx.smartcsv.fx.about.AboutController;
|
import ninja.javafx.smartcsv.fx.about.AboutController;
|
||||||
import ninja.javafx.smartcsv.fx.list.ErrorSideBar;
|
import ninja.javafx.smartcsv.fx.list.ErrorSideBar;
|
||||||
import ninja.javafx.smartcsv.fx.preferences.PreferencesController;
|
import ninja.javafx.smartcsv.fx.preferences.PreferencesController;
|
||||||
@@ -58,6 +53,7 @@ import ninja.javafx.smartcsv.fx.util.SaveFileService;
|
|||||||
import ninja.javafx.smartcsv.fx.validation.ValidationEditorController;
|
import ninja.javafx.smartcsv.fx.validation.ValidationEditorController;
|
||||||
import ninja.javafx.smartcsv.preferences.PreferencesFileReader;
|
import ninja.javafx.smartcsv.preferences.PreferencesFileReader;
|
||||||
import ninja.javafx.smartcsv.preferences.PreferencesFileWriter;
|
import ninja.javafx.smartcsv.preferences.PreferencesFileWriter;
|
||||||
|
import ninja.javafx.smartcsv.validation.ValidationConfiguration;
|
||||||
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 ninja.javafx.smartcsv.validation.ValidationFileWriter;
|
import ninja.javafx.smartcsv.validation.ValidationFileWriter;
|
||||||
@@ -103,24 +99,6 @@ public class SmartCSVController extends FXMLController {
|
|||||||
// injections
|
// injections
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private PreferencesFileReader preferencesLoader;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private PreferencesFileWriter preferencesWriter;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CSVFileReader csvLoader;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ValidationFileReader validationLoader;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CSVFileWriter csvFileWriter;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ValidationFileWriter validationFileWriter;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AboutController aboutController;
|
private AboutController aboutController;
|
||||||
|
|
||||||
@@ -134,7 +112,7 @@ public class SmartCSVController extends FXMLController {
|
|||||||
private LoadFileService loadFileService;
|
private LoadFileService loadFileService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SaveFileService saveFileService;;
|
private SaveFileService saveFileService;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private BorderPane applicationPane;
|
private BorderPane applicationPane;
|
||||||
@@ -199,13 +177,13 @@ public class SmartCSVController extends FXMLController {
|
|||||||
|
|
||||||
private ValidationCellFactory cellFactory;
|
private ValidationCellFactory cellFactory;
|
||||||
|
|
||||||
private CSVModel model;
|
|
||||||
private TableView<CSVRow> tableView;
|
private TableView<CSVRow> tableView;
|
||||||
private ErrorSideBar errorSideBar;
|
private ErrorSideBar errorSideBar;
|
||||||
private BooleanProperty fileChanged = new SimpleBooleanProperty(true);
|
|
||||||
private ResourceBundle resourceBundle;
|
private ResourceBundle resourceBundle;
|
||||||
private ObjectProperty<File> currentCsvFile = new SimpleObjectProperty<>();
|
|
||||||
private ObjectProperty<File> currentConfigFile= new SimpleObjectProperty<>();
|
private FileStorage<CSVModel> currentCsvFile = new FileStorage<>(new CSVFileReader(), new CSVFileWriter());
|
||||||
|
private FileStorage<ValidationConfiguration> currentConfigFile = new FileStorage<>(new ValidationFileReader(), new ValidationFileWriter());
|
||||||
|
private FileStorage<CsvPreference> csvPreferenceFile = new FileStorage<>(new PreferencesFileReader(), new PreferencesFileWriter());
|
||||||
|
|
||||||
private ListChangeListener<ValidationError> errorListListener = c -> tableView.refresh();
|
private ListChangeListener<ValidationError> errorListListener = c -> tableView.refresh();
|
||||||
private WeakListChangeListener<ValidationError> weakErrorListListener = new WeakListChangeListener<>(errorListListener);
|
private WeakListChangeListener<ValidationError> weakErrorListListener = new WeakListChangeListener<>(errorListListener);
|
||||||
@@ -230,6 +208,8 @@ public class SmartCSVController extends FXMLController {
|
|||||||
bindCsvFileName();
|
bindCsvFileName();
|
||||||
bindConfigFileName();
|
bindConfigFileName();
|
||||||
|
|
||||||
|
csvPreferenceFile.setFile(PREFERENCES_FILE);
|
||||||
|
|
||||||
loadCsvPreferencesFromFile();
|
loadCsvPreferencesFromFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,58 +243,32 @@ public class SmartCSVController extends FXMLController {
|
|||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void openCsv(ActionEvent actionEvent) {
|
public void openCsv(ActionEvent actionEvent) {
|
||||||
currentCsvFile.setValue(
|
loadFile(CSV_FILTER_TEXT, CSV_FILTER_EXTENSION, "Open CSV", currentCsvFile);
|
||||||
loadFile(
|
|
||||||
csvLoader,
|
|
||||||
CSV_FILTER_TEXT,
|
|
||||||
CSV_FILTER_EXTENSION,
|
|
||||||
"Open CSV",
|
|
||||||
currentCsvFile.getValue()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void openConfig(ActionEvent actionEvent) {
|
public void openConfig(ActionEvent actionEvent) {
|
||||||
currentConfigFile.setValue(
|
loadFile(JSON_FILTER_TEXT, JSON_FILTER_EXTENSION, "Open Validation Configuration", currentConfigFile);
|
||||||
loadFile(
|
|
||||||
validationLoader,
|
|
||||||
JSON_FILTER_TEXT,
|
|
||||||
JSON_FILTER_EXTENSION,
|
|
||||||
"Open Validation Configuration",
|
|
||||||
currentConfigFile.getValue()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void saveCsv(ActionEvent actionEvent) {
|
public void saveCsv(ActionEvent actionEvent) {
|
||||||
csvFileWriter.setModel(model);
|
useSaveFileService(currentCsvFile);
|
||||||
useSaveFileService(csvFileWriter, currentCsvFile.getValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void saveAsCsv(ActionEvent actionEvent) {
|
public void saveAsCsv(ActionEvent actionEvent) {
|
||||||
csvFileWriter.setModel(model);
|
saveFile(CSV_FILTER_TEXT, CSV_FILTER_EXTENSION, currentCsvFile);
|
||||||
currentCsvFile.setValue(
|
|
||||||
saveFile(
|
|
||||||
csvFileWriter,
|
|
||||||
CSV_FILTER_TEXT,
|
|
||||||
CSV_FILTER_EXTENSION,
|
|
||||||
currentCsvFile.getValue()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void saveConfig(ActionEvent actionEvent) {
|
public void saveConfig(ActionEvent actionEvent) {
|
||||||
validationFileWriter.setValidationConfiguration(validationLoader.getValidationConfiguration());
|
useSaveFileService(currentConfigFile);
|
||||||
useSaveFileService(validationFileWriter, currentConfigFile.getValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void saveAsConfig(ActionEvent actionEvent) {
|
public void saveAsConfig(ActionEvent actionEvent) {
|
||||||
validationFileWriter.setValidationConfiguration(validationLoader.getValidationConfiguration());
|
saveFile(JSON_FILTER_TEXT, JSON_FILTER_EXTENSION, currentConfigFile);
|
||||||
currentConfigFile.setValue(
|
|
||||||
saveFile(
|
|
||||||
validationFileWriter,
|
|
||||||
JSON_FILTER_TEXT,
|
|
||||||
JSON_FILTER_EXTENSION,
|
|
||||||
currentConfigFile.getValue()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -351,24 +305,24 @@ public class SmartCSVController extends FXMLController {
|
|||||||
setCsvPreference(csvPreference);
|
setCsvPreference(csvPreference);
|
||||||
saveCsvPreferences(csvPreference);
|
saveCsvPreferences(csvPreference);
|
||||||
} else {
|
} else {
|
||||||
preferencesController.setCsvPreference(preferencesLoader.getCSVpreference());
|
preferencesController.setCsvPreference(csvPreferenceFile.getContent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void deleteRow(ActionEvent actionEvent) {
|
public void deleteRow(ActionEvent actionEvent) {
|
||||||
model.getRows().removeAll(tableView.getSelectionModel().getSelectedItems());
|
currentCsvFile.getContent().getRows().removeAll(tableView.getSelectionModel().getSelectedItems());
|
||||||
fileChanged.setValue(true);
|
currentCsvFile.setFileChanged(true);
|
||||||
resetContent();
|
resetContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void addRow(ActionEvent actionEvent) {
|
public void addRow(ActionEvent actionEvent) {
|
||||||
CSVRow row = model.addRow();
|
CSVRow row = currentCsvFile.getContent().addRow();
|
||||||
for (String column : model.getHeader()) {
|
for (String column : currentCsvFile.getContent().getHeader()) {
|
||||||
row.addValue(column, "");
|
row.addValue(column, "");
|
||||||
}
|
}
|
||||||
fileChanged.setValue(true);
|
currentCsvFile.setFileChanged(true);
|
||||||
resetContent();
|
resetContent();
|
||||||
|
|
||||||
selectNewRow();
|
selectNewRow();
|
||||||
@@ -376,7 +330,7 @@ public class SmartCSVController extends FXMLController {
|
|||||||
|
|
||||||
public boolean canExit() {
|
public boolean canExit() {
|
||||||
boolean canExit = true;
|
boolean canExit = true;
|
||||||
if (model != null && fileChanged.get()) {
|
if (currentCsvFile.getContent() != null && currentCsvFile.isFileChanged()) {
|
||||||
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
||||||
alert.setTitle(resourceBundle.getString("dialog.exit.title"));
|
alert.setTitle(resourceBundle.getString("dialog.exit.title"));
|
||||||
alert.setHeaderText(resourceBundle.getString("dialog.exit.header.text"));
|
alert.setHeaderText(resourceBundle.getString("dialog.exit.header.text"));
|
||||||
@@ -404,8 +358,8 @@ public class SmartCSVController extends FXMLController {
|
|||||||
if (result.get() == ButtonType.OK){
|
if (result.get() == ButtonType.OK){
|
||||||
runLater(() -> {
|
runLater(() -> {
|
||||||
validationEditorController.updateConfiguration();
|
validationEditorController.updateConfiguration();
|
||||||
fileChanged.setValue(true);
|
currentCsvFile.setFileChanged(true);
|
||||||
model.revalidate();
|
currentCsvFile.getContent().revalidate();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -421,15 +375,15 @@ public class SmartCSVController extends FXMLController {
|
|||||||
tableView.getSelectionModel().select(lastRow);
|
tableView.getSelectionModel().select(lastRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bindMenuItemsToFileExistence(ObjectProperty<File> file, MenuItem... items) {
|
private void bindMenuItemsToFileExistence(FileStorage file, MenuItem... items) {
|
||||||
for (MenuItem item: items) {
|
for (MenuItem item: items) {
|
||||||
item.disableProperty().bind(isNull(file));
|
item.disableProperty().bind(isNull(file.fileProperty()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bindButtonsToFileExistence(ObjectProperty<File> file, Button... items) {
|
private void bindButtonsToFileExistence(FileStorage file, Button... items) {
|
||||||
for (Button item: items) {
|
for (Button item: items) {
|
||||||
item.disableProperty().bind(isNull(file));
|
item.disableProperty().bind(isNull(file.fileProperty()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -446,17 +400,16 @@ public class SmartCSVController extends FXMLController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void bindCsvFileName() {
|
private void bindCsvFileName() {
|
||||||
csvName.textProperty().bind(selectString(currentCsvFile, "name"));
|
csvName.textProperty().bind(selectString(currentCsvFile.fileProperty(), "name"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bindConfigFileName() {
|
private void bindConfigFileName() {
|
||||||
configurationName.textProperty().bind(selectString(currentConfigFile, "name"));
|
configurationName.textProperty().bind(selectString(currentConfigFile.fileProperty(), "name"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadCsvPreferencesFromFile() {
|
private void loadCsvPreferencesFromFile() {
|
||||||
if (PREFERENCES_FILE.exists()) {
|
if (csvPreferenceFile.getFile().exists()) {
|
||||||
useLoadFileService(preferencesLoader, PREFERENCES_FILE,
|
useLoadFileService(csvPreferenceFile, event -> setCsvPreference(csvPreferenceFile.getContent()));
|
||||||
event -> setCsvPreference(preferencesLoader.getCSVpreference()));
|
|
||||||
} else {
|
} else {
|
||||||
setCsvPreference(CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
|
setCsvPreference(CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
|
||||||
}
|
}
|
||||||
@@ -465,37 +418,34 @@ public class SmartCSVController extends FXMLController {
|
|||||||
private void saveCsvPreferences(CsvPreference csvPreference) {
|
private void saveCsvPreferences(CsvPreference csvPreference) {
|
||||||
try {
|
try {
|
||||||
createPreferenceFile();
|
createPreferenceFile();
|
||||||
preferencesWriter.setCsvPreference(csvPreference);
|
csvPreferenceFile.setContent(csvPreference);
|
||||||
useSaveFileService(preferencesWriter, PREFERENCES_FILE);
|
useSaveFileService(csvPreferenceFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createPreferenceFile() throws IOException {
|
private void createPreferenceFile() throws IOException {
|
||||||
if (!PREFERENCES_FILE.exists()) {
|
if (!csvPreferenceFile.getFile().exists()) {
|
||||||
createPreferencesFileFolder();
|
createPreferencesFileFolder();
|
||||||
PREFERENCES_FILE.createNewFile();
|
csvPreferenceFile.getFile().createNewFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createPreferencesFileFolder() {
|
private void createPreferencesFileFolder() {
|
||||||
if (!PREFERENCES_FILE.getParentFile().exists()) {
|
if (!csvPreferenceFile.getFile().getParentFile().exists()) {
|
||||||
PREFERENCES_FILE.getParentFile().mkdir();
|
csvPreferenceFile.getFile().getParentFile().mkdir();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setCsvPreference(CsvPreference csvPreference) {
|
private void setCsvPreference(CsvPreference csvPreference) {
|
||||||
csvLoader.setCsvPreference(csvPreference);
|
|
||||||
csvFileWriter.setCsvPreference(csvPreference);
|
|
||||||
preferencesController.setCsvPreference(csvPreference);
|
preferencesController.setCsvPreference(csvPreference);
|
||||||
}
|
}
|
||||||
|
|
||||||
private File loadFile(FileReader fileReader,
|
private void loadFile(String filterText,
|
||||||
String filterText,
|
|
||||||
String filter,
|
String filter,
|
||||||
String title,
|
String title,
|
||||||
File initChildFile) {
|
FileStorage storageFile) {
|
||||||
final FileChooser fileChooser = new FileChooser();
|
final FileChooser fileChooser = new FileChooser();
|
||||||
|
|
||||||
//Set extension filter
|
//Set extension filter
|
||||||
@@ -503,61 +453,57 @@ public class SmartCSVController extends FXMLController {
|
|||||||
fileChooser.getExtensionFilters().add(extFilter);
|
fileChooser.getExtensionFilters().add(extFilter);
|
||||||
fileChooser.setTitle(title);
|
fileChooser.setTitle(title);
|
||||||
|
|
||||||
if (initChildFile != null) {
|
if (storageFile.getFile() != null) {
|
||||||
fileChooser.setInitialDirectory(initChildFile.getParentFile());
|
fileChooser.setInitialDirectory(storageFile.getFile().getParentFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
//Show open file dialog
|
//Show open file dialog
|
||||||
File file = fileChooser.showOpenDialog(applicationPane.getScene().getWindow());
|
File file = fileChooser.showOpenDialog(applicationPane.getScene().getWindow());
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
useLoadFileService(fileReader, file, event -> runLater(() -> {
|
storageFile.setFile(file);
|
||||||
|
useLoadFileService(storageFile, event -> runLater(() -> {
|
||||||
resetContent();
|
resetContent();
|
||||||
fileChanged.setValue(false);
|
storageFile.setFileChanged(false);
|
||||||
}));
|
}));
|
||||||
return file;
|
|
||||||
} else {
|
|
||||||
return initChildFile;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private File saveFile(FileWriter writer, String filterText, String filter, File initFile) {
|
private File saveFile(String filterText, String filter, FileStorage initFile) {
|
||||||
File file = initFile;
|
File file = initFile.getFile();
|
||||||
if (model != null) {
|
if (initFile.getContent() != null) {
|
||||||
final FileChooser fileChooser = new FileChooser();
|
final FileChooser fileChooser = new FileChooser();
|
||||||
|
|
||||||
//Set extension filter
|
//Set extension filter
|
||||||
final FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(filterText, filter);
|
final FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(filterText, filter);
|
||||||
fileChooser.getExtensionFilters().add(extFilter);
|
fileChooser.getExtensionFilters().add(extFilter);
|
||||||
|
|
||||||
if (initFile != null) {
|
if (initFile.getFile() != null) {
|
||||||
fileChooser.setInitialDirectory(initFile.getParentFile());
|
fileChooser.setInitialDirectory(initFile.getFile().getParentFile());
|
||||||
fileChooser.setInitialFileName(initFile.getName());
|
fileChooser.setInitialFileName(initFile.getFile().getName());
|
||||||
}
|
}
|
||||||
fileChooser.setTitle("Save File");
|
fileChooser.setTitle("Save File");
|
||||||
|
|
||||||
//Show open file dialog
|
//Show open file dialog
|
||||||
file = fileChooser.showSaveDialog(applicationPane.getScene().getWindow());
|
file = fileChooser.showSaveDialog(applicationPane.getScene().getWindow());
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
useSaveFileService(writer, file);
|
initFile.setFile(file);
|
||||||
|
useSaveFileService(currentCsvFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void useLoadFileService(FileReader fileReader, File file, EventHandler<WorkerStateEvent> value) {
|
private void useLoadFileService(FileStorage fileStorage, EventHandler<WorkerStateEvent> value) {
|
||||||
loadFileService.setFile(file);
|
loadFileService.setFileStorage(fileStorage);
|
||||||
loadFileService.setFileReader(fileReader);
|
|
||||||
loadFileService.restart();
|
loadFileService.restart();
|
||||||
loadFileService.setOnSucceeded(value);
|
loadFileService.setOnSucceeded(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void useSaveFileService(FileWriter writer, File file) {
|
private void useSaveFileService(FileStorage fileStorage) {
|
||||||
saveFileService.setFile(file);
|
saveFileService.setFileStorage(fileStorage);
|
||||||
saveFileService.setWriter(writer);
|
|
||||||
saveFileService.restart();
|
saveFileService.restart();
|
||||||
saveFileService.setOnSucceeded(event -> runLater(() -> {
|
saveFileService.setOnSucceeded(event -> runLater(() -> {
|
||||||
resetContent();
|
resetContent();
|
||||||
fileChanged.setValue(false);
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -565,21 +511,20 @@ public class SmartCSVController extends FXMLController {
|
|||||||
* Creates new table view and add the new content
|
* Creates new table view and add the new content
|
||||||
*/
|
*/
|
||||||
private void resetContent() {
|
private void resetContent() {
|
||||||
model = csvLoader.getData();
|
if (currentCsvFile.getContent() != null) {
|
||||||
if (model != null) {
|
currentCsvFile.getContent().getValidationError().addListener(weakErrorListListener);
|
||||||
model.getValidationError().addListener(weakErrorListListener);
|
currentCsvFile.getContent().setValidationConfiguration(currentConfigFile.getContent());
|
||||||
model.setValidationConfiguration(validationLoader.getValidationConfiguration());
|
validationEditorController.setValidationConfiguration(currentConfigFile.getContent());
|
||||||
validationEditorController.setValidationConfiguration(validationLoader.getValidationConfiguration());
|
|
||||||
tableView = new TableView<>();
|
tableView = new TableView<>();
|
||||||
|
|
||||||
bindMenuItemsToTableSelection(deleteRowMenuItem);
|
bindMenuItemsToTableSelection(deleteRowMenuItem);
|
||||||
bindButtonsToTableSelection(deleteRowButton);
|
bindButtonsToTableSelection(deleteRowButton);
|
||||||
|
|
||||||
for (String column : model.getHeader()) {
|
for (String column : currentCsvFile.getContent().getHeader()) {
|
||||||
addColumn(column, tableView);
|
addColumn(column, tableView);
|
||||||
}
|
}
|
||||||
|
|
||||||
tableView.getItems().setAll(model.getRows());
|
tableView.getItems().setAll(currentCsvFile.getContent().getRows());
|
||||||
tableView.setEditable(true);
|
tableView.setEditable(true);
|
||||||
|
|
||||||
setBottomAnchor(tableView, 0.0);
|
setBottomAnchor(tableView, 0.0);
|
||||||
@@ -587,7 +532,7 @@ public class SmartCSVController extends FXMLController {
|
|||||||
setLeftAnchor(tableView, 0.0);
|
setLeftAnchor(tableView, 0.0);
|
||||||
setRightAnchor(tableView, 0.0);
|
setRightAnchor(tableView, 0.0);
|
||||||
tableWrapper.getChildren().setAll(tableView);
|
tableWrapper.getChildren().setAll(tableView);
|
||||||
errorSideBar.setModel(model);
|
errorSideBar.setModel(currentCsvFile.getContent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -612,8 +557,8 @@ public class SmartCSVController extends FXMLController {
|
|||||||
event.getTableView().getItems().get(event.getTablePosition().getRow()).
|
event.getTableView().getItems().get(event.getTablePosition().getRow()).
|
||||||
getColumns().get(header).setValue(event.getNewValue());
|
getColumns().get(header).setValue(event.getNewValue());
|
||||||
runLater(() -> {
|
runLater(() -> {
|
||||||
fileChanged.setValue(true);
|
currentCsvFile.setFileChanged(true);
|
||||||
model.revalidate();
|
currentCsvFile.getContent().revalidate();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ import ninja.javafx.smartcsv.validation.ValidationConfiguration;
|
|||||||
import ninja.javafx.smartcsv.validation.ValidationError;
|
import ninja.javafx.smartcsv.validation.ValidationError;
|
||||||
import ninja.javafx.smartcsv.validation.Validator;
|
import ninja.javafx.smartcsv.validation.Validator;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The CSVModel is the client representation for the csv filepath.
|
* The CSVModel is the client representation for the csv filepath.
|
||||||
* It holds the data in rows, stores the header and manages the validator.
|
* It holds the data in rows, stores the header and manages the validator.
|
||||||
@@ -100,8 +103,15 @@ public class CSVModel {
|
|||||||
public void revalidate() {
|
public void revalidate() {
|
||||||
validationError.clear();
|
validationError.clear();
|
||||||
|
|
||||||
if (header != null && validator != null) {
|
if (!hasValidator()) return;
|
||||||
addValidationError(validator.isHeaderValid(header));
|
|
||||||
|
List<ValidationError> errors = new ArrayList<>();
|
||||||
|
|
||||||
|
if (header != null) {
|
||||||
|
ValidationError headerError = validator.isHeaderValid(header);
|
||||||
|
if (headerError != null) {
|
||||||
|
errors.add(headerError);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int lineNumber = 0; lineNumber < rows.size(); lineNumber++) {
|
for (int lineNumber = 0; lineNumber < rows.size(); lineNumber++) {
|
||||||
@@ -113,7 +123,7 @@ public class CSVModel {
|
|||||||
if (validator != null) {
|
if (validator != null) {
|
||||||
ValidationError validationError = validator.isValid(column, value.getValue(), lineNumber);
|
ValidationError validationError = validator.isValid(column, value.getValue(), lineNumber);
|
||||||
if (validationError != null) {
|
if (validationError != null) {
|
||||||
addValidationError(validationError);
|
errors.add(validationError);
|
||||||
value.setValidationError(validationError);
|
value.setValidationError(validationError);
|
||||||
} else {
|
} else {
|
||||||
value.setValidationError(null);
|
value.setValidationError(null);
|
||||||
@@ -123,12 +133,12 @@ public class CSVModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validationError.setAll(errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addValidationError(ValidationError validationError) {
|
private boolean hasValidator() {
|
||||||
if (validationError != null) {
|
return validator != null && validator.hasConfig();
|
||||||
this.validationError.add(validationError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ package ninja.javafx.smartcsv.fx.util;
|
|||||||
import javafx.concurrent.Service;
|
import javafx.concurrent.Service;
|
||||||
import javafx.concurrent.Task;
|
import javafx.concurrent.Task;
|
||||||
import ninja.javafx.smartcsv.FileReader;
|
import ninja.javafx.smartcsv.FileReader;
|
||||||
|
import ninja.javafx.smartcsv.files.FileStorage;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
@@ -38,14 +39,10 @@ import java.io.File;
|
|||||||
@org.springframework.stereotype.Service
|
@org.springframework.stereotype.Service
|
||||||
public class LoadFileService extends Service {
|
public class LoadFileService extends Service {
|
||||||
|
|
||||||
private File file;
|
private FileStorage file;
|
||||||
private FileReader fileReader;
|
|
||||||
|
|
||||||
public void setFile(File value) {
|
public void setFileStorage(FileStorage file) {
|
||||||
file = value;
|
this.file = file;
|
||||||
}
|
|
||||||
public void setFileReader(FileReader fileReader) {
|
|
||||||
this.fileReader = fileReader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -54,7 +51,7 @@ public class LoadFileService extends Service {
|
|||||||
@Override
|
@Override
|
||||||
protected Void call() throws Exception {
|
protected Void call() throws Exception {
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
fileReader.read(file);
|
file.load();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import javafx.concurrent.Service;
|
|||||||
import javafx.concurrent.Task;
|
import javafx.concurrent.Task;
|
||||||
import ninja.javafx.smartcsv.FileWriter;
|
import ninja.javafx.smartcsv.FileWriter;
|
||||||
import ninja.javafx.smartcsv.csv.CSVFileWriter;
|
import ninja.javafx.smartcsv.csv.CSVFileWriter;
|
||||||
|
import ninja.javafx.smartcsv.files.FileStorage;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
@@ -41,14 +42,9 @@ import static javafx.application.Platform.runLater;
|
|||||||
@org.springframework.stereotype.Service
|
@org.springframework.stereotype.Service
|
||||||
public class SaveFileService extends Service {
|
public class SaveFileService extends Service {
|
||||||
|
|
||||||
private File file;
|
private FileStorage file;
|
||||||
private FileWriter writer;
|
|
||||||
|
|
||||||
public void setWriter(FileWriter writer) {
|
public void setFileStorage(FileStorage value) {
|
||||||
this.writer = writer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File value) {
|
|
||||||
file = value;
|
file = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +54,7 @@ public class SaveFileService extends Service {
|
|||||||
@Override
|
@Override
|
||||||
protected Void call() throws Exception {
|
protected Void call() throws Exception {
|
||||||
try {
|
try {
|
||||||
writer.write(file);
|
file.save();
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,8 +42,7 @@ import static ninja.javafx.smartcsv.preferences.QuoteModeHelper.getQuoteMode;
|
|||||||
/**
|
/**
|
||||||
* file reader for the preferences
|
* file reader for the preferences
|
||||||
*/
|
*/
|
||||||
@Service
|
public class PreferencesFileReader implements FileReader<CsvPreference> {
|
||||||
public class PreferencesFileReader implements FileReader {
|
|
||||||
|
|
||||||
private Map config;
|
private Map config;
|
||||||
private CsvPreference csvPreference;
|
private CsvPreference csvPreference;
|
||||||
@@ -74,7 +73,7 @@ public class PreferencesFileReader implements FileReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CsvPreference getCSVpreference() {
|
public CsvPreference getContent() {
|
||||||
return csvPreference;
|
return csvPreference;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,12 +41,11 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* Save preferences to configuration file
|
* Save preferences to configuration file
|
||||||
*/
|
*/
|
||||||
@Service
|
public class PreferencesFileWriter implements FileWriter<CsvPreference> {
|
||||||
public class PreferencesFileWriter implements FileWriter {
|
|
||||||
|
|
||||||
private CsvPreference csvPreference;
|
private CsvPreference csvPreference;
|
||||||
|
|
||||||
public void setCsvPreference(CsvPreference csvPreference) {
|
public void setContent(CsvPreference csvPreference) {
|
||||||
this.csvPreference = csvPreference;
|
this.csvPreference = csvPreference;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,8 +36,7 @@ import java.io.IOException;
|
|||||||
/**
|
/**
|
||||||
* This class loads the constraints as json config
|
* This class loads the constraints as json config
|
||||||
*/
|
*/
|
||||||
@Service
|
public class ValidationFileReader implements FileReader<ValidationConfiguration> {
|
||||||
public class ValidationFileReader implements FileReader {
|
|
||||||
|
|
||||||
private ValidationConfiguration config;
|
private ValidationConfiguration config;
|
||||||
|
|
||||||
@@ -46,7 +45,7 @@ public class ValidationFileReader implements FileReader {
|
|||||||
config = new GsonBuilder().create().fromJson(new java.io.FileReader(file), ValidationConfiguration.class);
|
config = new GsonBuilder().create().fromJson(new java.io.FileReader(file), ValidationConfiguration.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ValidationConfiguration getValidationConfiguration() {
|
public ValidationConfiguration getContent() {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,12 +38,11 @@ import java.nio.file.Files;
|
|||||||
/**
|
/**
|
||||||
* file writer for the validation configuration
|
* file writer for the validation configuration
|
||||||
*/
|
*/
|
||||||
@Service
|
public class ValidationFileWriter implements FileWriter<ValidationConfiguration> {
|
||||||
public class ValidationFileWriter implements FileWriter {
|
|
||||||
|
|
||||||
private ValidationConfiguration validationConfiguration;
|
private ValidationConfiguration validationConfiguration;
|
||||||
|
|
||||||
public void setValidationConfiguration(ValidationConfiguration validationConfiguration) {
|
public void setContent(ValidationConfiguration validationConfiguration) {
|
||||||
this.validationConfiguration = validationConfiguration;
|
this.validationConfiguration = validationConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ public class Validator {
|
|||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// oublic methods
|
// public methods
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,7 +78,7 @@ public class Validator {
|
|||||||
*/
|
*/
|
||||||
public ValidationError isValid(String column, String value, Integer lineNumber) {
|
public ValidationError isValid(String column, String value, Integer lineNumber) {
|
||||||
ValidationError result = null;
|
ValidationError result = null;
|
||||||
if (validationConfig != null) {
|
if (hasConfig()) {
|
||||||
|
|
||||||
ValidationError error = ValidationError.withLineNumber(lineNumber);
|
ValidationError error = ValidationError.withLineNumber(lineNumber);
|
||||||
checkBlankOrNull(column, value, error);
|
checkBlankOrNull(column, value, error);
|
||||||
@@ -101,6 +101,9 @@ public class Validator {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasConfig() {
|
||||||
|
return validationConfig != null;
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// private methods
|
// private methods
|
||||||
|
|||||||
Reference in New Issue
Block a user