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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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.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.event.dom.client.BlurEvent;
  22. import com.google.gwt.event.dom.client.BlurHandler;
  23. import com.google.gwt.event.dom.client.ClickEvent;
  24. import com.google.gwt.event.dom.client.ClickHandler;
  25. import com.google.gwt.event.dom.client.DomEvent;
  26. import com.google.gwt.event.dom.client.FocusEvent;
  27. import com.google.gwt.event.dom.client.FocusHandler;
  28. import com.google.gwt.event.dom.client.KeyDownEvent;
  29. import com.google.gwt.event.dom.client.KeyDownHandler;
  30. import com.google.gwt.event.dom.client.MouseMoveEvent;
  31. import com.google.gwt.event.dom.client.MouseMoveHandler;
  32. import com.google.gwt.user.client.DOM;
  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.VWindowOverlay;
  40. /**
  41. * TODO open for extension
  42. */
  43. public class VTooltip extends VWindowOverlay {
  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 TooltipInfo currentTooltipInfo = new TooltipInfo(" ");
  52. private boolean closing = false;
  53. private boolean opening = false;
  54. // Open next tooltip faster. Disabled after 2 sec of showTooltip-silence.
  55. private boolean justClosed = false;
  56. private String uniqueId = DOM.createUniqueId();
  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. DOM.appendChild(layout.getElement(), description);
  78. setSinkShadowEvents(true);
  79. // When a tooltip is shown, the content of the tooltip changes. With a
  80. // tooltip being a live-area, this change is notified to a assistive
  81. // device.
  82. Roles.getTooltipRole().set(getElement());
  83. Roles.getTooltipRole().setAriaLiveProperty(getElement(),
  84. LiveValue.ASSERTIVE);
  85. Roles.getTooltipRole().setAriaRelevantProperty(getElement(),
  86. RelevantValue.ADDITIONS);
  87. }
  88. /**
  89. * Show the tooltip with the provided info for assistive devices.
  90. *
  91. * @param info
  92. * with the content of the tooltip
  93. */
  94. public void showAssistive(TooltipInfo info) {
  95. updatePosition(null, true);
  96. setTooltipText(info);
  97. showTooltip();
  98. }
  99. private void setTooltipText(TooltipInfo info) {
  100. if (info.getErrorMessage() != null) {
  101. em.setVisible(true);
  102. em.updateMessage(info.getErrorMessage());
  103. } else {
  104. em.setVisible(false);
  105. }
  106. if (info.getTitle() != null && !"".equals(info.getTitle())) {
  107. description.setInnerHTML(info.getTitle());
  108. } else {
  109. description.setInnerHTML("");
  110. }
  111. currentTooltipInfo = info;
  112. }
  113. /**
  114. * Show a popup containing the currentTooltipInfo
  115. *
  116. */
  117. private void showTooltip() {
  118. boolean hasContent = false;
  119. if (currentTooltipInfo.getErrorMessage() != null
  120. || (currentTooltipInfo.getTitle() != null && !""
  121. .equals(currentTooltipInfo.getTitle()))) {
  122. hasContent = true;
  123. }
  124. if (hasContent) {
  125. // Issue #8454: With IE7 the tooltips size is calculated based on
  126. // the last tooltip's position, causing problems if the last one was
  127. // in the right or bottom edge. For this reason the tooltip is moved
  128. // first to 0,0 position so that the calculation goes correctly.
  129. setPopupPosition(0, 0);
  130. setPopupPositionAndShow(new PositionCallback() {
  131. @Override
  132. public void setPosition(int offsetWidth, int offsetHeight) {
  133. if (offsetWidth > getMaxWidth()) {
  134. setWidth(getMaxWidth() + "px");
  135. // Check new height and width with reflowed content
  136. offsetWidth = getOffsetWidth();
  137. offsetHeight = getOffsetHeight();
  138. }
  139. int x = tooltipEventMouseX + 10 + Window.getScrollLeft();
  140. int y = tooltipEventMouseY + 10 + Window.getScrollTop();
  141. if (x + offsetWidth + MARGIN - Window.getScrollLeft() > Window
  142. .getClientWidth()) {
  143. x = Window.getClientWidth() - offsetWidth - MARGIN
  144. + Window.getScrollLeft();
  145. }
  146. if (y + offsetHeight + MARGIN - Window.getScrollTop() > Window
  147. .getClientHeight()) {
  148. y = tooltipEventMouseY - 5 - offsetHeight
  149. + Window.getScrollTop();
  150. if (y - Window.getScrollTop() < 0) {
  151. // tooltip does not fit on top of the mouse either,
  152. // put it at the top of the screen
  153. y = Window.getScrollTop();
  154. }
  155. }
  156. setPopupPosition(x, y);
  157. sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
  158. }
  159. });
  160. } else {
  161. hide();
  162. }
  163. }
  164. /**
  165. * For assistive tooltips to work correctly we must have the tooltip visible
  166. * and attached to the DOM well in advance.
  167. *
  168. * @return
  169. */
  170. public boolean isActuallyVisible() {
  171. return super.isShowing() && getPopupLeft() > 0 && getPopupTop() > 0;
  172. }
  173. private void closeNow() {
  174. hide();
  175. setWidth("");
  176. closing = false;
  177. }
  178. private Timer showTimer = new Timer() {
  179. @Override
  180. public void run() {
  181. opening = false;
  182. showTooltip();
  183. }
  184. };
  185. private Timer closeTimer = new Timer() {
  186. @Override
  187. public void run() {
  188. closeNow();
  189. justClosedTimer.schedule(getQuickOpenTimeout());
  190. justClosed = true;
  191. }
  192. };
  193. private Timer justClosedTimer = new Timer() {
  194. @Override
  195. public void run() {
  196. justClosed = false;
  197. }
  198. };
  199. public void hideTooltip() {
  200. if (opening) {
  201. showTimer.cancel();
  202. opening = false;
  203. }
  204. if (!isAttached()) {
  205. return;
  206. }
  207. if (closing) {
  208. // already about to close
  209. return;
  210. }
  211. closeTimer.schedule(getCloseTimeout());
  212. closing = true;
  213. }
  214. @Override
  215. public void hide() {
  216. em.updateMessage("");
  217. description.setInnerHTML("");
  218. updatePosition(null, true);
  219. setPopupPosition(tooltipEventMouseX, tooltipEventMouseY);
  220. }
  221. private int tooltipEventMouseX;
  222. private int tooltipEventMouseY;
  223. public void updatePosition(Event event, boolean isFocused) {
  224. tooltipEventMouseX = getEventX(event, isFocused);
  225. tooltipEventMouseY = getEventY(event, isFocused);
  226. }
  227. private int getEventX(Event event, boolean isFocused) {
  228. return isFocused ? -5000 : DOM.eventGetClientX(event);
  229. }
  230. private int getEventY(Event event, boolean isFocused) {
  231. return isFocused ? -5000 : DOM.eventGetClientY(event);
  232. }
  233. @Override
  234. public void onBrowserEvent(Event event) {
  235. final int type = DOM.eventGetType(event);
  236. // cancel closing event if tooltip is mouseovered; the user might want
  237. // to scroll of cut&paste
  238. if (type == Event.ONMOUSEOVER) {
  239. // Cancel closing so tooltip stays open and user can copy paste the
  240. // tooltip
  241. closeTimer.cancel();
  242. closing = false;
  243. }
  244. }
  245. /**
  246. * Replace current open tooltip with new content
  247. */
  248. public void replaceCurrentTooltip() {
  249. if (closing) {
  250. closeTimer.cancel();
  251. closeNow();
  252. }
  253. showTooltip();
  254. opening = false;
  255. }
  256. private class TooltipEventHandler implements MouseMoveHandler,
  257. ClickHandler, KeyDownHandler, FocusHandler, BlurHandler {
  258. /**
  259. * Current element hovered
  260. */
  261. private com.google.gwt.dom.client.Element currentElement = null;
  262. /**
  263. * Marker for handling of tooltip through focus
  264. */
  265. private boolean handledByFocus;
  266. /**
  267. * Locate the tooltip for given element
  268. *
  269. * @param element
  270. * Element used in search
  271. * @return TooltipInfo if connector and tooltip found, null if not
  272. */
  273. private TooltipInfo getTooltipFor(Element element) {
  274. ApplicationConnection ac = getApplicationConnection();
  275. ComponentConnector connector = Util.getConnectorForElement(ac,
  276. RootPanel.get(), element);
  277. // Try to find first connector with proper tooltip info
  278. TooltipInfo info = null;
  279. while (connector != null) {
  280. info = connector.getTooltipInfo(element);
  281. if (info != null && info.hasMessage()) {
  282. break;
  283. }
  284. if (!(connector.getParent() instanceof ComponentConnector)) {
  285. connector = null;
  286. info = null;
  287. break;
  288. }
  289. connector = (ComponentConnector) connector.getParent();
  290. }
  291. if (connector != null && info != null) {
  292. assert connector.hasTooltip() : "getTooltipInfo for "
  293. + Util.getConnectorString(connector)
  294. + " returned a tooltip even though hasTooltip claims there are no tooltips for the connector.";
  295. return info;
  296. }
  297. return null;
  298. }
  299. /**
  300. * Handle hide event
  301. *
  302. * @param event
  303. * Event causing hide
  304. */
  305. private void handleHideEvent() {
  306. hideTooltip();
  307. }
  308. @Override
  309. public void onMouseMove(MouseMoveEvent mme) {
  310. handleShowHide(mme, false);
  311. }
  312. @Override
  313. public void onClick(ClickEvent event) {
  314. handleHideEvent();
  315. }
  316. @Override
  317. public void onKeyDown(KeyDownEvent event) {
  318. handleHideEvent();
  319. }
  320. /**
  321. * Displays Tooltip when page is navigated with the keyboard.
  322. *
  323. * Tooltip is not visible. This makes it possible for assistive devices
  324. * to recognize the tooltip.
  325. */
  326. @Override
  327. public void onFocus(FocusEvent fe) {
  328. handleShowHide(fe, true);
  329. }
  330. /**
  331. * Hides Tooltip when the page is navigated with the keyboard.
  332. *
  333. * Removes the Tooltip from page to make sure assistive devices don't
  334. * recognize it by accident.
  335. */
  336. @Override
  337. public void onBlur(BlurEvent be) {
  338. handledByFocus = false;
  339. handleHideEvent();
  340. }
  341. private void handleShowHide(DomEvent domEvent, boolean isFocused) {
  342. Event event = Event.as(domEvent.getNativeEvent());
  343. Element element = Element.as(event.getEventTarget());
  344. // We can ignore move event if it's handled by move or over already
  345. if (currentElement == element && handledByFocus == true) {
  346. return;
  347. }
  348. TooltipInfo info = getTooltipFor(element);
  349. if (info == null) {
  350. if (isActuallyVisible()) {
  351. handleHideEvent();
  352. }
  353. } else {
  354. setTooltipText(info);
  355. updatePosition(event, isFocused);
  356. if (isActuallyVisible() && !isFocused) {
  357. showTooltip();
  358. } else {
  359. if (isActuallyVisible()) {
  360. closeNow();
  361. }
  362. // Schedule timer for showing the tooltip according to if it
  363. // was
  364. // recently closed or not.
  365. int timeout = justClosed ? getQuickOpenDelay()
  366. : getOpenDelay();
  367. if (timeout == 0) {
  368. showTooltip();
  369. } else {
  370. showTimer.schedule(timeout);
  371. opening = true;
  372. }
  373. }
  374. }
  375. handledByFocus = 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. if (isAttached()) {
  407. callback.setPosition(getOffsetWidth(), getOffsetHeight());
  408. } else {
  409. super.setPopupPositionAndShow(callback);
  410. }
  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. }