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.

VAbstractCalendarPanel.java 65KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  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. package com.vaadin.client.ui;
  17. import java.util.Date;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.function.Predicate;
  21. import java.util.stream.Collectors;
  22. import java.util.stream.Stream;
  23. import com.google.gwt.aria.client.Roles;
  24. import com.google.gwt.aria.client.SelectedValue;
  25. import com.google.gwt.dom.client.Element;
  26. import com.google.gwt.dom.client.NativeEvent;
  27. import com.google.gwt.event.dom.client.BlurEvent;
  28. import com.google.gwt.event.dom.client.BlurHandler;
  29. import com.google.gwt.event.dom.client.ClickEvent;
  30. import com.google.gwt.event.dom.client.ClickHandler;
  31. import com.google.gwt.event.dom.client.DomEvent;
  32. import com.google.gwt.event.dom.client.FocusEvent;
  33. import com.google.gwt.event.dom.client.FocusHandler;
  34. import com.google.gwt.event.dom.client.KeyCodes;
  35. import com.google.gwt.event.dom.client.KeyDownEvent;
  36. import com.google.gwt.event.dom.client.KeyDownHandler;
  37. import com.google.gwt.event.dom.client.KeyPressEvent;
  38. import com.google.gwt.event.dom.client.KeyPressHandler;
  39. import com.google.gwt.event.dom.client.MouseDownEvent;
  40. import com.google.gwt.event.dom.client.MouseDownHandler;
  41. import com.google.gwt.event.dom.client.MouseOutEvent;
  42. import com.google.gwt.event.dom.client.MouseOutHandler;
  43. import com.google.gwt.event.dom.client.MouseUpEvent;
  44. import com.google.gwt.event.dom.client.MouseUpHandler;
  45. import com.google.gwt.user.client.DOM;
  46. import com.google.gwt.user.client.Timer;
  47. import com.google.gwt.user.client.ui.Button;
  48. import com.google.gwt.user.client.ui.FlexTable;
  49. import com.google.gwt.user.client.ui.InlineHTML;
  50. import com.google.gwt.user.client.ui.Widget;
  51. import com.vaadin.client.BrowserInfo;
  52. import com.vaadin.client.DateTimeService;
  53. import com.vaadin.client.VConsole;
  54. import com.vaadin.client.WidgetUtil;
  55. import com.vaadin.shared.util.SharedUtil;
  56. /**
  57. * Abstract calendar panel to show and select a date using a resolution. The
  58. * class is parameterized by the date resolution enumeration type.
  59. *
  60. * @author Vaadin Ltd
  61. *
  62. * @param <R>
  63. * the resolution type which this field is based on (day, month, ...)
  64. * @since 8.0
  65. */
  66. @SuppressWarnings("deprecation")
  67. public abstract class VAbstractCalendarPanel<R extends Enum<R>>
  68. extends FocusableFlexTable implements KeyDownHandler, KeyPressHandler,
  69. MouseOutHandler, MouseDownHandler, MouseUpHandler, BlurHandler,
  70. FocusHandler, SubPartAware {
  71. public interface SubmitListener {
  72. /**
  73. * Called when calendar user triggers a submitting operation in calendar
  74. * panel. Eg. clicking on day or hitting enter.
  75. */
  76. void onSubmit();
  77. /**
  78. * On eg. ESC key.
  79. */
  80. void onCancel();
  81. }
  82. /**
  83. * Blur listener that listens to blur event from the panel
  84. */
  85. public interface FocusOutListener {
  86. /**
  87. * @return true if the calendar panel is not used after focus moves out
  88. */
  89. boolean onFocusOut(DomEvent<?> event);
  90. }
  91. /**
  92. * FocusChangeListener is notified when the panel changes its _focused_
  93. * value.
  94. */
  95. public interface FocusChangeListener {
  96. void focusChanged(Date focusedDate);
  97. }
  98. /**
  99. * Represents a Date button in the calendar
  100. */
  101. private class VEventButton extends Button {
  102. public VEventButton() {
  103. addMouseDownHandler(VAbstractCalendarPanel.this);
  104. addMouseOutHandler(VAbstractCalendarPanel.this);
  105. addMouseUpHandler(VAbstractCalendarPanel.this);
  106. }
  107. }
  108. private static final String CN_FOCUSED = "focused";
  109. private static final String CN_TODAY = "today";
  110. private static final String CN_SELECTED = "selected";
  111. private static final String CN_OFFMONTH = "offmonth";
  112. private static final String CN_OUTSIDE_RANGE = "outside-range";
  113. /**
  114. * Represents a click handler for when a user selects a value by using the
  115. * mouse
  116. */
  117. private ClickHandler dayClickHandler = new ClickHandler() {
  118. /*
  119. * (non-Javadoc)
  120. *
  121. * @see
  122. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
  123. * .event.dom.client.ClickEvent)
  124. */
  125. @Override
  126. public void onClick(ClickEvent event) {
  127. if (!isEnabled() || isReadonly()) {
  128. return;
  129. }
  130. Date newDate = ((Day) event.getSource()).getDate();
  131. if (!isDateInsideRange(newDate,
  132. getResolution(VAbstractCalendarPanel.this::isDay))) {
  133. return;
  134. }
  135. if (newDate.getMonth() != displayedMonth.getMonth()
  136. || newDate.getYear() != displayedMonth.getYear()) {
  137. // If an off-month date was clicked, we must change the
  138. // displayed month and re-render the calendar (#8931)
  139. displayedMonth.setMonth(newDate.getMonth());
  140. displayedMonth.setYear(newDate.getYear());
  141. renderCalendar();
  142. }
  143. focusDay(newDate);
  144. selectFocused();
  145. onSubmit();
  146. }
  147. };
  148. private VEventButton prevYear;
  149. private VEventButton nextYear;
  150. private VEventButton prevMonth;
  151. private VEventButton nextMonth;
  152. private FlexTable days = new FlexTable();
  153. private R resolution;
  154. private Timer mouseTimer;
  155. private Date value;
  156. private DateTimeService dateTimeService;
  157. private boolean showISOWeekNumbers;
  158. private FocusedDate displayedMonth;
  159. private FocusedDate focusedDate;
  160. private Day selectedDay;
  161. private Day focusedDay;
  162. private FocusOutListener focusOutListener;
  163. private SubmitListener submitListener;
  164. private FocusChangeListener focusChangeListener;
  165. private boolean hasFocus = false;
  166. private VDateField<R> parent;
  167. private boolean initialRenderDone = false;
  168. public VAbstractCalendarPanel() {
  169. getElement().setId(DOM.createUniqueId());
  170. setStyleName(VDateField.CLASSNAME + "-calendarpanel");
  171. Roles.getGridRole().set(getElement());
  172. /*
  173. * Firefox auto-repeat works correctly only if we use a key press
  174. * handler, other browsers handle it correctly when using a key down
  175. * handler
  176. */
  177. if (BrowserInfo.get().isGecko()) {
  178. addKeyPressHandler(this);
  179. } else {
  180. addKeyDownHandler(this);
  181. }
  182. addFocusHandler(this);
  183. addBlurHandler(this);
  184. }
  185. public void setParentField(VDateField<R> parent) {
  186. this.parent = parent;
  187. }
  188. /**
  189. * Sets the focus to given date in the current view. Used when moving in the
  190. * calendar with the keyboard.
  191. *
  192. * @param date
  193. * A Date representing the day of month to be focused. Must be
  194. * one of the days currently visible.
  195. */
  196. private void focusDay(Date date) {
  197. // Only used when calender body is present
  198. if (acceptDayFocus()) {
  199. if (focusedDay != null) {
  200. focusedDay.removeStyleDependentName(CN_FOCUSED);
  201. }
  202. if (date != null && focusedDate != null) {
  203. focusedDate.setTime(date.getTime());
  204. int rowCount = days.getRowCount();
  205. for (int i = 0; i < rowCount; i++) {
  206. int cellCount = days.getCellCount(i);
  207. for (int j = 0; j < cellCount; j++) {
  208. Widget widget = days.getWidget(i, j);
  209. if (widget != null
  210. && widget instanceof VAbstractCalendarPanel.Day) {
  211. Day curday = (Day) widget;
  212. if (curday.getDate().equals(date)) {
  213. curday.addStyleDependentName(CN_FOCUSED);
  214. focusedDay = curday;
  215. return;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223. /**
  224. * Returns {@code true} if current resolution assumes handling focus event
  225. * for day UI component.
  226. *
  227. * @return {@code true} if day focus events should be handled, {@code false}
  228. * otherwise
  229. */
  230. protected abstract boolean acceptDayFocus();
  231. /**
  232. * Returns {@code true} if the provided {@code resolution} represents a day.
  233. *
  234. * @param resolution
  235. * the given resolution
  236. * @return {@code true} if the {@code resolution} represents a day
  237. */
  238. protected abstract boolean isDay(R resolution);
  239. /**
  240. * Returns {@code true} if the provided {@code resolution} represents a
  241. * month.
  242. *
  243. * @param resolution
  244. * the given resolution
  245. * @return {@code true} if the {@code resolution} represents a month
  246. */
  247. protected abstract boolean isMonth(R resolution);
  248. /**
  249. * Returns {@code true} if the provided {@code resolution} represents an
  250. * year.
  251. *
  252. * @param resolution
  253. * the given resolution
  254. * @return {@code true} if the {@code resolution} represents a year
  255. */
  256. protected boolean isYear(R resolution) {
  257. return parent.isYear(resolution);
  258. }
  259. /**
  260. * Returns {@code true} if the {@code resolution} representation is strictly
  261. * below month (day, hour, etc..).
  262. *
  263. * @param resolution
  264. * the given resolution
  265. * @return whether the {@code resolution} is below the month resolution
  266. */
  267. protected abstract boolean isBelowMonth(R resolution);
  268. /**
  269. * Returns all available resolutions for the widget.
  270. *
  271. * @return all available resolutions
  272. */
  273. protected Stream<R> getResolutions() {
  274. return parent.getResolutions();
  275. }
  276. /**
  277. * Finds the resolution by the {@code filter}.
  278. *
  279. * @param filter
  280. * predicate to filter resolutions
  281. * @return the resolution accepted by the {@code filter}
  282. */
  283. protected R getResolution(Predicate<R> filter) {
  284. List<R> resolutions = getResolutions().filter(filter)
  285. .collect(Collectors.toList());
  286. assert resolutions
  287. .size() == 1 : "The result of filtering by the predicate "
  288. + "contains unexpected number of resolution items :"
  289. + resolutions.size();
  290. return resolutions.get(0);
  291. }
  292. /**
  293. * Sets the selection highlight to a given day in the current view
  294. *
  295. * @param date
  296. * A Date representing the day of month to be selected. Must be
  297. * one of the days currently visible.
  298. *
  299. */
  300. private void selectDate(Date date) {
  301. if (selectedDay != null) {
  302. selectedDay.removeStyleDependentName(CN_SELECTED);
  303. Roles.getGridcellRole()
  304. .removeAriaSelectedState(selectedDay.getElement());
  305. }
  306. int rowCount = days.getRowCount();
  307. for (int i = 0; i < rowCount; i++) {
  308. int cellCount = days.getCellCount(i);
  309. for (int j = 0; j < cellCount; j++) {
  310. Widget widget = days.getWidget(i, j);
  311. if (widget != null
  312. && widget instanceof VAbstractCalendarPanel.Day) {
  313. Day curday = (Day) widget;
  314. if (curday.getDate().equals(date)) {
  315. curday.addStyleDependentName(CN_SELECTED);
  316. selectedDay = curday;
  317. Roles.getGridcellRole().setAriaSelectedState(
  318. selectedDay.getElement(), SelectedValue.TRUE);
  319. return;
  320. }
  321. }
  322. }
  323. }
  324. }
  325. /**
  326. * Updates year, month, day from focusedDate to value
  327. */
  328. private void selectFocused() {
  329. if (focusedDate != null
  330. && isDateInsideRange(focusedDate, getResolution())) {
  331. if (value == null) {
  332. // No previously selected value (set to null on server side).
  333. // Create a new date using current date and time
  334. value = new Date();
  335. }
  336. /*
  337. * #5594 set Date (day) to 1 in order to prevent any kind of
  338. * wrapping of months when later setting the month. (e.g. 31 ->
  339. * month with 30 days -> wraps to the 1st of the following month,
  340. * e.g. 31st of May -> 31st of April = 1st of May)
  341. */
  342. value.setDate(1);
  343. if (value.getYear() != focusedDate.getYear()) {
  344. value.setYear(focusedDate.getYear());
  345. }
  346. if (value.getMonth() != focusedDate.getMonth()) {
  347. value.setMonth(focusedDate.getMonth());
  348. }
  349. if (value.getDate() != focusedDate.getDate()) {
  350. }
  351. // We always need to set the date, even if it hasn't changed, since
  352. // it was forced to 1 above.
  353. value.setDate(focusedDate.getDate());
  354. selectDate(focusedDate);
  355. } else {
  356. VConsole.log("Trying to select a the focused date which is NULL!");
  357. }
  358. }
  359. protected boolean onValueChange() {
  360. return false;
  361. }
  362. public R getResolution() {
  363. return resolution;
  364. }
  365. public void setResolution(R resolution) {
  366. this.resolution = resolution;
  367. }
  368. /**
  369. * Checks whether the widget is not editable (read-only).
  370. *
  371. * @return {@code true} if the widget is read-only
  372. */
  373. protected boolean isReadonly() {
  374. return parent.isReadonly();
  375. }
  376. /**
  377. * Checks whether the widget is enabled.
  378. *
  379. * @return {@code true} is the widget is enabled
  380. */
  381. protected boolean isEnabled() {
  382. return parent.isEnabled();
  383. }
  384. @Override
  385. public void setStyleName(String style) {
  386. super.setStyleName(style);
  387. if (initialRenderDone) {
  388. // Dynamic updates to the stylename needs to render the calendar to
  389. // update the inner element stylenames
  390. renderCalendar();
  391. }
  392. }
  393. @Override
  394. public void setStylePrimaryName(String style) {
  395. super.setStylePrimaryName(style);
  396. if (initialRenderDone) {
  397. // Dynamic updates to the stylename needs to render the calendar to
  398. // update the inner element stylenames
  399. renderCalendar();
  400. }
  401. }
  402. private void clearCalendarBody(boolean remove) {
  403. if (!remove) {
  404. // Leave the cells in place but clear their contents
  405. // This has the side effect of ensuring that the calendar always
  406. // contain 7 rows.
  407. for (int row = 1; row < 7; row++) {
  408. for (int col = 0; col < 8; col++) {
  409. days.setHTML(row, col, "&nbsp;");
  410. }
  411. }
  412. } else if (getRowCount() > 1) {
  413. removeRow(1);
  414. days.clear();
  415. }
  416. }
  417. /**
  418. * Builds the top buttons and current month and year header.
  419. *
  420. * @param needsMonth
  421. * Should the month buttons be visible?
  422. */
  423. private void buildCalendarHeader(boolean needsMonth) {
  424. getRowFormatter().addStyleName(0,
  425. parent.getStylePrimaryName() + "-calendarpanel-header");
  426. if (prevMonth == null && needsMonth) {
  427. prevMonth = new VEventButton();
  428. prevMonth.setHTML("&lsaquo;");
  429. prevMonth.setStyleName("v-button-prevmonth");
  430. prevMonth.setTabIndex(-1);
  431. nextMonth = new VEventButton();
  432. nextMonth.setHTML("&rsaquo;");
  433. nextMonth.setStyleName("v-button-nextmonth");
  434. nextMonth.setTabIndex(-1);
  435. setWidget(0, 3, nextMonth);
  436. setWidget(0, 1, prevMonth);
  437. } else if (prevMonth != null && !needsMonth) {
  438. // Remove month traverse buttons
  439. remove(prevMonth);
  440. remove(nextMonth);
  441. prevMonth = null;
  442. nextMonth = null;
  443. }
  444. if (prevYear == null) {
  445. prevYear = new VEventButton();
  446. prevYear.setHTML("&laquo;");
  447. prevYear.setStyleName("v-button-prevyear");
  448. prevYear.setTabIndex(-1);
  449. nextYear = new VEventButton();
  450. nextYear.setHTML("&raquo;");
  451. nextYear.setStyleName("v-button-nextyear");
  452. nextYear.setTabIndex(-1);
  453. setWidget(0, 0, prevYear);
  454. setWidget(0, 4, nextYear);
  455. }
  456. updateControlButtonRangeStyles(needsMonth);
  457. final String monthName = needsMonth
  458. ? getDateTimeService().getMonth(displayedMonth.getMonth()) : "";
  459. final int year = displayedMonth.getYear() + 1900;
  460. getFlexCellFormatter().setStyleName(0, 2,
  461. parent.getStylePrimaryName() + "-calendarpanel-month");
  462. getFlexCellFormatter().setStyleName(0, 0,
  463. parent.getStylePrimaryName() + "-calendarpanel-prevyear");
  464. getFlexCellFormatter().setStyleName(0, 4,
  465. parent.getStylePrimaryName() + "-calendarpanel-nextyear");
  466. getFlexCellFormatter().setStyleName(0, 3,
  467. parent.getStylePrimaryName() + "-calendarpanel-nextmonth");
  468. getFlexCellFormatter().setStyleName(0, 1,
  469. parent.getStylePrimaryName() + "-calendarpanel-prevmonth");
  470. setHTML(0, 2,
  471. "<span class=\"" + parent.getStylePrimaryName()
  472. + "-calendarpanel-month\">" + monthName + " " + year
  473. + "</span>");
  474. }
  475. private void updateControlButtonRangeStyles(boolean needsMonth) {
  476. if (focusedDate == null) {
  477. return;
  478. }
  479. if (needsMonth) {
  480. Date prevMonthDate = (Date) focusedDate.clone();
  481. removeOneMonth(prevMonthDate);
  482. R month = getResolution(VAbstractCalendarPanel.this::isMonth);
  483. if (!isDateInsideRange(prevMonthDate, month)) {
  484. prevMonth.addStyleName(CN_OUTSIDE_RANGE);
  485. } else {
  486. prevMonth.removeStyleName(CN_OUTSIDE_RANGE);
  487. }
  488. Date nextMonthDate = (Date) focusedDate.clone();
  489. addOneMonth(nextMonthDate);
  490. if (!isDateInsideRange(nextMonthDate, month)) {
  491. nextMonth.addStyleName(CN_OUTSIDE_RANGE);
  492. } else {
  493. nextMonth.removeStyleName(CN_OUTSIDE_RANGE);
  494. }
  495. }
  496. Date prevYearDate = (Date) focusedDate.clone();
  497. prevYearDate.setYear(prevYearDate.getYear() - 1);
  498. R year = getResolution(VAbstractCalendarPanel.this::isYear);
  499. if (!isDateInsideRange(prevYearDate, year)) {
  500. prevYear.addStyleName(CN_OUTSIDE_RANGE);
  501. } else {
  502. prevYear.removeStyleName(CN_OUTSIDE_RANGE);
  503. }
  504. Date nextYearDate = (Date) focusedDate.clone();
  505. nextYearDate.setYear(nextYearDate.getYear() + 1);
  506. if (!isDateInsideRange(nextYearDate, year)) {
  507. nextYear.addStyleName(CN_OUTSIDE_RANGE);
  508. } else {
  509. nextYear.removeStyleName(CN_OUTSIDE_RANGE);
  510. }
  511. }
  512. /**
  513. * Returns date time service for the widget.
  514. *
  515. * @see #setDateTimeService(DateTimeService)
  516. *
  517. * @return date time service
  518. */
  519. protected DateTimeService getDateTimeService() {
  520. return dateTimeService;
  521. }
  522. /**
  523. * Returns the date field which this panel is attached to.
  524. *
  525. * @return the "parent" date field
  526. */
  527. protected VDateField<R> getDateField() {
  528. return parent;
  529. }
  530. public void setDateTimeService(DateTimeService dateTimeService) {
  531. this.dateTimeService = dateTimeService;
  532. }
  533. /**
  534. * Returns whether ISO 8601 week numbers should be shown in the value
  535. * selector or not. ISO 8601 defines that a week always starts with a Monday
  536. * so the week numbers are only shown if this is the case.
  537. *
  538. * @return true if week number should be shown, false otherwise
  539. */
  540. public boolean isShowISOWeekNumbers() {
  541. return showISOWeekNumbers;
  542. }
  543. public void setShowISOWeekNumbers(boolean showISOWeekNumbers) {
  544. this.showISOWeekNumbers = showISOWeekNumbers;
  545. if (initialRenderDone && isBelowMonth(resolution)) {
  546. clearCalendarBody(false);
  547. buildCalendarBody();
  548. }
  549. }
  550. /**
  551. * Checks inclusively whether a date is inside a range of dates or not.
  552. *
  553. * @param date
  554. * @return
  555. */
  556. private boolean isDateInsideRange(Date date, R minResolution) {
  557. assert (date != null);
  558. return isAcceptedByRangeEnd(date, minResolution)
  559. && isAcceptedByRangeStart(date, minResolution);
  560. }
  561. /**
  562. * Accepts dates greater than or equal to rangeStart, depending on the
  563. * resolution. If the resolution is set to DAY, the range will compare on a
  564. * day-basis. If the resolution is set to YEAR, only years are compared. So
  565. * even if the range is set to one millisecond in next year, also next year
  566. * will be included.
  567. *
  568. * @param date
  569. * @param minResolution
  570. * @return
  571. */
  572. private boolean isAcceptedByRangeStart(Date date, R minResolution) {
  573. assert (date != null);
  574. // rangeStart == null means that we accept all values below rangeEnd
  575. if (rangeStart == null) {
  576. return true;
  577. }
  578. Date valueDuplicate = (Date) date.clone();
  579. Date rangeStartDuplicate = (Date) rangeStart.clone();
  580. if (isYear(minResolution)) {
  581. return valueDuplicate.getYear() >= rangeStartDuplicate.getYear();
  582. }
  583. if (isMonth(minResolution)) {
  584. valueDuplicate = clearDateBelowMonth(valueDuplicate);
  585. rangeStartDuplicate = clearDateBelowMonth(rangeStartDuplicate);
  586. } else {
  587. valueDuplicate = clearDateBelowDay(valueDuplicate);
  588. rangeStartDuplicate = clearDateBelowDay(rangeStartDuplicate);
  589. }
  590. return !rangeStartDuplicate.after(valueDuplicate);
  591. }
  592. /**
  593. * Accepts dates earlier than or equal to rangeStart, depending on the
  594. * resolution. If the resolution is set to DAY, the range will compare on a
  595. * day-basis. If the resolution is set to YEAR, only years are compared. So
  596. * even if the range is set to one millisecond in next year, also next year
  597. * will be included.
  598. *
  599. * @param date
  600. * @param minResolution
  601. * @return
  602. */
  603. private boolean isAcceptedByRangeEnd(Date date, R minResolution) {
  604. assert (date != null);
  605. // rangeEnd == null means that we accept all values above rangeStart
  606. if (rangeEnd == null) {
  607. return true;
  608. }
  609. Date valueDuplicate = (Date) date.clone();
  610. Date rangeEndDuplicate = (Date) rangeEnd.clone();
  611. if (isYear(minResolution)) {
  612. return valueDuplicate.getYear() <= rangeEndDuplicate.getYear();
  613. }
  614. if (isMonth(minResolution)) {
  615. valueDuplicate = clearDateBelowMonth(valueDuplicate);
  616. rangeEndDuplicate = clearDateBelowMonth(rangeEndDuplicate);
  617. } else {
  618. valueDuplicate = clearDateBelowDay(valueDuplicate);
  619. rangeEndDuplicate = clearDateBelowDay(rangeEndDuplicate);
  620. }
  621. return !rangeEndDuplicate.before(valueDuplicate);
  622. }
  623. private static Date clearDateBelowMonth(Date date) {
  624. date.setDate(1);
  625. return clearDateBelowDay(date);
  626. }
  627. private static Date clearDateBelowDay(Date date) {
  628. date.setHours(0);
  629. date.setMinutes(0);
  630. date.setSeconds(0);
  631. // Clearing milliseconds
  632. long time = date.getTime() / 1000;
  633. date = new Date(time * 1000);
  634. return date;
  635. }
  636. /**
  637. * Builds the day and time selectors of the calendar.
  638. */
  639. private void buildCalendarBody() {
  640. final int weekColumn = 0;
  641. final int firstWeekdayColumn = 1;
  642. final int headerRow = 0;
  643. setWidget(1, 0, days);
  644. setCellPadding(0);
  645. setCellSpacing(0);
  646. getFlexCellFormatter().setColSpan(1, 0, 5);
  647. getFlexCellFormatter().setStyleName(1, 0,
  648. getDateField().getStylePrimaryName() + "-calendarpanel-body");
  649. days.getFlexCellFormatter().setStyleName(headerRow, weekColumn,
  650. "v-week");
  651. days.setHTML(headerRow, weekColumn, "<strong></strong>");
  652. // Hide the week column if week numbers are not to be displayed.
  653. days.getFlexCellFormatter().setVisible(headerRow, weekColumn,
  654. isShowISOWeekNumbers());
  655. days.getRowFormatter().setStyleName(headerRow,
  656. getDateField().getStylePrimaryName()
  657. + "-calendarpanel-weekdays");
  658. if (isShowISOWeekNumbers()) {
  659. days.getFlexCellFormatter().setStyleName(headerRow, weekColumn,
  660. "v-first");
  661. days.getFlexCellFormatter().setStyleName(headerRow,
  662. firstWeekdayColumn, "");
  663. days.getRowFormatter().addStyleName(headerRow,
  664. getDateField().getStylePrimaryName()
  665. + "-calendarpanel-weeknumbers");
  666. } else {
  667. days.getFlexCellFormatter().setStyleName(headerRow, weekColumn, "");
  668. days.getFlexCellFormatter().setStyleName(headerRow,
  669. firstWeekdayColumn, "v-first");
  670. }
  671. days.getFlexCellFormatter().setStyleName(headerRow,
  672. firstWeekdayColumn + 6, "v-last");
  673. // Print weekday names
  674. final int firstDay = getDateTimeService().getFirstDayOfWeek();
  675. for (int i = 0; i < 7; i++) {
  676. int day = i + firstDay;
  677. if (day > 6) {
  678. day = 0;
  679. }
  680. if (isBelowMonth(getResolution())) {
  681. days.setHTML(headerRow, firstWeekdayColumn + i, "<strong>"
  682. + getDateTimeService().getShortDay(day) + "</strong>");
  683. } else {
  684. days.setHTML(headerRow, firstWeekdayColumn + i, "");
  685. }
  686. Roles.getColumnheaderRole().set(days.getCellFormatter()
  687. .getElement(headerRow, firstWeekdayColumn + i));
  688. }
  689. // Zero out hours, minutes, seconds, and milliseconds to compare dates
  690. // without time part
  691. final Date tmp = new Date();
  692. final Date today = new Date(tmp.getYear(), tmp.getMonth(),
  693. tmp.getDate());
  694. final Date selectedDate = value == null ? null
  695. : new Date(value.getYear(), value.getMonth(), value.getDate());
  696. final int startWeekDay = getDateTimeService()
  697. .getStartWeekDay(displayedMonth);
  698. final Date curr = (Date) displayedMonth.clone();
  699. // Start from the first day of the week that at least partially belongs
  700. // to the current month
  701. curr.setDate(1 - startWeekDay);
  702. // No month has more than 6 weeks so 6 is a safe maximum for rows.
  703. for (int weekOfMonth = 1; weekOfMonth < 7; weekOfMonth++) {
  704. for (int dayOfWeek = 0; dayOfWeek < 7; dayOfWeek++) {
  705. // Actually write the day of month
  706. Date dayDate = (Date) curr.clone();
  707. Day day = new Day(dayDate);
  708. day.setStyleName(getDateField().getStylePrimaryName()
  709. + "-calendarpanel-day");
  710. if (!isDateInsideRange(dayDate, getResolution(this::isDay))) {
  711. day.addStyleDependentName(CN_OUTSIDE_RANGE);
  712. }
  713. if (curr.equals(selectedDate)) {
  714. day.addStyleDependentName(CN_SELECTED);
  715. Roles.getGridcellRole().setAriaSelectedState(
  716. day.getElement(), SelectedValue.TRUE);
  717. selectedDay = day;
  718. }
  719. if (curr.equals(today)) {
  720. day.addStyleDependentName(CN_TODAY);
  721. }
  722. if (curr.equals(focusedDate)) {
  723. focusedDay = day;
  724. if (hasFocus) {
  725. day.addStyleDependentName(CN_FOCUSED);
  726. }
  727. }
  728. if (curr.getMonth() != displayedMonth.getMonth()) {
  729. day.addStyleDependentName(CN_OFFMONTH);
  730. }
  731. days.setWidget(weekOfMonth, firstWeekdayColumn + dayOfWeek,
  732. day);
  733. Roles.getGridcellRole().set(days.getCellFormatter().getElement(
  734. weekOfMonth, firstWeekdayColumn + dayOfWeek));
  735. // ISO week numbers if requested
  736. days.getCellFormatter().setVisible(weekOfMonth, weekColumn,
  737. isShowISOWeekNumbers());
  738. if (isShowISOWeekNumbers()) {
  739. final String baseCssClass = getDateField()
  740. .getStylePrimaryName()
  741. + "-calendarpanel-weeknumber";
  742. String weekCssClass = baseCssClass;
  743. int weekNumber = DateTimeService.getISOWeekNumber(curr);
  744. days.setHTML(weekOfMonth, 0, "<span class=\"" + weekCssClass
  745. + "\"" + ">" + weekNumber + "</span>");
  746. }
  747. curr.setDate(curr.getDate() + 1);
  748. }
  749. }
  750. }
  751. /**
  752. * Updates the calendar and text field with the selected dates.
  753. */
  754. public void renderCalendar() {
  755. renderCalendar(true);
  756. }
  757. /**
  758. * For internal use only. May be removed or replaced in the future.
  759. *
  760. * Updates the calendar and text field with the selected dates.
  761. *
  762. * @param updateDate
  763. * The value false prevents setting the selected date of the
  764. * calendar based on focusedDate. That can be used when only the
  765. * resolution of the calendar is changed and no date has been
  766. * selected.
  767. */
  768. public void renderCalendar(boolean updateDate) {
  769. doRenderCalendar(updateDate);
  770. initialRenderDone = true;
  771. }
  772. /**
  773. * Performs the rendering required by the {@link #renderCalendar(boolean)}.
  774. * Subclasses may override this method to provide a custom implementation
  775. * avoiding {@link #renderCalendar(boolean)} override. The latter method
  776. * contains a common logic which should not be overriden.
  777. *
  778. * @param updateDate
  779. * The value false prevents setting the selected date of the
  780. * calendar based on focusedDate. That can be used when only the
  781. * resolution of the calendar is changed and no date has been
  782. * selected.
  783. */
  784. protected void doRenderCalendar(boolean updateDate) {
  785. super.setStylePrimaryName(
  786. getDateField().getStylePrimaryName() + "-calendarpanel");
  787. if (focusedDate == null) {
  788. Date now = new Date();
  789. // focusedDate must have zero hours, mins, secs, millisecs
  790. focusedDate = new FocusedDate(now.getYear(), now.getMonth(),
  791. now.getDate());
  792. displayedMonth = new FocusedDate(now.getYear(), now.getMonth(), 1);
  793. }
  794. if (updateDate && !isDay(getResolution())
  795. && focusChangeListener != null) {
  796. focusChangeListener.focusChanged(new Date(focusedDate.getTime()));
  797. }
  798. final boolean needsMonth = !isYear(getResolution());
  799. boolean needsBody = isBelowMonth(resolution);
  800. buildCalendarHeader(needsMonth);
  801. clearCalendarBody(!needsBody);
  802. if (needsBody) {
  803. buildCalendarBody();
  804. }
  805. }
  806. /**
  807. * Moves the focus forward the given number of days.
  808. */
  809. private void focusNextDay(int days) {
  810. if (focusedDate == null) {
  811. return;
  812. }
  813. Date focusCopy = ((Date) focusedDate.clone());
  814. focusCopy.setDate(focusedDate.getDate() + days);
  815. if (!isDateInsideRange(focusCopy, getResolution())) {
  816. // If not inside allowed range, then do not move anything
  817. return;
  818. }
  819. int oldMonth = focusedDate.getMonth();
  820. int oldYear = focusedDate.getYear();
  821. focusedDate.setDate(focusedDate.getDate() + days);
  822. if (focusedDate.getMonth() == oldMonth
  823. && focusedDate.getYear() == oldYear) {
  824. // Month did not change, only move the selection
  825. focusDay(focusedDate);
  826. } else {
  827. // If the month changed we need to re-render the calendar
  828. displayedMonth.setMonth(focusedDate.getMonth());
  829. displayedMonth.setYear(focusedDate.getYear());
  830. renderCalendar();
  831. }
  832. }
  833. /**
  834. * Moves the focus backward the given number of days.
  835. */
  836. private void focusPreviousDay(int days) {
  837. focusNextDay(-days);
  838. }
  839. /**
  840. * Selects the next month
  841. */
  842. private void focusNextMonth() {
  843. if (focusedDate == null) {
  844. return;
  845. }
  846. // Trying to request next month
  847. Date requestedNextMonthDate = (Date) focusedDate.clone();
  848. addOneMonth(requestedNextMonthDate);
  849. if (!isDateInsideRange(requestedNextMonthDate,
  850. getResolution(this::isMonth))) {
  851. return;
  852. }
  853. // Now also checking whether the day is inside the range or not. If not
  854. // inside,
  855. // correct it
  856. if (!isDateInsideRange(requestedNextMonthDate,
  857. getResolution(this::isDay))) {
  858. requestedNextMonthDate = adjustDateToFitInsideRange(
  859. requestedNextMonthDate);
  860. }
  861. focusedDate.setTime(requestedNextMonthDate.getTime());
  862. displayedMonth.setMonth(displayedMonth.getMonth() + 1);
  863. renderCalendar();
  864. }
  865. private static void addOneMonth(Date date) {
  866. int currentMonth = date.getMonth();
  867. int requestedMonth = (currentMonth + 1) % 12;
  868. date.setMonth(date.getMonth() + 1);
  869. /*
  870. * If the selected value was e.g. 31.3 the new value would be 31.4 but
  871. * this value is invalid so the new value will be 1.5. This is taken
  872. * care of by decreasing the value until we have the correct month.
  873. */
  874. while (date.getMonth() != requestedMonth) {
  875. date.setDate(date.getDate() - 1);
  876. }
  877. }
  878. private static void removeOneMonth(Date date) {
  879. int currentMonth = date.getMonth();
  880. date.setMonth(date.getMonth() - 1);
  881. /*
  882. * If the selected value was e.g. 31.12 the new value would be 31.11 but
  883. * this value is invalid so the new value will be 1.12. This is taken
  884. * care of by decreasing the value until we have the correct month.
  885. */
  886. while (date.getMonth() == currentMonth) {
  887. date.setDate(date.getDate() - 1);
  888. }
  889. }
  890. /**
  891. * Selects the previous month
  892. */
  893. private void focusPreviousMonth() {
  894. if (focusedDate == null) {
  895. return;
  896. }
  897. Date requestedPreviousMonthDate = (Date) focusedDate.clone();
  898. removeOneMonth(requestedPreviousMonthDate);
  899. if (!isDateInsideRange(requestedPreviousMonthDate,
  900. getResolution(this::isMonth))) {
  901. return;
  902. }
  903. if (!isDateInsideRange(requestedPreviousMonthDate,
  904. getResolution(this::isDay))) {
  905. requestedPreviousMonthDate = adjustDateToFitInsideRange(
  906. requestedPreviousMonthDate);
  907. }
  908. focusedDate.setTime(requestedPreviousMonthDate.getTime());
  909. displayedMonth.setMonth(displayedMonth.getMonth() - 1);
  910. renderCalendar();
  911. }
  912. /**
  913. * Selects the previous year
  914. */
  915. private void focusPreviousYear(int years) {
  916. if (focusedDate == null) {
  917. return;
  918. }
  919. Date previousYearDate = (Date) focusedDate.clone();
  920. previousYearDate.setYear(previousYearDate.getYear() - years);
  921. // Do not focus if not inside range
  922. if (!isDateInsideRange(previousYearDate, getResolution(this::isYear))) {
  923. return;
  924. }
  925. // If we remove one year, but have to roll back a bit, fit it
  926. // into the calendar. Also the months have to be changed
  927. if (!isDateInsideRange(previousYearDate, getResolution(this::isDay))) {
  928. previousYearDate = adjustDateToFitInsideRange(previousYearDate);
  929. focusedDate.setYear(previousYearDate.getYear());
  930. focusedDate.setMonth(previousYearDate.getMonth());
  931. focusedDate.setDate(previousYearDate.getDate());
  932. displayedMonth.setYear(previousYearDate.getYear());
  933. displayedMonth.setMonth(previousYearDate.getMonth());
  934. } else {
  935. int currentMonth = focusedDate.getMonth();
  936. focusedDate.setYear(focusedDate.getYear() - years);
  937. displayedMonth.setYear(displayedMonth.getYear() - years);
  938. /*
  939. * If the focused date was a leap day (Feb 29), the new date becomes
  940. * Mar 1 if the new year is not also a leap year. Set it to Feb 28
  941. * instead.
  942. */
  943. if (focusedDate.getMonth() != currentMonth) {
  944. focusedDate.setDate(0);
  945. }
  946. }
  947. renderCalendar();
  948. }
  949. /**
  950. * Selects the next year
  951. */
  952. private void focusNextYear(int years) {
  953. if (focusedDate == null) {
  954. return;
  955. }
  956. Date nextYearDate = (Date) focusedDate.clone();
  957. nextYearDate.setYear(nextYearDate.getYear() + years);
  958. // Do not focus if not inside range
  959. if (!isDateInsideRange(nextYearDate, getResolution(this::isYear))) {
  960. return;
  961. }
  962. // If we add one year, but have to roll back a bit, fit it
  963. // into the calendar. Also the months have to be changed
  964. if (!isDateInsideRange(nextYearDate, getResolution(this::isDay))) {
  965. nextYearDate = adjustDateToFitInsideRange(nextYearDate);
  966. focusedDate.setYear(nextYearDate.getYear());
  967. focusedDate.setMonth(nextYearDate.getMonth());
  968. focusedDate.setDate(nextYearDate.getDate());
  969. displayedMonth.setYear(nextYearDate.getYear());
  970. displayedMonth.setMonth(nextYearDate.getMonth());
  971. } else {
  972. int currentMonth = focusedDate.getMonth();
  973. focusedDate.setYear(focusedDate.getYear() + years);
  974. displayedMonth.setYear(displayedMonth.getYear() + years);
  975. /*
  976. * If the focused date was a leap day (Feb 29), the new date becomes
  977. * Mar 1 if the new year is not also a leap year. Set it to Feb 28
  978. * instead.
  979. */
  980. if (focusedDate.getMonth() != currentMonth) {
  981. focusedDate.setDate(0);
  982. }
  983. }
  984. renderCalendar();
  985. }
  986. /**
  987. * Handles a user click on the component
  988. *
  989. * @param sender
  990. * The component that was clicked
  991. * @param updateVariable
  992. * Should the value field be updated
  993. *
  994. */
  995. private void processClickEvent(Widget sender) {
  996. if (!isEnabled() || isReadonly()) {
  997. return;
  998. }
  999. if (sender == prevYear) {
  1000. focusPreviousYear(1);
  1001. } else if (sender == nextYear) {
  1002. focusNextYear(1);
  1003. } else if (sender == prevMonth) {
  1004. focusPreviousMonth();
  1005. } else if (sender == nextMonth) {
  1006. focusNextMonth();
  1007. }
  1008. }
  1009. /*
  1010. * (non-Javadoc)
  1011. *
  1012. * @see
  1013. * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt
  1014. * .event.dom.client.KeyDownEvent)
  1015. */
  1016. @Override
  1017. public void onKeyDown(KeyDownEvent event) {
  1018. handleKeyPress(event);
  1019. }
  1020. /*
  1021. * (non-Javadoc)
  1022. *
  1023. * @see
  1024. * com.google.gwt.event.dom.client.KeyPressHandler#onKeyPress(com.google
  1025. * .gwt.event.dom.client.KeyPressEvent)
  1026. */
  1027. @Override
  1028. public void onKeyPress(KeyPressEvent event) {
  1029. handleKeyPress(event);
  1030. }
  1031. /**
  1032. * Handles the keypress from both the onKeyPress event and the onKeyDown
  1033. * event
  1034. *
  1035. * @param event
  1036. * The keydown/keypress event
  1037. */
  1038. private void handleKeyPress(DomEvent<?> event) {
  1039. // Check tabs
  1040. int keycode = event.getNativeEvent().getKeyCode();
  1041. if (keycode == KeyCodes.KEY_TAB
  1042. && event.getNativeEvent().getShiftKey()) {
  1043. if (onTabOut(event)) {
  1044. return;
  1045. }
  1046. }
  1047. // Handle the navigation
  1048. if (handleNavigation(keycode,
  1049. event.getNativeEvent().getCtrlKey()
  1050. || event.getNativeEvent().getMetaKey(),
  1051. event.getNativeEvent().getShiftKey())) {
  1052. event.preventDefault();
  1053. }
  1054. }
  1055. /**
  1056. * Notifies submit-listeners of a submit event
  1057. */
  1058. private void onSubmit() {
  1059. if (getSubmitListener() != null) {
  1060. getSubmitListener().onSubmit();
  1061. }
  1062. }
  1063. /**
  1064. * Notifies submit-listeners of a cancel event
  1065. */
  1066. private void onCancel() {
  1067. if (getSubmitListener() != null) {
  1068. getSubmitListener().onCancel();
  1069. }
  1070. }
  1071. /**
  1072. * Handles the keyboard navigation when the resolution is set to years.
  1073. *
  1074. * @param keycode
  1075. * The keycode to process
  1076. * @param ctrl
  1077. * Is ctrl pressed?
  1078. * @param shift
  1079. * is shift pressed
  1080. * @return Returns true if the keycode was processed, else false
  1081. */
  1082. protected boolean handleNavigationYearMode(int keycode, boolean ctrl,
  1083. boolean shift) {
  1084. // Ctrl and Shift selection not supported
  1085. if (ctrl || shift) {
  1086. return false;
  1087. }
  1088. else if (keycode == getPreviousKey()) {
  1089. focusNextYear(10); // Add 10 years
  1090. return true;
  1091. }
  1092. else if (keycode == getForwardKey()) {
  1093. focusNextYear(1); // Add 1 year
  1094. return true;
  1095. }
  1096. else if (keycode == getNextKey()) {
  1097. focusPreviousYear(10); // Subtract 10 years
  1098. return true;
  1099. }
  1100. else if (keycode == getBackwardKey()) {
  1101. focusPreviousYear(1); // Subtract 1 year
  1102. return true;
  1103. } else if (keycode == getSelectKey()) {
  1104. value = (Date) focusedDate.clone();
  1105. onSubmit();
  1106. return true;
  1107. } else if (keycode == getResetKey()) {
  1108. // Restore showing value the selected value
  1109. focusedDate.setTime(value.getTime());
  1110. renderCalendar();
  1111. return true;
  1112. } else if (keycode == getCloseKey()) {
  1113. // TODO fire listener, on users responsibility??
  1114. onCancel();
  1115. return true;
  1116. }
  1117. return false;
  1118. }
  1119. /**
  1120. * Handle the keyboard navigation when the resolution is set to MONTH
  1121. *
  1122. * @param keycode
  1123. * The keycode to handle
  1124. * @param ctrl
  1125. * Was the ctrl key pressed?
  1126. * @param shift
  1127. * Was the shift key pressed?
  1128. * @return
  1129. */
  1130. protected boolean handleNavigationMonthMode(int keycode, boolean ctrl,
  1131. boolean shift) {
  1132. // Ctrl selection not supported
  1133. if (ctrl) {
  1134. return false;
  1135. } else if (keycode == getPreviousKey()) {
  1136. focusNextYear(1); // Add 1 year
  1137. return true;
  1138. } else if (keycode == getForwardKey()) {
  1139. focusNextMonth(); // Add 1 month
  1140. return true;
  1141. } else if (keycode == getNextKey()) {
  1142. focusPreviousYear(1); // Subtract 1 year
  1143. return true;
  1144. } else if (keycode == getBackwardKey()) {
  1145. focusPreviousMonth(); // Subtract 1 month
  1146. return true;
  1147. } else if (keycode == getSelectKey()) {
  1148. value = (Date) focusedDate.clone();
  1149. onSubmit();
  1150. return true;
  1151. } else if (keycode == getResetKey()) {
  1152. // Restore showing value the selected value
  1153. focusedDate.setTime(value.getTime());
  1154. renderCalendar();
  1155. return true;
  1156. } else if (keycode == getCloseKey() || keycode == KeyCodes.KEY_TAB) {
  1157. onCancel();
  1158. // TODO fire close event
  1159. return true;
  1160. }
  1161. return false;
  1162. }
  1163. /**
  1164. * Handle keyboard navigation what the resolution is set to DAY
  1165. *
  1166. * @param keycode
  1167. * The keycode to handle
  1168. * @param ctrl
  1169. * Was the ctrl key pressed?
  1170. * @param shift
  1171. * Was the shift key pressed?
  1172. * @return Return true if the key press was handled by the method, else
  1173. * return false.
  1174. */
  1175. protected boolean handleNavigationDayMode(int keycode, boolean ctrl,
  1176. boolean shift) {
  1177. // Ctrl key is not in use
  1178. if (ctrl) {
  1179. return false;
  1180. }
  1181. /*
  1182. * Jumps to the next day.
  1183. */
  1184. if (keycode == getForwardKey() && !shift) {
  1185. focusNextDay(1);
  1186. return true;
  1187. /*
  1188. * Jumps to the previous day
  1189. */
  1190. } else if (keycode == getBackwardKey() && !shift) {
  1191. focusPreviousDay(1);
  1192. return true;
  1193. /*
  1194. * Jumps one week forward in the calendar
  1195. */
  1196. } else if (keycode == getNextKey() && !shift) {
  1197. focusNextDay(7);
  1198. return true;
  1199. /*
  1200. * Jumps one week back in the calendar
  1201. */
  1202. } else if (keycode == getPreviousKey() && !shift) {
  1203. focusPreviousDay(7);
  1204. return true;
  1205. /*
  1206. * Selects the value that is chosen
  1207. */
  1208. } else if (keycode == getSelectKey() && !shift) {
  1209. selectFocused();
  1210. onSubmit(); // submit
  1211. return true;
  1212. } else if (keycode == getCloseKey()) {
  1213. onCancel();
  1214. // TODO close event
  1215. return true;
  1216. /*
  1217. * Jumps to the next month
  1218. */
  1219. } else if (shift && keycode == getForwardKey()) {
  1220. focusNextMonth();
  1221. return true;
  1222. /*
  1223. * Jumps to the previous month
  1224. */
  1225. } else if (shift && keycode == getBackwardKey()) {
  1226. focusPreviousMonth();
  1227. return true;
  1228. /*
  1229. * Jumps to the next year
  1230. */
  1231. } else if (shift && keycode == getPreviousKey()) {
  1232. focusNextYear(1);
  1233. return true;
  1234. /*
  1235. * Jumps to the previous year
  1236. */
  1237. } else if (shift && keycode == getNextKey()) {
  1238. focusPreviousYear(1);
  1239. return true;
  1240. /*
  1241. * Resets the selection
  1242. */
  1243. } else if (keycode == getResetKey() && !shift) {
  1244. // Restore showing value the selected value
  1245. focusedDate = new FocusedDate(value.getYear(), value.getMonth(),
  1246. value.getDate());
  1247. displayedMonth = new FocusedDate(value.getYear(), value.getMonth(),
  1248. 1);
  1249. renderCalendar();
  1250. return true;
  1251. }
  1252. return false;
  1253. }
  1254. /**
  1255. * Handles the keyboard navigation
  1256. *
  1257. * @param keycode
  1258. * The key code that was pressed
  1259. * @param ctrl
  1260. * Was the ctrl key pressed
  1261. * @param shift
  1262. * Was the shift key pressed
  1263. * @return Return true if key press was handled by the component, else
  1264. * return false
  1265. */
  1266. protected boolean handleNavigation(int keycode, boolean ctrl,
  1267. boolean shift) {
  1268. if (!isEnabled() || isReadonly()) {
  1269. return false;
  1270. }
  1271. else if (isYear(getResolution())) {
  1272. return handleNavigationYearMode(keycode, ctrl, shift);
  1273. }
  1274. else if (isMonth(getResolution())) {
  1275. return handleNavigationMonthMode(keycode, ctrl, shift);
  1276. }
  1277. else if (isDay(getResolution())) {
  1278. return handleNavigationDayMode(keycode, ctrl, shift);
  1279. }
  1280. else {
  1281. return handleNavigationDayMode(keycode, ctrl, shift);
  1282. }
  1283. }
  1284. /**
  1285. * Returns the reset key which will reset the calendar to the previous
  1286. * selection. By default this is backspace but it can be overriden to change
  1287. * the key to whatever you want.
  1288. *
  1289. * @return
  1290. */
  1291. protected int getResetKey() {
  1292. return KeyCodes.KEY_BACKSPACE;
  1293. }
  1294. /**
  1295. * Returns the select key which selects the value. By default this is the
  1296. * enter key but it can be changed to whatever you like by overriding this
  1297. * method.
  1298. *
  1299. * @return
  1300. */
  1301. protected int getSelectKey() {
  1302. return KeyCodes.KEY_ENTER;
  1303. }
  1304. /**
  1305. * Returns the key that closes the popup window if this is a VPopopCalendar.
  1306. * Else this does nothing. By default this is the Escape key but you can
  1307. * change the key to whatever you want by overriding this method.
  1308. *
  1309. * @return
  1310. */
  1311. protected int getCloseKey() {
  1312. return KeyCodes.KEY_ESCAPE;
  1313. }
  1314. /**
  1315. * The key that selects the next day in the calendar. By default this is the
  1316. * right arrow key but by overriding this method it can be changed to
  1317. * whatever you like.
  1318. *
  1319. * @return
  1320. */
  1321. protected int getForwardKey() {
  1322. return KeyCodes.KEY_RIGHT;
  1323. }
  1324. /**
  1325. * The key that selects the previous day in the calendar. By default this is
  1326. * the left arrow key but by overriding this method it can be changed to
  1327. * whatever you like.
  1328. *
  1329. * @return
  1330. */
  1331. protected int getBackwardKey() {
  1332. return KeyCodes.KEY_LEFT;
  1333. }
  1334. /**
  1335. * The key that selects the next week in the calendar. By default this is
  1336. * the down arrow key but by overriding this method it can be changed to
  1337. * whatever you like.
  1338. *
  1339. * @return
  1340. */
  1341. protected int getNextKey() {
  1342. return KeyCodes.KEY_DOWN;
  1343. }
  1344. /**
  1345. * The key that selects the previous week in the calendar. By default this
  1346. * is the up arrow key but by overriding this method it can be changed to
  1347. * whatever you like.
  1348. *
  1349. * @return
  1350. */
  1351. protected int getPreviousKey() {
  1352. return KeyCodes.KEY_UP;
  1353. }
  1354. /*
  1355. * (non-Javadoc)
  1356. *
  1357. * @see
  1358. * com.google.gwt.event.dom.client.MouseOutHandler#onMouseOut(com.google
  1359. * .gwt.event.dom.client.MouseOutEvent)
  1360. */
  1361. @Override
  1362. public void onMouseOut(MouseOutEvent event) {
  1363. if (mouseTimer != null) {
  1364. mouseTimer.cancel();
  1365. }
  1366. }
  1367. /*
  1368. * (non-Javadoc)
  1369. *
  1370. * @see
  1371. * com.google.gwt.event.dom.client.MouseDownHandler#onMouseDown(com.google
  1372. * .gwt.event.dom.client.MouseDownEvent)
  1373. */
  1374. @Override
  1375. public void onMouseDown(MouseDownEvent event) {
  1376. // Click-n-hold the left mouse button for fast-forward or fast-rewind.
  1377. // Timer is first used for a 500ms delay after mousedown. After that has
  1378. // elapsed, another timer is triggered to go off every 150ms. Both
  1379. // timers are cancelled on mouseup or mouseout.
  1380. if (event.getNativeButton() == NativeEvent.BUTTON_LEFT && event
  1381. .getSource() instanceof VAbstractCalendarPanel.VEventButton) {
  1382. final VEventButton sender = (VEventButton) event.getSource();
  1383. processClickEvent(sender);
  1384. mouseTimer = new Timer() {
  1385. @Override
  1386. public void run() {
  1387. mouseTimer = new Timer() {
  1388. @Override
  1389. public void run() {
  1390. processClickEvent(sender);
  1391. }
  1392. };
  1393. mouseTimer.scheduleRepeating(150);
  1394. }
  1395. };
  1396. mouseTimer.schedule(500);
  1397. }
  1398. }
  1399. /*
  1400. * (non-Javadoc)
  1401. *
  1402. * @see
  1403. * com.google.gwt.event.dom.client.MouseUpHandler#onMouseUp(com.google.gwt
  1404. * .event.dom.client.MouseUpEvent)
  1405. */
  1406. @Override
  1407. public void onMouseUp(MouseUpEvent event) {
  1408. if (mouseTimer != null) {
  1409. mouseTimer.cancel();
  1410. }
  1411. }
  1412. /**
  1413. * Adjusts a date to fit inside the range, only if outside
  1414. *
  1415. * @param date
  1416. */
  1417. private Date adjustDateToFitInsideRange(Date date) {
  1418. if (rangeStart != null && rangeStart.after(date)) {
  1419. date = (Date) rangeStart.clone();
  1420. } else if (rangeEnd != null && rangeEnd.before(date)) {
  1421. date = (Date) rangeEnd.clone();
  1422. }
  1423. return date;
  1424. }
  1425. /**
  1426. * Sets the data of the Panel.
  1427. *
  1428. * @param currentDate
  1429. * The date to set
  1430. */
  1431. public void setDate(Date currentDate) {
  1432. doSetDate(currentDate, false, () -> {
  1433. });
  1434. }
  1435. /**
  1436. * The actual implementation of the logic which sets the data of the Panel.
  1437. * The method {@link #setDate(Date)} just delegate a call to this method
  1438. * providing additional config parameters.
  1439. *
  1440. * @param currentDate
  1441. * currentDate The date to set
  1442. * @param needRerender
  1443. * if {@code true} then calendar will be rerendered regardless of
  1444. * internal logic, otherwise the decision will be made on the
  1445. * internal state inside the method
  1446. * @param focusAction
  1447. * an additional action which will be executed in case
  1448. * rerendering is not required
  1449. */
  1450. protected void doSetDate(Date currentDate, boolean needRerender,
  1451. Runnable focusAction) {
  1452. // Check that we are not re-rendering an already active date
  1453. if (currentDate == value && currentDate != null) {
  1454. return;
  1455. }
  1456. boolean currentDateWasAdjusted = false;
  1457. // Check that selected date is inside the allowed range
  1458. if (currentDate != null
  1459. && !isDateInsideRange(currentDate, getResolution())) {
  1460. currentDate = adjustDateToFitInsideRange(currentDate);
  1461. currentDateWasAdjusted = true;
  1462. }
  1463. Date oldDisplayedMonth = displayedMonth;
  1464. value = currentDate;
  1465. // If current date was adjusted, we will not select any date,
  1466. // since that will look like a date is selected. Instead we
  1467. // only focus on the adjusted value
  1468. if (value == null || currentDateWasAdjusted) {
  1469. // If ranges enabled, we may need to focus on a different view to
  1470. // potentially not get stuck
  1471. if (rangeStart != null || rangeEnd != null) {
  1472. Date dateThatFitsInsideRange = adjustDateToFitInsideRange(
  1473. new Date());
  1474. focusedDate = new FocusedDate(dateThatFitsInsideRange.getYear(),
  1475. dateThatFitsInsideRange.getMonth(),
  1476. dateThatFitsInsideRange.getDate());
  1477. displayedMonth = new FocusedDate(
  1478. dateThatFitsInsideRange.getYear(),
  1479. dateThatFitsInsideRange.getMonth(), 1);
  1480. // value was adjusted. Set selected to null to not cause
  1481. // confusion, but this is only needed (and allowed) when we have
  1482. // a day
  1483. // resolution
  1484. if (isDay(getResolution())) {
  1485. value = null;
  1486. }
  1487. } else {
  1488. focusedDate = displayedMonth = null;
  1489. }
  1490. } else {
  1491. focusedDate = new FocusedDate(value.getYear(), value.getMonth(),
  1492. value.getDate());
  1493. displayedMonth = new FocusedDate(value.getYear(), value.getMonth(),
  1494. 1);
  1495. }
  1496. // Re-render calendar if the displayed month is changed.
  1497. if (needRerender || oldDisplayedMonth == null || value == null
  1498. || oldDisplayedMonth.getYear() != value.getYear()
  1499. || oldDisplayedMonth.getMonth() != value.getMonth()) {
  1500. renderCalendar();
  1501. } else {
  1502. focusDay(focusedDate);
  1503. selectFocused();
  1504. focusAction.run();
  1505. }
  1506. if (!hasFocus) {
  1507. focusDay(null);
  1508. }
  1509. }
  1510. /**
  1511. * A widget representing a single day in the calendar panel.
  1512. */
  1513. private class Day extends InlineHTML {
  1514. private final Date date;
  1515. Day(Date date) {
  1516. super("" + date.getDate());
  1517. this.date = date;
  1518. addClickHandler(dayClickHandler);
  1519. }
  1520. public Date getDate() {
  1521. return date;
  1522. }
  1523. }
  1524. public Date getDate() {
  1525. return value;
  1526. }
  1527. /**
  1528. * If true should be returned if the panel will not be used after this
  1529. * event.
  1530. *
  1531. * @param event
  1532. * @return
  1533. */
  1534. protected boolean onTabOut(DomEvent<?> event) {
  1535. if (focusOutListener != null) {
  1536. return focusOutListener.onFocusOut(event);
  1537. }
  1538. return false;
  1539. }
  1540. /**
  1541. * A focus out listener is triggered when the panel loosed focus. This can
  1542. * happen either after a user clicks outside the panel or tabs out.
  1543. *
  1544. * @param listener
  1545. * The listener to trigger
  1546. */
  1547. public void setFocusOutListener(FocusOutListener listener) {
  1548. focusOutListener = listener;
  1549. }
  1550. /**
  1551. * The submit listener is called when the user selects a value from the
  1552. * calender either by clicking the day or selects it by keyboard.
  1553. *
  1554. * @param submitListener
  1555. * The listener to trigger
  1556. */
  1557. public void setSubmitListener(SubmitListener submitListener) {
  1558. this.submitListener = submitListener;
  1559. }
  1560. /**
  1561. * The given FocusChangeListener is notified when the focused date changes
  1562. * by user either clicking on a new date or by using the keyboard.
  1563. *
  1564. * @param listener
  1565. * The FocusChangeListener to be notified
  1566. */
  1567. public void setFocusChangeListener(FocusChangeListener listener) {
  1568. focusChangeListener = listener;
  1569. }
  1570. /**
  1571. * Returns the submit listener that listens to selection made from the panel
  1572. *
  1573. * @return The listener or NULL if no listener has been set
  1574. */
  1575. public SubmitListener getSubmitListener() {
  1576. return submitListener;
  1577. }
  1578. /*
  1579. * (non-Javadoc)
  1580. *
  1581. * @see
  1582. * com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event
  1583. * .dom.client.BlurEvent)
  1584. */
  1585. @Override
  1586. public void onBlur(final BlurEvent event) {
  1587. if (event.getSource() instanceof VAbstractCalendarPanel) {
  1588. hasFocus = false;
  1589. focusDay(null);
  1590. }
  1591. }
  1592. /*
  1593. * (non-Javadoc)
  1594. *
  1595. * @see
  1596. * com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event
  1597. * .dom.client.FocusEvent)
  1598. */
  1599. @Override
  1600. public void onFocus(FocusEvent event) {
  1601. if (event.getSource() instanceof VAbstractCalendarPanel) {
  1602. hasFocus = true;
  1603. // Focuses the current day if the calendar shows the days
  1604. if (focusedDay != null) {
  1605. focusDay(focusedDate);
  1606. }
  1607. }
  1608. }
  1609. private static final String SUBPART_NEXT_MONTH = "nextmon";
  1610. private static final String SUBPART_PREV_MONTH = "prevmon";
  1611. private static final String SUBPART_NEXT_YEAR = "nexty";
  1612. private static final String SUBPART_PREV_YEAR = "prevy";
  1613. private static final String SUBPART_HOUR_SELECT = "h";
  1614. private static final String SUBPART_MINUTE_SELECT = "m";
  1615. private static final String SUBPART_SECS_SELECT = "s";
  1616. private static final String SUBPART_AMPM_SELECT = "ampm";
  1617. private static final String SUBPART_DAY = "day";
  1618. private static final String SUBPART_MONTH_YEAR_HEADER = "header";
  1619. private Date rangeStart;
  1620. private Date rangeEnd;
  1621. @Override
  1622. public String getSubPartName(
  1623. com.google.gwt.user.client.Element subElement) {
  1624. if (contains(nextMonth, subElement)) {
  1625. return SUBPART_NEXT_MONTH;
  1626. } else if (contains(prevMonth, subElement)) {
  1627. return SUBPART_PREV_MONTH;
  1628. } else if (contains(nextYear, subElement)) {
  1629. return SUBPART_NEXT_YEAR;
  1630. } else if (contains(prevYear, subElement)) {
  1631. return SUBPART_PREV_YEAR;
  1632. } else if (contains(days, subElement)) {
  1633. // Day, find out which dayOfMonth and use that as the identifier
  1634. Day day = WidgetUtil.findWidget(subElement, Day.class);
  1635. if (day != null) {
  1636. Date date = day.getDate();
  1637. int id = date.getDate();
  1638. // Zero or negative ids map to days of the preceding month,
  1639. // past-the-end-of-month ids to days of the following month
  1640. if (date.getMonth() < displayedMonth.getMonth()) {
  1641. id -= DateTimeService.getNumberOfDaysInMonth(date);
  1642. } else if (date.getMonth() > displayedMonth.getMonth()) {
  1643. id += DateTimeService
  1644. .getNumberOfDaysInMonth(displayedMonth);
  1645. }
  1646. return SUBPART_DAY + id;
  1647. }
  1648. } else if (getCellFormatter().getElement(0, 2)
  1649. .isOrHasChild(subElement)) {
  1650. return SUBPART_MONTH_YEAR_HEADER;
  1651. }
  1652. return null;
  1653. }
  1654. /**
  1655. * Checks if subElement is inside the widget DOM hierarchy.
  1656. *
  1657. * @param w
  1658. * @param subElement
  1659. * @return true if {@code w} is a parent of subElement, false otherwise.
  1660. */
  1661. protected boolean contains(Widget w, Element subElement) {
  1662. if (w == null || w.getElement() == null) {
  1663. return false;
  1664. }
  1665. return w.getElement().isOrHasChild(subElement);
  1666. }
  1667. @Override
  1668. public com.google.gwt.user.client.Element getSubPartElement(
  1669. String subPart) {
  1670. if (SUBPART_NEXT_MONTH.equals(subPart)) {
  1671. return nextMonth.getElement();
  1672. }
  1673. if (SUBPART_PREV_MONTH.equals(subPart)) {
  1674. return prevMonth.getElement();
  1675. }
  1676. if (SUBPART_NEXT_YEAR.equals(subPart)) {
  1677. return nextYear.getElement();
  1678. }
  1679. if (SUBPART_PREV_YEAR.equals(subPart)) {
  1680. return prevYear.getElement();
  1681. }
  1682. if (subPart.startsWith(SUBPART_DAY)) {
  1683. // Zero or negative ids map to days in the preceding month,
  1684. // past-the-end-of-month ids to days in the following month
  1685. int dayOfMonth = Integer
  1686. .parseInt(subPart.substring(SUBPART_DAY.length()));
  1687. Date date = new Date(displayedMonth.getYear(),
  1688. displayedMonth.getMonth(), dayOfMonth);
  1689. Iterator<Widget> iter = days.iterator();
  1690. while (iter.hasNext()) {
  1691. Widget w = iter.next();
  1692. if (w instanceof VAbstractCalendarPanel.Day) {
  1693. Day day = (Day) w;
  1694. if (day.getDate().equals(date)) {
  1695. return day.getElement();
  1696. }
  1697. }
  1698. }
  1699. }
  1700. if (SUBPART_MONTH_YEAR_HEADER.equals(subPart)) {
  1701. return DOM.asOld(
  1702. (Element) getCellFormatter().getElement(0, 2).getChild(0));
  1703. }
  1704. return null;
  1705. }
  1706. @Override
  1707. protected void onDetach() {
  1708. super.onDetach();
  1709. if (mouseTimer != null) {
  1710. mouseTimer.cancel();
  1711. }
  1712. }
  1713. /**
  1714. * Helper class to inform the screen reader that the user changed the
  1715. * selected date. It sets the value of a field that is outside the view, and
  1716. * is defined as a live area. That way the screen reader recognizes the
  1717. * change and reads it to the user.
  1718. */
  1719. public class FocusedDate extends Date {
  1720. public FocusedDate(int year, int month, int date) {
  1721. super(year, month, date);
  1722. }
  1723. @Override
  1724. public void setTime(long time) {
  1725. super.setTime(time);
  1726. setLabel();
  1727. }
  1728. @Override
  1729. @Deprecated
  1730. public void setDate(int date) {
  1731. super.setDate(date);
  1732. setLabel();
  1733. }
  1734. @Override
  1735. @Deprecated
  1736. public void setMonth(int month) {
  1737. super.setMonth(month);
  1738. setLabel();
  1739. }
  1740. @Override
  1741. @Deprecated
  1742. public void setYear(int year) {
  1743. super.setYear(year);
  1744. setLabel();
  1745. }
  1746. private void setLabel() {
  1747. if (getDateField() instanceof VAbstractPopupCalendar) {
  1748. ((VAbstractPopupCalendar<?, ?>) getDateField()).setFocusedDate(this);
  1749. }
  1750. }
  1751. }
  1752. /**
  1753. * Sets the start range for this component. The start range is inclusive,
  1754. * and it depends on the current resolution, what is considered inside the
  1755. * range.
  1756. *
  1757. * @param startDate
  1758. * - the allowed range's start date
  1759. */
  1760. public void setRangeStart(Date newRangeStart) {
  1761. if (!SharedUtil.equals(rangeStart, newRangeStart)) {
  1762. rangeStart = newRangeStart;
  1763. if (initialRenderDone) {
  1764. // Dynamic updates to the range needs to render the calendar to
  1765. // update the element stylenames
  1766. renderCalendar();
  1767. }
  1768. }
  1769. }
  1770. /**
  1771. * Sets the end range for this component. The end range is inclusive, and it
  1772. * depends on the current resolution, what is considered inside the range.
  1773. *
  1774. * @param endDate
  1775. * - the allowed range's end date
  1776. */
  1777. public void setRangeEnd(Date newRangeEnd) {
  1778. if (!SharedUtil.equals(rangeEnd, newRangeEnd)) {
  1779. rangeEnd = newRangeEnd;
  1780. if (initialRenderDone) {
  1781. // Dynamic updates to the range needs to render the calendar to
  1782. // update the element stylenames
  1783. renderCalendar();
  1784. }
  1785. }
  1786. }
  1787. }