mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 21:48:22 +02:00
add support for i18n validation messages
This commit is contained in:
@@ -90,10 +90,10 @@ public class SmartCSVController extends FXMLController {
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// injections
|
||||
// members
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private ValidationCellFactory cellFactory = new ValidationCellFactory();
|
||||
private ValidationCellFactory cellFactory;
|
||||
private final LoadCSVService loadCSVService = new LoadCSVService();
|
||||
private final SaveCSVService saveCSVService = new SaveCSVService();
|
||||
private CSVModel model;
|
||||
@@ -105,9 +105,10 @@ public class SmartCSVController extends FXMLController {
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
public void initialize(URL location, ResourceBundle resourceBundle) {
|
||||
cellFactory = new ValidationCellFactory(resourceBundle);
|
||||
stateline.setVisible(false);
|
||||
errorList.setCellFactory(param -> new ValidationErrorListCell());
|
||||
errorList.setCellFactory(param -> new ValidationErrorListCell(resourceBundle));
|
||||
errorList.getSelectionModel().selectedItemProperty().addListener(
|
||||
observable -> scrollToError()
|
||||
);
|
||||
|
||||
@@ -30,12 +30,20 @@ import javafx.scene.control.ListCell;
|
||||
import javafx.scene.text.Text;
|
||||
import ninja.javafx.smartcsv.validation.ValidationError;
|
||||
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import static ninja.javafx.smartcsv.fx.util.I18nValidationUtil.getI18nValidatioMessage;
|
||||
|
||||
/**
|
||||
* TODO: DESCRIPTION!!!
|
||||
*/
|
||||
public class ValidationErrorListCell extends ListCell<ValidationError> {
|
||||
|
||||
private Text text;
|
||||
private ResourceBundle resourceBundle;
|
||||
|
||||
public ValidationErrorListCell(ResourceBundle resourceBundle) {
|
||||
this.resourceBundle = resourceBundle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateItem(ValidationError validationError, boolean empty) {
|
||||
@@ -55,7 +63,7 @@ public class ValidationErrorListCell extends ListCell<ValidationError> {
|
||||
|
||||
private void addContent(ValidationError validationError) {
|
||||
setText(null);
|
||||
text = new Text(validationError.getMessage());
|
||||
Text text = new Text(getI18nValidatioMessage(resourceBundle, validationError.getMessages()));
|
||||
text.setWrappingWidth(180);
|
||||
setGraphic(text);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,10 @@ 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.I18nValidationUtil.getI18nValidatioMessage;
|
||||
|
||||
/**
|
||||
* Created by Andreas on 27.11.2015.
|
||||
@@ -42,6 +45,11 @@ import static javafx.application.Platform.runLater;
|
||||
public class EditableValidationCell extends TableCell<CSVRow, CSVValue> {
|
||||
|
||||
private ValueTextField textField;
|
||||
private ResourceBundle resourceBundle;
|
||||
|
||||
public EditableValidationCell(ResourceBundle resourceBundle) {
|
||||
this.resourceBundle = resourceBundle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startEdit() {
|
||||
@@ -69,7 +77,7 @@ public class EditableValidationCell extends TableCell<CSVRow, CSVValue> {
|
||||
setTooltip(null);
|
||||
} else if (item.getValidationError() != null) {
|
||||
setStyle("-fx-background-color: #ff8888");
|
||||
setTooltip(new Tooltip(item.getValidationError().getMessage()));
|
||||
setTooltip(new Tooltip(getI18nValidatioMessage(resourceBundle, item.getValidationError().getMessages())));
|
||||
}
|
||||
|
||||
if (item == null || empty) {
|
||||
|
||||
@@ -32,13 +32,21 @@ import javafx.util.Callback;
|
||||
import ninja.javafx.smartcsv.fx.table.model.CSVRow;
|
||||
import ninja.javafx.smartcsv.fx.table.model.CSVValue;
|
||||
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* Created by Andreas on 18.11.2015.
|
||||
*/
|
||||
public class ValidationCellFactory implements Callback<TableColumn<CSVRow, CSVValue>, TableCell<CSVRow, CSVValue>> {
|
||||
|
||||
private ResourceBundle resourceBundle;
|
||||
|
||||
public ValidationCellFactory(ResourceBundle resourceBundle) {
|
||||
this.resourceBundle = resourceBundle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableCell<CSVRow, CSVValue> call(TableColumn<CSVRow, CSVValue> param) {
|
||||
return new EditableValidationCell();
|
||||
return new EditableValidationCell(resourceBundle);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
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.util;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* This class makes validation messages readable in supported languages
|
||||
*/
|
||||
public class I18nValidationUtil {
|
||||
|
||||
public static String getI18nValidatioMessage(ResourceBundle resourceBundle, List<String> messages) {
|
||||
|
||||
StringWriter message = new StringWriter();
|
||||
for (String key: messages) {
|
||||
if (resourceBundle.containsKey(key)) {
|
||||
message.append(resourceBundle.getString(key)).append("\n");
|
||||
} else {
|
||||
message.append(key).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
return message.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user