/*
* Geotoolkit - An Open Source Java GIS Toolkit
* http://www.geotoolkit.org
*
* (C) 2014, Geomatys
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotoolkit.gui.javafx.util;
import javafx.geometry.Pos;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TextField;
/**
*
* @author Johann Sorel (Geomatys)
*/
public class FXStringCell<S> extends FXTableCell<S, Object> {
private final TextField field = new TextField();
public FXStringCell() {
setGraphic(field);
setAlignment(Pos.CENTER);
setContentDisplay(ContentDisplay.CENTER);
}
@Override
public void terminateEdit() {
commitEdit(field.getText());
}
@Override
public void startEdit() {
String value = (String) getItem();
field.setText(value);
super.startEdit();
setText(null);
setGraphic(field);
field.requestFocus();
}
@Override
public void commitEdit(Object newValue) {
itemProperty().set(newValue);
super.commitEdit(newValue);
updateItem(newValue, false);
}
@Override
public void cancelEdit() {
super.cancelEdit();
updateItem(getItem(), false);
}
@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
setText(null);
setGraphic(null);
if (item != null) {
setText((String) item);
}
}
}