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. * @since 7.2.4
  114. */
  115. public void initializeAssistiveTooltips() {
  116. updatePosition(null, true);
  117. setTooltipText(new TooltipInfo(" "));
  118. showTooltip();
  119. hideTooltip();
  120. description.getParent().getElement().getStyle().clearWidth();
  121. }
  122. private void setTooltipText(TooltipInfo info) {
  123. if (info.getErrorMessage() != null
  124. && !info.getErrorMessage().isEmpty()) {
  125. em.setVisible(true);
  126. em.updateMessage(info.getErrorMessage());
  127. em.updateErrorLevel(info.getErrorLevel());
  128. } else {
  129. em.setVisible(false);
  130. }
  131. if (info.getTitle() != null && !info.getTitle().isEmpty()) {
  132. switch (info.getContentMode()) {
  133. case HTML:
  134. description.setHTML(info.getTitle());
  135. break;
  136. case TEXT:
  137. description.setText(info.getTitle());
  138. break;
  139. case PREFORMATTED:
  140. PreElement preElement = Document.get().createPreElement();
  141. preElement.addClassName(CLASSNAME + "-pre");
  142. preElement.setInnerText(info.getTitle());
  143. // clear existing content
  144. description.setHTML("");
  145. // add preformatted text to dom
  146. description.getElement().appendChild(preElement);
  147. break;
  148. default:
  149. break;
  150. }
  151. /*
  152. * Issue #11871: to correctly update the offsetWidth of description
  153. * element we need to clear style width of its parent DIV from old
  154. * value (in some strange cases this width=[tooltip MAX_WIDTH] after
  155. * tooltip text has been already updated to new shortly value:
  156. *
  157. * <div class="popupContent"> <div style="width:500px;"> <div
  158. * class="v-errormessage" aria-hidden="true" style="display: none;">
  159. * <div class="gwt-HTML"> </div> </div> <div
  160. * class="v-tooltip-text">This is a short tooltip</div> </div>
  161. *
  162. * and it leads to error during calculation offsetWidth (it is
  163. * native GWT method getSubPixelOffsetWidth()) of description
  164. * element")
  165. */
  166. description.getParent().getElement().getStyle().clearWidth();
  167. description.getElement().getStyle().clearDisplay();
  168. } else {
  169. description.setHTML("");
  170. description.getElement().getStyle().setDisplay(Display.NONE);
  171. }
  172. currentTooltipInfo = info;
  173. }
  174. /**
  175. * Show a popup containing the currentTooltipInfo
  176. *
  177. */
  178. private void showTooltip() {
  179. if (currentTooltipInfo.hasMessage()) {
  180. // Issue #8454: With IE7 the tooltips size is calculated based on
  181. // the last tooltip's position, causing problems if the last one was
  182. // in the right or bottom edge. For this reason the tooltip is moved
  183. // first to 0,0 position so that the calculation goes correctly.
  184. setPopupPosition(0, 0);
  185. setPopupPositionAndShow(new PositionCallback() {
  186. @Override
  187. public void setPosition(int offsetWidth, int offsetHeight) {
  188. if (offsetWidth > getMaxWidth()) {
  189. setWidth(getMaxWidth() + "px");
  190. // Check new height and width with reflowed content
  191. offsetWidth = getOffsetWidth();
  192. offsetHeight = getOffsetHeight();
  193. }
  194. int x = 0;
  195. int y = 0;
  196. if (BrowserInfo.get().isTouchDevice()) {
  197. setMaxWidth(Window.getClientWidth());
  198. offsetWidth = getOffsetWidth();
  199. offsetHeight = getOffsetHeight();
  200. x = getFinalTouchX(offsetWidth);
  201. y = getFinalTouchY(offsetHeight);
  202. } else {
  203. x = getFinalX(offsetWidth);
  204. y = getFinalY(offsetHeight);
  205. }
  206. setPopupPosition(x, y);
  207. sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
  208. }
  209. /**
  210. * Return the final X-coordinate of the tooltip based on cursor
  211. * position, size of the tooltip, size of the page and necessary
  212. * margins.
  213. *
  214. * @param offsetWidth
  215. * @return The final X-coordinate
  216. */
  217. private int getFinalX(int offsetWidth) {
  218. int x = 0;
  219. int widthNeeded = 10 + MARGIN + offsetWidth;
  220. int roomLeft = tooltipEventMouseX;
  221. int roomRight = Window.getClientWidth() - roomLeft;
  222. if (roomRight > widthNeeded) {
  223. x = tooltipEventMouseX + 10 + Window.getScrollLeft();
  224. } else {
  225. x = tooltipEventMouseX + Window.getScrollLeft() - 10
  226. - offsetWidth;
  227. }
  228. if (x + offsetWidth + MARGIN
  229. - Window.getScrollLeft() > Window
  230. .getClientWidth()) {
  231. x = Window.getClientWidth() - offsetWidth - MARGIN
  232. + Window.getScrollLeft();
  233. }
  234. if (tooltipEventMouseX != EVENT_XY_POSITION_OUTSIDE) {
  235. // Do not allow x to be zero, for otherwise the tooltip
  236. // does not close when the mouse is moved (see
  237. // isTooltipOpen()). #15129
  238. int minX = Window.getScrollLeft() + MARGIN;
  239. x = Math.max(x, minX);
  240. }
  241. return x;
  242. }
  243. /**
  244. * Return the final X-coordinate of the tooltip based on cursor
  245. * position, size of the tooltip, size of the page and necessary
  246. * margins.
  247. *
  248. * @param offsetWidth
  249. * @return The final X-coordinate
  250. */
  251. private int getFinalTouchX(int offsetWidth) {
  252. int x = 0;
  253. int widthNeeded = 10 + offsetWidth;
  254. int roomLeft = currentElement != null
  255. ? currentElement.getAbsoluteLeft()
  256. : EVENT_XY_POSITION_OUTSIDE;
  257. int viewPortWidth = Window.getClientWidth();
  258. int roomRight = viewPortWidth - roomLeft;
  259. if (roomRight > widthNeeded) {
  260. x = roomLeft;
  261. } else {
  262. x = roomLeft - offsetWidth;
  263. }
  264. if (x + offsetWidth
  265. - Window.getScrollLeft() > viewPortWidth) {
  266. x = viewPortWidth - offsetWidth
  267. + Window.getScrollLeft();
  268. }
  269. if (roomLeft != EVENT_XY_POSITION_OUTSIDE) {
  270. // Do not allow x to be zero, for otherwise the tooltip
  271. // does not close when the mouse is moved (see
  272. // isTooltipOpen()). #15129
  273. int minX = Window.getScrollLeft();
  274. x = Math.max(x, minX);
  275. }
  276. return x;
  277. }
  278. /**
  279. * Return the final Y-coordinate of the tooltip based on cursor
  280. * position, size of the tooltip, size of the page and necessary
  281. * margins.
  282. *
  283. * @param offsetHeight
  284. * @return The final y-coordinate
  285. *
  286. */
  287. private int getFinalY(int offsetHeight) {
  288. int y = 0;
  289. int heightNeeded = 10 + offsetHeight;
  290. int roomAbove = tooltipEventMouseY;
  291. int roomBelow = Window.getClientHeight() - roomAbove;
  292. if (roomBelow > heightNeeded) {
  293. y = tooltipEventMouseY + 10 + Window.getScrollTop();
  294. } else {
  295. y = tooltipEventMouseY + Window.getScrollTop() - 10
  296. - offsetHeight;
  297. }
  298. if (y + offsetHeight + MARGIN
  299. - Window.getScrollTop() > Window
  300. .getClientHeight()) {
  301. y = tooltipEventMouseY - 5 - offsetHeight
  302. + Window.getScrollTop();
  303. if (y - Window.getScrollTop() < 0) {
  304. // tooltip does not fit on top of the mouse either,
  305. // put it at the top of the screen
  306. y = Window.getScrollTop();
  307. }
  308. }
  309. if (tooltipEventMouseY != EVENT_XY_POSITION_OUTSIDE) {
  310. // Do not allow y to be zero, for otherwise the tooltip
  311. // does not close when the mouse is moved (see
  312. // isTooltipOpen()). #15129
  313. int minY = Window.getScrollTop() + MARGIN;
  314. y = Math.max(y, minY);
  315. }
  316. return y;
  317. }
  318. /**
  319. * Return the final Y-coordinate of the tooltip based on cursor
  320. * position, size of the tooltip, size of the page and necessary
  321. * margins.
  322. *
  323. * @param offsetHeight
  324. * @return The final y-coordinate
  325. *
  326. */
  327. private int getFinalTouchY(int offsetHeight) {
  328. int y = 0;
  329. int heightNeeded = 10 + offsetHeight;
  330. int roomAbove = currentElement != null
  331. ? currentElement.getAbsoluteTop()
  332. + currentElement.getOffsetHeight()
  333. : EVENT_XY_POSITION_OUTSIDE;
  334. int roomBelow = Window.getClientHeight() - roomAbove;
  335. if (roomBelow > heightNeeded) {
  336. y = roomAbove;
  337. } else {
  338. y = roomAbove - offsetHeight - (currentElement != null
  339. ? currentElement.getOffsetHeight() : 0);
  340. }
  341. if (y + offsetHeight - Window.getScrollTop() > Window
  342. .getClientHeight()) {
  343. y = roomAbove - 5 - offsetHeight
  344. + Window.getScrollTop();
  345. if (y - Window.getScrollTop() < 0) {
  346. // tooltip does not fit on top of the mouse either,
  347. // put it at the top of the screen
  348. y = Window.getScrollTop();
  349. }
  350. }
  351. if (roomAbove != EVENT_XY_POSITION_OUTSIDE) {
  352. // Do not allow y to be zero, for otherwise the tooltip
  353. // does not close when the mouse is moved (see
  354. // isTooltipOpen()). #15129
  355. int minY = Window.getScrollTop();
  356. y = Math.max(y, minY);
  357. }
  358. return y;
  359. }
  360. });
  361. } else {
  362. hide();
  363. }
  364. }
  365. /**
  366. * For assistive tooltips to work correctly we must have the tooltip visible
  367. * and attached to the DOM well in advance. For this reason both isShowing
  368. * and isVisible return false positives. We can't override either of them as
  369. * external code may depend on this behavior.
  370. *
  371. * @return boolean
  372. */
  373. public boolean isTooltipOpen() {
  374. return super.isShowing() && super.isVisible() && getPopupLeft() > 0
  375. && getPopupTop() > 0;
  376. }
  377. private void closeNow() {
  378. hide();
  379. setWidth("");
  380. closing = false;
  381. justClosedTimer.schedule(getQuickOpenTimeout());
  382. justClosed = true;
  383. }
  384. private Timer showTimer = new Timer() {
  385. @Override
  386. public void run() {
  387. opening = false;
  388. showTooltip();
  389. }
  390. };
  391. private Timer closeTimer = new Timer() {
  392. @Override
  393. public void run() {
  394. closeNow();
  395. }
  396. };
  397. private Timer justClosedTimer = new Timer() {
  398. @Override
  399. public void run() {
  400. justClosed = false;
  401. }
  402. };
  403. public void hideTooltip() {
  404. if (opening) {
  405. showTimer.cancel();
  406. opening = false;
  407. }
  408. if (!isAttached()) {
  409. return;
  410. }
  411. if (closing) {
  412. // already about to close
  413. return;
  414. }
  415. if (isTooltipOpen()) {
  416. closeTimer.schedule(getCloseTimeout());
  417. closing = true;
  418. }
  419. }
  420. @Override
  421. public void hide() {
  422. em.updateMessage("");
  423. em.updateErrorLevel(null);
  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. }