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

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