Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VAbstractPopupCalendar.java 24KB

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