jidefx.scene.control.field.LocalDateTimeField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jidefx-fields Show documentation
Show all versions of jidefx-fields Show documentation
JideFX Common Layer is a collection of several extend feature for JavaFX
The newest version!
/*
* @(#)LocalDateTimeField.java 5/19/2013
*
* Copyright 2002 - 2013 JIDE Software Inc. All rights reserved.
*/
package jidefx.scene.control.field;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.shape.Shape;
import javafx.util.Callback;
import jidefx.scene.control.decoration.Decorator;
import jidefx.scene.control.decoration.PredefinedDecorators;
import jidefx.scene.control.field.popup.LocalDateTimePopupContent;
import jidefx.scene.control.field.popup.PopupContent;
import jidefx.scene.control.field.verifier.PatternVerifierUtils;
import jidefx.utils.CommonUtils;
import jidefx.utils.PredefinedShapes;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
/**
* A {@code PopupField} for {@link LocalDateTime}.
*/
public class LocalDateTimeField extends PopupField {
public LocalDateTimeField() {
this(((SimpleDateFormat) SimpleDateFormat.getDateTimeInstance()).toPattern()); // TODO: find out the pattern for the DateTimeFormatter's FormatStyle.MEDIUM
}
public LocalDateTimeField(String pattern) {
this(pattern, LocalDateTime.now());
}
public LocalDateTimeField(String pattern, LocalDateTime value) {
setDateTimeFormatter(DateTimeFormatter.ofPattern(pattern));
setPattern(pattern);
setValue(value);
}
private static final String STYLE_CLASS_DEFAULT = "local-date-field"; //NON-NLS
@Override
protected void initializeStyle() {
super.initializeStyle();
getStyleClass().addAll(STYLE_CLASS_DEFAULT);
}
@Override
protected void initializeTextField() {
super.initializeTextField();
setPopupContentFactory(new Callback>() {
@Override
public PopupContent call(LocalDateTime param) {
LocalDateTimePopupContent content = new LocalDateTimePopupContent();
content.setValue(getValue());
return content;
}
});
}
@Override
protected void customizePopupContent(PopupContent popupContent) {
super.customizePopupContent(popupContent);
popupContent.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends LocalDateTime> observable, LocalDateTime oldValue, LocalDateTime newValue) {
hide();
}
});
}
@Override
protected boolean supportFromString() {
return getDateTimeFormatter() != null || super.supportFromString();
}
@Override
protected LocalDateTime fromString(String text) {
DateTimeFormatter dateTimeFormatter = getDateTimeFormatter();
if (dateTimeFormatter != null) {
try {
TemporalAccessor parse = dateTimeFormatter.parse(text);
try {
return LocalDateTime.from(parse);
}
catch (DateTimeException e) {
CommonUtils.ignoreException(e);
}
}
catch (DateTimeParseException e) {
CommonUtils.ignoreException(e);
}
}
return super.fromString(text);
}
@Override
protected String toString(LocalDateTime value) {
DateTimeFormatter formatter = getDateTimeFormatter();
if (formatter != null) {
try {
return formatter.format(value);
}
catch (Exception e) {
CommonUtils.ignoreException(e);
}
}
return super.toString(value);
}
private ObjectProperty _dateTimeFormatProperty;
public ObjectProperty dateTimeFormatterProperty() {
if (_dateTimeFormatProperty == null) {
_dateTimeFormatProperty = new SimpleObjectProperty(this, "dateTimeFormatter") { //NON-NLS
@Override
protected void invalidated() {
super.invalidated();
PatternVerifierUtils.initializePatternVerifiersForDateTimeFormatter(getPatternVerifiers());
setStringConverter(null);
}
};
}
return _dateTimeFormatProperty;
}
/**
* Gets the DateTimeFormatter.
*
* @return the DateTimeFormatter.
*/
public DateTimeFormatter getDateTimeFormatter() {
return dateTimeFormatterProperty().get();
}
/**
* Sets the DateTimeFormatter that will format a LocalDateTime, LocalTime or LocalDateTimeTime.
*
* @param format the new DateTimeFormatter.
*/
public void setDateTimeFormatter(DateTimeFormatter format) {
dateTimeFormatterProperty().set(format);
}
@Override
protected Decorator