package org.minimalj.frontend.edit.fields;

import org.minimalj.autofill.DateGenerator;
import org.minimalj.model.InvalidValues;
import org.minimalj.model.PropertyInterface;
import org.minimalj.util.StringUtils;
import org.threeten.bp.LocalDate;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.DateTimeParseException;
import org.threeten.bp.format.FormatStyle;

public class LocalDateField extends TextFormatField<LocalDate> {
	private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);

	public LocalDateField(PropertyInterface property, boolean editable) {
		super(property, editable);
	}
	
	@Override
	protected String getAllowedCharacters(PropertyInterface property) {
		return "01234567890.-";
	} 

	@Override
	protected int getAllowedSize(PropertyInterface property) {
		return 10;
	}

	@Override
	public LocalDate getObject() {
		String fieldText = textField.getText();
		try {
			return LocalDate.parse(fieldText, DATE_FORMATTER);
		} catch (DateTimeParseException x) {
			return InvalidValues.createInvalidLocalDate(fieldText);
		}
	}
	
	@Override
	public void setObject(LocalDate value) {
		if (InvalidValues.isInvalid(value)) {
			String text = InvalidValues.getInvalidValue(value);
			textField.setText(text);
		} else if (value != null) {
			String text = DATE_FORMATTER.format(value);
			if (!StringUtils.equals(textField.getText(), text)) {
				textField.setText(text);
			}
		} else {
			textField.setText(null);
		}
	}
	
	@Override
	public void fillWithDemoData() {
		setObject(DateGenerator.generateRandomDate());
	}
}
@@ -168,24 +168,61 @@
 Interface;%0D%0A
+import org.minimalj.util.DateUtils;%0D%0A
 import org.m
@@ -287,58 +287,8 @@
 e;%0D%0A
-import org.threeten.bp.format.DateTimeFormatter;%0D%0A
 impo
@@ -341,52 +341,8 @@
 on;%0D
-%0Aimport org.threeten.bp.format.FormatStyle;%0D
 %0A%0D%0Ap
@@ -409,121 +409,8 @@
 %3E %7B%0D
-%0A%09private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);%0D
 %0A%0D%0A%09
@@ -833,25 +833,25 @@
 %09return 
-LocalDate
+DateUtils
 .parse(f
@@ -862,24 +862,8 @@
 Text
-, DATE_FORMATTER
 );%0D%0A
@@ -1208,21 +1208,16 @@
  = D
-ATE_FORMATTER
+ateUtils
 .for

make inputs like 1.2.2013 possible (again)