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.

VPopupCalendar.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import java.util.Date;
  18. import com.google.gwt.aria.client.Id;
  19. import com.google.gwt.aria.client.LiveValue;
  20. import com.google.gwt.aria.client.Roles;
  21. import com.google.gwt.core.client.GWT;
  22. import com.google.gwt.dom.client.Element;
  23. import com.google.gwt.event.dom.client.ClickEvent;
  24. import com.google.gwt.event.dom.client.ClickHandler;
  25. import com.google.gwt.event.dom.client.DomEvent;
  26. import com.google.gwt.event.dom.client.KeyCodes;
  27. import com.google.gwt.event.dom.client.MouseOutEvent;
  28. import com.google.gwt.event.dom.client.MouseOutHandler;
  29. import com.google.gwt.event.dom.client.MouseOverEvent;
  30. import com.google.gwt.event.dom.client.MouseOverHandler;
  31. import com.google.gwt.event.logical.shared.CloseEvent;
  32. import com.google.gwt.event.logical.shared.CloseHandler;
  33. import com.google.gwt.i18n.client.DateTimeFormat;
  34. import com.google.gwt.user.client.DOM;
  35. import com.google.gwt.user.client.Event;
  36. import com.google.gwt.user.client.Timer;
  37. import com.google.gwt.user.client.Window;
  38. import com.google.gwt.user.client.ui.Button;
  39. import com.google.gwt.user.client.ui.FlowPanel;
  40. import com.google.gwt.user.client.ui.Label;
  41. import com.google.gwt.user.client.ui.PopupPanel;
  42. import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
  43. import com.google.gwt.user.client.ui.RootPanel;
  44. import com.google.gwt.user.client.ui.Widget;
  45. import com.vaadin.client.BrowserInfo;
  46. import com.vaadin.client.ComputedStyle;
  47. import com.vaadin.client.VConsole;
  48. import com.vaadin.client.ui.VCalendarPanel.FocusOutListener;
  49. import com.vaadin.client.ui.VCalendarPanel.SubmitListener;
  50. import com.vaadin.client.ui.aria.AriaHelper;
  51. import com.vaadin.shared.ui.datefield.PopupDateFieldState;
  52. import com.vaadin.shared.ui.datefield.Resolution;
  53. /**
  54. * Represents a date selection component with a text field and a popup date
  55. * selector.
  56. *
  57. * <b>Note:</b> To change the keyboard assignments used in the popup dialog you
  58. * should extend <code>com.vaadin.client.ui.VCalendarPanel</code> and then pass
  59. * set it by calling the <code>setCalendarPanel(VCalendarPanel panel)</code>
  60. * method.
  61. *
  62. */
  63. public class VPopupCalendar extends VTextualDate implements Field,
  64. ClickHandler, CloseHandler<PopupPanel>, SubPartAware {
  65. /** For internal use only. May be removed or replaced in the future. */
  66. public final Button calendarToggle = new Button();
  67. /** For internal use only. May be removed or replaced in the future. */
  68. public VCalendarPanel calendar;
  69. /** For internal use only. May be removed or replaced in the future. */
  70. public final VOverlay popup;
  71. /** For internal use only. May be removed or replaced in the future. */
  72. public boolean parsable = true;
  73. private boolean open = false;
  74. /*
  75. * #14857: If calendarToggle button is clicked when calendar popup is
  76. * already open we should prevent calling openCalendarPanel() in onClick,
  77. * since we don't want to reopen it again right after it closes.
  78. */
  79. private boolean preventOpenPopupCalendar = false;
  80. private boolean cursorOverCalendarToggleButton = false;
  81. private boolean toggleButtonClosesWithGuarantee = false;
  82. private boolean textFieldEnabled = true;
  83. private String captionId;
  84. private Label selectedDate;
  85. private Element descriptionForAssisitveDevicesElement;
  86. public VPopupCalendar() {
  87. super();
  88. calendarToggle.setText("");
  89. calendarToggle.addClickHandler(this);
  90. calendarToggle.addDomHandler(new MouseOverHandler() {
  91. @Override
  92. public void onMouseOver(MouseOverEvent event) {
  93. cursorOverCalendarToggleButton = true;
  94. }
  95. }, MouseOverEvent.getType());
  96. calendarToggle.addDomHandler(new MouseOutHandler() {
  97. @Override
  98. public void onMouseOut(MouseOutEvent event) {
  99. cursorOverCalendarToggleButton = false;
  100. }
  101. }, MouseOutEvent.getType());
  102. // -2 instead of -1 to avoid FocusWidget.onAttach to reset it
  103. calendarToggle.getElement().setTabIndex(-2);
  104. Roles.getButtonRole().set(calendarToggle.getElement());
  105. Roles.getButtonRole().setAriaHiddenState(calendarToggle.getElement(),
  106. true);
  107. add(calendarToggle);
  108. // Description of the usage of the widget for assisitve device users
  109. descriptionForAssisitveDevicesElement = DOM.createDiv();
  110. descriptionForAssisitveDevicesElement
  111. .setInnerText(PopupDateFieldState.DESCRIPTION_FOR_ASSISTIVE_DEVICES);
  112. AriaHelper.ensureHasId(descriptionForAssisitveDevicesElement);
  113. Roles.getTextboxRole().setAriaDescribedbyProperty(text.getElement(),
  114. Id.of(descriptionForAssisitveDevicesElement));
  115. AriaHelper.setVisibleForAssistiveDevicesOnly(
  116. descriptionForAssisitveDevicesElement, true);
  117. calendar = GWT.create(VCalendarPanel.class);
  118. calendar.setParentField(this);
  119. calendar.setFocusOutListener(new FocusOutListener() {
  120. @Override
  121. public boolean onFocusOut(DomEvent<?> event) {
  122. event.preventDefault();
  123. closeCalendarPanel();
  124. return true;
  125. }
  126. });
  127. // FIXME: Problem is, that the element with the provided id does not
  128. // exist yet in html. This is the same problem as with the context menu.
  129. // Apply here the same fix (#11795)
  130. Roles.getTextboxRole().setAriaControlsProperty(text.getElement(),
  131. Id.of(calendar.getElement()));
  132. Roles.getButtonRole().setAriaControlsProperty(
  133. calendarToggle.getElement(), Id.of(calendar.getElement()));
  134. calendar.setSubmitListener(new SubmitListener() {
  135. @Override
  136. public void onSubmit() {
  137. // Update internal value and send valuechange event if immediate
  138. updateValue(calendar.getDate());
  139. // Update text field (a must when not immediate).
  140. buildDate(true);
  141. closeCalendarPanel();
  142. }
  143. @Override
  144. public void onCancel() {
  145. closeCalendarPanel();
  146. }
  147. });
  148. popup = new VOverlay(true, false, true);
  149. popup.setOwner(this);
  150. FlowPanel wrapper = new FlowPanel();
  151. selectedDate = new Label();
  152. selectedDate.setStyleName(getStylePrimaryName() + "-selecteddate");
  153. AriaHelper.setVisibleForAssistiveDevicesOnly(selectedDate.getElement(),
  154. true);
  155. Roles.getTextboxRole().setAriaLiveProperty(selectedDate.getElement(),
  156. LiveValue.ASSERTIVE);
  157. Roles.getTextboxRole().setAriaAtomicProperty(selectedDate.getElement(),
  158. true);
  159. wrapper.add(selectedDate);
  160. wrapper.add(calendar);
  161. popup.setWidget(wrapper);
  162. popup.addCloseHandler(this);
  163. DOM.setElementProperty(calendar.getElement(), "id",
  164. "PID_VAADIN_POPUPCAL");
  165. sinkEvents(Event.ONKEYDOWN);
  166. updateStyleNames();
  167. }
  168. @Override
  169. protected void onAttach() {
  170. super.onAttach();
  171. DOM.appendChild(RootPanel.get().getElement(),
  172. descriptionForAssisitveDevicesElement);
  173. }
  174. @Override
  175. protected void onDetach() {
  176. super.onDetach();
  177. descriptionForAssisitveDevicesElement.removeFromParent();
  178. }
  179. @SuppressWarnings("deprecation")
  180. public void updateValue(Date newDate) {
  181. Date currentDate = getCurrentDate();
  182. if (currentDate == null || newDate.getTime() != currentDate.getTime()) {
  183. setCurrentDate((Date) newDate.clone());
  184. getClient().updateVariable(getId(), "year",
  185. newDate.getYear() + 1900, false);
  186. if (getCurrentResolution().getCalendarField() > Resolution.YEAR
  187. .getCalendarField()) {
  188. getClient().updateVariable(getId(), "month",
  189. newDate.getMonth() + 1, false);
  190. if (getCurrentResolution().getCalendarField() > Resolution.MONTH
  191. .getCalendarField()) {
  192. getClient().updateVariable(getId(), "day",
  193. newDate.getDate(), false);
  194. if (getCurrentResolution().getCalendarField() > Resolution.DAY
  195. .getCalendarField()) {
  196. getClient().updateVariable(getId(), "hour",
  197. newDate.getHours(), false);
  198. if (getCurrentResolution().getCalendarField() > Resolution.HOUR
  199. .getCalendarField()) {
  200. getClient().updateVariable(getId(), "min",
  201. newDate.getMinutes(), false);
  202. if (getCurrentResolution().getCalendarField() > Resolution.MINUTE
  203. .getCalendarField()) {
  204. getClient().updateVariable(getId(), "sec",
  205. newDate.getSeconds(), false);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. /**
  214. * Checks whether the text field is enabled.
  215. *
  216. * @see VPopupCalendar#setTextFieldEnabled(boolean)
  217. * @return The current state of the text field.
  218. */
  219. public boolean isTextFieldEnabled() {
  220. return textFieldEnabled;
  221. }
  222. /**
  223. * Sets the state of the text field of this component. By default the text
  224. * field is enabled. Disabling it causes only the button for date selection
  225. * to be active, thus preventing the user from entering invalid dates. See
  226. * {@link http://dev.vaadin.com/ticket/6790}.
  227. *
  228. * @param state
  229. */
  230. public void setTextFieldEnabled(boolean textFieldEnabled) {
  231. this.textFieldEnabled = textFieldEnabled;
  232. updateTextFieldEnabled();
  233. }
  234. protected void updateTextFieldEnabled() {
  235. boolean reallyEnabled = isEnabled() && isTextFieldEnabled();
  236. // IE has a non input disabled themeing that can not be overridden so we
  237. // must fake the functionality using readonly and unselectable
  238. if (BrowserInfo.get().isIE()) {
  239. if (!reallyEnabled) {
  240. text.getElement().setAttribute("unselectable", "on");
  241. text.getElement().setAttribute("readonly", "");
  242. text.setTabIndex(-2);
  243. } else if (reallyEnabled
  244. && text.getElement().hasAttribute("unselectable")) {
  245. text.getElement().removeAttribute("unselectable");
  246. text.getElement().removeAttribute("readonly");
  247. text.setTabIndex(0);
  248. }
  249. } else {
  250. text.setEnabled(reallyEnabled);
  251. }
  252. if (reallyEnabled) {
  253. calendarToggle.setTabIndex(-1);
  254. Roles.getButtonRole().setAriaHiddenState(
  255. calendarToggle.getElement(), true);
  256. } else {
  257. calendarToggle.setTabIndex(0);
  258. Roles.getButtonRole().setAriaHiddenState(
  259. calendarToggle.getElement(), false);
  260. }
  261. handleAriaAttributes();
  262. }
  263. /**
  264. * Set correct tab index for disabled text field in IE as the value set in
  265. * setTextFieldEnabled(...) gets overridden in
  266. * TextualDateConnection.updateFromUIDL(...)
  267. *
  268. * @since 7.3.1
  269. */
  270. public void setTextFieldTabIndex() {
  271. if (BrowserInfo.get().isIE() && !textFieldEnabled) {
  272. // index needs to be -2 because FocusWidget updates -1 to 0 onAttach
  273. text.setTabIndex(-2);
  274. }
  275. }
  276. @Override
  277. public void bindAriaCaption(
  278. com.google.gwt.user.client.Element captionElement) {
  279. if (captionElement == null) {
  280. captionId = null;
  281. } else {
  282. captionId = captionElement.getId();
  283. }
  284. if (isTextFieldEnabled()) {
  285. super.bindAriaCaption(captionElement);
  286. } else {
  287. AriaHelper.bindCaption(calendarToggle, captionElement);
  288. }
  289. handleAriaAttributes();
  290. }
  291. private void handleAriaAttributes() {
  292. Widget removeFromWidget;
  293. Widget setForWidget;
  294. if (isTextFieldEnabled()) {
  295. setForWidget = text;
  296. removeFromWidget = calendarToggle;
  297. } else {
  298. setForWidget = calendarToggle;
  299. removeFromWidget = text;
  300. }
  301. Roles.getFormRole().removeAriaLabelledbyProperty(
  302. removeFromWidget.getElement());
  303. if (captionId == null) {
  304. Roles.getFormRole().removeAriaLabelledbyProperty(
  305. setForWidget.getElement());
  306. } else {
  307. Roles.getFormRole().setAriaLabelledbyProperty(
  308. setForWidget.getElement(), Id.of(captionId));
  309. }
  310. }
  311. /*
  312. * (non-Javadoc)
  313. *
  314. * @see
  315. * com.google.gwt.user.client.ui.UIObject#setStyleName(java.lang.String)
  316. */
  317. @Override
  318. public void setStyleName(String style) {
  319. super.setStyleName(style);
  320. updateStyleNames();
  321. }
  322. @Override
  323. public void setStylePrimaryName(String style) {
  324. removeStyleName(getStylePrimaryName() + "-popupcalendar");
  325. super.setStylePrimaryName(style);
  326. updateStyleNames();
  327. }
  328. @Override
  329. protected void updateStyleNames() {
  330. super.updateStyleNames();
  331. if (getStylePrimaryName() != null && calendarToggle != null) {
  332. addStyleName(getStylePrimaryName() + "-popupcalendar");
  333. calendarToggle.setStyleName(getStylePrimaryName() + "-button");
  334. popup.setStyleName(getStylePrimaryName() + "-popup");
  335. calendar.setStyleName(getStylePrimaryName() + "-calendarpanel");
  336. }
  337. }
  338. /**
  339. * Opens the calendar panel popup
  340. */
  341. public void openCalendarPanel() {
  342. if (!open && !readonly && isEnabled()) {
  343. open = true;
  344. if (getCurrentDate() != null) {
  345. calendar.setDate((Date) getCurrentDate().clone());
  346. } else {
  347. calendar.setDate(new Date());
  348. }
  349. // clear previous values
  350. popup.setWidth("");
  351. popup.setHeight("");
  352. popup.setPopupPositionAndShow(new PopupPositionCallback());
  353. } else {
  354. VConsole.error("Cannot reopen popup, it is already open!");
  355. }
  356. }
  357. /*
  358. * (non-Javadoc)
  359. *
  360. * @see
  361. * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
  362. * .dom.client.ClickEvent)
  363. */
  364. @Override
  365. public void onClick(ClickEvent event) {
  366. if (event.getSource() == calendarToggle && isEnabled()) {
  367. if (!preventOpenPopupCalendar) {
  368. openCalendarPanel();
  369. }
  370. preventOpenPopupCalendar = false;
  371. }
  372. }
  373. /*
  374. * (non-Javadoc)
  375. *
  376. * @see
  377. * com.google.gwt.event.logical.shared.CloseHandler#onClose(com.google.gwt
  378. * .event.logical.shared.CloseEvent)
  379. */
  380. @Override
  381. public void onClose(CloseEvent<PopupPanel> event) {
  382. if (event.getSource() == popup) {
  383. buildDate();
  384. if (!BrowserInfo.get().isTouchDevice() && textFieldEnabled) {
  385. /*
  386. * Move focus to textbox, unless on touch device (avoids opening
  387. * virtual keyboard) or if textField is disabled.
  388. */
  389. focus();
  390. }
  391. open = false;
  392. if (cursorOverCalendarToggleButton
  393. && !toggleButtonClosesWithGuarantee) {
  394. preventOpenPopupCalendar = true;
  395. }
  396. toggleButtonClosesWithGuarantee = false;
  397. }
  398. }
  399. /**
  400. * Sets focus to Calendar panel.
  401. *
  402. * @param focus
  403. */
  404. public void setFocus(boolean focus) {
  405. calendar.setFocus(focus);
  406. }
  407. @Override
  408. public void setEnabled(boolean enabled) {
  409. super.setEnabled(enabled);
  410. updateTextFieldEnabled();
  411. calendarToggle.setEnabled(enabled);
  412. Roles.getButtonRole().setAriaDisabledState(calendarToggle.getElement(),
  413. !enabled);
  414. }
  415. /**
  416. * Sets the content of a special field for assistive devices, so that they
  417. * can recognize the change and inform the user (reading out in case of
  418. * screen reader)
  419. *
  420. * @param selectedDate
  421. * Date that is currently selected
  422. */
  423. public void setFocusedDate(Date selectedDate) {
  424. this.selectedDate.setText(DateTimeFormat.getFormat("dd, MMMM, yyyy")
  425. .format(selectedDate));
  426. }
  427. /**
  428. * For internal use only. May be removed or replaced in the future.
  429. *
  430. * @see com.vaadin.client.ui.VTextualDate#buildDate()
  431. */
  432. @Override
  433. public void buildDate() {
  434. // Save previous value
  435. String previousValue = getText();
  436. super.buildDate();
  437. // Restore previous value if the input could not be parsed
  438. if (!parsable) {
  439. setText(previousValue);
  440. }
  441. updateTextFieldEnabled();
  442. }
  443. /**
  444. * Update the text field contents from the date. See {@link #buildDate()}.
  445. *
  446. * @param forceValid
  447. * true to force the text field to be updated, false to only
  448. * update if the parsable flag is true.
  449. */
  450. protected void buildDate(boolean forceValid) {
  451. if (forceValid) {
  452. parsable = true;
  453. }
  454. buildDate();
  455. }
  456. /*
  457. * (non-Javadoc)
  458. *
  459. * @see com.vaadin.client.ui.VDateField#onBrowserEvent(com.google
  460. * .gwt.user.client.Event)
  461. */
  462. @Override
  463. public void onBrowserEvent(com.google.gwt.user.client.Event event) {
  464. super.onBrowserEvent(event);
  465. if (DOM.eventGetType(event) == Event.ONKEYDOWN
  466. && event.getKeyCode() == getOpenCalenderPanelKey()) {
  467. openCalendarPanel();
  468. event.preventDefault();
  469. }
  470. }
  471. /**
  472. * Get the key code that opens the calendar panel. By default it is the down
  473. * key but you can override this to be whatever you like
  474. *
  475. * @return
  476. */
  477. protected int getOpenCalenderPanelKey() {
  478. return KeyCodes.KEY_DOWN;
  479. }
  480. /**
  481. * Closes the open popup panel
  482. */
  483. public void closeCalendarPanel() {
  484. if (open) {
  485. toggleButtonClosesWithGuarantee = true;
  486. popup.hide(true);
  487. }
  488. }
  489. private final String CALENDAR_TOGGLE_ID = "popupButton";
  490. @Override
  491. public com.google.gwt.user.client.Element getSubPartElement(String subPart) {
  492. if (subPart.equals(CALENDAR_TOGGLE_ID)) {
  493. return calendarToggle.getElement();
  494. }
  495. return super.getSubPartElement(subPart);
  496. }
  497. @Override
  498. public String getSubPartName(com.google.gwt.user.client.Element subElement) {
  499. if (calendarToggle.getElement().isOrHasChild(subElement)) {
  500. return CALENDAR_TOGGLE_ID;
  501. }
  502. return super.getSubPartName(subElement);
  503. }
  504. /**
  505. * Set a description that explains the usage of the Widget for users of
  506. * assistive devices.
  507. *
  508. * @param descriptionForAssistiveDevices
  509. * String with the description
  510. */
  511. public void setDescriptionForAssistiveDevices(
  512. String descriptionForAssistiveDevices) {
  513. descriptionForAssisitveDevicesElement
  514. .setInnerText(descriptionForAssistiveDevices);
  515. }
  516. /**
  517. * Get the description that explains the usage of the Widget for users of
  518. * assistive devices.
  519. *
  520. * @return String with the description
  521. */
  522. public String getDescriptionForAssistiveDevices() {
  523. return descriptionForAssisitveDevicesElement.getInnerText();
  524. }
  525. /**
  526. * Sets the start range for this component. The start range is inclusive,
  527. * and it depends on the current resolution, what is considered inside the
  528. * range.
  529. *
  530. * @param startDate
  531. * - the allowed range's start date
  532. */
  533. public void setRangeStart(Date rangeStart) {
  534. calendar.setRangeStart(rangeStart);
  535. }
  536. /**
  537. * Sets the end range for this component. The end range is inclusive, and it
  538. * depends on the current resolution, what is considered inside the range.
  539. *
  540. * @param endDate
  541. * - the allowed range's end date
  542. */
  543. public void setRangeEnd(Date rangeEnd) {
  544. calendar.setRangeEnd(rangeEnd);
  545. }
  546. private class PopupPositionCallback implements PositionCallback {
  547. @Override
  548. public void setPosition(int offsetWidth, int offsetHeight) {
  549. final int width = offsetWidth;
  550. final int height = offsetHeight;
  551. final int browserWindowWidth = Window.getClientWidth()
  552. + Window.getScrollLeft();
  553. final int windowHeight = Window.getClientHeight()
  554. + Window.getScrollTop();
  555. int left = calendarToggle.getAbsoluteLeft();
  556. // Add a little extra space to the right to avoid
  557. // problems with IE7 scrollbars and to make it look
  558. // nicer.
  559. int extraSpace = 30;
  560. boolean overflow = left + width + extraSpace > browserWindowWidth;
  561. if (overflow) {
  562. // Part of the popup is outside the browser window
  563. // (to the right)
  564. left = browserWindowWidth - width - extraSpace;
  565. }
  566. int top = calendarToggle.getAbsoluteTop();
  567. int extraHeight = 2;
  568. boolean verticallyRepositioned = false;
  569. ComputedStyle style = new ComputedStyle(popup.getElement());
  570. int[] margins = style.getMargin();
  571. int desiredPopupBottom = top + height
  572. + calendarToggle.getOffsetHeight() + margins[0]
  573. + margins[2];
  574. if (desiredPopupBottom > windowHeight) {
  575. int updatedLeft = left;
  576. left = getLeftPosition(left, width, style, overflow);
  577. // if position has not been changed then it means there is no
  578. // space to make popup fully visible
  579. if (updatedLeft == left) {
  580. // let's try to show popup on the top of the field
  581. int updatedTop = top - extraHeight - height - margins[0]
  582. - margins[2];
  583. verticallyRepositioned = updatedTop >= 0;
  584. if (verticallyRepositioned) {
  585. top = updatedTop;
  586. }
  587. }
  588. // Part of the popup is outside the browser window
  589. // (below)
  590. if (!verticallyRepositioned) {
  591. verticallyRepositioned = true;
  592. top = windowHeight - height - extraSpace + extraHeight;
  593. }
  594. }
  595. if (verticallyRepositioned) {
  596. popup.setPopupPosition(left, top);
  597. } else {
  598. popup.setPopupPosition(left,
  599. top + calendarToggle.getOffsetHeight() + extraHeight);
  600. }
  601. doSetFocus();
  602. }
  603. private int getLeftPosition(int left, int width, ComputedStyle style,
  604. boolean overflow) {
  605. if (positionRightSide()) {
  606. // Show to the right of the popup button unless we
  607. // are in the lower right corner of the screen
  608. if (overflow) {
  609. return left;
  610. } else {
  611. return left + calendarToggle.getOffsetWidth();
  612. }
  613. } else {
  614. int[] margins = style.getMargin();
  615. int desiredLeftPosition = calendarToggle.getAbsoluteLeft()
  616. - width - margins[1] - margins[3];
  617. if (desiredLeftPosition >= 0) {
  618. return desiredLeftPosition;
  619. } else {
  620. return left;
  621. }
  622. }
  623. }
  624. private boolean positionRightSide() {
  625. int buttonRightSide = calendarToggle.getAbsoluteLeft()
  626. + calendarToggle.getOffsetWidth();
  627. int textRightSide = text.getAbsoluteLeft() + text.getOffsetWidth();
  628. return buttonRightSide >= textRightSide;
  629. }
  630. private void doSetFocus() {
  631. /*
  632. * We have to wait a while before focusing since the popup needs to
  633. * be opened before we can focus
  634. */
  635. Timer focusTimer = new Timer() {
  636. @Override
  637. public void run() {
  638. setFocus(true);
  639. }
  640. };
  641. focusTimer.schedule(100);
  642. }
  643. }
  644. }