bug4210 Added first version of new date and time input handling

This commit is contained in:
Benjamin Barth committed 2017-11-13 17:00:07 +01:00
1 parent fff60d4aaa
commit 93b28b50ec
8 files changed
+410

No files matched your search

@@ -0,0 +1,72 @@
package com.sap.sailing.gwt.common.client.datetime;
import java.util.Date;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
public class DateAndTimeInput extends Composite implements DateTimeInput {
private final DateTimeInput delegate;
public DateAndTimeInput(Accuracy accuracy) {
this.delegate = DateTimeInputType.DATETIME_LOCAL.isSupported() ? new NativeDatetimeLocalInput(accuracy)
: new DateAndTimePanel(accuracy);
initWidget(delegate.asWidget());
}
@Override
public Date getValue() {
return delegate.getValue();
}
@Override
public void setValue(Date value) {
this.delegate.setValue(value);
}
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
return delegate.addValueChangeHandler(handler);
}
private class DateAndTimePanel extends FlowPanel implements DateTimeInput {
private final DateInput dateInput;
private final TimeInput timeInput;
private final DateTimeFormat dateFormat, timeFormat, datetimeFormat;
public DateAndTimePanel(Accuracy accuracy) {
add(this.dateInput = new DateInput());
add(this.timeInput = new TimeInput(accuracy));
this.dateFormat = DateTimeFormat.getFormat("yyyy-MM-dd");
this.timeFormat = DateTimeFormat.getFormat(accuracy.getTimeFormat());
this.datetimeFormat = DateTimeFormat.getFormat(accuracy.getDatetimeFormat());
}
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
return addHandler(handler, ValueChangeEvent.getType());
}
@Override
public Date getValue() {
final String dateString = dateFormat.format(dateInput.getValue());
final String timeString = timeFormat.format(timeInput.getValue());
final String dateAndTimeString = dateString + "T" + timeString;
return datetimeFormat.parse(dateAndTimeString);
}
@Override
public void setValue(Date value) {
this.dateInput.setValue(value);
this.timeInput.setValue(value);
}
}
}
@@ -0,0 +1,42 @@
package com.sap.sailing.gwt.common.client.datetime;
import java.util.Date;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.datepicker.client.DateBox;
public class DateInput extends Composite implements DateTimeInput {
private final HasValue<Date> delegate;
public DateInput() {
if (DateTimeInputType.TIME.isSupported()) {
final NativeDateInput input = new NativeDateInput();
initWidget(input);
this.delegate = input;
} else {
final DateBox input = new DateBox();
initWidget(input);
this.delegate = input;
}
}
@Override
public Date getValue() {
return delegate.getValue();
}
@Override
public void setValue(Date value) {
this.delegate.setValue(value);
}
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
return delegate.addValueChangeHandler(handler);
}
}
@@ -0,0 +1,43 @@
package com.sap.sailing.gwt.common.client.datetime;
import java.util.Date;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.IsWidget;
public interface DateTimeInput extends IsWidget, HasValue<Date> {
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 final String step, datetimeFormat, timeFormat;
private Accuracy(int step, String datetimeFormat, String timeFormat) {
this.step = String.valueOf(step);
this.datetimeFormat = datetimeFormat;
this.timeFormat = timeFormat;
}
public String getStep() {
return step;
}
public String getDatetimeFormat() {
return datetimeFormat;
}
public String getTimeFormat() {
return timeFormat;
}
}
@Override
default void setValue(Date value, boolean fireEvents) {
this.setValue(value);
if (fireEvents) {
ValueChangeEvent.fire(this, value);
}
}
}
@@ -0,0 +1,53 @@
package com.sap.sailing.gwt.common.client.datetime;
import com.google.gwt.core.shared.GWT;
/**
* Enumeration of date and/or time related input {@link #getType() types} whose {@link #isSupported() support} in the
* currently used browser was checked at runtime.
*/
public enum DateTimeInputType {
DATETIME_LOCAL("datetime-local"), DATE("date"), TIME("time");
private final String type;
private final boolean supported;
private DateTimeInputType(String type) {
this.type = type;
this.supported = checkSupport(type);
GWT.log("Input type support: " + type + " -> " + supported);
}
/**
* @return the type string for this {@link DateTimeInputType}
*/
public String getType() {
return type;
}
/**
* @return <code>true</code> if this {@link DateTimeInputType} is supported, <code>false</code> otherwise
*/
public boolean isSupported() {
return supported;
}
private static final native boolean checkSupport(String type) /*-{
var input = document.createElement("input");
input.setAttribute("type", type);
var desiredType = input.getAttribute('type');
var supported = false;
if (input.type === desiredType) {
supported = true;
}
input.value = 'Hello world';
var helloWorldAccepted = (input.value === 'Hello world');
if (helloWorldAccepted) {
supported = false;
}
input.value = '';
return supported;
}-*/;
}
@@ -0,0 +1,40 @@
package com.sap.sailing.gwt.common.client.datetime;
import java.util.Date;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Widget;
class NativeDateInput extends Widget implements DateTimeInput {
private static final String ATTR_TYPE = "type";
private final DateTimeFormat format = DateTimeFormat.getFormat("yyyy-MM-dd");
private final InputElement input;
NativeDateInput() {
this.input = Document.get().createElement(InputElement.TAG).cast();
this.input.setAttribute(ATTR_TYPE, DateTimeInputType.DATE.getType());
setElement(input);
}
@Override
public Date getValue() {
return this.format.parse(input.getValue());
}
@Override
public void setValue(Date value) {
this.input.setValue(format.format(value));
}
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
return addHandler(handler, ValueChangeEvent.getType());
}
}
@@ -0,0 +1,42 @@
package com.sap.sailing.gwt.common.client.datetime;
import java.util.Date;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Widget;
class NativeDatetimeLocalInput extends Widget implements DateTimeInput {
private static final String ATTR_TYPE = "type", ATTR_STEP = "step";
private final DateTimeFormat format;
private final InputElement input;
NativeDatetimeLocalInput(Accuracy accuracy) {
this.input = Document.get().createElement(InputElement.TAG).cast();
this.input.setAttribute(ATTR_TYPE, DateTimeInputType.DATETIME_LOCAL.getType());
this.input.setAttribute(ATTR_STEP, accuracy.getStep());
this.format = DateTimeFormat.getFormat(accuracy.getDatetimeFormat());
setElement(input);
}
@Override
public Date getValue() {
return this.format.parse(input.getValue());
}
@Override
public void setValue(Date value) {
this.input.setValue(format.format(value));
}
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
return addHandler(handler, ValueChangeEvent.getType());
}
}
@@ -0,0 +1,42 @@
package com.sap.sailing.gwt.common.client.datetime;
import java.util.Date;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Widget;
class NativeTimeInput extends Widget implements DateTimeInput {
private static final String ATTR_TYPE = "type", ATTR_STEP = "step";
private final DateTimeFormat format;
private final InputElement input;
NativeTimeInput(Accuracy accuracy) {
this.input = Document.get().createElement(InputElement.TAG).cast();
this.input.setAttribute(ATTR_TYPE, DateTimeInputType.TIME.getType());
this.input.setAttribute(ATTR_STEP, accuracy.getStep());
this.format = DateTimeFormat.getFormat(accuracy.getTimeFormat());
setElement(input);
}
@Override
public Date getValue() {
return this.format.parse(input.getValue());
}
@Override
public void setValue(Date value) {
this.input.setValue(format.format(value));
}
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
return addHandler(handler, ValueChangeEvent.getType());
}
}
@@ -0,0 +1,76 @@
package com.sap.sailing.gwt.common.client.datetime;
import java.text.ParseException;
import java.util.Date;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.text.shared.Parser;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.ValueBox;
public class TimeInput extends Composite implements DateTimeInput {
private final HasValue<Date> delegate;
public TimeInput(Accuracy accuracy) {
if (DateTimeInputType.TIME.isSupported()) {
final NativeTimeInput input = new NativeTimeInput(accuracy);
initWidget(input);
this.delegate = input;
} else {
final TimeConverter conv = new TimeConverter(accuracy);
final ValueBox<Date> input = ValueBox.wrap(Document.get().createElement(InputElement.TAG), conv, conv);
initWidget(input);
this.delegate = input;
}
}
@Override
public Date getValue() {
return delegate.getValue();
}
@Override
public void setValue(Date value) {
this.delegate.setValue(value);
}
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
return delegate.addValueChangeHandler(handler);
}
private class TimeConverter extends AbstractRenderer<Date> implements Parser<Date> {
private final DateTimeFormat dateTimeFormat;
private TimeConverter(Accuracy accuracy) {
this.dateTimeFormat = DateTimeFormat.getFormat(accuracy.getTimeFormat());
}
@Override
public String render(Date object) {
return dateTimeFormat.format(object);
}
@Override
public Date parse(CharSequence text) throws ParseException {
if ("".equals(text.toString())) {
return null;
}
try {
return dateTimeFormat.parse(text.toString());
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage(), 0);
}
}
}
}