mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 21:48:22 +02:00
Initial commit
This commit is contained in:
121
src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java
Normal file
121
src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
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.table.model;
|
||||
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import ninja.javafx.smartcsv.validation.ValidationState;
|
||||
import ninja.javafx.smartcsv.validation.Validator;
|
||||
|
||||
/**
|
||||
* The CSVModel is the client representation for the csv filepath.
|
||||
* It holds the data in rows, stores the header and manages the validator.
|
||||
*/
|
||||
public class CSVModel {
|
||||
|
||||
private Validator validator;
|
||||
private ObservableList<CSVRow> rows = FXCollections.observableArrayList();
|
||||
private String[] header;
|
||||
private String filepath;
|
||||
|
||||
public CSVModel(String filepath) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
|
||||
public String getFilepath() {
|
||||
return this.filepath;
|
||||
}
|
||||
|
||||
public void setFilepath(String filepath) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the validator for the data revalidates
|
||||
* @param validator the validator for this data
|
||||
*/
|
||||
public void setValidator(Validator validator) {
|
||||
this.validator = validator;
|
||||
revalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the data as a list of rows of the
|
||||
* @return list of rows
|
||||
*/
|
||||
public ObservableList<CSVRow> getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a new and empty row
|
||||
* @return the new row
|
||||
*/
|
||||
public CSVRow addRow() {
|
||||
CSVRow row = new CSVRow();
|
||||
row.setValidator(validator);
|
||||
row.setRowNumber(rows.size());
|
||||
rows.add(row);
|
||||
return row;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the column headers as string array
|
||||
* @param header the headers of the columns
|
||||
*/
|
||||
public void setHeader(String[] header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the column headers
|
||||
* @return the column headers
|
||||
*/
|
||||
public String[] getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* walks through the data and validates each value
|
||||
*/
|
||||
private void revalidate() {
|
||||
for (CSVRow row: rows) {
|
||||
row.setValidator(validator);
|
||||
for (String column: row.getColumns().keySet()) {
|
||||
CSVValue value = row.getColumns().get(column).getValue();
|
||||
value.setValidator(validator);
|
||||
if (validator != null) {
|
||||
value.setValid(validator.isValid(column, value.getValue()));
|
||||
} else {
|
||||
value.setValid(new ValidationState());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
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.table.model;
|
||||
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableMap;
|
||||
import ninja.javafx.smartcsv.validation.Validator;
|
||||
|
||||
/**
|
||||
* This class represents a single row in the csv file.
|
||||
*/
|
||||
public class CSVRow {
|
||||
private Validator validator;
|
||||
private ObservableMap<String, ObjectProperty<CSVValue>> columns = FXCollections.observableHashMap();
|
||||
private int rowNumber;
|
||||
|
||||
/**
|
||||
* single row
|
||||
* @param validator the reference to the validator
|
||||
*/
|
||||
public void setValidator(Validator validator) {
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the row number
|
||||
* @param rowNumber
|
||||
*/
|
||||
public void setRowNumber(int rowNumber) {
|
||||
this.rowNumber = rowNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* return the row number
|
||||
* @return row number
|
||||
*/
|
||||
public int getRowNumber() {
|
||||
return rowNumber;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* returns the columns with data as Map
|
||||
* @return columns with data
|
||||
*/
|
||||
public ObservableMap<String, ObjectProperty<CSVValue>> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* stores the given value in the given column of this row
|
||||
* @param column column name
|
||||
* @param value the value to store
|
||||
*/
|
||||
public void addValue(String column, String value) {
|
||||
CSVValue v = new CSVValue();
|
||||
v.setValidator(validator);
|
||||
v.setValue(value);
|
||||
v.setColumn(column);
|
||||
v.setRowNumber(rowNumber);
|
||||
columns.put(column, new SimpleObjectProperty<>(v));
|
||||
}
|
||||
|
||||
}
|
||||
112
src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVValue.java
Normal file
112
src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVValue.java
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
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.table.model;
|
||||
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.property.StringProperty;
|
||||
import ninja.javafx.smartcsv.validation.ValidationState;
|
||||
import ninja.javafx.smartcsv.validation.Validator;
|
||||
|
||||
/**
|
||||
* The csv value represents the value of a single cell.
|
||||
* It also knows about the position (row and column)
|
||||
* and if the value is valid based on the validator.
|
||||
*/
|
||||
public class CSVValue {
|
||||
private Validator validator;
|
||||
private int rowNumber;
|
||||
private String column;
|
||||
private StringProperty value = new SimpleStringProperty();
|
||||
private ValidationState valid = new ValidationState();
|
||||
|
||||
/**
|
||||
* single value of a cell
|
||||
* @param validator the reference to the validator
|
||||
*/
|
||||
public void setValidator(Validator validator) {
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* the row number this value is stored in
|
||||
* @param row row number
|
||||
*/
|
||||
public void setRowNumber(int row) {
|
||||
this.rowNumber = row;
|
||||
}
|
||||
|
||||
/**
|
||||
* the column this value is stored in
|
||||
* @param column header name of the column
|
||||
*/
|
||||
public void setColumn(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the real value
|
||||
* @return the real value
|
||||
*/
|
||||
public String getValue() {
|
||||
return value.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* JavaFX property representation of the real value
|
||||
* @return property of real value
|
||||
*/
|
||||
public StringProperty valueProperty() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the real value
|
||||
* @param value the real value
|
||||
*/
|
||||
public void setValue(String value) {
|
||||
if (validator != null) {
|
||||
valid = validator.isValid(column, value);
|
||||
}
|
||||
this.value.set(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns if the value is valid to the rules of the validator
|
||||
* @return
|
||||
*/
|
||||
public ValidationState getValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the state if a value is valid or not
|
||||
* @param valid the validation state
|
||||
*/
|
||||
protected void setValid(ValidationState valid) {
|
||||
this.valid = valid;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user