I think I'd have extended ValueBoxBase instead (which already implements
LeafValueEditor –through IsEditor and ValueBoxEditor– and handles malformed
input by recording an error on an EditorDelegate — ValueBox could also be
used, just make sure you set the type=date after you called the super
constructor as it asserts type=text); use a DateTimeFormatRenderer and
implement a Parser<Date> using your DateTimeFormat.
Alternatively, extend FocusWidget (no need for a composite here) and
directly use the input's valueAsDate property (as a JsDate, turn it
into/from a java.util.Date using getTime(), or
getDate()/getMonth()/getFullYear() if you prefer).
The benefit of the ValueBoxBase and working from the text value though is
that it works even in browsers that don't support the type=date input (e.g.
Firefox; as they fallback to a type=text input in this case.) This is also
why it's important to handle malformed input: you cannot count on the
browser to sanitize the value if it doesn't support type=date, and you
should then return 'null' rather than throwing.
In any case, to create the input, you don't need JSNI; use
com.google.gwt.dom.client.Document.get().createElement("input") then
setPropertyString("type", "date").
BTW, your JSNI should have used $doc rather than document.
On Tuesday, February 7, 2017 at 8:40:44 AM UTC+1, 129pierre wrote:
>
> Hi,
>
> I did it like this. Any comment/improvement ?
>
> public class DateEditor extends Composite implements LeafValueEditor<Date>
> {
> static final DateTimeFormat html5Fmt =
> DateTimeFormat.getFormat("yyyy-MM-dd");
>
> TextBox textBox;
>
> public DateEditor() {
> super();
> textBox = new TextBox(createDateInput());
> initWidget(textBox);
> }
>
> @Override
> public void setValue(Datevalue) {
> if (value == null)
> {
> textBox.setText("");
> }
> else
> {
> textBox.setText(html5Fmt.format(value));
> }
>
> }
>
> @Override
> public Date getValue() {
> // TODO Auto-generated method stub
> String value = textBox.getText();
> if (value == null || value.trim().length() == 0) {
> return null;
> }
> return new Date(html5Fmt.parse(value));
> }
>
> private static native InputElement createDateInput() /*-{
> var input = document.createElement("input");
> input.setAttribute("type", "date");
> return input;
> }-*/;
> }
>
--
You received this message because you are subscribed to the Google Groups "GWT
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.