mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 13:38:23 +02:00
Initial commit
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
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;
|
||||
|
||||
import javafx.scene.control.ContentDisplay;
|
||||
import javafx.scene.control.TableCell;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.control.Tooltip;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import ninja.javafx.smartcsv.fx.table.model.CSVRow;
|
||||
import ninja.javafx.smartcsv.fx.table.model.CSVValue;
|
||||
|
||||
import static javafx.application.Platform.runLater;
|
||||
|
||||
/**
|
||||
* Created by Andreas on 27.11.2015.
|
||||
*/
|
||||
public class EditableValidationCell extends TableCell<CSVRow, CSVValue> {
|
||||
|
||||
private ValueTextField textField;
|
||||
|
||||
@Override
|
||||
public void startEdit() {
|
||||
super.startEdit();
|
||||
setTextField();
|
||||
runLater(() -> {
|
||||
textField.requestFocus();
|
||||
textField.selectAll();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelEdit() {
|
||||
super.cancelEdit();
|
||||
setText(getItem().getValue());
|
||||
setContentDisplay(ContentDisplay.TEXT_ONLY);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateItem(CSVValue item, boolean empty) {
|
||||
super.updateItem(item, empty);
|
||||
|
||||
if (item == null || item.getValid().isValid() || isEditing()) {
|
||||
setStyle("");
|
||||
setTooltip(null);
|
||||
} else if (!item.getValid().isValid()) {
|
||||
setStyle("-fx-background-color: #ff8888");
|
||||
setTooltip(new Tooltip(item.getValid().error()));
|
||||
}
|
||||
|
||||
if (item == null || empty) {
|
||||
setTextInCell(null);
|
||||
} else {
|
||||
if (isEditing()) {
|
||||
setTextField();
|
||||
textField.setValue(item);
|
||||
} else {
|
||||
setTextInCell(item.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setTextField() {
|
||||
if (textField == null) {
|
||||
createTextField();
|
||||
}
|
||||
setGraphic(textField);
|
||||
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
|
||||
}
|
||||
|
||||
private void setTextInCell(String text) {
|
||||
setGraphic(null);
|
||||
setText(text);
|
||||
setContentDisplay(ContentDisplay.TEXT_ONLY);
|
||||
}
|
||||
|
||||
private void createTextField() {
|
||||
textField = new ValueTextField(getItem());
|
||||
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
|
||||
textField.setOnKeyPressed(t -> {
|
||||
if (t.getCode() == KeyCode.ENTER) {
|
||||
commitEdit(textField.getValue());
|
||||
} else if (t.getCode() == KeyCode.ESCAPE) {
|
||||
cancelEdit();
|
||||
}
|
||||
});
|
||||
textField.focusedProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if (!newValue && textField != null) {
|
||||
commitEdit(textField.getValue());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class ValueTextField extends TextField {
|
||||
private CSVValue value;
|
||||
|
||||
public ValueTextField(CSVValue value) {
|
||||
setValue(value);
|
||||
}
|
||||
|
||||
public void setValue(CSVValue value) {
|
||||
this.value = value;
|
||||
setText(value.getValue());
|
||||
}
|
||||
|
||||
public CSVValue getValue() {
|
||||
value.setValue(getText());
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
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;
|
||||
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.scene.control.TableColumn;
|
||||
import javafx.util.Callback;
|
||||
import ninja.javafx.smartcsv.fx.table.model.CSVRow;
|
||||
import ninja.javafx.smartcsv.fx.table.model.CSVValue;
|
||||
|
||||
/**
|
||||
* Created by Andreas on 18.11.2015.
|
||||
*/
|
||||
public class ObservableMapValueFactory implements
|
||||
Callback<TableColumn.CellDataFeatures<CSVRow, CSVValue>, ObjectProperty<CSVValue>> {
|
||||
|
||||
private final Object key;
|
||||
|
||||
public ObservableMapValueFactory(Object key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectProperty<CSVValue> call(TableColumn.CellDataFeatures<CSVRow, CSVValue> features) {
|
||||
CSVRow row = features.getValue();
|
||||
ObjectProperty<CSVValue> value = row.getColumns().get(key);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -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.fx.table;
|
||||
|
||||
import javafx.scene.control.TableCell;
|
||||
import javafx.scene.control.TableColumn;
|
||||
import javafx.util.Callback;
|
||||
import ninja.javafx.smartcsv.fx.table.model.CSVModel;
|
||||
import ninja.javafx.smartcsv.fx.table.model.CSVRow;
|
||||
import ninja.javafx.smartcsv.fx.table.model.CSVValue;
|
||||
|
||||
/**
|
||||
* Created by Andreas on 18.11.2015.
|
||||
*/
|
||||
public class ValidationCellFactory implements Callback<TableColumn<CSVRow, CSVValue>, TableCell<CSVRow, CSVValue>> {
|
||||
|
||||
@Override
|
||||
public TableCell<CSVRow, CSVValue> call(TableColumn<CSVRow, CSVValue> param) {
|
||||
return new EditableValidationCell();
|
||||
}
|
||||
}
|
||||
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