bug4210 Refactored view and value formats of date time inputs

This commit is contained in:
Benjamin Barth committed 2017-11-20 18:28:08 +01:00
1 parent de4253cf56
commit 39eb67d2a8
5 files changed
+23 -11

No files matched your search

@@ -29,7 +29,7 @@ public class DateAndTimeInput extends AbstractInput {
add(this.timeInput = new TimeInput(accuracy));
dateInput.addStyleName(DateTimeInputResources.INSTANCE.css().dateTimeInput());
timeInput.addStyleName(DateTimeInputResources.INSTANCE.css().dateTimeInput());
this.dateFormat = DateInput.DATE_FORMAT;
this.dateFormat = Accuracy.getDateFormat();
this.timeFormat = accuracy.getTimeFormat();
this.datetimeFormat = accuracy.getDatetimeFormat();
}
@@ -6,17 +6,18 @@ import com.google.gwt.user.datepicker.client.DateBox;
public class DateInput extends AbstractInput {
static final DateTimeFormat DATE_FORMAT = DateTimeFormat.getFormat("yyyy-MM-dd");
public DateInput() {
super(DateTimeInputType.TIME.isSupported() ? NativeDateTimeInput.date(DATE_FORMAT) : new CustomDateBox());
super(DateTimeInputType.TIME.isSupported() ? NativeDateTimeInput.date() : new CustomDateBox());
}
private static class CustomDateBox extends DateBox {
private final DateTimeFormat viewFormat = DateTimeFormat.getFormat(PredefinedFormat.DATE_MEDIUM);
private CustomDateBox() {
setFormat(new DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_MEDIUM)));
setFormat(new DefaultFormat(viewFormat));
addStyleName(DateTimeInputResources.INSTANCE.css().dateTimeInput());
getElement().setAttribute("placeholder", viewFormat.getPattern());
}
}
@@ -12,6 +12,8 @@ public interface DateTimeInput extends IsWidget, HasValue<Date> {
public enum Accuracy {
MINUTES(60, "yyyy-MM-dd'T'HH:mm", "HH:mm"), SECONDS(1, "yyyy-MM-dd'T'HH:mm:ss", "HH:mm:ss");
private static final DateTimeFormat DATE_FORMAT = DateTimeFormat.getFormat("yyyy-MM-dd");
private final String step;
private final DateTimeFormat datetimeFormat, timeFormat;
@@ -21,6 +23,10 @@ public interface DateTimeInput extends IsWidget, HasValue<Date> {
this.timeFormat = DateTimeFormat.getFormat(timeFormat);
}
public static DateTimeFormat getDateFormat() {
return DATE_FORMAT;
}
public String getStep() {
return step;
}
@@ -16,8 +16,8 @@ class NativeDateTimeInput extends Widget implements DateTimeInput {
private static final String ATTR_TYPE = "type", ATTR_STEP = "step";
static DateTimeInput date(DateTimeFormat dateFormat) {
return new NativeDateTimeInput(DateTimeInputType.DATE, "", dateFormat);
static DateTimeInput date() {
return new NativeDateTimeInput(DateTimeInputType.DATE, "", Accuracy.getDateFormat());
}
static DateTimeInput time(Accuracy accuracy) {
@@ -6,6 +6,7 @@ import java.util.Date;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.text.shared.Parser;
import com.google.gwt.user.client.ui.ValueBox;
@@ -22,21 +23,25 @@ public class TimeInput extends AbstractInput {
private TimeBox(TimeConverter converter) {
super(Document.get().createElement(InputElement.TAG), converter, converter);
addStyleName(DateTimeInputResources.INSTANCE.css().dateTimeInput());
getElement().setAttribute("placeholder", converter.viewFormat.getPattern());
}
}
private static class TimeConverter extends AbstractRenderer<Date> implements Parser<Date> {
private final DateTimeFormat timeFormat;
private static final DateTimeFormat VIEW_FORMAT_SHORT = DateTimeFormat.getFormat(PredefinedFormat.TIME_SHORT);
private static final DateTimeFormat VIEW_FORMAT_MEDIUM = DateTimeFormat.getFormat(PredefinedFormat.TIME_MEDIUM);
private final DateTimeFormat viewFormat;
private TimeConverter(Accuracy accuracy) {
this.timeFormat = accuracy.getTimeFormat();
this.viewFormat = accuracy == Accuracy.SECONDS ? VIEW_FORMAT_SHORT : VIEW_FORMAT_MEDIUM;
}
@Override
public String render(Date object) {
return timeFormat.format(object);
return viewFormat.format(object);
}
@Override
@@ -46,7 +51,7 @@ public class TimeInput extends AbstractInput {
}
try {
return timeFormat.parse(text.toString());
return viewFormat.parse(text.toString());
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage(), 0);
}