mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 13:38:23 +02:00
handle validation in own thread
This commit is contained in:
@@ -28,6 +28,8 @@ package ninja.javafx.smartcsv.fx.table.model;
|
|||||||
|
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
|
import javafx.concurrent.Service;
|
||||||
|
import javafx.concurrent.Task;
|
||||||
import ninja.javafx.smartcsv.validation.ValidationConfiguration;
|
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;
|
||||||
@@ -49,6 +51,7 @@ public class CSVModel {
|
|||||||
private ObservableList<CSVRow> rows = FXCollections.observableArrayList();
|
private ObservableList<CSVRow> rows = FXCollections.observableArrayList();
|
||||||
private String[] header;
|
private String[] header;
|
||||||
private ObservableList<ValidationError> validationError = FXCollections.observableArrayList();
|
private ObservableList<ValidationError> validationError = FXCollections.observableArrayList();
|
||||||
|
private RevalidationService revalidationService = new RevalidationService();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sets the validator configuration for the data revalidates
|
* sets the validator configuration for the data revalidates
|
||||||
@@ -111,6 +114,41 @@ public class CSVModel {
|
|||||||
|
|
||||||
if (!hasValidator()) return;
|
if (!hasValidator()) return;
|
||||||
|
|
||||||
|
revalidationService.setHeader(header);
|
||||||
|
revalidationService.setRows(rows);
|
||||||
|
revalidationService.setValidator(validator);
|
||||||
|
revalidationService.setOnSucceeded(t -> validationError.setAll(revalidationService.getValue()));
|
||||||
|
revalidationService.setOnFailed(t -> logger.error("revalidation service failed!"));
|
||||||
|
revalidationService.restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasValidator() {
|
||||||
|
return validator != null && validator.hasConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class RevalidationService extends Service<List<ValidationError>> {
|
||||||
|
|
||||||
|
private Validator validator;
|
||||||
|
private List<CSVRow> rows;
|
||||||
|
private String[] header;
|
||||||
|
|
||||||
|
public void setValidator(Validator validator) {
|
||||||
|
this.validator = validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRows(List<CSVRow> rows) {
|
||||||
|
this.rows = rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHeader(String[] header) {
|
||||||
|
this.header = header;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Task<List<ValidationError>> createTask() {
|
||||||
|
return new Task<List<ValidationError>>() {
|
||||||
|
@Override
|
||||||
|
protected List<ValidationError> call() throws Exception {
|
||||||
List<ValidationError> errors = new ArrayList<>();
|
List<ValidationError> errors = new ArrayList<>();
|
||||||
|
|
||||||
if (header != null) {
|
if (header != null) {
|
||||||
@@ -141,12 +179,10 @@ public class CSVModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return errors;
|
||||||
validationError.setAll(errors);
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasValidator() {
|
|
||||||
return validator != null && validator.hasConfig();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user