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.

VCalendarPanel.java 73KB

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