All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.panemu.tiwulfx.table.DateTableCell Maven / Gradle / Ivy

Go to download

TiwulFX provides JavaFX custom components specially designed to work with java POJO object.

There is a newer version: 3.4.2
Show newest version
/*
 * Copyright (C) 2014 Panemu.
 *
 * 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; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */
package com.panemu.tiwulfx.table;

import com.panemu.tiwulfx.control.DateField;
import com.panemu.tiwulfx.control.skin.DateFieldSkin;
import java.util.Date;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.control.Control;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 *
 * @author Amrullah
 */
public class DateTableCell extends BaseCell {

	private DateField datePicker;

	public DateTableCell(DateColumn column) {
		super(column);
	}

	@Override
	protected void setValueToEditor(Date value) {
		datePicker.setSelectedDate(value);
	}

	@Override
	protected Date getValueFromEditor() {
		return datePicker.getSelectedDate();
	}

	@Override
	protected Control getEditor() {
		if (datePicker == null) {
			DateColumn column = (DateColumn) getTableColumn();
			datePicker = new DateField();
			/**
			 * Disable traversing focus using LEFT and RIGHT.
			 */
			datePicker.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler() {
				@Override
				public void handle(KeyEvent event) {
					if ((event.getCode() == KeyCode.LEFT || event.getCode() == KeyCode.RIGHT)
							  && isEditing()) {
						event.consume();
					}
				}
			});

			datePicker.selectedDateProperty().addListener(new ChangeListener() {

				@Override
				public void changed(ObservableValue ov, Date t, Date newValue) {
					for (CellEditorListener svl : column.getCellEditorListeners()) {
						svl.valueChanged(DateTableCell.this, getTableRow().getIndex(), column.getPropertyName(), (R) getTableRow().getItem(), newValue);
					}
				}
			});
		}
		return datePicker;
	}

	@Override
	protected Control getFocusableControl() {
		if (datePicker.getSkin() != null) {
			return ((DateFieldSkin) datePicker.getSkin()).getTextField();
		}
		return null;
	}

	@Override
	protected void attachEnterEscapeEventHandler() {
		/**
		 * Use event filter instead on onKeyPressed because Enter and Escape have
		 * been consumed by Combobox it self
		 */
		datePicker.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler() {
			@Override
			public void handle(KeyEvent t) {
				if (t.getCode() == KeyCode.ENTER) {
					commitEdit(getValueFromEditor());
					t.consume();
				} else if (t.getCode() == KeyCode.ESCAPE) {
					cancelEdit();
					/**
					 * Propagate ESCAPE key press to cell to go to Browsing mode on
					 * Agile editing only
					 */
					DateTableCell.this.fireEvent(t);
				}
			}
		});
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy