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

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