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

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