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

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