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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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.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. description.setHTML("");
  424. updatePosition(null, true);
  425. setPopupPosition(tooltipEventMouseX, tooltipEventMouseY);
  426. }
  427. private int EVENT_XY_POSITION_OUTSIDE = -5000;
  428. private int tooltipEventMouseX;
  429. private int tooltipEventMouseY;
  430. public void updatePosition(Event event, boolean isFocused) {
  431. tooltipEventMouseX = getEventX(event, isFocused);
  432. tooltipEventMouseY = getEventY(event, isFocused);
  433. }
  434. private int getEventX(Event event, boolean isFocused) {
  435. return isFocused ? EVENT_XY_POSITION_OUTSIDE
  436. : DOM.eventGetClientX(event);
  437. }
  438. private int getEventY(Event event, boolean isFocused) {
  439. return isFocused ? EVENT_XY_POSITION_OUTSIDE
  440. : DOM.eventGetClientY(event);
  441. }
  442. @Override
  443. public void onBrowserEvent(Event event) {
  444. final int type = DOM.eventGetType(event);
  445. // cancel closing event if tooltip is mouseovered; the user might want
  446. // to scroll of cut&paste
  447. if (type == Event.ONMOUSEOVER) {
  448. // Cancel closing so tooltip stays open and user can copy paste the
  449. // tooltip
  450. closeTimer.cancel();
  451. closing = false;
  452. } else if (type == Event.ONMOUSEOUT) {
  453. tooltipEventHandler.handleOnMouseOut(DOM.eventGetTarget(event));
  454. }
  455. }
  456. /**
  457. * Replace current open tooltip with new content
  458. */
  459. public void replaceCurrentTooltip() {
  460. if (closing) {
  461. closeTimer.cancel();
  462. closeNow();
  463. justClosedTimer.cancel();
  464. justClosed = false;
  465. }
  466. showTooltip();
  467. opening = false;
  468. }
  469. private class TooltipEventHandler
  470. implements MouseMoveHandler, KeyDownHandler, FocusHandler,
  471. BlurHandler, MouseDownHandler, MouseOutHandler {
  472. /**
  473. * Marker for handling of tooltip through focus
  474. */
  475. private boolean handledByFocus;
  476. /**
  477. * Locate the tooltip for given element
  478. *
  479. * @param element
  480. * Element used in search
  481. * @return TooltipInfo if connector and tooltip found, null if not
  482. */
  483. private TooltipInfo getTooltipFor(Element element) {
  484. ApplicationConnection ac = getApplicationConnection();
  485. ComponentConnector connector = Util.getConnectorForElement(ac,
  486. RootPanel.get(), element);
  487. // Try to find first connector with proper tooltip info
  488. TooltipInfo info = null;
  489. while (connector != null) {
  490. info = connector.getTooltipInfo(element);
  491. if (info != null && info.hasMessage()) {
  492. break;
  493. }
  494. if (!(connector.getParent() instanceof ComponentConnector)) {
  495. connector = null;
  496. info = null;
  497. break;
  498. }
  499. connector = (ComponentConnector) connector.getParent();
  500. }
  501. if (connector != null && info != null) {
  502. assert connector.hasTooltip() : "getTooltipInfo for "
  503. + Util.getConnectorString(connector)
  504. + " returned a tooltip even though hasTooltip claims there are no tooltips for the connector.";
  505. return info;
  506. }
  507. return null;
  508. }
  509. /**
  510. * Handle hide event
  511. *
  512. */
  513. private void handleHideEvent() {
  514. hideTooltip();
  515. }
  516. @Override
  517. public void onMouseMove(MouseMoveEvent mme) {
  518. handleShowHide(mme, false);
  519. }
  520. @Override
  521. public void onMouseDown(MouseDownEvent event) {
  522. handleHideEvent();
  523. }
  524. @Override
  525. public void onKeyDown(KeyDownEvent event) {
  526. handleHideEvent();
  527. }
  528. /**
  529. * Displays Tooltip when page is navigated with the keyboard.
  530. *
  531. * Tooltip is not visible. This makes it possible for assistive devices
  532. * to recognize the tooltip.
  533. */
  534. @Override
  535. public void onFocus(FocusEvent fe) {
  536. handleShowHide(fe, true);
  537. }
  538. /**
  539. * Hides Tooltip when the page is navigated with the keyboard.
  540. *
  541. * Removes the Tooltip from page to make sure assistive devices don't
  542. * recognize it by accident.
  543. */
  544. @Override
  545. public void onBlur(BlurEvent be) {
  546. handledByFocus = false;
  547. handleHideEvent();
  548. }
  549. private void handleShowHide(DomEvent domEvent, boolean isFocused) {
  550. Event event = Event.as(domEvent.getNativeEvent());
  551. Element element = Element.as(event.getEventTarget());
  552. // We can ignore move event if it's handled by move or over already
  553. if (currentElement == element && handledByFocus == true) {
  554. return;
  555. }
  556. // If the parent (sub)component already has a tooltip open and it
  557. // hasn't changed, we ignore the event.
  558. // TooltipInfo contains a reference to the parent component that is
  559. // checked in it's equals-method.
  560. if (currentElement != null && isTooltipOpen()) {
  561. TooltipInfo newTooltip = getTooltipFor(element);
  562. if (currentTooltipInfo != null
  563. && currentTooltipInfo.equals(newTooltip)) {
  564. return;
  565. }
  566. }
  567. TooltipInfo info = getTooltipFor(element);
  568. if (info == null) {
  569. handleHideEvent();
  570. } else {
  571. if (closing) {
  572. closeTimer.cancel();
  573. closing = false;
  574. }
  575. if (isTooltipOpen()) {
  576. closeNow();
  577. }
  578. setTooltipText(info);
  579. updatePosition(event, isFocused);
  580. // Schedule timer for showing the tooltip according to if it
  581. // was recently closed or not.
  582. if (BrowserInfo.get().isIOS()) {
  583. element.focus();
  584. }
  585. int timeout = justClosed ? getQuickOpenDelay() : getOpenDelay();
  586. if (timeout == 0) {
  587. showTooltip();
  588. } else {
  589. showTimer.schedule(timeout);
  590. opening = true;
  591. }
  592. }
  593. handledByFocus = isFocused;
  594. currentElement = element;
  595. }
  596. @Override
  597. public void onMouseOut(MouseOutEvent moe) {
  598. Element element = WidgetUtil
  599. .getElementUnderMouse(moe.getNativeEvent());
  600. handleOnMouseOut(element);
  601. }
  602. private void handleOnMouseOut(Element element) {
  603. if (element == null) {
  604. // hide if mouse is outside of browser window
  605. handleHideEvent();
  606. } else {
  607. Widget owner = getOwner();
  608. if (owner != null && !owner.getElement().isOrHasChild(element)
  609. && !hasCommonOwner(owner, element)) {
  610. // hide if mouse is no longer within the UI nor an overlay
  611. // that belongs to the UI, e.g. a Window
  612. handleHideEvent();
  613. }
  614. }
  615. }
  616. private boolean hasCommonOwner(Widget owner, Element element) {
  617. ComponentConnector connector = Util
  618. .findPaintable(getApplicationConnection(), element);
  619. if (connector != null && connector.getConnection() != null
  620. && connector.getConnection().getUIConnector() != null) {
  621. return owner.equals(
  622. connector.getConnection().getUIConnector().getWidget());
  623. }
  624. return false;
  625. }
  626. }
  627. private final TooltipEventHandler tooltipEventHandler = new TooltipEventHandler();
  628. /**
  629. * Connects DOM handlers to widget that are needed for tooltip presentation.
  630. *
  631. * @param widget
  632. * Widget which DOM handlers are connected
  633. */
  634. public void connectHandlersToWidget(Widget widget) {
  635. Profiler.enter("VTooltip.connectHandlersToWidget");
  636. widget.addDomHandler(tooltipEventHandler, MouseOutEvent.getType());
  637. widget.addDomHandler(tooltipEventHandler, MouseMoveEvent.getType());
  638. widget.addDomHandler(tooltipEventHandler, MouseDownEvent.getType());
  639. widget.addDomHandler(tooltipEventHandler, KeyDownEvent.getType());
  640. widget.addDomHandler(tooltipEventHandler, FocusEvent.getType());
  641. widget.addDomHandler(tooltipEventHandler, BlurEvent.getType());
  642. Profiler.leave("VTooltip.connectHandlersToWidget");
  643. }
  644. /**
  645. * Returns the unique id of the tooltip element.
  646. *
  647. * @return String containing the unique id of the tooltip, which always has
  648. * a value
  649. */
  650. public String getUniqueId() {
  651. return uniqueId;
  652. }
  653. @Override
  654. public void setPopupPositionAndShow(PositionCallback callback) {
  655. if (isAttached()) {
  656. callback.setPosition(getOffsetWidth(), getOffsetHeight());
  657. } else {
  658. super.setPopupPositionAndShow(callback);
  659. }
  660. }
  661. /**
  662. * Returns the time (in ms) the tooltip should be displayed after an event
  663. * that will cause it to be closed (e.g. mouse click outside the component,
  664. * key down).
  665. *
  666. * @return The close timeout (in ms)
  667. */
  668. public int getCloseTimeout() {
  669. return closeTimeout;
  670. }
  671. /**
  672. * Sets the time (in ms) the tooltip should be displayed after an event that
  673. * will cause it to be closed (e.g. mouse click outside the component, key
  674. * down).
  675. *
  676. * @param closeTimeout
  677. * The close timeout (in ms)
  678. */
  679. public void setCloseTimeout(int closeTimeout) {
  680. this.closeTimeout = closeTimeout;
  681. }
  682. /**
  683. * Returns the time (in ms) during which {@link #getQuickOpenDelay()} should
  684. * be used instead of {@link #getOpenDelay()}. The quick open delay is used
  685. * when the tooltip has very recently been shown, is currently hidden but
  686. * about to be shown again.
  687. *
  688. * @return The quick open timeout (in ms)
  689. */
  690. public int getQuickOpenTimeout() {
  691. return quickOpenTimeout;
  692. }
  693. /**
  694. * Sets the time (in ms) that determines when {@link #getQuickOpenDelay()}
  695. * should be used instead of {@link #getOpenDelay()}. The quick open delay
  696. * is used when the tooltip has very recently been shown, is currently
  697. * hidden but about to be shown again.
  698. *
  699. * @param quickOpenTimeout
  700. * The quick open timeout (in ms)
  701. */
  702. public void setQuickOpenTimeout(int quickOpenTimeout) {
  703. this.quickOpenTimeout = quickOpenTimeout;
  704. }
  705. /**
  706. * Returns the time (in ms) that should elapse before a tooltip will be
  707. * shown, in the situation when a tooltip has very recently been shown
  708. * (within {@link #getQuickOpenDelay()} ms).
  709. *
  710. * @return The quick open delay (in ms)
  711. */
  712. public int getQuickOpenDelay() {
  713. return quickOpenDelay;
  714. }
  715. /**
  716. * Sets the time (in ms) that should elapse before a tooltip will be shown,
  717. * in the situation when a tooltip has very recently been shown (within
  718. * {@link #getQuickOpenDelay()} ms).
  719. *
  720. * @param quickOpenDelay
  721. * The quick open delay (in ms)
  722. */
  723. public void setQuickOpenDelay(int quickOpenDelay) {
  724. this.quickOpenDelay = quickOpenDelay;
  725. }
  726. /**
  727. * Returns the time (in ms) that should elapse after an event triggering
  728. * tooltip showing has occurred (e.g. mouse over) before the tooltip is
  729. * shown. If a tooltip has recently been shown, then
  730. * {@link #getQuickOpenDelay()} is used instead of this.
  731. *
  732. * @return The open delay (in ms)
  733. */
  734. public int getOpenDelay() {
  735. return openDelay;
  736. }
  737. /**
  738. * Sets the time (in ms) that should elapse after an event triggering
  739. * tooltip showing has occurred (e.g. mouse over) before the tooltip is
  740. * shown. If a tooltip has recently been shown, then
  741. * {@link #getQuickOpenDelay()} is used instead of this.
  742. *
  743. * @param openDelay
  744. * The open delay (in ms)
  745. */
  746. public void setOpenDelay(int openDelay) {
  747. this.openDelay = openDelay;
  748. }
  749. /**
  750. * Sets the maximum width of the tooltip popup.
  751. *
  752. * @param maxWidth
  753. * The maximum width the tooltip popup (in pixels)
  754. */
  755. public void setMaxWidth(int maxWidth) {
  756. this.maxWidth = maxWidth;
  757. }
  758. /**
  759. * Returns the maximum width of the tooltip popup.
  760. *
  761. * @return The maximum width the tooltip popup (in pixels)
  762. */
  763. public int getMaxWidth() {
  764. return maxWidth;
  765. }
  766. }