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

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