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.

VAbstractTextualDate.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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.client.ui;
  17. import java.util.Date;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.google.gwt.aria.client.Roles;
  21. import com.google.gwt.core.client.Scheduler;
  22. import com.google.gwt.dom.client.Element;
  23. import com.google.gwt.event.dom.client.ChangeEvent;
  24. import com.google.gwt.event.dom.client.ChangeHandler;
  25. import com.google.gwt.event.dom.client.DomEvent;
  26. import com.google.gwt.event.dom.client.KeyCodes;
  27. import com.google.gwt.event.dom.client.KeyDownEvent;
  28. import com.google.gwt.event.dom.client.KeyDownHandler;
  29. import com.google.gwt.i18n.client.DateTimeFormat;
  30. import com.google.gwt.i18n.client.TimeZone;
  31. import com.google.gwt.user.client.ui.TextBox;
  32. import com.vaadin.client.BrowserInfo;
  33. import com.vaadin.client.Focusable;
  34. import com.vaadin.client.LocaleNotLoadedException;
  35. import com.vaadin.client.LocaleService;
  36. import com.vaadin.client.ui.aria.AriaHelper;
  37. import com.vaadin.client.ui.aria.HandlesAriaCaption;
  38. import com.vaadin.client.ui.aria.HandlesAriaInvalid;
  39. import com.vaadin.client.ui.aria.HandlesAriaRequired;
  40. import com.vaadin.shared.EventId;
  41. /**
  42. * Abstract textual date field base implementation. Provides a text box as an
  43. * editor for a date. The class is parameterized by the date resolution
  44. * enumeration type.
  45. *
  46. * @author Vaadin Ltd
  47. *
  48. * @param <R>
  49. * the resolution type which this field is based on (day, month, ...)
  50. * @since 8.0
  51. */
  52. public abstract class VAbstractTextualDate<R extends Enum<R>>
  53. extends VDateField<R>
  54. implements ChangeHandler, Focusable, SubPartAware, HandlesAriaCaption,
  55. HandlesAriaInvalid, HandlesAriaRequired, KeyDownHandler {
  56. private static final String PARSE_ERROR_CLASSNAME = "-parseerror";
  57. private static final String ISO_DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
  58. private static final String ISO_DATE_PATTERN = "yyyy-MM-dd";
  59. /** For internal use only. May be removed or replaced in the future. */
  60. public final TextBox text;
  61. /** For internal use only. May be removed or replaced in the future. */
  62. public boolean lenient;
  63. private static final String TEXTFIELD_ID = "field";
  64. /** For internal use only. May be removed or replaced in the future. */
  65. private String formatStr;
  66. /** For internal use only. May be removed or replaced in the future. */
  67. private TimeZone timeZone;
  68. /**
  69. * Specifies whether the group of components has focus or not.
  70. */
  71. private boolean groupFocus;
  72. public VAbstractTextualDate(R resoluton) {
  73. super(resoluton);
  74. text = new TextBox();
  75. text.addChangeHandler(this);
  76. text.addFocusHandler(event -> fireBlurFocusEvent(event, true));
  77. text.addBlurHandler(event -> fireBlurFocusEvent(event, false));
  78. if (BrowserInfo.get().isIE()) {
  79. addDomHandler(this, KeyDownEvent.getType());
  80. }
  81. add(text);
  82. publishJSHelpers(getElement());
  83. }
  84. /**
  85. * Updates style names for the widget (and its children).
  86. */
  87. protected void updateStyleNames() {
  88. if (text != null) {
  89. text.setStyleName(VTextField.CLASSNAME);
  90. text.addStyleName(getStylePrimaryName() + "-textfield");
  91. }
  92. }
  93. /**
  94. * Gets the date format string for the current locale.
  95. *
  96. * @return the format string
  97. */
  98. public String getFormatString() {
  99. if (formatStr == null) {
  100. setFormatString(createFormatString());
  101. }
  102. return formatStr;
  103. }
  104. /**
  105. * Create a format string suitable for the widget in its current state.
  106. *
  107. * @return a date format string to use when formatting and parsing the text
  108. * in the input field
  109. * @since 8.1
  110. */
  111. protected String createFormatString() {
  112. if (isYear(getCurrentResolution())) {
  113. return "yyyy"; // force full year
  114. }
  115. try {
  116. String frmString = LocaleService.getDateFormat(currentLocale);
  117. return cleanFormat(frmString);
  118. } catch (LocaleNotLoadedException e) {
  119. // TODO should die instead? Can the component survive
  120. // without format string?
  121. getLogger().log(Level.SEVERE,
  122. e.getMessage() == null ? "" : e.getMessage(), e);
  123. return null;
  124. }
  125. }
  126. /**
  127. * Sets the date format string to use for the text field.
  128. *
  129. * @param formatString
  130. * the format string to use, or {@code null} to force re-creating
  131. * the format string from the locale the next time it is needed
  132. * @since 8.1
  133. */
  134. public void setFormatString(String formatString) {
  135. this.formatStr = formatString;
  136. }
  137. @Override
  138. public void bindAriaCaption(
  139. com.google.gwt.user.client.Element captionElement) {
  140. AriaHelper.bindCaption(text, captionElement);
  141. }
  142. @Override
  143. public void setAriaRequired(boolean required) {
  144. AriaHelper.handleInputRequired(text, required);
  145. }
  146. @Override
  147. public void setAriaInvalid(boolean invalid) {
  148. AriaHelper.handleInputInvalid(text, invalid);
  149. }
  150. /**
  151. * Updates the text field according to the current date (provided by
  152. * {@link #getDate()}). Takes care of updating text, enabling and disabling
  153. * the field, setting/removing readonly status and updating readonly styles.
  154. * <p>
  155. * For internal use only. May be removed or replaced in the future.
  156. * <p>
  157. * TODO: Split part of this into a method that only updates the text as this
  158. * is what usually is needed except for updateFromUIDL.
  159. */
  160. public void buildDate() {
  161. removeStyleName(getStylePrimaryName() + PARSE_ERROR_CLASSNAME);
  162. // Create the initial text for the textfield
  163. String dateText;
  164. Date currentDate = getDate();
  165. // Always call this to ensure the format ends up in the element
  166. String formatString = getFormatString();
  167. if (currentDate != null) {
  168. dateText = getDateTimeService().formatDate(currentDate,
  169. formatString, timeZone);
  170. } else {
  171. dateText = "";
  172. }
  173. setText(dateText);
  174. text.setEnabled(enabled);
  175. text.setReadOnly(readonly);
  176. if (readonly) {
  177. text.addStyleName("v-readonly");
  178. Roles.getTextboxRole().setAriaReadonlyProperty(text.getElement(),
  179. true);
  180. } else {
  181. text.removeStyleName("v-readonly");
  182. Roles.getTextboxRole()
  183. .removeAriaReadonlyProperty(text.getElement());
  184. }
  185. }
  186. /**
  187. * Sets the time zone for the field.
  188. *
  189. * @param timeZone
  190. * the new time zone to use
  191. * @since 8.2
  192. */
  193. public void setTimeZone(TimeZone timeZone) {
  194. this.timeZone = timeZone;
  195. }
  196. @Override
  197. public void setEnabled(boolean enabled) {
  198. super.setEnabled(enabled);
  199. text.setEnabled(enabled);
  200. }
  201. @Override
  202. public void onChange(ChangeEvent event) {
  203. updateBufferedValues();
  204. sendBufferedValues();
  205. }
  206. @Override
  207. public void updateBufferedValues() {
  208. updateDate();
  209. bufferedDateString = text.getText();
  210. updateBufferedResolutions();
  211. }
  212. private void updateDate() {
  213. if (!text.getText().isEmpty()) {
  214. try {
  215. String enteredDate = text.getText();
  216. setDate(getDateTimeService().parseDate(enteredDate,
  217. getFormatString(), lenient));
  218. if (lenient) {
  219. // If date value was leniently parsed, normalize text
  220. // presentation.
  221. // FIXME: Add a description/example here of when this is
  222. // needed
  223. text.setValue(getDateTimeService().formatDate(getDate(),
  224. getFormatString(), timeZone), false);
  225. }
  226. // remove possibly added invalid value indication
  227. removeStyleName(getStylePrimaryName() + PARSE_ERROR_CLASSNAME);
  228. } catch (final Exception e) {
  229. getLogger().log(Level.INFO,
  230. e.getMessage() == null ? "" : e.getMessage(), e);
  231. addStyleName(getStylePrimaryName() + PARSE_ERROR_CLASSNAME);
  232. setDate(null);
  233. }
  234. } else {
  235. setDate(null);
  236. // remove possibly added invalid value indication
  237. removeStyleName(getStylePrimaryName() + PARSE_ERROR_CLASSNAME);
  238. }
  239. }
  240. /**
  241. * Updates the {@link VDateField#bufferedResolutions bufferedResolutions},
  242. * then {@link #sendBufferedValues() sends} the values to the server.
  243. *
  244. * @since 8.2
  245. * @deprecated Use {@link #updateBufferedResolutions()} and
  246. * {@link #sendBufferedValues()} instead.
  247. */
  248. @Deprecated
  249. protected final void updateAndSendBufferedValues() {
  250. updateBufferedResolutions();
  251. sendBufferedValues();
  252. }
  253. /**
  254. * Updates {@link VDateField#bufferedResolutions bufferedResolutions} before
  255. * sending a response to the server.
  256. * <p>
  257. * The method can be overridden by subclasses to provide a custom logic for
  258. * date variables to avoid overriding the {@link #onChange(ChangeEvent)}
  259. * method.
  260. *
  261. * <p>
  262. * Note that this method should not send the buffered values. For that, use
  263. * {@link #sendBufferedValues()}.
  264. *
  265. * @since 8.2
  266. */
  267. protected void updateBufferedResolutions() {
  268. Date currentDate = getDate();
  269. if (currentDate != null) {
  270. bufferedResolutions.put(
  271. getResolutions().filter(this::isYear).findFirst().get(),
  272. currentDate.getYear() + 1900);
  273. }
  274. }
  275. /**
  276. * Clean date format string to make it suitable for
  277. * {@link #getFormatString()}.
  278. *
  279. * @see #getFormatString()
  280. *
  281. * @param format
  282. * date format string
  283. * @return cleaned up string
  284. */
  285. protected String cleanFormat(String format) {
  286. // Remove unsupported patterns
  287. // TODO support for 'G', era designator (used at least in Japan)
  288. format = format.replaceAll("[GzZwWkK]", "");
  289. // Remove extra delimiters ('/' and '.')
  290. while (format.startsWith("/") || format.startsWith(".")
  291. || format.startsWith("-")) {
  292. format = format.substring(1);
  293. }
  294. while (format.endsWith("/") || format.endsWith(".")
  295. || format.endsWith("-")) {
  296. format = format.substring(0, format.length() - 1);
  297. }
  298. // Remove duplicate delimiters
  299. format = format.replaceAll("//", "/");
  300. format = format.replaceAll("\\.\\.", ".");
  301. format = format.replaceAll("--", "-");
  302. return format.trim();
  303. }
  304. @Override
  305. public void focus() {
  306. text.setFocus(true);
  307. }
  308. /**
  309. * Sets the placeholder for this textual date input.
  310. *
  311. * @param placeholder
  312. * the placeholder to set, or {@code null} to clear
  313. */
  314. public void setPlaceholder(String placeholder) {
  315. if (placeholder != null) {
  316. text.getElement().setAttribute("placeholder", placeholder);
  317. } else {
  318. text.getElement().removeAttribute("placeholder");
  319. }
  320. }
  321. /**
  322. * Gets the set placeholder this textual date input, or an empty string if
  323. * none is set.
  324. *
  325. * @return the placeholder or an empty string if none set
  326. */
  327. public String getPlaceHolder() {
  328. return text.getElement().getAttribute("placeholder");
  329. }
  330. protected String getText() {
  331. return text.getText();
  332. }
  333. protected void setText(String text) {
  334. this.text.setText(text);
  335. }
  336. @Override
  337. public com.google.gwt.user.client.Element getSubPartElement(
  338. String subPart) {
  339. if (subPart.equals(TEXTFIELD_ID)) {
  340. return text.getElement();
  341. }
  342. return null;
  343. }
  344. @Override
  345. public String getSubPartName(
  346. com.google.gwt.user.client.Element subElement) {
  347. if (text.getElement().isOrHasChild(subElement)) {
  348. return TEXTFIELD_ID;
  349. }
  350. return null;
  351. }
  352. @Override
  353. public void onKeyDown(KeyDownEvent event) {
  354. if (BrowserInfo.get().isIE()
  355. && event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
  356. // IE does not send change events when pressing enter in a text
  357. // input so we handle it using a key listener instead
  358. onChange(null);
  359. }
  360. }
  361. private void fireBlurFocusEvent(DomEvent<?> event, boolean focus) {
  362. String styleName = VTextField.CLASSNAME + "-"
  363. + VTextField.CLASSNAME_FOCUS;
  364. if (focus) {
  365. text.addStyleName(styleName);
  366. } else {
  367. text.removeStyleName(styleName);
  368. }
  369. Scheduler.get().scheduleDeferred(() -> checkGroupFocus(focus));
  370. // Needed for tooltip event handling
  371. fireEvent(event);
  372. }
  373. /**
  374. * Checks if the group focus has changed, and sends to the server if needed.
  375. *
  376. * @param textFocus
  377. * the focus of the {@link #text}
  378. * @since 8.3
  379. */
  380. protected void checkGroupFocus(boolean textFocus) {
  381. boolean newGroupFocus = textFocus | hasChildFocus();
  382. if (getClient() != null
  383. && connector.hasEventListener(
  384. textFocus ? EventId.FOCUS : EventId.BLUR)
  385. && groupFocus != newGroupFocus) {
  386. if (newGroupFocus) {
  387. rpc.focus();
  388. } else {
  389. rpc.blur();
  390. }
  391. sendBufferedValues();
  392. groupFocus = newGroupFocus;
  393. }
  394. }
  395. /**
  396. * Returns whether any of the child components has focus.
  397. *
  398. * @return {@code true} if any of the child component has focus,
  399. * {@code false} otherwise
  400. * @since 8.3
  401. */
  402. protected boolean hasChildFocus() {
  403. return false;
  404. }
  405. /**
  406. * Publish methods/properties on the element to be used from JavaScript.
  407. *
  408. * @since 8.1
  409. */
  410. private native void publishJSHelpers(Element root)
  411. /*-{
  412. var self = this;
  413. root.setISOValue = $entry(function (value) {
  414. self.@VAbstractTextualDate::setISODate(*)(value);
  415. });
  416. root.getISOValue = $entry(function () {
  417. return self.@VAbstractTextualDate::getISODate()();
  418. });
  419. }-*/;
  420. /**
  421. * Sets the value of the date field as a locale independent ISO date
  422. * (yyyy-MM-dd'T'HH:mm:ss or yyyy-MM-dd depending on whether this is a date
  423. * field or a date and time field).
  424. *
  425. * @param isoDate
  426. * the date to set in ISO8601 format, or null to clear the date
  427. * value
  428. * @since 8.1
  429. */
  430. public void setISODate(String isoDate) {
  431. Date date = null;
  432. if (isoDate != null) {
  433. date = getIsoFormatter().parse(isoDate);
  434. }
  435. setDate(date);
  436. updateBufferedResolutions();
  437. sendBufferedValues();
  438. }
  439. /**
  440. * Gets the value of the date field as a locale independent ISO date
  441. * (yyyy-MM-dd'T'HH:mm:ss or yyyy-MM-dd depending on whether this is a date
  442. * field or a date and time field).
  443. *
  444. * @return the current date in ISO8601 format, or null if no date is set
  445. *
  446. * @since 8.1
  447. */
  448. public String getISODate() {
  449. Date date = getDate();
  450. if (date == null) {
  451. return null;
  452. }
  453. return getIsoFormatter().format(date);
  454. }
  455. private DateTimeFormat getIsoFormatter() {
  456. if (supportsTime()) {
  457. return DateTimeFormat.getFormat(ISO_DATE_TIME_PATTERN);
  458. }
  459. return DateTimeFormat.getFormat(ISO_DATE_PATTERN);
  460. }
  461. private static Logger getLogger() {
  462. return Logger.getLogger(VAbstractTextualDate.class.getName());
  463. }
  464. }