package de.saxsys.jfx.tabellen;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

import javafx.application.Platform;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableCell;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class AgePickerCell<Inputs> extends TableCell<Inputs, LocalDate> {

	private final DatePicker datePicker = new DatePicker();;

	public AgePickerCell() {

		datePicker.addEventFilter(KeyEvent.KEY_PRESSED, t -> {
			if (t.getCode() == KeyCode.ENTER) {
				cancelEdit();
			} else if (t.getCode() == KeyCode.ESCAPE) {
				cancelEdit();
			} else if (t.getCode() == KeyCode.TAB) {
				cancelEdit();
			}
		});

		datePicker.valueProperty().addListener((bean, oldVal, newVal) -> {
			if (newVal != null) {
				Platform.runLater(() -> commitEdit(newVal));
			}
		});

		datePicker.focusedProperty().addListener((bean, oldVal, newVal) -> {
			if (!newVal) {
				cancelEdit();
			}
		});

		setOnMouseClicked(e -> {
			if (getContentDisplay() == ContentDisplay.TEXT_ONLY) {
				startEdit();
			}
		});

		setContentDisplay(ContentDisplay.TEXT_ONLY);
	}

	@Override
	public void commitEdit(LocalDate newValue) {
		System.out.println("---------->" + newValue);
		super.commitEdit(newValue);
	}

	@Override
	public void startEdit() {
		super.startEdit();
		if (!isEmpty()) {
			setGraphic(datePicker);
			setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
			Platform.runLater(() -> datePicker.requestFocus());
		}
	}

	@Override
	public void cancelEdit() {
		super.cancelEdit();
		setContentDisplay(ContentDisplay.TEXT_ONLY);
	}

	@Override
	protected void updateItem(LocalDate item, boolean empty) {
		super.updateItem(item, empty);
		// TODO UPDATE DATE
		if (item != null) {
			this.setItem(item);
			datePicker.setValue(item);
			int between = (int) ChronoUnit.YEARS.between(item, LocalDate.now());
			setText(String.valueOf(between));
		}
	}
}

@@ -265,44 +265,8 @@
 l;%0D%0A
-import javafx.scene.input.KeyCode;%0D%0A
 impo
@@ -528,18 +528,22 @@
 %3E %7B%0D%0A%09%09%09
-if
+switch
  (t.getC
@@ -551,28 +551,27 @@
 de()
- == KeyCode.
+) %7B%0D%0A%09%09%09case 
 ENTER
-) %7B
+:
 %0D%0A%09%09
@@ -594,51 +594,32 @@
 %0A%09%09%09
-%7D else if (t.getCode() == KeyCode.
+%09break;%0D%0A%09%09%09case 
 ESCAPE
-) %7B
+:
 %0D%0A%09%09
@@ -642,66 +642,84 @@
 %0A%09%09%09
-%7D else if (t.getCode() == KeyCode.TAB) %7B%0D%0A%09%09%09%09cancelEdit()
+%09break;%0D%0A%09%09%09case TAB:%0D%0A%09%09%09%09cancelEdit();%0D%0A%09%09%09%09break;%0D%0A%09%09%09default:%0D%0A%09%09%09%09break
 ;%0D%0A%09
@@ -1255,57 +1255,8 @@
  %7B%0D%0A
-%09%09System.out.println(%22----------%3E%22 + newValue);%0D%0A
 %09%09su

Switch Case instead of If