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

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