Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DateTimeField.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2000-2018 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.ui;
  17. import java.time.LocalDateTime;
  18. import com.vaadin.shared.ui.datefield.LocalDateTimeFieldState;
  19. /**
  20. * A date time entry component, which displays the actual date selector as a
  21. * popup.
  22. *
  23. * @see AbstractLocalDateTimeField
  24. * @see InlineDateTimeField
  25. * @author Vaadin Ltd.
  26. * @since 8.0
  27. */
  28. public class DateTimeField extends AbstractLocalDateTimeField {
  29. /**
  30. * Constructs an empty <code>DateTimeField</code> with no caption.
  31. */
  32. public DateTimeField() {
  33. super();
  34. }
  35. /**
  36. * Constructs a new <code>DateTimeField</code> with the given caption and
  37. * initial text contents.
  38. *
  39. * @param caption
  40. * the caption <code>String</code> for the editor.
  41. * @param value
  42. * the LocalDateTime value.
  43. */
  44. public DateTimeField(String caption, LocalDateTime value) {
  45. super(caption, value);
  46. }
  47. /**
  48. * Constructs an empty <code>DateTimeField</code> with caption.
  49. *
  50. * @param caption
  51. * the caption of the datefield.
  52. */
  53. public DateTimeField(String caption) {
  54. super(caption);
  55. }
  56. /**
  57. * Constructs a new {@code DateTimeField} with a value change listener.
  58. * <p>
  59. * The listener is called when the value of this {@code DateTimeField} is
  60. * changed either by the user or programmatically.
  61. *
  62. * @param valueChangeListener
  63. * the value change listener, not {@code null}
  64. */
  65. public DateTimeField(
  66. ValueChangeListener<LocalDateTime> valueChangeListener) {
  67. super();
  68. addValueChangeListener(valueChangeListener);
  69. }
  70. /**
  71. * Constructs a new {@code DateTimeField} with the given caption and a value
  72. * change listener.
  73. * <p>
  74. * The listener is called when the value of this {@code DateTimeField} is
  75. * changed either by the user or programmatically.
  76. *
  77. * @param caption
  78. * the caption for the field
  79. * @param valueChangeListener
  80. * the value change listener, not {@code null}
  81. */
  82. public DateTimeField(String caption,
  83. ValueChangeListener<LocalDateTime> valueChangeListener) {
  84. this(valueChangeListener);
  85. setCaption(caption);
  86. }
  87. /**
  88. * Constructs a new {@code DateTimeField} with the given caption, initial
  89. * text contents and a value change listener.
  90. * <p>
  91. * The listener is called when the value of this {@code DateTimeField} is
  92. * changed either by the user or programmatically.
  93. *
  94. * @param caption
  95. * the caption for the field
  96. * @param value
  97. * the value for the field, not {@code null}
  98. * @param valueChangeListener
  99. * the value change listener, not {@code null}
  100. */
  101. public DateTimeField(String caption, LocalDateTime value,
  102. ValueChangeListener<LocalDateTime> valueChangeListener) {
  103. this(caption, value);
  104. addValueChangeListener(valueChangeListener);
  105. }
  106. /**
  107. * Returns the current placeholder text.
  108. *
  109. * @see #setPlaceholder(String)
  110. * @return the placeholder text
  111. */
  112. public String getPlaceholder() {
  113. return getState(false).placeholder;
  114. }
  115. /**
  116. * Sets the placeholder text. The placeholder is text that is displayed when
  117. * the field would otherwise be empty, to prompt the user for input.
  118. *
  119. * @param placeholder
  120. * the placeholder text to set
  121. */
  122. public void setPlaceholder(String placeholder) {
  123. getState().placeholder = placeholder;
  124. }
  125. @Override
  126. protected LocalDateTimeFieldState getState() {
  127. return (LocalDateTimeFieldState) super.getState();
  128. }
  129. @Override
  130. protected LocalDateTimeFieldState getState(boolean markAsDirty) {
  131. return (LocalDateTimeFieldState) super.getState(markAsDirty);
  132. }
  133. /**
  134. * Checks whether the text field is enabled (default) or not.
  135. *
  136. * @see #setTextFieldEnabled(boolean)
  137. *
  138. * @return <b>true</b> if the text field is enabled, <b>false</b> otherwise.
  139. */
  140. public boolean isTextFieldEnabled() {
  141. return getState(false).textFieldEnabled;
  142. }
  143. /**
  144. * Enables or disables the text field. By default the text field is enabled.
  145. * Disabling it causes only the button for date selection to be active, thus
  146. * preventing the user from entering invalid dates.
  147. *
  148. * See <a href="http://dev.vaadin.com/ticket/6790">issue 6790</a>.
  149. *
  150. * @param state
  151. * <b>true</b> to enable text field, <b>false</b> to disable it.
  152. */
  153. public void setTextFieldEnabled(boolean state) {
  154. getState().textFieldEnabled = state;
  155. }
  156. /**
  157. * Set a description that explains the usage of the Widget for users of
  158. * assistive devices.
  159. *
  160. * @param description
  161. * String with the description
  162. */
  163. public void setAssistiveText(String description) {
  164. getState().descriptionForAssistiveDevices = description;
  165. }
  166. /**
  167. * Get the description that explains the usage of the Widget for users of
  168. * assistive devices.
  169. *
  170. * @return String with the description
  171. */
  172. public String getAssistiveText() {
  173. return getState(false).descriptionForAssistiveDevices;
  174. }
  175. }