選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LocaleService.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.server;
  17. import java.io.Serializable;
  18. import java.text.DateFormat;
  19. import java.text.DateFormatSymbols;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Calendar;
  22. import java.util.GregorianCalendar;
  23. import java.util.Locale;
  24. import java.util.logging.Logger;
  25. import com.vaadin.shared.ui.ui.UIState.LocaleData;
  26. import com.vaadin.shared.ui.ui.UIState.LocaleServiceState;
  27. import com.vaadin.ui.UI;
  28. /**
  29. * Server side service which handles locale and the transmission of locale date
  30. * to the client side LocaleService.
  31. *
  32. * @since 7.1
  33. * @author Vaadin Ltd
  34. */
  35. public class LocaleService implements Serializable {
  36. private final UI ui;
  37. private final LocaleServiceState state;
  38. /**
  39. * Creates a LocaleService bound to the given UI.
  40. *
  41. * @since 7.1
  42. * @param ui
  43. * The UI which owns the LocaleService
  44. */
  45. public LocaleService(UI ui, LocaleServiceState state) {
  46. this.ui = ui;
  47. this.state = state;
  48. }
  49. /**
  50. * Retrieves the UI this service is bound to.
  51. *
  52. * @since 7.1
  53. * @return the UI for this service
  54. */
  55. public UI getUI() {
  56. return ui;
  57. }
  58. /**
  59. * Adds a locale to be sent to the client (browser) for date and time entry
  60. * etc. All locale specific information is derived from server-side
  61. * {@link Locale} instances and sent to the client when needed, eliminating
  62. * the need to use the {@link Locale} class and all the framework behind it
  63. * on the client.
  64. *
  65. * @param locale
  66. * The locale which is required on the client side
  67. */
  68. public void addLocale(Locale locale) {
  69. for (LocaleData data : getState(false).localeData) {
  70. if (data.name.equals(locale.toString())) {
  71. // Already there
  72. return;
  73. }
  74. }
  75. getState(true).localeData.add(createLocaleData(locale));
  76. }
  77. /**
  78. * Returns the state for this service
  79. * <p>
  80. * The state is transmitted inside the UI state rather than as an individual
  81. * entity.
  82. * </p>
  83. *
  84. * @since 7.1
  85. * @param markAsDirty
  86. * true to mark the state as dirty
  87. * @return a LocaleServiceState object that can be read in any case and
  88. * modified if markAsDirty is true
  89. */
  90. private LocaleServiceState getState(boolean markAsDirty) {
  91. if (markAsDirty) {
  92. getUI().markAsDirty();
  93. }
  94. return state;
  95. }
  96. /**
  97. * Creates a LocaleData instance for transportation to the client.
  98. *
  99. * @since 7.1
  100. * @param locale
  101. * The locale for which to create a LocaleData object
  102. * @return A LocaleData object with information about the given locale
  103. */
  104. protected LocaleData createLocaleData(Locale locale) {
  105. LocaleData localeData = new LocaleData();
  106. localeData.name = locale.toString();
  107. Calendar c = Calendar.getInstance(locale);
  108. c.set(2015, 0, 1);
  109. SimpleDateFormat shortMonthFormat = new SimpleDateFormat("MMM", locale);
  110. SimpleDateFormat longMonthFormat = new SimpleDateFormat("MMMM", locale);
  111. int monthsInYear = c.getMaximum(Calendar.MONTH) + 1;
  112. localeData.shortMonthNames = new String[monthsInYear];
  113. localeData.monthNames = new String[monthsInYear];
  114. for (int month = 0; month < monthsInYear; month++) {
  115. c.set(Calendar.MONTH, month);
  116. String shortMonth = shortMonthFormat.format(c.getTime());
  117. String longMonth = longMonthFormat.format(c.getTime());
  118. localeData.shortMonthNames[month] = shortMonth;
  119. localeData.monthNames[month] = longMonth;
  120. }
  121. final DateFormatSymbols dfs = new DateFormatSymbols(locale);
  122. // Client expects 0 based indexing, DateFormatSymbols use 1 based
  123. localeData.shortDayNames = new String[7];
  124. localeData.dayNames = new String[7];
  125. String[] sDayNames = dfs.getShortWeekdays();
  126. String[] lDayNames = dfs.getWeekdays();
  127. for (int i = 0; i < 7; i++) {
  128. localeData.shortDayNames[i] = sDayNames[i + 1];
  129. localeData.dayNames[i] = lDayNames[i + 1];
  130. }
  131. /*
  132. * First day of week (0 = sunday, 1 = monday)
  133. */
  134. final Calendar cal = new GregorianCalendar(locale);
  135. localeData.firstDayOfWeek = cal.getFirstDayOfWeek() - 1;
  136. /*
  137. * Date formatting (MM/DD/YYYY etc.)
  138. */
  139. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,
  140. locale);
  141. DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT,
  142. locale);
  143. if (!(dateFormat instanceof SimpleDateFormat)) {
  144. getLogger().warning("Unable to get default date pattern for locale "
  145. + locale.toString());
  146. dateFormat = new SimpleDateFormat();
  147. }
  148. if (!(timeFormat instanceof SimpleDateFormat)) {
  149. getLogger().warning("Unable to get default time pattern for locale "
  150. + locale.toString());
  151. timeFormat = new SimpleDateFormat();
  152. }
  153. final String datePattern = ((SimpleDateFormat) dateFormat).toPattern();
  154. final String timePattern = ((SimpleDateFormat) timeFormat).toPattern();
  155. localeData.dateFormat = datePattern.trim();
  156. final boolean twelveHourClock = timePattern.indexOf("a") > -1;
  157. // TODO there are other possibilities as well, like 'h' in french
  158. // (ignore them, too complicated)
  159. final String hourMinDelimiter = timePattern.indexOf(".") > -1 ? "."
  160. : ":";
  161. localeData.twelveHourClock = twelveHourClock;
  162. localeData.hourMinuteDelimiter = hourMinDelimiter;
  163. if (twelveHourClock) {
  164. final String[] ampm = dfs.getAmPmStrings();
  165. localeData.am = ampm[0];
  166. localeData.pm = ampm[1];
  167. }
  168. return localeData;
  169. }
  170. private static Logger getLogger() {
  171. return Logger.getLogger(LocaleService.class.getName());
  172. }
  173. }