|
/* |
|
* Copyright (c) 2017 Sebastian Teumert (<http://teumert.net>) |
|
* Some rights reserved. |
|
* |
|
* Redistribution and use in source and binary forms, with or without |
|
* modification, are permitted provided that the following conditions are met: |
|
* |
|
* 1. Redistributions of source code must retain the above copyright notice, this |
|
* list of conditions and the following disclaimer. |
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
* this list of conditions and the following disclaimer in the documentation |
|
* and/or other materials provided with the distribution. |
|
* |
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
*/ |
|
package net.teumert.javafx.scene.control.cell; |
|
|
|
import java.time.LocalDate; |
|
import java.time.format.DateTimeFormatter; |
|
|
|
import javafx.scene.control.DatePicker; |
|
import javafx.scene.control.TableCell; |
|
|
|
/** |
|
* <p>DatePicker implementation for use inside JavaFX table cells.</p> |
|
* |
|
* <p>Allows setting of a formatter to allow localized display of dates.</p> |
|
* |
|
* <p> |
|
* <b>Example</b> |
|
* </p> |
|
* <pre> |
|
* {@code |
|
* column.setCellFactory( |
|
* column -> new DatePickerTableCell<Person>( |
|
* DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM))); |
|
* } |
|
* </pre> |
|
* |
|
* @author Sebastian Teumert |
|
*/ |
|
public class DatePickerTableCell<S> extends TableCell<S, LocalDate> { |
|
|
|
private DatePicker picker; |
|
private DateTimeFormatter formatter; |
|
|
|
public DatePickerTableCell () { |
|
this (null); |
|
} |
|
|
|
/** |
|
* Creates a new table cell with date picker |
|
* @param formatter A {@link DateTimeFormatter} that can convert the selected |
|
* LocalDate (from what the user typed in) into an string with the given format. |
|
*/ |
|
public DatePickerTableCell (DateTimeFormatter formatter) { |
|
this.formatter = formatter; |
|
} |
|
|
|
@Override |
|
public void startEdit () { |
|
if (!isEditable() |
|
|| !getTableView().isEditable() |
|
|| !getTableColumn().isEditable()) { return; } |
|
super.startEdit(); |
|
|
|
if (isEditing()) { |
|
if (picker == null) { |
|
picker = new DatePicker(); |
|
|
|
// ATTENTION: Value MUST be set before the change listener(!) |
|
picker.setValue(this.getItem()); |
|
|
|
// stop editing when loosing focus |
|
// (e.g. when another cell is clicked) |
|
picker.focusedProperty().addListener( |
|
(observable, oldValue, newValue) -> { |
|
if (!newValue) |
|
cancelEdit(); |
|
} |
|
); |
|
|
|
// make sure events propagate back through to the data model |
|
picker.valueProperty().addListener( |
|
(observable, oldValue, newValue) -> commitEdit(newValue)); |
|
} |
|
|
|
this.setText(null); |
|
this.setGraphic(picker); |
|
picker.requestFocus(); |
|
} |
|
} |
|
|
|
@Override |
|
protected void updateItem (LocalDate item, boolean empty) { |
|
super.updateItem(item, empty); |
|
|
|
if (isEmpty()) { |
|
setText(null); |
|
setGraphic(null); |
|
} |
|
|
|
else { |
|
if (isEditing()) { |
|
setText(null); |
|
setGraphic(picker); |
|
} |
|
else { |
|
setGraphic(null); |
|
setText(formatText(item)); |
|
} |
|
} |
|
} |
|
|
|
private String formatText (LocalDate item) { |
|
if (item == null) |
|
return null; |
|
else if (formatter != null) |
|
return formatter.format(item); |
|
else |
|
return item.toString(); |
|
} |
|
|
|
@Override |
|
public void cancelEdit () { |
|
super.cancelEdit(); |
|
setText(formatText(getItem())); |
|
setGraphic(null); |
|
} |
|
} |