You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateField.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tokka.ui.components.fields;
  17. import java.time.LocalDate;
  18. import java.time.format.DateTimeFormatter;
  19. import java.time.temporal.TemporalQueries;
  20. import org.jsoup.nodes.Element;
  21. import com.vaadin.event.FieldEvents.BlurEvent;
  22. import com.vaadin.event.FieldEvents.FocusEvent;
  23. import com.vaadin.shared.ui.components.field.DateFieldServerRpc;
  24. import com.vaadin.shared.ui.components.field.DateFieldState;
  25. import com.vaadin.tokka.event.Event;
  26. import com.vaadin.tokka.event.Handler;
  27. import com.vaadin.tokka.event.Registration;
  28. import com.vaadin.tokka.ui.components.HasValue;
  29. import com.vaadin.ui.AbstractComponent;
  30. import com.vaadin.ui.declarative.DesignContext;
  31. /**
  32. * A simple textual date input component.
  33. *
  34. * @author Vaadin Ltd.
  35. */
  36. public class DateField extends AbstractComponent
  37. implements HasValue<LocalDate> {
  38. private static final DateTimeFormatter FORMATTER = DateTimeFormatter
  39. .ofPattern("dd-MM-uuuu");
  40. public static class DateChange extends Event<LocalDate> {
  41. protected DateChange(DateField source, LocalDate date,
  42. boolean userOriginated) {
  43. super(source, date, userOriginated);
  44. }
  45. }
  46. public DateField() {
  47. registerRpc(new DateFieldServerRpc() {
  48. @Override
  49. public void blur() {
  50. fireEvent(new BlurEvent(DateField.this));
  51. }
  52. @Override
  53. public void focus() {
  54. fireEvent(new FocusEvent(DateField.this));
  55. }
  56. @Override
  57. public void setDate(String value) {
  58. if (!isReadOnly()) {
  59. LocalDate localDate = FORMATTER.parse(value,
  60. TemporalQueries.localDate());
  61. setValue(localDate);
  62. fireEvent(new DateChange(DateField.this, localDate, true));
  63. }
  64. }
  65. });
  66. }
  67. @Override
  68. protected DateFieldState getState() {
  69. return (DateFieldState) super.getState();
  70. }
  71. @Override
  72. protected DateFieldState getState(boolean markAsDirty) {
  73. return (DateFieldState) super.getState(markAsDirty);
  74. }
  75. @Override
  76. public void setValue(LocalDate date) {
  77. DateFieldState state = getState();
  78. state.value = date.format(FORMATTER);
  79. }
  80. @Override
  81. public LocalDate getValue() {
  82. DateFieldState state = getState(false);
  83. return FORMATTER.parse(state.value, TemporalQueries.localDate());
  84. }
  85. @Override
  86. public Registration onChange(Handler<LocalDate> handler) {
  87. return onEvent(DateChange.class, handler);
  88. }
  89. @Override
  90. public void readDesign(Element design, DesignContext designContext) {
  91. super.readDesign(design, designContext);
  92. }
  93. }