// Path: src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVRow.java
// public class CSVRow {
//     private ObservableMap<String, ObjectProperty<CSVValue>> columns = FXCollections.observableHashMap();
//     private int rowNumber;
// 
//     /**
//      * 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
//      */
//     CSVValue addValue(String column, String value) {
//         CSVValue v = new CSVValue();
//         v.setValue(value);
//         columns.put(column, new SimpleObjectProperty<>(v));
//         return v;
//     }
// 
// }
// 
// Path: src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVValue.java
// public class CSVValue {
//     private StringProperty value = new SimpleStringProperty();
//     private ValidationError valid;
// 
//     /**
//      * 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) {
//         this.value.set(value);
//     }
// 
//     /**
//      * returns if the value is valid to the rules of the validator
//      * @return
//      */
//     public ValidationError getValidationError() {
//         return valid;
//     }
// 
//     /**
//      * sets the state if a value is valid or not
//      * @param valid the validation state
//      */
//     public void setValidationError(ValidationError valid) {
//         this.valid = valid;
//     }
// }
// 
// Path: src/main/java/ninja/javafx/smartcsv/fx/util/ColorConstants.java
// public static final String ERROR_COLOR = "#ff3333";
// 
// Path: src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java
// public static String getI18nValidatioMessage(ResourceBundle resourceBundle, List<ValidationError> errors) {
// 
//     StringWriter message = new StringWriter();
//     errors.forEach(error -> message.append(getI18nValidatioMessage(resourceBundle, error)).append("\n"));
// 
// 
//     if (message.toString().length() != 0) {
//         return cutOffLastLineBreak(message.toString());
//     }
// 
//     return "";
// }


import javafx.scene.input.KeyCode;
import ninja.javafx.smartcsv.fx.table.model.CSVRow;
import ninja.javafx.smartcsv.fx.table.model.CSVValue;
import java.util.ResourceBundle;
import static javafx.application.Platform.runLater;
import static ninja.javafx.smartcsv.fx.util.ColorConstants.ERROR_COLOR;
import static ninja.javafx.smartcsv.fx.util.I18nValidationUtil.getI18nValidatioMessage;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
/*
   The MIT License (MIT)
   -----------------------------------------------------------------------------

   Copyright (c) 2015-2021 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;




/**
 * cell representation which indicates if a cell is valid and not
 * and allows editing
 */
public class EditableValidationCell extends TableCell<CSVRow, CSVValue> {

    private ValueTextField textField;
    private ResourceBundle resourceBundle;

    public EditableValidationCell(ResourceBundle resourceBundle) {
        this.resourceBundle = resourceBundle;
    }

    @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.getValidationError() == null || isEditing()) {
            setStyle("");
            setTooltip(null);
        } else if (item.getValidationError() != null) {
            setStyle("-fx-background-color: derive("+ ERROR_COLOR +", 30%)");
// Path: src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVRow.java
// public class CSVRow {
//     private ObservableMap<String, ObjectProperty<CSVValue>> columns = FXCollections.observableHashMap();
//     private int rowNumber;
// 
//     /**
//      * 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
//      */
//     CSVValue addValue(String column, String value) {
//         CSVValue v = new CSVValue();
//         v.setValue(value);
//         columns.put(column, new SimpleObjectProperty<>(v));
//         return v;
//     }
// 
// }
// 
// Path: src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVValue.java
// public class CSVValue {
//     private StringProperty value = new SimpleStringProperty();
//     private ValidationError valid;
// 
//     /**
//      * 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) {
//         this.value.set(value);
//     }
// 
//     /**
//      * returns if the value is valid to the rules of the validator
//      * @return
//      */
//     public ValidationError getValidationError() {
//         return valid;
//     }
// 
//     /**
//      * sets the state if a value is valid or not
//      * @param valid the validation state
//      */
//     public void setValidationError(ValidationError valid) {
//         this.valid = valid;
//     }
// }
// 
// Path: src/main/java/ninja/javafx/smartcsv/fx/util/ColorConstants.java
// public static final String ERROR_COLOR = "#ff3333";
// 
// Path: src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java
// public static String getI18nValidatioMessage(ResourceBundle resourceBundle, List<ValidationError> errors) {
// 
//     StringWriter message = new StringWriter();
//     errors.forEach(error -> message.append(getI18nValidatioMessage(resourceBundle, error)).append("\n"));
// 
// 
//     if (message.toString().length() != 0) {
//         return cutOffLastLineBreak(message.toString());
//     }
// 
//     return "";
// }


// Path: src/main/java/ninja/javafx/smartcsv/fx/table/EditableValidationCell.java
import javafx.scene.input.KeyCode;
import ninja.javafx.smartcsv.fx.table.model.CSVRow;
import ninja.javafx.smartcsv.fx.table.model.CSVValue;
import java.util.ResourceBundle;
import static javafx.application.Platform.runLater;
import static ninja.javafx.smartcsv.fx.util.ColorConstants.ERROR_COLOR;
import static ninja.javafx.smartcsv.fx.util.I18nValidationUtil.getI18nValidatioMessage;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;

/*
   The MIT License (MIT)
   -----------------------------------------------------------------------------

   Copyright (c) 2015-2021 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;




/**
 * cell representation which indicates if a cell is valid and not
 * and allows editing
 */
public class EditableValidationCell extends TableCell<CSVRow, CSVValue> {

    private ValueTextField textField;
    private ResourceBundle resourceBundle;

    public EditableValidationCell(ResourceBundle resourceBundle) {
        this.resourceBundle = resourceBundle;
    }

    @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.getValidationError() == null || isEditing()) {
            setStyle("");
            setTooltip(null);
        } else if (item.getValidationError() != null) {
            setStyle("-fx-background-color: derive("+ ERROR_COLOR +", 30%)");
            setTooltip(new Tooltip(getI18nValidatioMessage(resourceBundle, item.getValidationError())));