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

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