diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings index e30861b5b187ba9e6eaba5b2918da8900bcf079a..6cf75f172549d1a985d1af27fdb24bcb1ccc48af 100644 --- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings +++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings @@ -1,6 +1,6 @@ DynamicTreeContentProviderBase.java 6760a6dc5721175b1dada8f30fd9da05f7bcc4b3 GREEN DynamicTreeItem.java 75dc5534b119ffdb3c10a65810c2a0f330b7955e GREEN -DynamicTreeTableUIProviderBase.java 75ddf3e91c08fd6a5853ab261593040d1039d774 GREEN +DynamicTreeTableUIProviderBase.java f4fd20e3d267bc39a33a3bbb18416312779ea886 GREEN DynamicTreeTableViewer.java 431ac62cbd6ad7df25852fce1b5a62a05ba510e3 GREEN DynamicTreeUIProviderBase.java e9b68607683de279d0cb8712a28dc131c5c33ece GREEN DynamicTreeViewer.java e9f19d16a2a7e5a1b03b8a8b543453ac7eb6a051 GREEN diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableUIProviderBase.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableUIProviderBase.java index 75ddf3e91c08fd6a5853ab261593040d1039d774..f4fd20e3d267bc39a33a3bbb18416312779ea886 100644 --- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableUIProviderBase.java +++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeTableUIProviderBase.java @@ -13,14 +13,17 @@ *******************************************************************************/ package org.fortiss.tooling.common.ui.javafx.control.treetableview; -import static javafx.scene.control.cell.TextFieldTreeTableCell.forTreeTableColumn; - import javafx.scene.Node; import javafx.scene.control.ContextMenu; +import javafx.scene.control.TextField; import javafx.scene.control.TreeTableCell; import javafx.scene.control.TreeTableColumn; +import javafx.scene.control.cell.TextFieldTreeTableCell; import javafx.scene.input.Dragboard; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; import javafx.util.Callback; +import javafx.util.converter.DefaultStringConverter; /** * This UI provider is responsible to return the label, the icon, and the context menu for each cell @@ -128,8 +131,11 @@ public abstract class DynamicTreeTableUIProviderBase<T> { column.setOnEditCommit(null); return; } - column.setCellFactory(createEditableCellFactory()); + column.setCellFactory(createEditableCellFactory(i)); column.setEditable(true); + column.setOnEditStart(event -> { + event.getEventType(); + }); column.setOnEditCommit(event -> { T element = event.getRowValue().getValue(); int colIndex = event.getTreeTablePosition().getColumn(); @@ -141,8 +147,82 @@ public abstract class DynamicTreeTableUIProviderBase<T> { /** Creates a cell factory for editable cells. */ private Callback<TreeTableColumn<T, String>, TreeTableCell<T, String>> - createEditableCellFactory() { - return forTreeTableColumn(); + createEditableCellFactory(int colIndex) { + return param -> { + return new MyTextFieldTreeTableCell(colIndex); + }; + } + + /** Custom table cell implementation to allow on key released validation. */ + private class MyTextFieldTreeTableCell extends TextFieldTreeTableCell<T, String> { + /** The column index of this cell. */ + private final int columnIndex; + + /** Constructor. */ + public MyTextFieldTreeTableCell(int colIndex) { + this.columnIndex = colIndex; + setConverter(new DefaultStringConverter()); + } + + /** {@inheritDoc} */ + @Override + public void startEdit() { + if(!isEditable() || !getTreeTableView().isEditable() || + !getTableColumn().isEditable()) { + return; + } + super.startEdit(); + // the following code uses implementation details of + // CellUtils.startEdit(...) and CellUtils.createTextfield(...) + TextField tf = (TextField)getGraphic(); // CellUtils:228 + tf.setOnKeyReleased(t -> { + DynamicTreeTableUIProviderBase<T>.MyTextFieldTreeTableCell mythis = + MyTextFieldTreeTableCell.this; + if(t.getCode() == KeyCode.ESCAPE) { + mythis.cancelEdit(); + t.consume(); + } else { + T item = mythis.getTreeTableRow().getItem(); + validateOnKeyReleased(t, columnIndex, item, tf.getText()); + } + }); + } + + /** {@inheritDoc} */ + @Override + public void cancelEdit() { + super.cancelEdit(); + T item = getTreeTableRow().getItem(); + validateOnCancelEdit(columnIndex, item); + } + } + + /** + * Called when editing a cell was canceled. + * + * @param colIndex + * the index of the column + * @param item + * the row item + */ + public void validateOnCancelEdit(int colIndex, T item) { + // nothing to do + } + + /** + * Called when a key was released during editing. Implementors should consume the event. + * + * @param event + * the key released event + * @param colIndex + * the index of the column + * @param item + * the row item + * @param text + * the current textfield content + */ + public void validateOnKeyReleased(KeyEvent event, int colIndex, T item, String text) { + // nothing to do } /** Creates a cell factory for read-only cells. */