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 57KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Date;
  6. import java.util.Iterator;
  7. import com.google.gwt.dom.client.Node;
  8. import com.google.gwt.event.dom.client.BlurEvent;
  9. import com.google.gwt.event.dom.client.BlurHandler;
  10. import com.google.gwt.event.dom.client.ChangeEvent;
  11. import com.google.gwt.event.dom.client.ChangeHandler;
  12. import com.google.gwt.event.dom.client.ClickEvent;
  13. import com.google.gwt.event.dom.client.ClickHandler;
  14. import com.google.gwt.event.dom.client.DomEvent;
  15. import com.google.gwt.event.dom.client.FocusEvent;
  16. import com.google.gwt.event.dom.client.FocusHandler;
  17. import com.google.gwt.event.dom.client.KeyCodes;
  18. import com.google.gwt.event.dom.client.KeyDownEvent;
  19. import com.google.gwt.event.dom.client.KeyDownHandler;
  20. import com.google.gwt.event.dom.client.KeyPressEvent;
  21. import com.google.gwt.event.dom.client.KeyPressHandler;
  22. import com.google.gwt.event.dom.client.MouseDownEvent;
  23. import com.google.gwt.event.dom.client.MouseDownHandler;
  24. import com.google.gwt.event.dom.client.MouseOutEvent;
  25. import com.google.gwt.event.dom.client.MouseOutHandler;
  26. import com.google.gwt.event.dom.client.MouseUpEvent;
  27. import com.google.gwt.event.dom.client.MouseUpHandler;
  28. import com.google.gwt.user.client.Element;
  29. import com.google.gwt.user.client.Timer;
  30. import com.google.gwt.user.client.ui.Button;
  31. import com.google.gwt.user.client.ui.FlexTable;
  32. import com.google.gwt.user.client.ui.FlowPanel;
  33. import com.google.gwt.user.client.ui.InlineHTML;
  34. import com.google.gwt.user.client.ui.ListBox;
  35. import com.google.gwt.user.client.ui.Widget;
  36. import com.vaadin.terminal.gwt.client.BrowserInfo;
  37. import com.vaadin.terminal.gwt.client.DateTimeService;
  38. import com.vaadin.terminal.gwt.client.Util;
  39. import com.vaadin.terminal.gwt.client.VConsole;
  40. @SuppressWarnings("deprecation")
  41. public class VCalendarPanel extends FocusableFlexTable implements
  42. KeyDownHandler, KeyPressHandler, MouseOutHandler, MouseDownHandler,
  43. MouseUpHandler, BlurHandler, FocusHandler, SubPartAware {
  44. public interface SubmitListener {
  45. /**
  46. * Called when calendar user triggers a submitting operation in calendar
  47. * panel. Eg. clicking on day or hitting enter.
  48. */
  49. void onSubmit();
  50. /**
  51. * On eg. ESC key.
  52. */
  53. void onCancel();
  54. }
  55. /**
  56. * Blur listener that listens to blur event from the panel
  57. */
  58. public interface FocusOutListener {
  59. /**
  60. * @return true if the calendar panel is not used after focus moves out
  61. */
  62. boolean onFocusOut(DomEvent<?> event);
  63. }
  64. /**
  65. * FocusChangeListener is notified when the panel changes its _focused_
  66. * value. It can be set with
  67. */
  68. public interface FocusChangeListener {
  69. void focusChanged(Date focusedDate);
  70. }
  71. /**
  72. * Dispatches an event when the panel when time is changed
  73. */
  74. public interface TimeChangeListener {
  75. void changed(int hour, int min, int sec, int msec);
  76. }
  77. /**
  78. * Represents a Date button in the calendar
  79. */
  80. private class VEventButton extends Button {
  81. public VEventButton() {
  82. addMouseDownHandler(VCalendarPanel.this);
  83. addMouseOutHandler(VCalendarPanel.this);
  84. addMouseUpHandler(VCalendarPanel.this);
  85. }
  86. }
  87. private static final String CN_FOCUSED = "focused";
  88. private static final String CN_TODAY = "today";
  89. private static final String CN_SELECTED = "selected";
  90. /**
  91. * Represents a click handler for when a user selects a value by using the
  92. * mouse
  93. */
  94. private ClickHandler dayClickHandler = new ClickHandler() {
  95. /*
  96. * (non-Javadoc)
  97. *
  98. * @see
  99. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
  100. * .event.dom.client.ClickEvent)
  101. */
  102. public void onClick(ClickEvent event) {
  103. Day day = (Day) event.getSource();
  104. focusDay(day.getDay());
  105. selectFocused();
  106. onSubmit();
  107. }
  108. };
  109. private VEventButton prevYear;
  110. private VEventButton nextYear;
  111. private VEventButton prevMonth;
  112. private VEventButton nextMonth;
  113. private VTime time;
  114. private FlexTable days = new FlexTable();
  115. private int resolution = VDateField.RESOLUTION_YEAR;
  116. private int focusedRow;
  117. private Timer mouseTimer;
  118. private Date value;
  119. private boolean enabled = true;
  120. private boolean readonly = false;
  121. private DateTimeService dateTimeService;
  122. private boolean showISOWeekNumbers;
  123. private Date focusedDate;
  124. private Day selectedDay;
  125. private Day focusedDay;
  126. private FocusOutListener focusOutListener;
  127. private SubmitListener submitListener;
  128. private FocusChangeListener focusChangeListener;
  129. private TimeChangeListener timeChangeListener;
  130. private boolean hasFocus = false;
  131. public VCalendarPanel() {
  132. setStyleName(VDateField.CLASSNAME + "-calendarpanel");
  133. /*
  134. * Firefox auto-repeat works correctly only if we use a key press
  135. * handler, other browsers handle it correctly when using a key down
  136. * handler
  137. */
  138. if (BrowserInfo.get().isGecko()) {
  139. addKeyPressHandler(this);
  140. } else {
  141. addKeyDownHandler(this);
  142. }
  143. addFocusHandler(this);
  144. addBlurHandler(this);
  145. }
  146. /**
  147. * Sets the focus to given day of current time. Used when moving in the
  148. * calender with the keyboard.
  149. *
  150. * @param day
  151. * The day number from by Date.getDate()
  152. */
  153. private void focusDay(int day) {
  154. // Only used when calender body is present
  155. if (resolution > VDateField.RESOLUTION_MONTH) {
  156. if (focusedDay != null) {
  157. focusedDay.removeStyleDependentName(CN_FOCUSED);
  158. }
  159. if (day > 0 && focusedDate != null) {
  160. focusedDate.setDate(day);
  161. int rowCount = days.getRowCount();
  162. for (int i = 0; i < rowCount; i++) {
  163. int cellCount = days.getCellCount(i);
  164. for (int j = 0; j < cellCount; j++) {
  165. Widget widget = days.getWidget(i, j);
  166. if (widget != null && widget instanceof Day) {
  167. Day curday = (Day) widget;
  168. if (curday.getDay() == day) {
  169. curday.addStyleDependentName(CN_FOCUSED);
  170. focusedDay = curday;
  171. focusedRow = i;
  172. return;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * Sets the selection hightlight to a given date of current time
  182. *
  183. * @param day
  184. */
  185. private void selectDate(int day) {
  186. if (selectedDay != null) {
  187. selectedDay.removeStyleDependentName(CN_SELECTED);
  188. }
  189. int rowCount = days.getRowCount();
  190. for (int i = 0; i < rowCount; i++) {
  191. int cellCount = days.getCellCount(i);
  192. for (int j = 0; j < cellCount; j++) {
  193. Widget widget = days.getWidget(i, j);
  194. if (widget != null && widget instanceof Day) {
  195. Day curday = (Day) widget;
  196. if (curday.getDay() == day) {
  197. curday.addStyleDependentName(CN_SELECTED);
  198. selectedDay = curday;
  199. return;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. /**
  206. * Updates year, month, day from focusedDate to value
  207. */
  208. private void selectFocused() {
  209. if (focusedDate != null) {
  210. if (value == null) {
  211. // No previously selected value (set to null on server side).
  212. // Create a new date using current date and time
  213. value = new Date();
  214. }
  215. /*
  216. * #5594 set Date (day) to 1 in order to prevent any kind of
  217. * wrapping of months when later setting the month. (e.g. 31 ->
  218. * month with 30 days -> wraps to the 1st of the following month,
  219. * e.g. 31st of May -> 31st of April = 1st of May)
  220. */
  221. value.setDate(1);
  222. if (value.getYear() != focusedDate.getYear()) {
  223. value.setYear(focusedDate.getYear());
  224. }
  225. if (value.getMonth() != focusedDate.getMonth()) {
  226. value.setMonth(focusedDate.getMonth());
  227. }
  228. if (value.getDate() != focusedDate.getDate()) {
  229. }
  230. // We always need to set the date, even if it hasn't changed, since
  231. // it was forced to 1 above.
  232. value.setDate(focusedDate.getDate());
  233. selectDate(focusedDate.getDate());
  234. } else {
  235. VConsole.log("Trying to select a the focused date which is NULL!");
  236. }
  237. }
  238. protected boolean onValueChange() {
  239. return false;
  240. }
  241. public int getResolution() {
  242. return resolution;
  243. }
  244. public void setResolution(int resolution) {
  245. this.resolution = resolution;
  246. if (time != null) {
  247. time.removeFromParent();
  248. time = null;
  249. }
  250. }
  251. private boolean isReadonly() {
  252. return readonly;
  253. }
  254. private boolean isEnabled() {
  255. return enabled;
  256. }
  257. private void clearCalendarBody(boolean remove) {
  258. if (!remove) {
  259. // Leave the cells in place but clear their contents
  260. // This has the side effect of ensuring that the calendar always
  261. // contain 7 rows.
  262. for (int row = 1; row < 7; row++) {
  263. for (int col = 0; col < 8; col++) {
  264. days.setHTML(row, col, "&nbsp;");
  265. }
  266. }
  267. } else if (getRowCount() > 1) {
  268. removeRow(1);
  269. days.clear();
  270. }
  271. }
  272. /**
  273. * Builds the top buttons and current month and year header.
  274. *
  275. * @param needsMonth
  276. * Should the month buttons be visible?
  277. */
  278. private void buildCalendarHeader(boolean needsMonth) {
  279. getRowFormatter().addStyleName(0,
  280. VDateField.CLASSNAME + "-calendarpanel-header");
  281. if (prevMonth == null && needsMonth) {
  282. prevMonth = new VEventButton();
  283. prevMonth.setHTML("&lsaquo;");
  284. prevMonth.setStyleName("v-button-prevmonth");
  285. prevMonth.setTabIndex(-1);
  286. nextMonth = new VEventButton();
  287. nextMonth.setHTML("&rsaquo;");
  288. nextMonth.setStyleName("v-button-nextmonth");
  289. nextMonth.setTabIndex(-1);
  290. getFlexCellFormatter().setStyleName(0, 3,
  291. VDateField.CLASSNAME + "-calendarpanel-nextmonth");
  292. getFlexCellFormatter().setStyleName(0, 1,
  293. VDateField.CLASSNAME + "-calendarpanel-prevmonth");
  294. setWidget(0, 3, nextMonth);
  295. setWidget(0, 1, prevMonth);
  296. } else if (prevMonth != null && !needsMonth) {
  297. // Remove month traverse buttons
  298. remove(prevMonth);
  299. remove(nextMonth);
  300. prevMonth = null;
  301. nextMonth = null;
  302. }
  303. if (prevYear == null) {
  304. prevYear = new VEventButton();
  305. prevYear.setHTML("&laquo;");
  306. prevYear.setStyleName("v-button-prevyear");
  307. prevYear.setTabIndex(-1);
  308. nextYear = new VEventButton();
  309. nextYear.setHTML("&raquo;");
  310. nextYear.setStyleName("v-button-nextyear");
  311. nextYear.setTabIndex(-1);
  312. setWidget(0, 0, prevYear);
  313. setWidget(0, 4, nextYear);
  314. getFlexCellFormatter().setStyleName(0, 0,
  315. VDateField.CLASSNAME + "-calendarpanel-prevyear");
  316. getFlexCellFormatter().setStyleName(0, 4,
  317. VDateField.CLASSNAME + "-calendarpanel-nextyear");
  318. }
  319. final String monthName = needsMonth ? getDateTimeService().getMonth(
  320. focusedDate.getMonth()) : "";
  321. final int year = focusedDate.getYear() + 1900;
  322. getFlexCellFormatter().setStyleName(0, 2,
  323. VDateField.CLASSNAME + "-calendarpanel-month");
  324. setHTML(0, 2, "<span class=\"" + VDateField.CLASSNAME
  325. + "-calendarpanel-month\">" + monthName + " " + year
  326. + "</span>");
  327. }
  328. private DateTimeService getDateTimeService() {
  329. return dateTimeService;
  330. }
  331. public void setDateTimeService(DateTimeService dateTimeService) {
  332. this.dateTimeService = dateTimeService;
  333. }
  334. /**
  335. * Returns whether ISO 8601 week numbers should be shown in the value
  336. * selector or not. ISO 8601 defines that a week always starts with a Monday
  337. * so the week numbers are only shown if this is the case.
  338. *
  339. * @return true if week number should be shown, false otherwise
  340. */
  341. public boolean isShowISOWeekNumbers() {
  342. return showISOWeekNumbers;
  343. }
  344. public void setShowISOWeekNumbers(boolean showISOWeekNumbers) {
  345. this.showISOWeekNumbers = showISOWeekNumbers;
  346. }
  347. /**
  348. * Builds the day and time selectors of the calendar.
  349. */
  350. private void buildCalendarBody() {
  351. final int weekColumn = 0;
  352. final int firstWeekdayColumn = 1;
  353. final int headerRow = 0;
  354. setWidget(1, 0, days);
  355. setCellPadding(0);
  356. setCellSpacing(0);
  357. getFlexCellFormatter().setColSpan(1, 0, 5);
  358. getFlexCellFormatter().setStyleName(1, 0,
  359. VDateField.CLASSNAME + "-calendarpanel-body");
  360. days.getFlexCellFormatter().setStyleName(headerRow, weekColumn,
  361. "v-week");
  362. days.setHTML(headerRow, weekColumn, "<strong></strong>");
  363. // Hide the week column if week numbers are not to be displayed.
  364. days.getFlexCellFormatter().setVisible(headerRow, weekColumn,
  365. isShowISOWeekNumbers());
  366. days.getRowFormatter().setStyleName(headerRow,
  367. VDateField.CLASSNAME + "-calendarpanel-weekdays");
  368. if (isShowISOWeekNumbers()) {
  369. days.getFlexCellFormatter().setStyleName(headerRow, weekColumn,
  370. "v-first");
  371. days.getFlexCellFormatter().setStyleName(headerRow,
  372. firstWeekdayColumn, "");
  373. days.getRowFormatter().addStyleName(headerRow,
  374. VDateField.CLASSNAME + "-calendarpanel-weeknumbers");
  375. } else {
  376. days.getFlexCellFormatter().setStyleName(headerRow, weekColumn, "");
  377. days.getFlexCellFormatter().setStyleName(headerRow,
  378. firstWeekdayColumn, "v-first");
  379. }
  380. days.getFlexCellFormatter().setStyleName(headerRow,
  381. firstWeekdayColumn + 6, "v-last");
  382. // Print weekday names
  383. final int firstDay = getDateTimeService().getFirstDayOfWeek();
  384. for (int i = 0; i < 7; i++) {
  385. int day = i + firstDay;
  386. if (day > 6) {
  387. day = 0;
  388. }
  389. if (getResolution() > VDateField.RESOLUTION_MONTH) {
  390. days.setHTML(headerRow, firstWeekdayColumn + i, "<strong>"
  391. + getDateTimeService().getShortDay(day) + "</strong>");
  392. } else {
  393. days.setHTML(headerRow, firstWeekdayColumn + i, "");
  394. }
  395. }
  396. // The day of month that is selected, -1 if no day of this month is
  397. // selected (i.e, showing another month/year than selected or nothing is
  398. // selected)
  399. int dayOfMonthSelected = -1;
  400. // The day of month that is today, -1 if no day of this month is today
  401. // (i.e., showing another month/year than current)
  402. int dayOfMonthToday = -1;
  403. boolean initiallyNull = value == null;
  404. if (!initiallyNull && value.getMonth() == focusedDate.getMonth()
  405. && value.getYear() == focusedDate.getYear()) {
  406. dayOfMonthSelected = value.getDate();
  407. }
  408. final Date today = new Date();
  409. if (today.getMonth() == focusedDate.getMonth()
  410. && today.getYear() == focusedDate.getYear()) {
  411. dayOfMonthToday = today.getDate();
  412. }
  413. final int startWeekDay = getDateTimeService().getStartWeekDay(
  414. focusedDate);
  415. final int daysInMonth = DateTimeService
  416. .getNumberOfDaysInMonth(focusedDate);
  417. int dayCount = 0;
  418. final Date curr = new Date(focusedDate.getTime());
  419. // No month has more than 6 weeks so 6 is a safe maximum for rows.
  420. for (int weekOfMonth = 1; weekOfMonth < 7; weekOfMonth++) {
  421. boolean weekNumberProcessed[] = new boolean[] { false, false,
  422. false, false, false, false, false };
  423. for (int dayOfWeek = 0; dayOfWeek < 7; dayOfWeek++) {
  424. if (!(weekOfMonth == 1 && dayOfWeek < startWeekDay)) {
  425. if (dayCount >= daysInMonth) {
  426. // All days printed and we are done
  427. break;
  428. }
  429. final int dayOfMonth = ++dayCount;
  430. curr.setDate(dayCount);
  431. // Actually write the day of month
  432. Day day = new Day(dayOfMonth);
  433. if (dayOfMonthSelected == dayOfMonth) {
  434. day.addStyleDependentName(CN_SELECTED);
  435. selectedDay = day;
  436. }
  437. if (dayOfMonthToday == dayOfMonth) {
  438. day.addStyleDependentName(CN_TODAY);
  439. }
  440. if (dayOfMonth == focusedDate.getDate()) {
  441. focusedDay = day;
  442. focusedRow = weekOfMonth;
  443. if (hasFocus) {
  444. day.addStyleDependentName(CN_FOCUSED);
  445. }
  446. }
  447. days.setWidget(weekOfMonth, firstWeekdayColumn + dayOfWeek,
  448. day);
  449. // ISO week numbers if requested
  450. if (!weekNumberProcessed[weekOfMonth]) {
  451. days.getCellFormatter().setVisible(weekOfMonth,
  452. weekColumn, isShowISOWeekNumbers());
  453. if (isShowISOWeekNumbers()) {
  454. final String baseCssClass = VDateField.CLASSNAME
  455. + "-calendarpanel-weeknumber";
  456. String weekCssClass = baseCssClass;
  457. int weekNumber = DateTimeService
  458. .getISOWeekNumber(curr);
  459. days.setHTML(weekOfMonth, 0, "<span class=\""
  460. + weekCssClass + "\"" + ">" + weekNumber
  461. + "</span>");
  462. weekNumberProcessed[weekOfMonth] = true;
  463. }
  464. }
  465. }
  466. }
  467. }
  468. }
  469. /**
  470. * Do we need the time selector
  471. *
  472. * @return True if it is required
  473. */
  474. private boolean isTimeSelectorNeeded() {
  475. return getResolution() > VDateField.RESOLUTION_DAY;
  476. }
  477. /**
  478. * Updates the calendar and text field with the selected dates.
  479. */
  480. public void renderCalendar() {
  481. if (focusedDate == null) {
  482. focusedDate = new Date();
  483. }
  484. if (getResolution() <= VDateField.RESOLUTION_MONTH
  485. && focusChangeListener != null) {
  486. focusChangeListener.focusChanged(new Date(focusedDate.getTime()));
  487. }
  488. final boolean needsMonth = getResolution() > VDateField.RESOLUTION_YEAR;
  489. boolean needsBody = getResolution() >= VDateField.RESOLUTION_DAY;
  490. buildCalendarHeader(needsMonth);
  491. clearCalendarBody(!needsBody);
  492. if (needsBody) {
  493. buildCalendarBody();
  494. }
  495. if (isTimeSelectorNeeded() && time == null) {
  496. time = new VTime();
  497. setWidget(2, 0, time);
  498. getFlexCellFormatter().setColSpan(2, 0, 5);
  499. getFlexCellFormatter().setStyleName(2, 0,
  500. VDateField.CLASSNAME + "-calendarpanel-time");
  501. } else if (isTimeSelectorNeeded()) {
  502. time.updateTimes();
  503. } else if (time != null) {
  504. remove(time);
  505. }
  506. }
  507. /**
  508. * Selects the next month
  509. */
  510. private void focusNextMonth() {
  511. int currentMonth = focusedDate.getMonth();
  512. focusedDate.setMonth(currentMonth + 1);
  513. int requestedMonth = (currentMonth + 1) % 12;
  514. /*
  515. * If the selected value was e.g. 31.3 the new value would be 31.4 but
  516. * this value is invalid so the new value will be 1.5. This is taken
  517. * care of by decreasing the value until we have the correct month.
  518. */
  519. while (focusedDate.getMonth() != requestedMonth) {
  520. focusedDate.setDate(focusedDate.getDate() - 1);
  521. }
  522. renderCalendar();
  523. }
  524. /**
  525. * Selects the previous month
  526. */
  527. private void focusPreviousMonth() {
  528. int currentMonth = focusedDate.getMonth();
  529. focusedDate.setMonth(currentMonth - 1);
  530. /*
  531. * If the selected value was e.g. 31.12 the new value would be 31.11 but
  532. * this value is invalid so the new value will be 1.12. This is taken
  533. * care of by decreasing the value until we have the correct month.
  534. */
  535. while (focusedDate.getMonth() == currentMonth) {
  536. focusedDate.setDate(focusedDate.getDate() - 1);
  537. }
  538. renderCalendar();
  539. }
  540. /**
  541. * Selects the previous year
  542. */
  543. private void focusPreviousYear(int years) {
  544. focusedDate.setYear(focusedDate.getYear() - years);
  545. renderCalendar();
  546. }
  547. /**
  548. * Selects the next year
  549. */
  550. private void focusNextYear(int years) {
  551. focusedDate.setYear(focusedDate.getYear() + years);
  552. renderCalendar();
  553. }
  554. /**
  555. * Handles a user click on the component
  556. *
  557. * @param sender
  558. * The component that was clicked
  559. * @param updateVariable
  560. * Should the value field be updated
  561. *
  562. */
  563. private void processClickEvent(Widget sender) {
  564. if (!isEnabled() || isReadonly()) {
  565. return;
  566. }
  567. if (sender == prevYear) {
  568. focusPreviousYear(1);
  569. } else if (sender == nextYear) {
  570. focusNextYear(1);
  571. } else if (sender == prevMonth) {
  572. focusPreviousMonth();
  573. } else if (sender == nextMonth) {
  574. focusNextMonth();
  575. }
  576. }
  577. /*
  578. * (non-Javadoc)
  579. *
  580. * @see
  581. * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt
  582. * .event.dom.client.KeyDownEvent)
  583. */
  584. public void onKeyDown(KeyDownEvent event) {
  585. handleKeyPress(event);
  586. }
  587. /*
  588. * (non-Javadoc)
  589. *
  590. * @see
  591. * com.google.gwt.event.dom.client.KeyPressHandler#onKeyPress(com.google
  592. * .gwt.event.dom.client.KeyPressEvent)
  593. */
  594. public void onKeyPress(KeyPressEvent event) {
  595. handleKeyPress(event);
  596. }
  597. /**
  598. * Handles the keypress from both the onKeyPress event and the onKeyDown
  599. * event
  600. *
  601. * @param event
  602. * The keydown/keypress event
  603. */
  604. private void handleKeyPress(DomEvent<?> event) {
  605. if (time != null
  606. && time.getElement().isOrHasChild(
  607. (Node) event.getNativeEvent().getEventTarget().cast())) {
  608. int nativeKeyCode = event.getNativeEvent().getKeyCode();
  609. if (nativeKeyCode == getSelectKey()) {
  610. onSubmit(); // submit happens if enter key hit down on listboxes
  611. event.preventDefault();
  612. event.stopPropagation();
  613. }
  614. return;
  615. }
  616. // Check tabs
  617. int keycode = event.getNativeEvent().getKeyCode();
  618. if (keycode == KeyCodes.KEY_TAB && event.getNativeEvent().getShiftKey()) {
  619. if (onTabOut(event)) {
  620. return;
  621. }
  622. }
  623. // Handle the navigation
  624. if (handleNavigation(keycode, event.getNativeEvent().getCtrlKey()
  625. || event.getNativeEvent().getMetaKey(), event.getNativeEvent()
  626. .getShiftKey())) {
  627. event.preventDefault();
  628. }
  629. }
  630. /**
  631. * Notifies submit-listeners of a submit event
  632. */
  633. private void onSubmit() {
  634. if (getSubmitListener() != null) {
  635. getSubmitListener().onSubmit();
  636. }
  637. }
  638. /**
  639. * Notifies submit-listeners of a cancel event
  640. */
  641. private void onCancel() {
  642. if (getSubmitListener() != null) {
  643. getSubmitListener().onCancel();
  644. }
  645. }
  646. /**
  647. * Handles the keyboard navigation when the resolution is set to years.
  648. *
  649. * @param keycode
  650. * The keycode to process
  651. * @param ctrl
  652. * Is ctrl pressed?
  653. * @param shift
  654. * is shift pressed
  655. * @return Returns true if the keycode was processed, else false
  656. */
  657. protected boolean handleNavigationYearMode(int keycode, boolean ctrl,
  658. boolean shift) {
  659. // Ctrl and Shift selection not supported
  660. if (ctrl || shift) {
  661. return false;
  662. }
  663. else if (keycode == getPreviousKey()) {
  664. focusNextYear(10); // Add 10 years
  665. return true;
  666. }
  667. else if (keycode == getForwardKey()) {
  668. focusNextYear(1); // Add 1 year
  669. return true;
  670. }
  671. else if (keycode == getNextKey()) {
  672. focusPreviousYear(10); // Subtract 10 years
  673. return true;
  674. }
  675. else if (keycode == getBackwardKey()) {
  676. focusPreviousYear(1); // Subtract 1 year
  677. return true;
  678. } else if (keycode == getSelectKey()) {
  679. value = (Date) focusedDate.clone();
  680. onSubmit();
  681. return true;
  682. } else if (keycode == getResetKey()) {
  683. // Restore showing value the selected value
  684. focusedDate.setTime(value.getTime());
  685. renderCalendar();
  686. return true;
  687. } else if (keycode == getCloseKey()) {
  688. // TODO fire listener, on users responsibility??
  689. return true;
  690. }
  691. return false;
  692. }
  693. /**
  694. * Handle the keyboard navigation when the resolution is set to MONTH
  695. *
  696. * @param keycode
  697. * The keycode to handle
  698. * @param ctrl
  699. * Was the ctrl key pressed?
  700. * @param shift
  701. * Was the shift key pressed?
  702. * @return
  703. */
  704. protected boolean handleNavigationMonthMode(int keycode, boolean ctrl,
  705. boolean shift) {
  706. // Ctrl selection not supported
  707. if (ctrl) {
  708. return false;
  709. } else if (keycode == getPreviousKey()) {
  710. focusNextYear(1); // Add 1 year
  711. return true;
  712. } else if (keycode == getForwardKey()) {
  713. focusNextMonth(); // Add 1 month
  714. return true;
  715. } else if (keycode == getNextKey()) {
  716. focusPreviousYear(1); // Subtract 1 year
  717. return true;
  718. } else if (keycode == getBackwardKey()) {
  719. focusPreviousMonth(); // Subtract 1 month
  720. return true;
  721. } else if (keycode == getSelectKey()) {
  722. value = (Date) focusedDate.clone();
  723. onSubmit();
  724. return true;
  725. } else if (keycode == getResetKey()) {
  726. // Restore showing value the selected value
  727. focusedDate.setTime(value.getTime());
  728. renderCalendar();
  729. return true;
  730. } else if (keycode == getCloseKey() || keycode == KeyCodes.KEY_TAB) {
  731. // TODO fire close event
  732. return true;
  733. }
  734. return false;
  735. }
  736. /**
  737. * Handle keyboard navigation what the resolution is set to DAY
  738. *
  739. * @param keycode
  740. * The keycode to handle
  741. * @param ctrl
  742. * Was the ctrl key pressed?
  743. * @param shift
  744. * Was the shift key pressed?
  745. * @return Return true if the key press was handled by the method, else
  746. * return false.
  747. */
  748. protected boolean handleNavigationDayMode(int keycode, boolean ctrl,
  749. boolean shift) {
  750. // Ctrl key is not in use
  751. if (ctrl) {
  752. return false;
  753. }
  754. /*
  755. * Jumps to the next day.
  756. */
  757. if (keycode == getForwardKey() && !shift) {
  758. // Calculate new showing value
  759. Date newCurrentDate = (Date) focusedDate.clone();
  760. newCurrentDate.setDate(newCurrentDate.getDate() + 1);
  761. if (newCurrentDate.getMonth() == focusedDate.getMonth()) {
  762. // Month did not change, only move the selection
  763. focusDay(focusedDate.getDate() + 1);
  764. } else {
  765. // If the month changed we need to re-render the calendar
  766. focusedDate.setDate(focusedDate.getDate() + 1);
  767. renderCalendar();
  768. }
  769. return true;
  770. /*
  771. * Jumps to the previous day
  772. */
  773. } else if (keycode == getBackwardKey() && !shift) {
  774. // Calculate new showing value
  775. Date newCurrentDate = (Date) focusedDate.clone();
  776. newCurrentDate.setDate(newCurrentDate.getDate() - 1);
  777. if (newCurrentDate.getMonth() == focusedDate.getMonth()) {
  778. // Month did not change, only move the selection
  779. focusDay(focusedDate.getDate() - 1);
  780. } else {
  781. // If the month changed we need to re-render the calendar
  782. focusedDate.setDate(focusedDate.getDate() - 1);
  783. renderCalendar();
  784. }
  785. return true;
  786. /*
  787. * Jumps one week back in the calendar
  788. */
  789. } else if (keycode == getPreviousKey() && !shift) {
  790. // Calculate new showing value
  791. Date newCurrentDate = (Date) focusedDate.clone();
  792. newCurrentDate.setDate(newCurrentDate.getDate() - 7);
  793. if (newCurrentDate.getMonth() == focusedDate.getMonth()
  794. && focusedRow > 1) {
  795. // Month did not change, only move the selection
  796. focusDay(focusedDate.getDate() - 7);
  797. } else {
  798. // If the month changed we need to re-render the calendar
  799. focusedDate.setDate(focusedDate.getDate() - 7);
  800. renderCalendar();
  801. }
  802. return true;
  803. /*
  804. * Jumps one week forward in the calendar
  805. */
  806. } else if (keycode == getNextKey() && !ctrl && !shift) {
  807. // Calculate new showing value
  808. Date newCurrentDate = (Date) focusedDate.clone();
  809. newCurrentDate.setDate(newCurrentDate.getDate() + 7);
  810. if (newCurrentDate.getMonth() == focusedDate.getMonth()) {
  811. // Month did not change, only move the selection
  812. focusDay(focusedDate.getDate() + 7);
  813. } else {
  814. // If the month changed we need to re-render the calendar
  815. focusedDate.setDate(focusedDate.getDate() + 7);
  816. renderCalendar();
  817. }
  818. return true;
  819. /*
  820. * Selects the value that is chosen
  821. */
  822. } else if (keycode == getSelectKey() && !shift) {
  823. selectFocused();
  824. onSubmit(); // submit
  825. return true;
  826. } else if (keycode == getCloseKey()) {
  827. onCancel();
  828. // TODO close event
  829. return true;
  830. /*
  831. * Jumps to the next month
  832. */
  833. } else if (shift && keycode == getForwardKey()) {
  834. focusNextMonth();
  835. return true;
  836. /*
  837. * Jumps to the previous month
  838. */
  839. } else if (shift && keycode == getBackwardKey()) {
  840. focusPreviousMonth();
  841. return true;
  842. /*
  843. * Jumps to the next year
  844. */
  845. } else if (shift && keycode == getPreviousKey()) {
  846. focusNextYear(1);
  847. return true;
  848. /*
  849. * Jumps to the previous year
  850. */
  851. } else if (shift && keycode == getNextKey()) {
  852. focusPreviousYear(1);
  853. return true;
  854. /*
  855. * Resets the selection
  856. */
  857. } else if (keycode == getResetKey() && !shift) {
  858. // Restore showing value the selected value
  859. focusedDate.setTime(value.getTime());
  860. renderCalendar();
  861. return true;
  862. }
  863. return false;
  864. }
  865. /**
  866. * Handles the keyboard navigation
  867. *
  868. * @param keycode
  869. * The key code that was pressed
  870. * @param ctrl
  871. * Was the ctrl key pressed
  872. * @param shift
  873. * Was the shift key pressed
  874. * @return Return true if key press was handled by the component, else
  875. * return false
  876. */
  877. protected boolean handleNavigation(int keycode, boolean ctrl, boolean shift) {
  878. if (!isEnabled() || isReadonly()) {
  879. return false;
  880. }
  881. else if (resolution == VDateField.RESOLUTION_YEAR) {
  882. return handleNavigationYearMode(keycode, ctrl, shift);
  883. }
  884. else if (resolution == VDateField.RESOLUTION_MONTH) {
  885. return handleNavigationMonthMode(keycode, ctrl, shift);
  886. }
  887. else if (resolution == VDateField.RESOLUTION_DAY) {
  888. return handleNavigationDayMode(keycode, ctrl, shift);
  889. }
  890. else {
  891. return handleNavigationDayMode(keycode, ctrl, shift);
  892. }
  893. }
  894. /**
  895. * Returns the reset key which will reset the calendar to the previous
  896. * selection. By default this is backspace but it can be overriden to change
  897. * the key to whatever you want.
  898. *
  899. * @return
  900. */
  901. protected int getResetKey() {
  902. return KeyCodes.KEY_BACKSPACE;
  903. }
  904. /**
  905. * Returns the select key which selects the value. By default this is the
  906. * enter key but it can be changed to whatever you like by overriding this
  907. * method.
  908. *
  909. * @return
  910. */
  911. protected int getSelectKey() {
  912. return KeyCodes.KEY_ENTER;
  913. }
  914. /**
  915. * Returns the key that closes the popup window if this is a VPopopCalendar.
  916. * Else this does nothing. By default this is the Escape key but you can
  917. * change the key to whatever you want by overriding this method.
  918. *
  919. * @return
  920. */
  921. protected int getCloseKey() {
  922. return KeyCodes.KEY_ESCAPE;
  923. }
  924. /**
  925. * The key that selects the next day in the calendar. By default this is the
  926. * right arrow key but by overriding this method it can be changed to
  927. * whatever you like.
  928. *
  929. * @return
  930. */
  931. protected int getForwardKey() {
  932. return KeyCodes.KEY_RIGHT;
  933. }
  934. /**
  935. * The key that selects the previous day in the calendar. By default this is
  936. * the left arrow key but by overriding this method it can be changed to
  937. * whatever you like.
  938. *
  939. * @return
  940. */
  941. protected int getBackwardKey() {
  942. return KeyCodes.KEY_LEFT;
  943. }
  944. /**
  945. * The key that selects the next week in the calendar. By default this is
  946. * the down arrow key but by overriding this method it can be changed to
  947. * whatever you like.
  948. *
  949. * @return
  950. */
  951. protected int getNextKey() {
  952. return KeyCodes.KEY_DOWN;
  953. }
  954. /**
  955. * The key that selects the previous week in the calendar. By default this
  956. * is the up arrow key but by overriding this method it can be changed to
  957. * whatever you like.
  958. *
  959. * @return
  960. */
  961. protected int getPreviousKey() {
  962. return KeyCodes.KEY_UP;
  963. }
  964. /*
  965. * (non-Javadoc)
  966. *
  967. * @see
  968. * com.google.gwt.event.dom.client.MouseOutHandler#onMouseOut(com.google
  969. * .gwt.event.dom.client.MouseOutEvent)
  970. */
  971. public void onMouseOut(MouseOutEvent event) {
  972. if (mouseTimer != null) {
  973. mouseTimer.cancel();
  974. }
  975. }
  976. /*
  977. * (non-Javadoc)
  978. *
  979. * @see
  980. * com.google.gwt.event.dom.client.MouseDownHandler#onMouseDown(com.google
  981. * .gwt.event.dom.client.MouseDownEvent)
  982. */
  983. public void onMouseDown(MouseDownEvent event) {
  984. // Allow user to click-n-hold for fast-forward or fast-rewind.
  985. // Timer is first used for a 500ms delay after mousedown. After that has
  986. // elapsed, another timer is triggered to go off every 150ms. Both
  987. // timers are cancelled on mouseup or mouseout.
  988. if (event.getSource() instanceof VEventButton) {
  989. final VEventButton sender = (VEventButton) event.getSource();
  990. processClickEvent(sender);
  991. mouseTimer = new Timer() {
  992. @Override
  993. public void run() {
  994. mouseTimer = new Timer() {
  995. @Override
  996. public void run() {
  997. processClickEvent(sender);
  998. }
  999. };
  1000. mouseTimer.scheduleRepeating(150);
  1001. }
  1002. };
  1003. mouseTimer.schedule(500);
  1004. }
  1005. }
  1006. /*
  1007. * (non-Javadoc)
  1008. *
  1009. * @see
  1010. * com.google.gwt.event.dom.client.MouseUpHandler#onMouseUp(com.google.gwt
  1011. * .event.dom.client.MouseUpEvent)
  1012. */
  1013. public void onMouseUp(MouseUpEvent event) {
  1014. if (mouseTimer != null) {
  1015. mouseTimer.cancel();
  1016. }
  1017. }
  1018. /**
  1019. * Sets the data of the Panel.
  1020. *
  1021. * @param currentDate
  1022. * The date to set
  1023. */
  1024. public void setDate(Date currentDate) {
  1025. // Check that we are not re-rendering an already active date
  1026. if (currentDate == value && currentDate != null) {
  1027. return;
  1028. }
  1029. Date oldFocusedValue = focusedDate;
  1030. value = currentDate;
  1031. if (value == null) {
  1032. focusedDate = null;
  1033. } else {
  1034. focusedDate = (Date) value.clone();
  1035. }
  1036. // Re-render calendar if month or year of focused date has changed
  1037. if (oldFocusedValue == null || value == null
  1038. || oldFocusedValue.getYear() != value.getYear()
  1039. || oldFocusedValue.getMonth() != value.getMonth()) {
  1040. renderCalendar();
  1041. } else {
  1042. focusDay(currentDate.getDate());
  1043. selectFocused();
  1044. }
  1045. if (!hasFocus) {
  1046. focusDay(-1);
  1047. }
  1048. }
  1049. /**
  1050. * TimeSelector is a widget consisting of list boxes that modifie the Date
  1051. * object that is given for.
  1052. *
  1053. */
  1054. public class VTime extends FlowPanel implements ChangeHandler {
  1055. private ListBox hours;
  1056. private ListBox mins;
  1057. private ListBox sec;
  1058. private ListBox ampm;
  1059. /**
  1060. * Constructor
  1061. */
  1062. public VTime() {
  1063. super();
  1064. setStyleName(VDateField.CLASSNAME + "-time");
  1065. buildTime();
  1066. }
  1067. private ListBox createListBox() {
  1068. ListBox lb = new ListBox();
  1069. lb.setStyleName(VNativeSelect.CLASSNAME);
  1070. lb.addChangeHandler(this);
  1071. lb.addBlurHandler(VCalendarPanel.this);
  1072. lb.addFocusHandler(VCalendarPanel.this);
  1073. return lb;
  1074. }
  1075. /**
  1076. * Constructs the ListBoxes and updates their value
  1077. *
  1078. * @param redraw
  1079. * Should new instances of the listboxes be created
  1080. */
  1081. private void buildTime() {
  1082. clear();
  1083. hours = createListBox();
  1084. if (getDateTimeService().isTwelveHourClock()) {
  1085. hours.addItem("12");
  1086. for (int i = 1; i < 12; i++) {
  1087. hours.addItem((i < 10) ? "0" + i : "" + i);
  1088. }
  1089. } else {
  1090. for (int i = 0; i < 24; i++) {
  1091. hours.addItem((i < 10) ? "0" + i : "" + i);
  1092. }
  1093. }
  1094. hours.addChangeHandler(this);
  1095. if (getDateTimeService().isTwelveHourClock()) {
  1096. ampm = createListBox();
  1097. final String[] ampmText = getDateTimeService().getAmPmStrings();
  1098. ampm.addItem(ampmText[0]);
  1099. ampm.addItem(ampmText[1]);
  1100. ampm.addChangeHandler(this);
  1101. }
  1102. if (getResolution() >= VDateField.RESOLUTION_MIN) {
  1103. mins = createListBox();
  1104. for (int i = 0; i < 60; i++) {
  1105. mins.addItem((i < 10) ? "0" + i : "" + i);
  1106. }
  1107. mins.addChangeHandler(this);
  1108. }
  1109. if (getResolution() >= VDateField.RESOLUTION_SEC) {
  1110. sec = createListBox();
  1111. for (int i = 0; i < 60; i++) {
  1112. sec.addItem((i < 10) ? "0" + i : "" + i);
  1113. }
  1114. sec.addChangeHandler(this);
  1115. }
  1116. final String delimiter = getDateTimeService().getClockDelimeter();
  1117. if (isReadonly()) {
  1118. int h = 0;
  1119. if (value != null) {
  1120. h = value.getHours();
  1121. }
  1122. if (getDateTimeService().isTwelveHourClock()) {
  1123. h -= h < 12 ? 0 : 12;
  1124. }
  1125. add(new VLabel(h < 10 ? "0" + h : "" + h));
  1126. } else {
  1127. add(hours);
  1128. }
  1129. if (getResolution() >= VDateField.RESOLUTION_MIN) {
  1130. add(new VLabel(delimiter));
  1131. if (isReadonly()) {
  1132. final int m = mins.getSelectedIndex();
  1133. add(new VLabel(m < 10 ? "0" + m : "" + m));
  1134. } else {
  1135. add(mins);
  1136. }
  1137. }
  1138. if (getResolution() >= VDateField.RESOLUTION_SEC) {
  1139. add(new VLabel(delimiter));
  1140. if (isReadonly()) {
  1141. final int s = sec.getSelectedIndex();
  1142. add(new VLabel(s < 10 ? "0" + s : "" + s));
  1143. } else {
  1144. add(sec);
  1145. }
  1146. }
  1147. if (getResolution() == VDateField.RESOLUTION_HOUR) {
  1148. add(new VLabel(delimiter + "00")); // o'clock
  1149. }
  1150. if (getDateTimeService().isTwelveHourClock()) {
  1151. add(new VLabel("&nbsp;"));
  1152. if (isReadonly()) {
  1153. int i = 0;
  1154. if (value != null) {
  1155. i = (value.getHours() < 12) ? 0 : 1;
  1156. }
  1157. add(new VLabel(ampm.getItemText(i)));
  1158. } else {
  1159. add(ampm);
  1160. }
  1161. }
  1162. if (isReadonly()) {
  1163. return;
  1164. }
  1165. // Update times
  1166. updateTimes();
  1167. ListBox lastDropDown = getLastDropDown();
  1168. lastDropDown.addKeyDownHandler(new KeyDownHandler() {
  1169. public void onKeyDown(KeyDownEvent event) {
  1170. boolean shiftKey = event.getNativeEvent().getShiftKey();
  1171. if (shiftKey) {
  1172. return;
  1173. } else {
  1174. int nativeKeyCode = event.getNativeKeyCode();
  1175. if (nativeKeyCode == KeyCodes.KEY_TAB) {
  1176. onTabOut(event);
  1177. }
  1178. }
  1179. }
  1180. });
  1181. }
  1182. private ListBox getLastDropDown() {
  1183. int i = getWidgetCount() - 1;
  1184. while (i >= 0) {
  1185. Widget widget = getWidget(i);
  1186. if (widget instanceof ListBox) {
  1187. return (ListBox) widget;
  1188. }
  1189. i--;
  1190. }
  1191. return null;
  1192. }
  1193. /**
  1194. * Updates the valus to correspond to the values in value
  1195. */
  1196. public void updateTimes() {
  1197. boolean selected = true;
  1198. if (value == null) {
  1199. value = new Date();
  1200. selected = false;
  1201. }
  1202. if (getDateTimeService().isTwelveHourClock()) {
  1203. int h = value.getHours();
  1204. ampm.setSelectedIndex(h < 12 ? 0 : 1);
  1205. h -= ampm.getSelectedIndex() * 12;
  1206. hours.setSelectedIndex(h);
  1207. } else {
  1208. hours.setSelectedIndex(value.getHours());
  1209. }
  1210. if (getResolution() >= VDateField.RESOLUTION_MIN) {
  1211. mins.setSelectedIndex(value.getMinutes());
  1212. }
  1213. if (getResolution() >= VDateField.RESOLUTION_SEC) {
  1214. sec.setSelectedIndex(value.getSeconds());
  1215. }
  1216. if (getDateTimeService().isTwelveHourClock()) {
  1217. ampm.setSelectedIndex(value.getHours() < 12 ? 0 : 1);
  1218. }
  1219. hours.setEnabled(isEnabled());
  1220. if (mins != null) {
  1221. mins.setEnabled(isEnabled());
  1222. }
  1223. if (sec != null) {
  1224. sec.setEnabled(isEnabled());
  1225. }
  1226. if (ampm != null) {
  1227. ampm.setEnabled(isEnabled());
  1228. }
  1229. }
  1230. private int getMilliseconds() {
  1231. return DateTimeService.getMilliseconds(value);
  1232. }
  1233. private DateTimeService getDateTimeService() {
  1234. if (dateTimeService == null) {
  1235. dateTimeService = new DateTimeService();
  1236. }
  1237. return dateTimeService;
  1238. }
  1239. /*
  1240. * (non-Javadoc) VT
  1241. *
  1242. * @see
  1243. * com.google.gwt.event.dom.client.ChangeHandler#onChange(com.google.gwt
  1244. * .event.dom.client.ChangeEvent)
  1245. */
  1246. public void onChange(ChangeEvent event) {
  1247. /*
  1248. * Value from dropdowns gets always set for the value. Like year and
  1249. * month when resolution is month or year.
  1250. */
  1251. if (event.getSource() == hours) {
  1252. int h = hours.getSelectedIndex();
  1253. if (getDateTimeService().isTwelveHourClock()) {
  1254. h = h + ampm.getSelectedIndex() * 12;
  1255. }
  1256. value.setHours(h);
  1257. if (timeChangeListener != null) {
  1258. timeChangeListener.changed(h, value.getMinutes(),
  1259. value.getSeconds(),
  1260. DateTimeService.getMilliseconds(value));
  1261. }
  1262. event.preventDefault();
  1263. event.stopPropagation();
  1264. } else if (event.getSource() == mins) {
  1265. final int m = mins.getSelectedIndex();
  1266. value.setMinutes(m);
  1267. if (timeChangeListener != null) {
  1268. timeChangeListener.changed(value.getHours(), m,
  1269. value.getSeconds(),
  1270. DateTimeService.getMilliseconds(value));
  1271. }
  1272. event.preventDefault();
  1273. event.stopPropagation();
  1274. } else if (event.getSource() == sec) {
  1275. final int s = sec.getSelectedIndex();
  1276. value.setSeconds(s);
  1277. if (timeChangeListener != null) {
  1278. timeChangeListener.changed(value.getHours(),
  1279. value.getMinutes(), s,
  1280. DateTimeService.getMilliseconds(value));
  1281. }
  1282. event.preventDefault();
  1283. event.stopPropagation();
  1284. } else if (event.getSource() == ampm) {
  1285. final int h = hours.getSelectedIndex()
  1286. + (ampm.getSelectedIndex() * 12);
  1287. value.setHours(h);
  1288. if (timeChangeListener != null) {
  1289. timeChangeListener.changed(h, value.getMinutes(),
  1290. value.getSeconds(),
  1291. DateTimeService.getMilliseconds(value));
  1292. }
  1293. event.preventDefault();
  1294. event.stopPropagation();
  1295. }
  1296. }
  1297. }
  1298. private class Day extends InlineHTML {
  1299. private static final String BASECLASS = VDateField.CLASSNAME
  1300. + "-calendarpanel-day";
  1301. private final int day;
  1302. Day(int dayOfMonth) {
  1303. super("" + dayOfMonth);
  1304. setStyleName(BASECLASS);
  1305. day = dayOfMonth;
  1306. addClickHandler(dayClickHandler);
  1307. }
  1308. public int getDay() {
  1309. return day;
  1310. }
  1311. }
  1312. public Date getDate() {
  1313. return value;
  1314. }
  1315. /**
  1316. * If true should be returned if the panel will not be used after this
  1317. * event.
  1318. *
  1319. * @param event
  1320. * @return
  1321. */
  1322. protected boolean onTabOut(DomEvent<?> event) {
  1323. if (focusOutListener != null) {
  1324. return focusOutListener.onFocusOut(event);
  1325. }
  1326. return false;
  1327. }
  1328. /**
  1329. * A focus out listener is triggered when the panel loosed focus. This can
  1330. * happen either after a user clicks outside the panel or tabs out.
  1331. *
  1332. * @param listener
  1333. * The listener to trigger
  1334. */
  1335. public void setFocusOutListener(FocusOutListener listener) {
  1336. focusOutListener = listener;
  1337. }
  1338. /**
  1339. * The submit listener is called when the user selects a value from the
  1340. * calender either by clicking the day or selects it by keyboard.
  1341. *
  1342. * @param submitListener
  1343. * The listener to trigger
  1344. */
  1345. public void setSubmitListener(SubmitListener submitListener) {
  1346. this.submitListener = submitListener;
  1347. }
  1348. /**
  1349. * The given FocusChangeListener is notified when the focused date changes
  1350. * by user either clicking on a new date or by using the keyboard.
  1351. *
  1352. * @param listener
  1353. * The FocusChangeListener to be notified
  1354. */
  1355. public void setFocusChangeListener(FocusChangeListener listener) {
  1356. focusChangeListener = listener;
  1357. }
  1358. /**
  1359. * The time change listener is triggered when the user changes the time.
  1360. *
  1361. * @param listener
  1362. */
  1363. public void setTimeChangeListener(TimeChangeListener listener) {
  1364. timeChangeListener = listener;
  1365. }
  1366. /**
  1367. * Returns the submit listener that listens to selection made from the panel
  1368. *
  1369. * @return The listener or NULL if no listener has been set
  1370. */
  1371. public SubmitListener getSubmitListener() {
  1372. return submitListener;
  1373. }
  1374. /*
  1375. * (non-Javadoc)
  1376. *
  1377. * @see
  1378. * com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event
  1379. * .dom.client.BlurEvent)
  1380. */
  1381. public void onBlur(final BlurEvent event) {
  1382. if (event.getSource() instanceof VCalendarPanel) {
  1383. hasFocus = false;
  1384. focusDay(-1);
  1385. }
  1386. }
  1387. /*
  1388. * (non-Javadoc)
  1389. *
  1390. * @see
  1391. * com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event
  1392. * .dom.client.FocusEvent)
  1393. */
  1394. public void onFocus(FocusEvent event) {
  1395. if (event.getSource() instanceof VCalendarPanel) {
  1396. hasFocus = true;
  1397. // Focuses the current day if the calendar shows the days
  1398. if (focusedDay != null) {
  1399. focusDay(focusedDay.getDay());
  1400. }
  1401. }
  1402. }
  1403. private static final String SUBPART_NEXT_MONTH = "nextmon";
  1404. private static final String SUBPART_PREV_MONTH = "prevmon";
  1405. private static final String SUBPART_NEXT_YEAR = "nexty";
  1406. private static final String SUBPART_PREV_YEAR = "prevy";
  1407. private static final String SUBPART_HOUR_SELECT = "h";
  1408. private static final String SUBPART_MINUTE_SELECT = "m";
  1409. private static final String SUBPART_SECS_SELECT = "s";
  1410. private static final String SUBPART_MSECS_SELECT = "ms";
  1411. private static final String SUBPART_AMPM_SELECT = "ampm";
  1412. private static final String SUBPART_DAY = "day";
  1413. private static final String SUBPART_MONTH_YEAR_HEADER = "header";
  1414. public String getSubPartName(Element subElement) {
  1415. if (contains(nextMonth, subElement)) {
  1416. return SUBPART_NEXT_MONTH;
  1417. } else if (contains(prevMonth, subElement)) {
  1418. return SUBPART_PREV_MONTH;
  1419. } else if (contains(nextYear, subElement)) {
  1420. return SUBPART_NEXT_YEAR;
  1421. } else if (contains(prevYear, subElement)) {
  1422. return SUBPART_PREV_YEAR;
  1423. } else if (contains(days, subElement)) {
  1424. // Day, find out which dayOfMonth and use that as the identifier
  1425. Day day = Util.findWidget(subElement, Day.class);
  1426. if (day != null) {
  1427. return SUBPART_DAY + day.getDay();
  1428. }
  1429. } else if (time != null) {
  1430. if (contains(time.hours, subElement)) {
  1431. return SUBPART_HOUR_SELECT;
  1432. } else if (contains(time.mins, subElement)) {
  1433. return SUBPART_MINUTE_SELECT;
  1434. } else if (contains(time.sec, subElement)) {
  1435. return SUBPART_SECS_SELECT;
  1436. } else if (contains(time.ampm, subElement)) {
  1437. return SUBPART_AMPM_SELECT;
  1438. }
  1439. } else if (getCellFormatter().getElement(0, 2).isOrHasChild(subElement)) {
  1440. return SUBPART_MONTH_YEAR_HEADER;
  1441. }
  1442. return null;
  1443. }
  1444. /**
  1445. * Checks if subElement is inside the widget DOM hierarchy.
  1446. *
  1447. * @param w
  1448. * @param subElement
  1449. * @return true if {@code w} is a parent of subElement, false otherwise.
  1450. */
  1451. private boolean contains(Widget w, Element subElement) {
  1452. if (w == null || w.getElement() == null) {
  1453. return false;
  1454. }
  1455. return w.getElement().isOrHasChild(subElement);
  1456. }
  1457. public Element getSubPartElement(String subPart) {
  1458. if (SUBPART_NEXT_MONTH.equals(subPart)) {
  1459. return nextMonth.getElement();
  1460. }
  1461. if (SUBPART_PREV_MONTH.equals(subPart)) {
  1462. return prevMonth.getElement();
  1463. }
  1464. if (SUBPART_NEXT_YEAR.equals(subPart)) {
  1465. return nextYear.getElement();
  1466. }
  1467. if (SUBPART_PREV_YEAR.equals(subPart)) {
  1468. return prevYear.getElement();
  1469. }
  1470. if (SUBPART_HOUR_SELECT.equals(subPart)) {
  1471. return time.hours.getElement();
  1472. }
  1473. if (SUBPART_MINUTE_SELECT.equals(subPart)) {
  1474. return time.mins.getElement();
  1475. }
  1476. if (SUBPART_SECS_SELECT.equals(subPart)) {
  1477. return time.sec.getElement();
  1478. }
  1479. if (SUBPART_AMPM_SELECT.equals(subPart)) {
  1480. return time.ampm.getElement();
  1481. }
  1482. if (subPart.startsWith(SUBPART_DAY)) {
  1483. int dayOfMonth = Integer.parseInt(subPart.substring(SUBPART_DAY
  1484. .length()));
  1485. Iterator<Widget> iter = days.iterator();
  1486. while (iter.hasNext()) {
  1487. Widget w = iter.next();
  1488. if (w instanceof Day) {
  1489. Day day = (Day) w;
  1490. if (day.getDay() == dayOfMonth) {
  1491. return day.getElement();
  1492. }
  1493. }
  1494. }
  1495. }
  1496. if (SUBPART_MONTH_YEAR_HEADER.equals(subPart)) {
  1497. return (Element) getCellFormatter().getElement(0, 2).getChild(0);
  1498. }
  1499. return null;
  1500. }
  1501. @Override
  1502. protected void onDetach() {
  1503. super.onDetach();
  1504. if (mouseTimer != null) {
  1505. mouseTimer.cancel();
  1506. }
  1507. }
  1508. }