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.

LocaleService.java 7.4KB

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