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

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