From 1ee8032a40a8b228a10650d36a8aed4e729336d9 Mon Sep 17 00:00:00 2001
From: Florian Hoelzl <hoelzl@fortiss.org>
Date: Fri, 27 Mar 2020 14:54:50 +0100
Subject: [PATCH] Kernel: added callback method for key event of table cell
 editing.

Issue-Ref: 3970
Issue-Url: https://af3-developer.fortiss.org/issues/3970
Signed-off-by: Florian Hoelzl <hoelzl@fortiss.org>
---
 .../ui/javafx/control/treetableview/.ratings  |  2 +-
 .../DynamicTreeTableUIProviderBase.java       | 66 +++++++++++++++++--
 2 files changed, 62 insertions(+), 6 deletions(-)

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 e30861b5b..309b0cac9 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 4fbc6171f3e95900678d7c2baea680e91fcc5be1 YELLOW
 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 75ddf3e91..4fbc6171f 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,58 @@ 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() {
+			super.startEdit();
+			// the following code uses implementation details of
+			// CellUtils.startEdit(...) and CellUtils.createTextfield(...)
+			TextField tf = (TextField)getGraphic(); // CellUtil: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());
+				}
+			});
+		}
+	}
+
+	/**
+	 * 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. */
-- 
GitLab