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.

VTooltip.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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;
  17. import com.google.gwt.aria.client.LiveValue;
  18. import com.google.gwt.aria.client.RelevantValue;
  19. import com.google.gwt.aria.client.Roles;
  20. import com.google.gwt.core.shared.GWT;
  21. import com.google.gwt.dom.client.Document;
  22. import com.google.gwt.dom.client.Element;
  23. import com.google.gwt.dom.client.PreElement;
  24. import com.google.gwt.dom.client.Style.Display;
  25. import com.google.gwt.event.dom.client.BlurEvent;
  26. import com.google.gwt.event.dom.client.BlurHandler;
  27. import com.google.gwt.event.dom.client.DomEvent;
  28. import com.google.gwt.event.dom.client.FocusEvent;
  29. import com.google.gwt.event.dom.client.FocusHandler;
  30. import com.google.gwt.event.dom.client.KeyDownEvent;
  31. import com.google.gwt.event.dom.client.KeyDownHandler;
  32. import com.google.gwt.event.dom.client.MouseDownEvent;
  33. import com.google.gwt.event.dom.client.MouseDownHandler;
  34. import com.google.gwt.event.dom.client.MouseMoveEvent;
  35. import com.google.gwt.event.dom.client.MouseMoveHandler;
  36. import com.google.gwt.event.dom.client.MouseOutEvent;
  37. import com.google.gwt.event.dom.client.MouseOutHandler;
  38. import com.google.gwt.user.client.DOM;
  39. import com.google.gwt.user.client.Event;
  40. import com.google.gwt.user.client.Timer;
  41. import com.google.gwt.user.client.Window;
  42. import com.google.gwt.user.client.ui.FlowPanel;
  43. import com.google.gwt.user.client.ui.HTML;
  44. import com.google.gwt.user.client.ui.RootPanel;
  45. import com.google.gwt.user.client.ui.Widget;
  46. import com.vaadin.client.ui.VOverlay;
  47. /**
  48. * TODO open for extension
  49. */
  50. public class VTooltip extends VOverlay {
  51. private static final String CLASSNAME = "v-tooltip";
  52. private static final int MARGIN = 4;
  53. public static final int TOOLTIP_EVENTS = Event.ONKEYDOWN | Event.ONMOUSEOVER
  54. | Event.ONMOUSEOUT | Event.ONMOUSEMOVE | Event.ONCLICK;
  55. VErrorMessage em = new VErrorMessage();
  56. HTML description = GWT.create(HTML.class);
  57. private TooltipInfo currentTooltipInfo = new TooltipInfo(" ");
  58. private boolean closing = false;
  59. private boolean opening = false;
  60. // Open next tooltip faster. Disabled after 2 sec of showTooltip-silence.
  61. private boolean justClosed = false;
  62. private String uniqueId = DOM.createUniqueId();
  63. private int maxWidth;
  64. // Delays for the tooltip, configurable on the server side
  65. private int openDelay;
  66. private int quickOpenDelay;
  67. private int quickOpenTimeout;
  68. private int closeTimeout;
  69. /**
  70. * Current element hovered
  71. */
  72. private com.google.gwt.dom.client.Element currentElement = null;
  73. /**
  74. * Used to show tooltips; usually used via the singleton in
  75. * {@link ApplicationConnection}. NOTE that #setOwner(Widget)} should be
  76. * called after instantiating.
  77. *
  78. * @see ApplicationConnection#getVTooltip()
  79. */
  80. public VTooltip() {
  81. super(false, false); // no autohide, not modal
  82. setStyleName(CLASSNAME);
  83. FlowPanel layout = new FlowPanel();
  84. setWidget(layout);
  85. layout.add(em);
  86. description.setStyleName(CLASSNAME + "-text");
  87. layout.add(description);
  88. // When a tooltip is shown, the content of the tooltip changes. With a
  89. // tooltip being a live-area, this change is notified to a assistive
  90. // device.
  91. Roles.getTooltipRole().set(getElement());
  92. Roles.getTooltipRole().setAriaLiveProperty(getElement(),
  93. LiveValue.ASSERTIVE);
  94. Roles.getTooltipRole().setAriaRelevantProperty(getElement(),
  95. RelevantValue.ADDITIONS);
  96. // Tooltip needs to be on top of other VOverlay elements.
  97. setZIndex(VOverlay.Z_INDEX + 1);
  98. }
  99. /**
  100. * Show the tooltip with the provided info for assistive devices.
  101. *
  102. * @param info
  103. * with the content of the tooltip
  104. */
  105. public void showAssistive(TooltipInfo info) {
  106. updatePosition(null, true);
  107. setTooltipText(info);
  108. showTooltip();
  109. }
  110. /**
  111. * Initialize the tooltip overlay for assistive devices.
  112. *
  113. * @param info
  114. * with the content of the tooltip
  115. * @since 7.2.4
  116. */
  117. public void initializeAssistiveTooltips() {
  118. updatePosition(null, true);
  119. setTooltipText(new TooltipInfo(" "));
  120. showTooltip();
  121. hideTooltip();
  122. description.getParent().getElement().getStyle().clearWidth();
  123. }
  124. private void setTooltipText(TooltipInfo info) {
  125. if (info.getErrorMessage() != null
  126. && !info.getErrorMessage().isEmpty()) {
  127. em.setVisible(true);
  128. em.updateMessage(info.getErrorMessage());
  129. } else {
  130. em.setVisible(false);
  131. }
  132. if (info.getTitle() != null && !info.getTitle().isEmpty()) {
  133. switch (info.getContentMode()) {
  134. case HTML:
  135. description.setHTML(info.getTitle());
  136. break;
  137. case TEXT:
  138. description.setText(info.getTitle());
  139. break;
  140. case PREFORMATTED:
  141. PreElement preElement = Document.get().createPreElement();
  142. preElement.addClassName(CLASSNAME + "-pre");
  143. preElement.setInnerText(info.getTitle());
  144. // clear existing content
  145. description.setHTML("");
  146. // add preformatted text to dom
  147. description.getElement().appendChild(preElement);
  148. break;
  149. default:
  150. break;
  151. }
  152. /*
  153. * Issue #11871: to correctly update the offsetWidth of description
  154. * element we need to clear style width of its parent DIV from old
  155. * value (in some strange cases this width=[tooltip MAX_WIDTH] after
  156. * tooltip text has been already updated to new shortly value:
  157. *
  158. * <div class="popupContent"> <div style="width:500px;"> <div
  159. * class="v-errormessage" aria-hidden="true" style="display: none;">
  160. * <div class="gwt-HTML"> </div> </div> <div
  161. * class="v-tooltip-text">This is a short tooltip</div> </div>
  162. *
  163. * and it leads to error during calculation offsetWidth (it is
  164. * native GWT method getSubPixelOffsetWidth()) of description
  165. * element")
  166. */
  167. description.getParent().getElement().getStyle().clearWidth();
  168. description.getElement().getStyle().clearDisplay();
  169. } else {
  170. description.setHTML("");
  171. description.getElement().getStyle().setDisplay(Display.NONE);
  172. }
  173. currentTooltipInfo = info;
  174. }
  175. /**
  176. * Show a popup containing the currentTooltipInfo
  177. *
  178. */
  179. private void showTooltip() {
  180. if (currentTooltipInfo.hasMessage()) {
  181. // Issue #8454: With IE7 the tooltips size is calculated based on
  182. // the last tooltip's position, causing problems if the last one was
  183. // in the right or bottom edge. For this reason the tooltip is moved
  184. // first to 0,0 position so that the calculation goes correctly.
  185. setPopupPosition(0, 0);
  186. setPopupPositionAndShow(new PositionCallback() {
  187. @Override
  188. public void setPosition(int offsetWidth, int offsetHeight) {
  189. if (offsetWidth > getMaxWidth()) {
  190. setWidth(getMaxWidth() + "px");
  191. // Check new height and width with reflowed content
  192. offsetWidth = getOffsetWidth();
  193. offsetHeight = getOffsetHeight();
  194. }
  195. int x = 0;
  196. int y = 0;
  197. if (BrowserInfo.get().isTouchDevice()) {
  198. setMaxWidth(Window.getClientWidth());
  199. offsetWidth = getOffsetWidth();
  200. offsetHeight = getOffsetHeight();
  201. x = getFinalTouchX(offsetWidth);
  202. y = getFinalTouchY(offsetHeight);
  203. } else {
  204. x = getFinalX(offsetWidth);
  205. y = getFinalY(offsetHeight);
  206. }
  207. setPopupPosition(x, y);
  208. sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
  209. }
  210. /**
  211. * Return the final X-coordinate of the tooltip based on cursor
  212. * position, size of the tooltip, size of the page and necessary
  213. * margins.
  214. *
  215. * @param offsetWidth
  216. * @return The final X-coordinate
  217. */
  218. private int getFinalX(int offsetWidth) {
  219. int x = 0;
  220. int widthNeeded = 10 + MARGIN + offsetWidth;
  221. int roomLeft = tooltipEventMouseX;
  222. int roomRight = Window.getClientWidth() - roomLeft;
  223. if (roomRight > widthNeeded) {
  224. x = tooltipEventMouseX + 10 + Window.getScrollLeft();
  225. } else {
  226. x = tooltipEventMouseX + Window.getScrollLeft() - 10
  227. - offsetWidth;
  228. }
  229. if (x + offsetWidth + MARGIN
  230. - Window.getScrollLeft() > Window
  231. .getClientWidth()) {
  232. x = Window.getClientWidth() - offsetWidth - MARGIN
  233. + Window.getScrollLeft();
  234. }
  235. if (tooltipEventMouseX != EVENT_XY_POSITION_OUTSIDE) {
  236. // Do not allow x to be zero, for otherwise the tooltip
  237. // does not close when the mouse is moved (see
  238. // isTooltipOpen()). #15129
  239. int minX = Window.getScrollLeft() + MARGIN;
  240. x = Math.max(x, minX);
  241. }
  242. return x;
  243. }
  244. /**
  245. * Return the final X-coordinate of the tooltip based on cursor
  246. * position, size of the tooltip, size of the page and necessary
  247. * margins.
  248. *
  249. * @param offsetWidth
  250. * @return The final X-coordinate
  251. */
  252. private int getFinalTouchX(int offsetWidth) {
  253. int x = 0;
  254. int widthNeeded = 10 + offsetWidth;
  255. int roomLeft = currentElement != null
  256. ? currentElement.getAbsoluteLeft()
  257. : EVENT_XY_POSITION_OUTSIDE;
  258. int viewPortWidth = Window.getClientWidth();
  259. int roomRight = viewPortWidth - roomLeft;
  260. if (roomRight > widthNeeded) {
  261. x = roomLeft;
  262. } else {
  263. x = roomLeft - offsetWidth;
  264. }
  265. if (x + offsetWidth
  266. - Window.getScrollLeft() > viewPortWidth) {
  267. x = viewPortWidth - offsetWidth
  268. + Window.getScrollLeft();
  269. }
  270. if (roomLeft != EVENT_XY_POSITION_OUTSIDE) {
  271. // Do not allow x to be zero, for otherwise the tooltip
  272. // does not close when the mouse is moved (see
  273. // isTooltipOpen()). #15129
  274. int minX = Window.getScrollLeft();
  275. x = Math.max(x, minX);
  276. }
  277. return x;
  278. }
  279. /**
  280. * Return the final Y-coordinate of the tooltip based on cursor
  281. * position, size of the tooltip, size of the page and necessary
  282. * margins.
  283. *
  284. * @param offsetHeight
  285. * @return The final y-coordinate
  286. *
  287. */
  288. private int getFinalY(int offsetHeight) {
  289. int y = 0;
  290. int heightNeeded = 10 + offsetHeight;
  291. int roomAbove = tooltipEventMouseY;
  292. int roomBelow = Window.getClientHeight() - roomAbove;
  293. if (roomBelow > heightNeeded) {
  294. y = tooltipEventMouseY + 10 + Window.getScrollTop();
  295. } else {
  296. y = tooltipEventMouseY + Window.getScrollTop() - 10
  297. - offsetHeight;
  298. }
  299. if (y + offsetHeight + MARGIN
  300. - Window.getScrollTop() > Window
  301. .getClientHeight()) {
  302. y = tooltipEventMouseY - 5 - offsetHeight
  303. + Window.getScrollTop();
  304. if (y - Window.getScrollTop() < 0) {
  305. // tooltip does not fit on top of the mouse either,
  306. // put it at the top of the screen
  307. y = Window.getScrollTop();
  308. }
  309. }
  310. if (tooltipEventMouseY != EVENT_XY_POSITION_OUTSIDE) {
  311. // Do not allow y to be zero, for otherwise the tooltip
  312. // does not close when the mouse is moved (see
  313. // isTooltipOpen()). #15129
  314. int minY = Window.getScrollTop() + MARGIN;
  315. y = Math.max(y, minY);
  316. }
  317. return y;
  318. }
  319. /**
  320. * Return the final Y-coordinate of the tooltip based on cursor
  321. * position, size of the tooltip, size of the page and necessary
  322. * margins.
  323. *
  324. * @param offsetHeight
  325. * @return The final y-coordinate
  326. *
  327. */
  328. private int getFinalTouchY(int offsetHeight) {
  329. int y = 0;
  330. int heightNeeded = 10 + offsetHeight;
  331. int roomAbove = currentElement != null
  332. ? currentElement.getAbsoluteTop()
  333. + currentElement.getOffsetHeight()
  334. : EVENT_XY_POSITION_OUTSIDE;
  335. int roomBelow = Window.getClientHeight() - roomAbove;
  336. if (roomBelow > heightNeeded) {
  337. y = roomAbove;
  338. } else {
  339. y = roomAbove - offsetHeight - (currentElement != null
  340. ? currentElement.getOffsetHeight() : 0);
  341. }
  342. if (y + offsetHeight - Window.getScrollTop() > Window
  343. .getClientHeight()) {
  344. y = roomAbove - 5 - offsetHeight
  345. + Window.getScrollTop();
  346. if (y - Window.getScrollTop() < 0) {
  347. // tooltip does not fit on top of the mouse either,
  348. // put it at the top of the screen
  349. y = Window.getScrollTop();
  350. }
  351. }
  352. if (roomAbove != EVENT_XY_POSITION_OUTSIDE) {
  353. // Do not allow y to be zero, for otherwise the tooltip
  354. // does not close when the mouse is moved (see
  355. // isTooltipOpen()). #15129
  356. int minY = Window.getScrollTop();
  357. y = Math.max(y, minY);
  358. }
  359. return y;
  360. }
  361. });
  362. } else {
  363. hide();
  364. }
  365. }
  366. /**
  367. * For assistive tooltips to work correctly we must have the tooltip visible
  368. * and attached to the DOM well in advance. For this reason both isShowing
  369. * and isVisible return false positives. We can't override either of them as
  370. * external code may depend on this behavior.
  371. *
  372. * @return boolean
  373. */
  374. public boolean isTooltipOpen() {
  375. return super.isShowing() && super.isVisible() && getPopupLeft() > 0
  376. && getPopupTop() > 0;
  377. }
  378. private void closeNow() {
  379. hide();
  380. setWidth("");
  381. closing = false;
  382. justClosedTimer.schedule(getQuickOpenTimeout());
  383. justClosed = true;
  384. }
  385. private Timer showTimer = new Timer() {
  386. @Override
  387. public void run() {
  388. opening = false;
  389. showTooltip();
  390. }
  391. };
  392. private Timer closeTimer = new Timer() {
  393. @Override
  394. public void run() {
  395. closeNow();
  396. }
  397. };
  398. private Timer justClosedTimer = new Timer() {
  399. @Override
  400. public void run() {
  401. justClosed = false;
  402. }
  403. };
  404. public void hideTooltip() {
  405. if (opening) {
  406. showTimer.cancel();
  407. opening = false;
  408. }
  409. if (!isAttached()) {
  410. return;
  411. }
  412. if (closing) {
  413. // already about to close
  414. return;
  415. }
  416. if (isTooltipOpen()) {
  417. closeTimer.schedule(getCloseTimeout());
  418. closing = true;
  419. }
  420. }
  421. @Override
  422. public void hide() {
  423. em.updateMessage("");
  424. description.setHTML("");
  425. updatePosition(null, true);
  426. setPopupPosition(tooltipEventMouseX, tooltipEventMouseY);
  427. }
  428. private int EVENT_XY_POSITION_OUTSIDE = -5000;
  429. private int tooltipEventMouseX;
  430. private int tooltipEventMouseY;
  431. public void updatePosition(Event event, boolean isFocused) {
  432. tooltipEventMouseX = getEventX(event, isFocused);
  433. tooltipEventMouseY = getEventY(event, isFocused);
  434. }
  435. private int getEventX(Event event, boolean isFocused) {
  436. return isFocused ? EVENT_XY_POSITION_OUTSIDE
  437. : DOM.eventGetClientX(event);
  438. }
  439. private int getEventY(Event event, boolean isFocused) {
  440. return isFocused ? EVENT_XY_POSITION_OUTSIDE
  441. : DOM.eventGetClientY(event);
  442. }
  443. @Override
  444. public void onBrowserEvent(Event event) {
  445. final int type = DOM.eventGetType(event);
  446. // cancel closing event if tooltip is mouseovered; the user might want
  447. // to scroll of cut&paste
  448. if (type == Event.ONMOUSEOVER) {
  449. // Cancel closing so tooltip stays open and user can copy paste the
  450. // tooltip
  451. closeTimer.cancel();
  452. closing = false;
  453. } else if (type == Event.ONMOUSEOUT) {
  454. tooltipEventHandler.handleOnMouseOut(DOM.eventGetTarget(event));
  455. }
  456. }
  457. /**
  458. * Replace current open tooltip with new content
  459. */
  460. public void replaceCurrentTooltip() {
  461. if (closing) {
  462. closeTimer.cancel();
  463. closeNow();
  464. justClosedTimer.cancel();
  465. justClosed = false;
  466. }
  467. showTooltip();
  468. opening = false;
  469. }
  470. private class TooltipEventHandler
  471. implements MouseMoveHandler, KeyDownHandler, FocusHandler,
  472. BlurHandler, MouseDownHandler, MouseOutHandler {
  473. /**
  474. * Marker for handling of tooltip through focus
  475. */
  476. private boolean handledByFocus;
  477. /**
  478. * Locate the tooltip for given element
  479. *
  480. * @param element
  481. * Element used in search
  482. * @return TooltipInfo if connector and tooltip found, null if not
  483. */
  484. private TooltipInfo getTooltipFor(Element element) {
  485. ApplicationConnection ac = getApplicationConnection();
  486. ComponentConnector connector = Util.getConnectorForElement(ac,
  487. RootPanel.get(), element);
  488. // Try to find first connector with proper tooltip info
  489. TooltipInfo info = null;
  490. while (connector != null) {
  491. info = connector.getTooltipInfo(element);
  492. if (info != null && info.hasMessage()) {
  493. break;
  494. }
  495. if (!(connector.getParent() instanceof ComponentConnector)) {
  496. connector = null;
  497. info = null;
  498. break;
  499. }
  500. connector = (ComponentConnector) connector.getParent();
  501. }
  502. if (connector != null && info != null) {
  503. assert connector.hasTooltip() : "getTooltipInfo for "
  504. + Util.getConnectorString(connector)
  505. + " returned a tooltip even though hasTooltip claims there are no tooltips for the connector.";
  506. return info;
  507. }
  508. return null;
  509. }
  510. /**
  511. * Handle hide event
  512. *
  513. */
  514. private void handleHideEvent() {
  515. hideTooltip();
  516. }
  517. @Override
  518. public void onMouseMove(MouseMoveEvent mme) {
  519. handleShowHide(mme, false);
  520. }
  521. @Override
  522. public void onMouseDown(MouseDownEvent event) {
  523. handleHideEvent();
  524. }
  525. @Override
  526. public void onKeyDown(KeyDownEvent event) {
  527. handleHideEvent();
  528. }
  529. /**
  530. * Displays Tooltip when page is navigated with the keyboard.
  531. *
  532. * Tooltip is not visible. This makes it possible for assistive devices
  533. * to recognize the tooltip.
  534. */
  535. @Override
  536. public void onFocus(FocusEvent fe) {
  537. handleShowHide(fe, true);
  538. }
  539. /**
  540. * Hides Tooltip when the page is navigated with the keyboard.
  541. *
  542. * Removes the Tooltip from page to make sure assistive devices don't
  543. * recognize it by accident.
  544. */
  545. @Override
  546. public void onBlur(BlurEvent be) {
  547. handledByFocus = false;
  548. handleHideEvent();
  549. }
  550. private void handleShowHide(DomEvent domEvent, boolean isFocused) {
  551. Event event = Event.as(domEvent.getNativeEvent());
  552. Element element = Element.as(event.getEventTarget());
  553. // We can ignore move event if it's handled by move or over already
  554. if (currentElement == element && handledByFocus) {
  555. return;
  556. }
  557. // If the parent (sub)component already has a tooltip open and it
  558. // hasn't changed, we ignore the event.
  559. // TooltipInfo contains a reference to the parent component that is
  560. // checked in it's equals-method.
  561. if (currentElement != null && isTooltipOpen()) {
  562. TooltipInfo newTooltip = getTooltipFor(element);
  563. if (currentTooltipInfo != null
  564. && currentTooltipInfo.equals(newTooltip)) {
  565. return;
  566. }
  567. }
  568. TooltipInfo info = getTooltipFor(element);
  569. if (info == null) {
  570. handleHideEvent();
  571. } else {
  572. if (closing) {
  573. closeTimer.cancel();
  574. closing = false;
  575. }
  576. if (isTooltipOpen()) {
  577. closeNow();
  578. }
  579. setTooltipText(info);
  580. updatePosition(event, isFocused);
  581. // Schedule timer for showing the tooltip according to if it
  582. // was recently closed or not.
  583. if (BrowserInfo.get().isIOS()) {
  584. element.focus();
  585. }
  586. int timeout = justClosed ? getQuickOpenDelay() : getOpenDelay();
  587. if (timeout == 0) {
  588. showTooltip();
  589. } else {
  590. showTimer.schedule(timeout);
  591. opening = true;
  592. }
  593. }
  594. handledByFocus = isFocused;
  595. currentElement = element;
  596. }
  597. @Override
  598. public void onMouseOut(MouseOutEvent moe) {
  599. Element element = WidgetUtil
  600. .getElementUnderMouse(moe.getNativeEvent());
  601. handleOnMouseOut(element);
  602. }
  603. private void handleOnMouseOut(Element element) {
  604. if (element == null) {
  605. // hide if mouse is outside of browser window
  606. handleHideEvent();
  607. } else {
  608. Widget owner = getOwner();
  609. if (owner != null && !owner.getElement().isOrHasChild(element)
  610. && !hasCommonOwner(owner, element)) {
  611. // hide if mouse is no longer within the UI nor an overlay
  612. // that belongs to the UI, e.g. a Window
  613. handleHideEvent();
  614. }
  615. }
  616. }
  617. private boolean hasCommonOwner(Widget owner, Element element) {
  618. ComponentConnector connector = Util
  619. .findPaintable(getApplicationConnection(), element);
  620. if (connector != null && connector.getConnection() != null
  621. && connector.getConnection().getUIConnector() != null) {
  622. return owner.equals(
  623. connector.getConnection().getUIConnector().getWidget());
  624. }
  625. return false;
  626. }
  627. }
  628. private final TooltipEventHandler tooltipEventHandler = new TooltipEventHandler();
  629. /**
  630. * Connects DOM handlers to widget that are needed for tooltip presentation.
  631. *
  632. * @param widget
  633. * Widget which DOM handlers are connected
  634. */
  635. public void connectHandlersToWidget(Widget widget) {
  636. Profiler.enter("VTooltip.connectHandlersToWidget");
  637. widget.addDomHandler(tooltipEventHandler, MouseOutEvent.getType());
  638. widget.addDomHandler(tooltipEventHandler, MouseMoveEvent.getType());
  639. widget.addDomHandler(tooltipEventHandler, MouseDownEvent.getType());
  640. widget.addDomHandler(tooltipEventHandler, KeyDownEvent.getType());
  641. widget.addDomHandler(tooltipEventHandler, FocusEvent.getType());
  642. widget.addDomHandler(tooltipEventHandler, BlurEvent.getType());
  643. Profiler.leave("VTooltip.connectHandlersToWidget");
  644. }
  645. /**
  646. * Returns the unique id of the tooltip element.
  647. *
  648. * @return String containing the unique id of the tooltip, which always has
  649. * a value
  650. */
  651. public String getUniqueId() {
  652. return uniqueId;
  653. }
  654. @Override
  655. public void setPopupPositionAndShow(PositionCallback callback) {
  656. if (isAttached()) {
  657. callback.setPosition(getOffsetWidth(), getOffsetHeight());
  658. } else {
  659. super.setPopupPositionAndShow(callback);
  660. }
  661. }
  662. /**
  663. * Returns the time (in ms) the tooltip should be displayed after an event
  664. * that will cause it to be closed (e.g. mouse click outside the component,
  665. * key down).
  666. *
  667. * @return The close timeout (in ms)
  668. */
  669. public int getCloseTimeout() {
  670. return closeTimeout;
  671. }
  672. /**
  673. * Sets the time (in ms) the tooltip should be displayed after an event that
  674. * will cause it to be closed (e.g. mouse click outside the component, key
  675. * down).
  676. *
  677. * @param closeTimeout
  678. * The close timeout (in ms)
  679. */
  680. public void setCloseTimeout(int closeTimeout) {
  681. this.closeTimeout = closeTimeout;
  682. }
  683. /**
  684. * Returns the time (in ms) during which {@link #getQuickOpenDelay()} should
  685. * be used instead of {@link #getOpenDelay()}. The quick open delay is used
  686. * when the tooltip has very recently been shown, is currently hidden but
  687. * about to be shown again.
  688. *
  689. * @return The quick open timeout (in ms)
  690. */
  691. public int getQuickOpenTimeout() {
  692. return quickOpenTimeout;
  693. }
  694. /**
  695. * Sets the time (in ms) that determines when {@link #getQuickOpenDelay()}
  696. * should be used instead of {@link #getOpenDelay()}. The quick open delay
  697. * is used when the tooltip has very recently been shown, is currently
  698. * hidden but about to be shown again.
  699. *
  700. * @param quickOpenTimeout
  701. * The quick open timeout (in ms)
  702. */
  703. public void setQuickOpenTimeout(int quickOpenTimeout) {
  704. this.quickOpenTimeout = quickOpenTimeout;
  705. }
  706. /**
  707. * Returns the time (in ms) that should elapse before a tooltip will be
  708. * shown, in the situation when a tooltip has very recently been shown
  709. * (within {@link #getQuickOpenDelay()} ms).
  710. *
  711. * @return The quick open delay (in ms)
  712. */
  713. public int getQuickOpenDelay() {
  714. return quickOpenDelay;
  715. }
  716. /**
  717. * Sets the time (in ms) that should elapse before a tooltip will be shown,
  718. * in the situation when a tooltip has very recently been shown (within
  719. * {@link #getQuickOpenDelay()} ms).
  720. *
  721. * @param quickOpenDelay
  722. * The quick open delay (in ms)
  723. */
  724. public void setQuickOpenDelay(int quickOpenDelay) {
  725. this.quickOpenDelay = quickOpenDelay;
  726. }
  727. /**
  728. * Returns the time (in ms) that should elapse after an event triggering
  729. * tooltip showing has occurred (e.g. mouse over) before the tooltip is
  730. * shown. If a tooltip has recently been shown, then
  731. * {@link #getQuickOpenDelay()} is used instead of this.
  732. *
  733. * @return The open delay (in ms)
  734. */
  735. public int getOpenDelay() {
  736. return openDelay;
  737. }
  738. /**
  739. * Sets the time (in ms) that should elapse after an event triggering
  740. * tooltip showing has occurred (e.g. mouse over) before the tooltip is
  741. * shown. If a tooltip has recently been shown, then
  742. * {@link #getQuickOpenDelay()} is used instead of this.
  743. *
  744. * @param openDelay
  745. * The open delay (in ms)
  746. */
  747. public void setOpenDelay(int openDelay) {
  748. this.openDelay = openDelay;
  749. }
  750. /**
  751. * Sets the maximum width of the tooltip popup.
  752. *
  753. * @param maxWidth
  754. * The maximum width the tooltip popup (in pixels)
  755. */
  756. public void setMaxWidth(int maxWidth) {
  757. this.maxWidth = maxWidth;
  758. }
  759. /**
  760. * Returns the maximum width of the tooltip popup.
  761. *
  762. * @return The maximum width the tooltip popup (in pixels)
  763. */
  764. public int getMaxWidth() {
  765. return maxWidth;
  766. }
  767. }