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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * Copyright 2000-2013 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.Id;
  18. import com.google.gwt.aria.client.LiveValue;
  19. import com.google.gwt.aria.client.Roles;
  20. import com.google.gwt.event.dom.client.BlurEvent;
  21. import com.google.gwt.event.dom.client.BlurHandler;
  22. import com.google.gwt.event.dom.client.ClickEvent;
  23. import com.google.gwt.event.dom.client.ClickHandler;
  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.MouseMoveEvent;
  30. import com.google.gwt.event.dom.client.MouseMoveHandler;
  31. import com.google.gwt.user.client.DOM;
  32. import com.google.gwt.user.client.Element;
  33. import com.google.gwt.user.client.Event;
  34. import com.google.gwt.user.client.Timer;
  35. import com.google.gwt.user.client.Window;
  36. import com.google.gwt.user.client.ui.FlowPanel;
  37. import com.google.gwt.user.client.ui.RootPanel;
  38. import com.google.gwt.user.client.ui.Widget;
  39. import com.vaadin.client.ui.VOverlay;
  40. /**
  41. * TODO open for extension
  42. */
  43. public class VTooltip extends VOverlay {
  44. private static final String CLASSNAME = "v-tooltip";
  45. private static final int MARGIN = 4;
  46. public static final int TOOLTIP_EVENTS = Event.ONKEYDOWN
  47. | Event.ONMOUSEOVER | Event.ONMOUSEOUT | Event.ONMOUSEMOVE
  48. | Event.ONCLICK;
  49. VErrorMessage em = new VErrorMessage();
  50. Element description = DOM.createDiv();
  51. private boolean closing = false;
  52. private boolean opening = false;
  53. // Open next tooltip faster. Disabled after 2 sec of showTooltip-silence.
  54. private boolean justClosed = false;
  55. private String uniqueId = DOM.createUniqueId();
  56. private Element layoutElement;
  57. private int maxWidth;
  58. // Delays for the tooltip, configurable on the server side
  59. private int openDelay;
  60. private int quickOpenDelay;
  61. private int quickOpenTimeout;
  62. private int closeTimeout;
  63. /**
  64. * Used to show tooltips; usually used via the singleton in
  65. * {@link ApplicationConnection}. NOTE that #setOwner(Widget)} should be
  66. * called after instantiating.
  67. *
  68. * @see ApplicationConnection#getVTooltip()
  69. */
  70. public VTooltip() {
  71. super(false, false, true);
  72. setStyleName(CLASSNAME);
  73. FlowPanel layout = new FlowPanel();
  74. setWidget(layout);
  75. layout.add(em);
  76. DOM.setElementProperty(description, "className", CLASSNAME + "-text");
  77. layoutElement = layout.getElement();
  78. DOM.appendChild(layoutElement, description);
  79. setSinkShadowEvents(true);
  80. // WAI-ARIA additions
  81. layoutElement.setId(uniqueId);
  82. Roles.getTooltipRole().setAriaLiveProperty(getElement(),
  83. LiveValue.POLITE);
  84. description.setId(DOM.createUniqueId());
  85. Roles.getTooltipRole().set(layoutElement);
  86. Roles.getTooltipRole().setAriaHiddenState(layoutElement, true);
  87. }
  88. /**
  89. * Show a popup containing the information in the "info" tooltip
  90. *
  91. * @param info
  92. */
  93. private void show(TooltipInfo info) {
  94. boolean hasContent = false;
  95. if (info.getErrorMessage() != null) {
  96. em.setVisible(true);
  97. em.updateMessage(info.getErrorMessage());
  98. hasContent = true;
  99. } else {
  100. em.setVisible(false);
  101. }
  102. if (info.getTitle() != null && !"".equals(info.getTitle())) {
  103. DOM.setInnerHTML(description, info.getTitle());
  104. DOM.setStyleAttribute(description, "display", "");
  105. hasContent = true;
  106. } else {
  107. DOM.setInnerHTML(description, "");
  108. DOM.setStyleAttribute(description, "display", "none");
  109. }
  110. if (hasContent) {
  111. // Issue #8454: With IE7 the tooltips size is calculated based on
  112. // the last tooltip's position, causing problems if the last one was
  113. // in the right or bottom edge. For this reason the tooltip is moved
  114. // first to 0,0 position so that the calculation goes correctly.
  115. setPopupPosition(0, 0);
  116. setPopupPositionAndShow(new PositionCallback() {
  117. @Override
  118. public void setPosition(int offsetWidth, int offsetHeight) {
  119. if (offsetWidth > getMaxWidth()) {
  120. setWidth(getMaxWidth() + "px");
  121. // Check new height and width with reflowed content
  122. offsetWidth = getOffsetWidth();
  123. offsetHeight = getOffsetHeight();
  124. }
  125. int x = tooltipEventMouseX + 10 + Window.getScrollLeft();
  126. int y = tooltipEventMouseY + 10 + Window.getScrollTop();
  127. if (x + offsetWidth + MARGIN - Window.getScrollLeft() > Window
  128. .getClientWidth()) {
  129. x = Window.getClientWidth() - offsetWidth - MARGIN
  130. + Window.getScrollLeft();
  131. }
  132. if (y + offsetHeight + MARGIN - Window.getScrollTop() > Window
  133. .getClientHeight()) {
  134. y = tooltipEventMouseY - 5 - offsetHeight
  135. + Window.getScrollTop();
  136. if (y - Window.getScrollTop() < 0) {
  137. // tooltip does not fit on top of the mouse either,
  138. // put it at the top of the screen
  139. y = Window.getScrollTop();
  140. }
  141. }
  142. setPopupPosition(x, y);
  143. sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
  144. }
  145. });
  146. } else {
  147. hide();
  148. }
  149. }
  150. private void showTooltip() {
  151. // Close current tooltip
  152. if (isShowing()) {
  153. closeNow();
  154. }
  155. // Schedule timer for showing the tooltip according to if it was
  156. // recently closed or not.
  157. int timeout = justClosed ? getQuickOpenDelay() : getOpenDelay();
  158. showTimer.schedule(timeout);
  159. opening = true;
  160. }
  161. private void closeNow() {
  162. hide();
  163. setWidth("");
  164. closing = false;
  165. }
  166. private Timer showTimer = new Timer() {
  167. @Override
  168. public void run() {
  169. TooltipInfo info = tooltipEventHandler.getTooltipInfo();
  170. if (null != info) {
  171. show(info);
  172. }
  173. opening = false;
  174. }
  175. };
  176. private Timer closeTimer = new Timer() {
  177. @Override
  178. public void run() {
  179. closeNow();
  180. justClosedTimer.schedule(2000);
  181. justClosed = true;
  182. }
  183. };
  184. private Timer justClosedTimer = new Timer() {
  185. @Override
  186. public void run() {
  187. justClosed = false;
  188. }
  189. };
  190. public void hideTooltip() {
  191. if (opening) {
  192. showTimer.cancel();
  193. opening = false;
  194. }
  195. if (!isAttached()) {
  196. return;
  197. }
  198. if (closing) {
  199. // already about to close
  200. return;
  201. }
  202. closeTimer.schedule(getCloseTimeout());
  203. closing = true;
  204. justClosed = true;
  205. justClosedTimer.schedule(getQuickOpenTimeout());
  206. }
  207. @Override
  208. public void hide() {
  209. super.hide();
  210. Roles.getTooltipRole().setAriaHiddenState(layoutElement, true);
  211. Roles.getTooltipRole().removeAriaDescribedbyProperty(
  212. tooltipEventHandler.currentElement);
  213. }
  214. private int tooltipEventMouseX;
  215. private int tooltipEventMouseY;
  216. public void updatePosition(Event event, boolean isFocused) {
  217. if (isFocused) {
  218. tooltipEventMouseX = -1000;
  219. tooltipEventMouseY = -1000;
  220. } else {
  221. tooltipEventMouseX = DOM.eventGetClientX(event);
  222. tooltipEventMouseY = DOM.eventGetClientY(event);
  223. }
  224. }
  225. @Override
  226. public void onBrowserEvent(Event event) {
  227. final int type = DOM.eventGetType(event);
  228. // cancel closing event if tooltip is mouseovered; the user might want
  229. // to scroll of cut&paste
  230. if (type == Event.ONMOUSEOVER) {
  231. // Cancel closing so tooltip stays open and user can copy paste the
  232. // tooltip
  233. closeTimer.cancel();
  234. closing = false;
  235. }
  236. }
  237. /**
  238. * Replace current open tooltip with new content
  239. */
  240. public void replaceCurrentTooltip() {
  241. if (closing) {
  242. closeTimer.cancel();
  243. closeNow();
  244. }
  245. TooltipInfo info = tooltipEventHandler.getTooltipInfo();
  246. if (null != info) {
  247. show(info);
  248. }
  249. opening = false;
  250. }
  251. private class TooltipEventHandler implements MouseMoveHandler,
  252. ClickHandler, KeyDownHandler, FocusHandler, BlurHandler {
  253. /**
  254. * Current element hovered
  255. */
  256. private com.google.gwt.dom.client.Element currentElement = null;
  257. /**
  258. * Current element focused
  259. */
  260. private boolean currentIsFocused;
  261. /**
  262. * Current tooltip active
  263. */
  264. private TooltipInfo currentTooltipInfo = null;
  265. /**
  266. * Get current active tooltip information
  267. *
  268. * @return Current active tooltip information or null
  269. */
  270. public TooltipInfo getTooltipInfo() {
  271. return currentTooltipInfo;
  272. }
  273. /**
  274. * Locate connector and it's tooltip for given element
  275. *
  276. * @param element
  277. * Element used in search
  278. * @return true if connector and tooltip found
  279. */
  280. private boolean resolveConnector(Element element) {
  281. ApplicationConnection ac = getApplicationConnection();
  282. ComponentConnector connector = Util.getConnectorForElement(ac,
  283. RootPanel.get(), element);
  284. // Try to find first connector with proper tooltip info
  285. TooltipInfo info = null;
  286. while (connector != null) {
  287. info = connector.getTooltipInfo(element);
  288. if (info != null && info.hasMessage()) {
  289. break;
  290. }
  291. if (!(connector.getParent() instanceof ComponentConnector)) {
  292. connector = null;
  293. info = null;
  294. break;
  295. }
  296. connector = (ComponentConnector) connector.getParent();
  297. }
  298. if (connector != null && info != null) {
  299. assert connector.hasTooltip() : "getTooltipInfo for "
  300. + Util.getConnectorString(connector)
  301. + " returned a tooltip even though hasTooltip claims there are no tooltips for the connector.";
  302. currentTooltipInfo = info;
  303. return true;
  304. }
  305. return false;
  306. }
  307. /**
  308. * Handle hide event
  309. *
  310. * @param event
  311. * Event causing hide
  312. */
  313. private void handleHideEvent() {
  314. hideTooltip();
  315. currentTooltipInfo = null;
  316. }
  317. @Override
  318. public void onMouseMove(MouseMoveEvent mme) {
  319. handleShowHide(mme, false);
  320. }
  321. @Override
  322. public void onClick(ClickEvent event) {
  323. handleHideEvent();
  324. }
  325. @Override
  326. public void onKeyDown(KeyDownEvent event) {
  327. handleHideEvent();
  328. }
  329. /**
  330. * Displays Tooltip when page is navigated with the keyboard.
  331. *
  332. * Tooltip is not visible. This makes it possible for assistive devices
  333. * to recognize the tooltip.
  334. */
  335. @Override
  336. public void onFocus(FocusEvent fe) {
  337. handleShowHide(fe, true);
  338. }
  339. /**
  340. * Hides Tooltip when the page is navigated with the keyboard.
  341. *
  342. * Removes the Tooltip from page to make sure assistive devices don't
  343. * recognize it by accident.
  344. */
  345. @Override
  346. public void onBlur(BlurEvent be) {
  347. handleHideEvent();
  348. }
  349. private void handleShowHide(DomEvent domEvent, boolean isFocused) {
  350. Event event = Event.as(domEvent.getNativeEvent());
  351. com.google.gwt.dom.client.Element element = Element.as(event
  352. .getEventTarget());
  353. // We can ignore move event if it's handled by move or over already
  354. if (currentElement == element && currentIsFocused == isFocused) {
  355. return;
  356. }
  357. boolean connectorAndTooltipFound = resolveConnector((com.google.gwt.user.client.Element) element);
  358. if (!connectorAndTooltipFound) {
  359. if (isShowing()) {
  360. handleHideEvent();
  361. Roles.getButtonRole()
  362. .removeAriaDescribedbyProperty(element);
  363. } else {
  364. currentTooltipInfo = null;
  365. }
  366. } else {
  367. updatePosition(event, isFocused);
  368. if (isShowing()) {
  369. replaceCurrentTooltip();
  370. Roles.getTooltipRole().removeAriaDescribedbyProperty(
  371. currentElement);
  372. } else {
  373. showTooltip();
  374. }
  375. Roles.getTooltipRole().setAriaDescribedbyProperty(element,
  376. Id.of(uniqueId));
  377. }
  378. currentIsFocused = isFocused;
  379. currentElement = element;
  380. }
  381. }
  382. private final TooltipEventHandler tooltipEventHandler = new TooltipEventHandler();
  383. /**
  384. * Connects DOM handlers to widget that are needed for tooltip presentation.
  385. *
  386. * @param widget
  387. * Widget which DOM handlers are connected
  388. */
  389. public void connectHandlersToWidget(Widget widget) {
  390. Profiler.enter("VTooltip.connectHandlersToWidget");
  391. widget.addDomHandler(tooltipEventHandler, MouseMoveEvent.getType());
  392. widget.addDomHandler(tooltipEventHandler, ClickEvent.getType());
  393. widget.addDomHandler(tooltipEventHandler, KeyDownEvent.getType());
  394. widget.addDomHandler(tooltipEventHandler, FocusEvent.getType());
  395. widget.addDomHandler(tooltipEventHandler, BlurEvent.getType());
  396. Profiler.leave("VTooltip.connectHandlersToWidget");
  397. }
  398. /**
  399. * Returns the unique id of the tooltip element.
  400. *
  401. * @return String containing the unique id of the tooltip, which always has
  402. * a value
  403. */
  404. public String getUniqueId() {
  405. return uniqueId;
  406. }
  407. @Override
  408. public void setPopupPositionAndShow(PositionCallback callback) {
  409. super.setPopupPositionAndShow(callback);
  410. Roles.getTooltipRole().setAriaHiddenState(layoutElement, false);
  411. }
  412. /**
  413. * Returns the time (in ms) the tooltip should be displayed after an event
  414. * that will cause it to be closed (e.g. mouse click outside the component,
  415. * key down).
  416. *
  417. * @return The close timeout (in ms)
  418. */
  419. public int getCloseTimeout() {
  420. return closeTimeout;
  421. }
  422. /**
  423. * Sets the time (in ms) the tooltip should be displayed after an event that
  424. * will cause it to be closed (e.g. mouse click outside the component, key
  425. * down).
  426. *
  427. * @param closeTimeout
  428. * The close timeout (in ms)
  429. */
  430. public void setCloseTimeout(int closeTimeout) {
  431. this.closeTimeout = closeTimeout;
  432. }
  433. /**
  434. * Returns the time (in ms) during which {@link #getQuickOpenDelay()} should
  435. * be used instead of {@link #getOpenDelay()}. The quick open delay is used
  436. * when the tooltip has very recently been shown, is currently hidden but
  437. * about to be shown again.
  438. *
  439. * @return The quick open timeout (in ms)
  440. */
  441. public int getQuickOpenTimeout() {
  442. return quickOpenTimeout;
  443. }
  444. /**
  445. * Sets the time (in ms) that determines when {@link #getQuickOpenDelay()}
  446. * should be used instead of {@link #getOpenDelay()}. The quick open delay
  447. * is used when the tooltip has very recently been shown, is currently
  448. * hidden but about to be shown again.
  449. *
  450. * @param quickOpenTimeout
  451. * The quick open timeout (in ms)
  452. */
  453. public void setQuickOpenTimeout(int quickOpenTimeout) {
  454. this.quickOpenTimeout = quickOpenTimeout;
  455. }
  456. /**
  457. * Returns the time (in ms) that should elapse before a tooltip will be
  458. * shown, in the situation when a tooltip has very recently been shown
  459. * (within {@link #getQuickOpenDelay()} ms).
  460. *
  461. * @return The quick open delay (in ms)
  462. */
  463. public int getQuickOpenDelay() {
  464. return quickOpenDelay;
  465. }
  466. /**
  467. * Sets the time (in ms) that should elapse before a tooltip will be shown,
  468. * in the situation when a tooltip has very recently been shown (within
  469. * {@link #getQuickOpenDelay()} ms).
  470. *
  471. * @param quickOpenDelay
  472. * The quick open delay (in ms)
  473. */
  474. public void setQuickOpenDelay(int quickOpenDelay) {
  475. this.quickOpenDelay = quickOpenDelay;
  476. }
  477. /**
  478. * Returns the time (in ms) that should elapse after an event triggering
  479. * tooltip showing has occurred (e.g. mouse over) before the tooltip is
  480. * shown. If a tooltip has recently been shown, then
  481. * {@link #getQuickOpenDelay()} is used instead of this.
  482. *
  483. * @return The open delay (in ms)
  484. */
  485. public int getOpenDelay() {
  486. return openDelay;
  487. }
  488. /**
  489. * Sets the time (in ms) that should elapse after an event triggering
  490. * tooltip showing has occurred (e.g. mouse over) before the tooltip is
  491. * shown. If a tooltip has recently been shown, then
  492. * {@link #getQuickOpenDelay()} is used instead of this.
  493. *
  494. * @param openDelay
  495. * The open delay (in ms)
  496. */
  497. public void setOpenDelay(int openDelay) {
  498. this.openDelay = openDelay;
  499. }
  500. /**
  501. * Sets the maximum width of the tooltip popup.
  502. *
  503. * @param maxWidth
  504. * The maximum width the tooltip popup (in pixels)
  505. */
  506. public void setMaxWidth(int maxWidth) {
  507. this.maxWidth = maxWidth;
  508. }
  509. /**
  510. * Returns the maximum width of the tooltip popup.
  511. *
  512. * @return The maximum width the tooltip popup (in pixels)
  513. */
  514. public int getMaxWidth() {
  515. return maxWidth;
  516. }
  517. }