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.

Util.java 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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 java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.google.gwt.core.client.GWT;
  25. import com.google.gwt.core.client.JavaScriptObject;
  26. import com.google.gwt.dom.client.Element;
  27. import com.google.gwt.dom.client.NativeEvent;
  28. import com.google.gwt.event.dom.client.KeyEvent;
  29. import com.google.gwt.user.client.DOM;
  30. import com.google.gwt.user.client.Event;
  31. import com.google.gwt.user.client.ui.HasWidgets;
  32. import com.google.gwt.user.client.ui.Widget;
  33. import com.vaadin.client.HasChildMeasurementHintConnector.ChildMeasurementHint;
  34. import com.vaadin.client.RenderInformation.FloatSize;
  35. import com.vaadin.client.ui.AbstractLayoutConnector;
  36. import com.vaadin.client.ui.ManagedLayout;
  37. import com.vaadin.client.ui.VOverlay;
  38. import com.vaadin.client.ui.layout.ElementResizeListener;
  39. import com.vaadin.shared.AbstractComponentState;
  40. import com.vaadin.shared.ApplicationConstants;
  41. import com.vaadin.shared.communication.MethodInvocation;
  42. import com.vaadin.shared.ui.ComponentStateUtil;
  43. import com.vaadin.shared.util.SharedUtil;
  44. import elemental.js.json.JsJsonValue;
  45. import elemental.json.JsonValue;
  46. public class Util {
  47. /**
  48. * Helper method for debugging purposes.
  49. *
  50. * Stops execution on firefox browsers on a breakpoint.
  51. *
  52. * @deprecated As of 7.4.0, use {@link WidgetUtil#browserDebugger()}
  53. * instead.
  54. */
  55. @Deprecated
  56. public static void browserDebugger() {
  57. WidgetUtil.browserDebugger();
  58. }
  59. /**
  60. * Helper method for a bug fix #14041. For mozilla getKeyCode return 0 for
  61. * space bar (because space is considered as char). If return 0 use
  62. * getCharCode.
  63. *
  64. * @deprecated As of 7.4.0, use {@link WidgetUtil#getKeyCode(KeyEvent)}
  65. * instead.
  66. *
  67. * @param event
  68. * @return return key code
  69. * @since 7.2.4
  70. */
  71. @Deprecated
  72. public static int getKeyCode(KeyEvent<?> event) {
  73. return WidgetUtil.getKeyCode(event);
  74. }
  75. /**
  76. *
  77. * Returns the topmost element of from given coordinates.
  78. *
  79. * TODO fix crossplat issues clientX vs pageX. See quircksmode. Not critical
  80. * for vaadin as we scroll div istead of page.
  81. *
  82. * @deprecated As of 7.4.0, use
  83. * {@link WidgetUtil#getElementFromPoint(int, int)} instead.
  84. *
  85. * @param x
  86. * @param y
  87. * @return the element at given coordinates
  88. */
  89. @Deprecated
  90. public static com.google.gwt.user.client.Element getElementFromPoint(
  91. int clientX, int clientY) {
  92. return DOM.asOld(WidgetUtil.getElementFromPoint(clientX, clientY));
  93. }
  94. /**
  95. * This helper method can be called if components size have been changed
  96. * outside rendering phase. It notifies components parent about the size
  97. * change so it can react.
  98. *
  99. * When using this method, developer should consider if size changes could
  100. * be notified lazily. If lazy flag is true, method will save widget and
  101. * wait for a moment until it notifies parents in chunks. This may vastly
  102. * optimize layout in various situation. Example: if component have a lot of
  103. * images their onload events may fire "layout phase" many times in a short
  104. * period.
  105. *
  106. * @param widget
  107. * @param lazy
  108. * run componentSizeUpdated lazyly
  109. *
  110. * @deprecated As of 7.0, use
  111. * {@link LayoutManager#setNeedsMeasure(ComponentConnector)}
  112. * instead
  113. */
  114. @Deprecated
  115. public static void notifyParentOfSizeChange(Widget widget, boolean lazy) {
  116. ComponentConnector connector = findConnectorFor(widget);
  117. if (connector != null) {
  118. connector.getLayoutManager().setNeedsMeasure(connector);
  119. if (!lazy) {
  120. connector.getLayoutManager().layoutNow();
  121. }
  122. }
  123. }
  124. public static ComponentConnector findConnectorFor(Widget widget) {
  125. List<ApplicationConnection> runningApplications = ApplicationConfiguration
  126. .getRunningApplications();
  127. for (ApplicationConnection applicationConnection : runningApplications) {
  128. ConnectorMap connectorMap = applicationConnection.getConnectorMap();
  129. ComponentConnector connector = connectorMap.getConnector(widget);
  130. if (connector == null) {
  131. continue;
  132. }
  133. if (connector.getConnection() == applicationConnection) {
  134. return connector;
  135. }
  136. }
  137. return null;
  138. }
  139. /**
  140. * @deprecated As of 7.4.0, use {@link WidgetUtil#parseRelativeSize(String)}
  141. * instead.
  142. */
  143. @Deprecated
  144. public static float parseRelativeSize(String size) {
  145. return WidgetUtil.parseRelativeSize(size);
  146. }
  147. /**
  148. * Converts html entities to text.
  149. *
  150. * @deprecated As of 7.4.0, use {@link WidgetUtil#escapeHTML(String)}
  151. * instead.
  152. *
  153. * @param html
  154. * @return escaped string presentation of given html
  155. */
  156. @Deprecated
  157. public static String escapeHTML(String html) {
  158. return WidgetUtil.escapeHTML(html);
  159. }
  160. /**
  161. * Escapes the string so it is safe to write inside an HTML attribute.
  162. *
  163. * @deprecated As of 7.4.0, use {@link WidgetUtil#escapeAttribute(String)}
  164. * instead.
  165. *
  166. * @param attribute
  167. * The string to escape
  168. * @return An escaped version of <literal>attribute</literal>.
  169. */
  170. @Deprecated
  171. public static String escapeAttribute(String attribute) {
  172. return WidgetUtil.escapeAttribute(attribute);
  173. }
  174. /**
  175. * Clones given element as in JavaScript.
  176. *
  177. * Deprecate this if there appears similar method into GWT someday.
  178. *
  179. * @deprecated As of 7.4.0, use
  180. * {@link WidgetUtil#cloneNode(Element, boolean)} instead.
  181. *
  182. * @param element
  183. * @param deep
  184. * clone child tree also
  185. * @return
  186. */
  187. @Deprecated
  188. public static com.google.gwt.user.client.Element cloneNode(Element element,
  189. boolean deep) {
  190. return DOM.asOld(WidgetUtil.cloneNode(element, deep));
  191. }
  192. /**
  193. * @deprecated As of 7.4.0, use
  194. * {@link WidgetUtil#measureHorizontalPaddingAndBorder(Element, int)}
  195. * instead.
  196. */
  197. @Deprecated
  198. public static int measureHorizontalPaddingAndBorder(Element element,
  199. int paddingGuess) {
  200. return WidgetUtil.measureHorizontalPaddingAndBorder(element,
  201. paddingGuess);
  202. }
  203. /**
  204. * @deprecated As of 7.4.0, use
  205. * {@link WidgetUtil#measureVerticalPaddingAndBorder(Element, int)}
  206. * instead.
  207. */
  208. @Deprecated
  209. public static int measureVerticalPaddingAndBorder(Element element,
  210. int paddingGuess) {
  211. return WidgetUtil
  212. .measureVerticalPaddingAndBorder(element, paddingGuess);
  213. }
  214. /**
  215. * @deprecated As of 7.4.0, use
  216. * {@link WidgetUtil#measureHorizontalBorder(Element)} instead.
  217. */
  218. @Deprecated
  219. public static int measureHorizontalBorder(Element element) {
  220. return WidgetUtil.measureHorizontalBorder(element);
  221. }
  222. /**
  223. * @deprecated As of 7.4.0, use
  224. * {@link WidgetUtil#measureVerticalBorder(Element)} instead.
  225. */
  226. @Deprecated
  227. public static int measureVerticalBorder(Element element) {
  228. return WidgetUtil.measureVerticalBorder(element);
  229. }
  230. /**
  231. * @deprecated As of 7.4.0, use
  232. * {@link WidgetUtil#measureMarginLeft(Element)} instead.
  233. */
  234. @Deprecated
  235. public static int measureMarginLeft(Element element) {
  236. return WidgetUtil.measureMarginLeft(element);
  237. }
  238. /**
  239. * @deprecated As of 7.4.0, use
  240. * {@link WidgetUtil#setHeightExcludingPaddingAndBorder(Widget, String, int)}
  241. * instead.
  242. */
  243. @Deprecated
  244. public static int setHeightExcludingPaddingAndBorder(Widget widget,
  245. String height, int paddingBorderGuess) {
  246. return WidgetUtil.setHeightExcludingPaddingAndBorder(widget, height,
  247. paddingBorderGuess);
  248. }
  249. /**
  250. * @deprecated As of 7.4.0, use
  251. * {@link WidgetUtil#setWidthExcludingPaddingAndBorder(Widget, String, int)}
  252. * instead.
  253. */
  254. @Deprecated
  255. public static int setWidthExcludingPaddingAndBorder(Widget widget,
  256. String width, int paddingBorderGuess) {
  257. return WidgetUtil.setWidthExcludingPaddingAndBorder(widget, width,
  258. paddingBorderGuess);
  259. }
  260. /**
  261. * @deprecated As of 7.4.0, use
  262. * {@link WidgetUtil#setWidthExcludingPaddingAndBorder(Element, int, int, boolean)}
  263. * instead.
  264. */
  265. @Deprecated
  266. public static int setWidthExcludingPaddingAndBorder(Element element,
  267. int requestedWidth, int horizontalPaddingBorderGuess,
  268. boolean requestedWidthIncludesPaddingBorder) {
  269. return WidgetUtil.setWidthExcludingPaddingAndBorder(element,
  270. requestedWidth, horizontalPaddingBorderGuess,
  271. requestedWidthIncludesPaddingBorder);
  272. }
  273. /**
  274. * @deprecated As of 7.4.0, use
  275. * {@link WidgetUtil#setHeightExcludingPaddingAndBorder(Element, int, int, boolean)}
  276. * instead.
  277. */
  278. @Deprecated
  279. public static int setHeightExcludingPaddingAndBorder(Element element,
  280. int requestedHeight, int verticalPaddingBorderGuess,
  281. boolean requestedHeightIncludesPaddingBorder) {
  282. return WidgetUtil.setHeightExcludingPaddingAndBorder(element,
  283. requestedHeight, verticalPaddingBorderGuess,
  284. requestedHeightIncludesPaddingBorder);
  285. }
  286. /**
  287. * @deprecated As of 7.4, use {@link Class#getSimpleName()} instead.
  288. */
  289. @Deprecated
  290. public static String getSimpleName(Object widget) {
  291. if (widget == null) {
  292. return "(null)";
  293. }
  294. String name = widget.getClass().getName();
  295. return name.substring(name.lastIndexOf('.') + 1);
  296. }
  297. /**
  298. * @deprecated As of 7.4.0, use {@link WidgetUtil#setFloat(Element, String)}
  299. * instead.
  300. */
  301. @Deprecated
  302. public static void setFloat(Element element, String value) {
  303. WidgetUtil.setFloat(element, value);
  304. }
  305. /**
  306. * @deprecated As of 7.4.0, use {@link WidgetUtil#getNativeScrollbarSize()}
  307. * instead.
  308. */
  309. @Deprecated
  310. public static int getNativeScrollbarSize() {
  311. return WidgetUtil.getNativeScrollbarSize();
  312. }
  313. /**
  314. * Defers the execution of {@link #runWebkitOverflowAutoFix(Element)}
  315. *
  316. * @deprecated As of 7.4.0, use
  317. * {@link WidgetUtil#runWebkitOverflowAutoFixDeferred(Element)}
  318. * instead.
  319. *
  320. * @since 7.2.6
  321. * @param elem
  322. * with overflow auto
  323. */
  324. @Deprecated
  325. public static void runWebkitOverflowAutoFixDeferred(final Element elem) {
  326. WidgetUtil.runWebkitOverflowAutoFixDeferred(elem);
  327. }
  328. /**
  329. * Run workaround for webkits overflow auto issue.
  330. *
  331. * See: our bug #2138 and https://bugs.webkit.org/show_bug.cgi?id=21462
  332. *
  333. * @deprecated As of 7.4.0, use
  334. * {@link WidgetUtil#runWebkitOverflowAutoFix(Element)} instead.
  335. *
  336. * @param elem
  337. * with overflow auto
  338. */
  339. @Deprecated
  340. public static void runWebkitOverflowAutoFix(final Element elem) {
  341. WidgetUtil.runWebkitOverflowAutoFix(elem);
  342. }
  343. /**
  344. * Parses shared state and fetches the relative size of the component. If a
  345. * dimension is not specified as relative it will return -1. If the shared
  346. * state does not contain width or height specifications this will return
  347. * null.
  348. *
  349. * @param state
  350. * @return
  351. */
  352. public static FloatSize parseRelativeSize(AbstractComponentState state) {
  353. if (ComponentStateUtil.isUndefinedHeight(state)
  354. && ComponentStateUtil.isUndefinedWidth(state)) {
  355. return null;
  356. }
  357. float relativeWidth = WidgetUtil.parseRelativeSize(state.width);
  358. float relativeHeight = WidgetUtil.parseRelativeSize(state.height);
  359. FloatSize relativeSize = new FloatSize(relativeWidth, relativeHeight);
  360. return relativeSize;
  361. }
  362. @Deprecated
  363. public static boolean isCached(UIDL uidl) {
  364. return uidl.getBooleanAttribute("cached");
  365. }
  366. /**
  367. * @deprecated As of 7.4.0, use {@link WidgetUtil#alert(String)} instead.
  368. */
  369. @Deprecated
  370. public static void alert(String string) {
  371. WidgetUtil.alert(string);
  372. }
  373. /**
  374. * Checks if a and b are equals using {@link #equals(Object)}. Handles null
  375. * values as well. Does not ensure that objects are of the same type.
  376. * Assumes that the first object's equals method handle equals properly.
  377. *
  378. * @param a
  379. * The first value to compare
  380. * @param b
  381. * The second value to compare
  382. * @return
  383. * @deprecated As of 7.1 use {@link SharedUtil#equals(Object)} instead
  384. */
  385. @Deprecated
  386. public static boolean equals(Object a, Object b) {
  387. return SharedUtil.equals(a, b);
  388. }
  389. public static void updateRelativeChildrenAndSendSizeUpdateEvent(
  390. ApplicationConnection client, HasWidgets container, Widget widget) {
  391. notifyParentOfSizeChange(widget, false);
  392. }
  393. /**
  394. * Gets the border-box width for the given element, i.e. element width +
  395. * border + padding. Always rounds up to nearest integer.
  396. *
  397. * @deprecated As of 7.4.0, use {@link WidgetUtil#getRequiredWidth(Element)}
  398. * instead.
  399. *
  400. * @param element
  401. * The element to check
  402. * @return The border-box width for the element
  403. */
  404. @Deprecated
  405. public static int getRequiredWidth(com.google.gwt.dom.client.Element element) {
  406. return WidgetUtil.getRequiredWidth(element);
  407. }
  408. /**
  409. * Gets the border-box height for the given element, i.e. element height +
  410. * border + padding. Always rounds up to nearest integer.
  411. *
  412. * @deprecated As of 7.4.0, use
  413. * {@link WidgetUtil#getRequiredHeight(Element)} instead.
  414. *
  415. * @param element
  416. * The element to check
  417. * @return The border-box height for the element
  418. */
  419. @Deprecated
  420. public static int getRequiredHeight(
  421. com.google.gwt.dom.client.Element element) {
  422. return WidgetUtil.getRequiredHeight(element);
  423. }
  424. /**
  425. * @deprecated As of 7.4.0, use
  426. * {@link WidgetUtil#getRequiredWidthBoundingClientRect(Element)}
  427. * instead.
  428. */
  429. @Deprecated
  430. public int getRequiredWidthBoundingClientRect(
  431. com.google.gwt.dom.client.Element element) {
  432. return WidgetUtil.getRequiredWidthBoundingClientRect(element);
  433. }
  434. /**
  435. * @deprecated As of 7.4.0, use
  436. * {@link WidgetUtil#getRequiredHeightComputedStyle(Element)}
  437. * instead.
  438. */
  439. @Deprecated
  440. public static int getRequiredHeightComputedStyle(
  441. com.google.gwt.dom.client.Element element) {
  442. return WidgetUtil.getRequiredHeightComputedStyle(element);
  443. }
  444. /**
  445. * @deprecated As of 7.4.0, use
  446. * {@link WidgetUtil#getRequiredWidthComputedStyle(Element)}
  447. * instead.
  448. */
  449. @Deprecated
  450. public static int getRequiredWidthComputedStyle(
  451. com.google.gwt.dom.client.Element element) {
  452. return WidgetUtil.getRequiredWidthComputedStyle(element);
  453. }
  454. /**
  455. * @deprecated As of 7.4.0, use
  456. * {@link WidgetUtil#getRequiredHeightBoundingClientRect(Element)}
  457. * instead.
  458. */
  459. @Deprecated
  460. public static int getRequiredHeightBoundingClientRect(
  461. com.google.gwt.dom.client.Element element) {
  462. return WidgetUtil.getRequiredHeightBoundingClientRect(element);
  463. }
  464. /**
  465. * @deprecated As of 7.4.0, use {@link WidgetUtil#getRequiredWidth(Widget)}
  466. * instead.
  467. */
  468. @Deprecated
  469. public static int getRequiredWidth(Widget widget) {
  470. return WidgetUtil.getRequiredWidth(widget);
  471. }
  472. /**
  473. * @deprecated As of 7.4.0, use {@link WidgetUtil#getRequiredHeight(Widget)}
  474. * instead.
  475. */
  476. @Deprecated
  477. public static int getRequiredHeight(Widget widget) {
  478. return WidgetUtil.getRequiredHeight(widget);
  479. }
  480. /**
  481. * Detects what is currently the overflow style attribute in given element.
  482. *
  483. * @deprecated As of 7.4.0, use
  484. * {@link WidgetUtil#mayHaveScrollBars(Element)} instead.
  485. *
  486. * @param pe
  487. * the element to detect
  488. * @return true if auto or scroll
  489. */
  490. @Deprecated
  491. public static boolean mayHaveScrollBars(com.google.gwt.dom.client.Element pe) {
  492. return WidgetUtil.mayHaveScrollBars(pe);
  493. }
  494. /**
  495. * Locates the nested child component of <literal>parent</literal> which
  496. * contains the element <literal>element</literal>. The child component is
  497. * also returned if "element" is part of its caption. If
  498. * <literal>element</literal> is not part of any child component, null is
  499. * returned.
  500. *
  501. * This method returns the deepest nested VPaintableWidget.
  502. *
  503. * @param client
  504. * A reference to ApplicationConnection
  505. * @param parent
  506. * The widget that contains <literal>element</literal>.
  507. * @param element
  508. * An element that is a sub element of the parent
  509. * @return The VPaintableWidget which the element is a part of. Null if the
  510. * element does not belong to a child.
  511. */
  512. public static ComponentConnector getConnectorForElement(
  513. ApplicationConnection client, Widget parent, Element element) {
  514. Element browseElement = element;
  515. Element rootElement = parent.getElement();
  516. while (browseElement != null && browseElement != rootElement) {
  517. ComponentConnector connector = ConnectorMap.get(client)
  518. .getConnector(browseElement);
  519. if (connector == null) {
  520. String ownerPid = VCaption.getCaptionOwnerPid(browseElement);
  521. if (ownerPid != null) {
  522. connector = (ComponentConnector) ConnectorMap.get(client)
  523. .getConnector(ownerPid);
  524. }
  525. }
  526. if (connector != null) {
  527. // check that inside the rootElement
  528. while (browseElement != null && browseElement != rootElement) {
  529. browseElement = browseElement.getParentElement();
  530. }
  531. if (browseElement != rootElement) {
  532. return null;
  533. } else {
  534. return connector;
  535. }
  536. }
  537. browseElement = browseElement.getParentElement();
  538. }
  539. // No connector found, element is possibly inside a VOverlay
  540. // If the overlay has an owner, try to find the owner's connector
  541. VOverlay overlay = findWidget(element, VOverlay.class);
  542. if (overlay != null && overlay.getOwner() != null) {
  543. return getConnectorForElement(client, client.getUIConnector()
  544. .getWidget(), overlay.getOwner().getElement());
  545. } else {
  546. return null;
  547. }
  548. }
  549. /**
  550. * Will (attempt) to focus the given DOM Element.
  551. *
  552. * @deprecated As of 7.4.0, use {@link WidgetUtil#focus(Element)} instead.
  553. *
  554. * @param el
  555. * the element to focus
  556. */
  557. @Deprecated
  558. public static void focus(Element el) {
  559. WidgetUtil.focus(el);
  560. }
  561. /**
  562. * Helper method to find the nearest parent paintable instance by traversing
  563. * the DOM upwards from given element.
  564. *
  565. * @param element
  566. * the element to start from
  567. */
  568. public static ComponentConnector findPaintable(
  569. ApplicationConnection client, Element element) {
  570. Widget widget = Util.findWidget(element, null);
  571. ConnectorMap vPaintableMap = ConnectorMap.get(client);
  572. while (widget != null && !vPaintableMap.isConnector(widget)) {
  573. widget = widget.getParent();
  574. }
  575. return vPaintableMap.getConnector(widget);
  576. }
  577. /**
  578. * Helper method to find first instance of given Widget type found by
  579. * traversing DOM upwards from given element.
  580. *
  581. * @deprecated As of 7.4.0, use
  582. * {@link WidgetUtil#findWidget(Element, Class)} instead.
  583. *
  584. * @param element
  585. * the element where to start seeking of Widget
  586. * @param class1
  587. * the Widget type to seek for
  588. */
  589. @Deprecated
  590. public static <T> T findWidget(Element element,
  591. Class<? extends Widget> class1) {
  592. return WidgetUtil.findWidget(element, class1);
  593. }
  594. /**
  595. * Force webkit to redraw an element
  596. *
  597. * @deprecated As of 7.4.0, use
  598. * {@link WidgetUtil#forceWebkitRedraw(Element)} instead.
  599. *
  600. * @param element
  601. * The element that should be redrawn
  602. */
  603. @Deprecated
  604. public static void forceWebkitRedraw(Element element) {
  605. WidgetUtil.forceWebkitRedraw(element);
  606. }
  607. /**
  608. * Performs a hack to trigger a re-layout in the IE8. This is usually
  609. * necessary in cases where IE8 "forgets" to update child elements when they
  610. * resize.
  611. *
  612. * @deprecated As of 7.4.0, use {@link WidgetUtil#forceIE8Redraw(Element)}
  613. * instead.
  614. *
  615. * @param e
  616. * The element to perform the hack on
  617. */
  618. @Deprecated
  619. public static final void forceIE8Redraw(Element e) {
  620. WidgetUtil.forceIE8Redraw(e);
  621. }
  622. /**
  623. * Performs a hack to trigger a re-layout in the IE browser. This is usually
  624. * necessary in cases where IE "forgets" to update child elements when they
  625. * resize.
  626. *
  627. * @deprecated As of 7.4.0, use {@link WidgetUtil#forceIERedraw(Element)}
  628. * instead.
  629. *
  630. * @since 7.3
  631. * @param e
  632. * The element to perform the hack on
  633. */
  634. @Deprecated
  635. public static void forceIERedraw(Element e) {
  636. WidgetUtil.forceIERedraw(e);
  637. }
  638. /**
  639. * Detaches and re-attaches the element from its parent. The element is
  640. * reattached at the same position in the DOM as it was before.
  641. *
  642. * Does nothing if the element is not attached to the DOM.
  643. *
  644. * @deprecated As of 7.4.0, use {@link WidgetUtil#detachAttach(Element)}
  645. * instead.
  646. *
  647. * @param element
  648. * The element to detach and re-attach
  649. */
  650. @Deprecated
  651. public static void detachAttach(Element element) {
  652. WidgetUtil.detachAttach(element);
  653. }
  654. /**
  655. * @deprecated As of 7.4.0, use
  656. * {@link WidgetUtil#sinkOnloadForImages(Element)} instead.
  657. */
  658. @Deprecated
  659. public static void sinkOnloadForImages(Element element) {
  660. WidgetUtil.sinkOnloadForImages(element);
  661. }
  662. /**
  663. * Returns the index of the childElement within its parent.
  664. *
  665. * @deprecated As of 7.4.0, use
  666. * {@link WidgetUtil#getChildElementIndex(Element)} instead.
  667. *
  668. * @param subElement
  669. * @return
  670. */
  671. @Deprecated
  672. public static int getChildElementIndex(Element childElement) {
  673. return WidgetUtil.getChildElementIndex(childElement);
  674. }
  675. private static void printConnectorInvocations(
  676. ArrayList<MethodInvocation> invocations, String id,
  677. ApplicationConnection c) {
  678. ServerConnector connector = ConnectorMap.get(c).getConnector(id);
  679. if (connector != null) {
  680. getLogger().info("\t" + id + " (" + connector.getClass() + ") :");
  681. } else {
  682. getLogger().warning(
  683. "\t" + id + ": Warning: no corresponding connector for id "
  684. + id);
  685. }
  686. for (MethodInvocation invocation : invocations) {
  687. getLogger().info("\t\t" + getInvocationDebugString(invocation));
  688. }
  689. }
  690. /**
  691. * Produces a string representation of a method invocation, suitable for
  692. * debug output
  693. *
  694. * @since 7.5
  695. * @param invocation
  696. * @return
  697. */
  698. private static String getInvocationDebugString(MethodInvocation invocation) {
  699. Object[] parameters = invocation.getParameters();
  700. String formattedParams = null;
  701. if (ApplicationConstants.UPDATE_VARIABLE_METHOD.equals(invocation
  702. .getMethodName()) && parameters.length == 2) {
  703. // name, value
  704. Object value = parameters[1];
  705. // TODO paintables inside lists/maps get rendered as
  706. // components in the debug console
  707. String formattedValue = value instanceof ServerConnector ? ((ServerConnector) value)
  708. .getConnectorId() : String.valueOf(value);
  709. formattedParams = parameters[0] + " : " + formattedValue;
  710. }
  711. if (null == formattedParams) {
  712. formattedParams = (null != parameters) ? Arrays
  713. .toString(parameters) : null;
  714. }
  715. return invocation.getInterfaceName() + "." + invocation.getMethodName()
  716. + "(" + formattedParams + ")";
  717. }
  718. public static void logMethodInvocations(ApplicationConnection c,
  719. Collection<MethodInvocation> methodInvocations) {
  720. try {
  721. getLogger().info("RPC invocations to be sent to the server:");
  722. String curId = null;
  723. ArrayList<MethodInvocation> invocations = new ArrayList<MethodInvocation>();
  724. for (MethodInvocation methodInvocation : methodInvocations) {
  725. String id = methodInvocation.getConnectorId();
  726. if (curId == null) {
  727. curId = id;
  728. } else if (!curId.equals(id)) {
  729. printConnectorInvocations(invocations, curId, c);
  730. invocations.clear();
  731. curId = id;
  732. }
  733. invocations.add(methodInvocation);
  734. }
  735. if (!invocations.isEmpty()) {
  736. printConnectorInvocations(invocations, curId, c);
  737. }
  738. } catch (Exception e) {
  739. getLogger()
  740. .log(Level.SEVERE, "Error logging method invocations", e);
  741. }
  742. }
  743. /**
  744. * Temporarily sets the {@code styleProperty} to {@code tempValue} and then
  745. * resets it to its current value. Used mainly to work around rendering
  746. * issues in IE (and possibly in other browsers)
  747. *
  748. * @deprecated As of 7.4.0, use
  749. * {@link WidgetUtil#setStyleTemporarily(Element, String, String)}
  750. * instead.
  751. *
  752. * @param element
  753. * The target element
  754. * @param styleProperty
  755. * The name of the property to set
  756. * @param tempValue
  757. * The temporary value
  758. */
  759. @Deprecated
  760. public static void setStyleTemporarily(Element element,
  761. final String styleProperty, String tempValue) {
  762. WidgetUtil.setStyleTemporarily(element, styleProperty, tempValue);
  763. }
  764. /**
  765. * A helper method to return the client position from an event. Returns
  766. * position from either first changed touch (if touch event) or from the
  767. * event itself.
  768. *
  769. * @deprecated As of 7.4.0, use
  770. * {@link WidgetUtil#getTouchOrMouseClientX(Event)} instead.
  771. *
  772. * @param event
  773. * @return
  774. */
  775. @Deprecated
  776. public static int getTouchOrMouseClientX(Event event) {
  777. return WidgetUtil.getTouchOrMouseClientX(event);
  778. }
  779. /**
  780. * Find the element corresponding to the coordinates in the passed mouse
  781. * event. Please note that this is not always the same as the target of the
  782. * event e.g. if event capture is used.
  783. *
  784. * @deprecated As of 7.4.0, use
  785. * {@link WidgetUtil#getElementUnderMouse(NativeEvent)} instead.
  786. *
  787. * @param event
  788. * the mouse event to get coordinates from
  789. * @return the element at the coordinates of the event
  790. */
  791. @Deprecated
  792. public static com.google.gwt.user.client.Element getElementUnderMouse(
  793. NativeEvent event) {
  794. return DOM.asOld(WidgetUtil.getElementUnderMouse(event));
  795. }
  796. /**
  797. * A helper method to return the client position from an event. Returns
  798. * position from either first changed touch (if touch event) or from the
  799. * event itself.
  800. *
  801. * @deprecated As of 7.4.0, use
  802. * {@link WidgetUtil#getTouchOrMouseClientY(Event)} instead.
  803. *
  804. * @param event
  805. * @return
  806. */
  807. @Deprecated
  808. public static int getTouchOrMouseClientY(Event event) {
  809. return WidgetUtil.getTouchOrMouseClientY(event);
  810. }
  811. /**
  812. * @deprecated As of 7.4.0, use
  813. * {@link WidgetUtil#getTouchOrMouseClientY(NativeEvent)}
  814. * instead.
  815. *
  816. * @see #getTouchOrMouseClientY(Event)
  817. * @param currentGwtEvent
  818. * @return
  819. */
  820. @Deprecated
  821. public static int getTouchOrMouseClientY(NativeEvent currentGwtEvent) {
  822. return WidgetUtil.getTouchOrMouseClientY(currentGwtEvent);
  823. }
  824. /**
  825. * @deprecated As of 7.4.0, use
  826. * {@link WidgetUtil#getTouchOrMouseClientX(NativeEvent)}
  827. * instead.
  828. *
  829. * @see #getTouchOrMouseClientX(Event)
  830. *
  831. * @param event
  832. * @return
  833. */
  834. @Deprecated
  835. public static int getTouchOrMouseClientX(NativeEvent event) {
  836. return WidgetUtil.getTouchOrMouseClientX(event);
  837. }
  838. /**
  839. * @deprecated As of 7.4.0, use {@link WidgetUtil#isTouchEvent(Event)}
  840. * instead.
  841. */
  842. @Deprecated
  843. public static boolean isTouchEvent(Event event) {
  844. return WidgetUtil.isTouchEvent(event);
  845. }
  846. /**
  847. * @deprecated As of 7.4.0, use {@link WidgetUtil#isTouchEvent(NativeEvent)}
  848. * instead.
  849. */
  850. @Deprecated
  851. public static boolean isTouchEvent(NativeEvent event) {
  852. return WidgetUtil.isTouchEvent(event);
  853. }
  854. /**
  855. * @deprecated As of 7.4.0, use
  856. * {@link WidgetUtil#simulateClickFromTouchEvent(Event, Widget)}
  857. * instead.
  858. */
  859. @Deprecated
  860. public static void simulateClickFromTouchEvent(Event touchevent,
  861. Widget widget) {
  862. WidgetUtil.simulateClickFromTouchEvent(touchevent, widget);
  863. }
  864. /**
  865. * Gets the currently focused element.
  866. *
  867. * @deprecated As of 7.4.0, use {@link WidgetUtil#getFocusedElement()}
  868. * instead.
  869. *
  870. * @return The active element or null if no active element could be found.
  871. */
  872. @Deprecated
  873. public static com.google.gwt.user.client.Element getFocusedElement() {
  874. return DOM.asOld(WidgetUtil.getFocusedElement());
  875. }
  876. /**
  877. * Gets the currently focused element for Internet Explorer.
  878. *
  879. * @return The currently focused element
  880. * @deprecated Use #getFocusedElement instead
  881. */
  882. @Deprecated
  883. public static com.google.gwt.user.client.Element getIEFocusedElement() {
  884. return getFocusedElement();
  885. }
  886. /**
  887. * Gets currently focused element and checks if it's editable
  888. *
  889. * @deprecated As of 7.4.0, use
  890. * {@link WidgetUtil#isFocusedElementEditable()} instead.
  891. *
  892. * @since 7.4
  893. *
  894. * @return true if focused element is editable
  895. */
  896. @Deprecated
  897. public static boolean isFocusedElementEditable() {
  898. return WidgetUtil.isFocusedElementEditable();
  899. }
  900. /**
  901. * Kind of stronger version of isAttached(). In addition to std isAttached,
  902. * this method checks that this widget nor any of its parents is hidden. Can
  903. * be e.g used to check whether component should react to some events or
  904. * not.
  905. *
  906. * @deprecated As of 7.4.0, use
  907. * {@link WidgetUtil#isAttachedAndDisplayed(Widget)} instead.
  908. *
  909. * @param widget
  910. * @return true if attached and displayed
  911. */
  912. @Deprecated
  913. public static boolean isAttachedAndDisplayed(Widget widget) {
  914. return WidgetUtil.isAttachedAndDisplayed(widget);
  915. }
  916. /**
  917. * Scrolls an element into view vertically only. Modified version of
  918. * Element.scrollIntoView.
  919. *
  920. * @deprecated As of 7.4.0, use
  921. * {@link WidgetUtil#scrollIntoViewVertically(Element)} instead.
  922. *
  923. * @param elem
  924. * The element to scroll into view
  925. */
  926. @Deprecated
  927. public static void scrollIntoViewVertically(Element elem) {
  928. WidgetUtil.scrollIntoViewVertically(elem);
  929. }
  930. /**
  931. * Checks if the given event is either a touch event or caused by the left
  932. * mouse button
  933. *
  934. * @deprecated As of 7.4.0, use
  935. * {@link WidgetUtil#isTouchEventOrLeftMouseButton(Event)}
  936. * instead.
  937. *
  938. * @param event
  939. * @return true if the event is a touch event or caused by the left mouse
  940. * button, false otherwise
  941. */
  942. @Deprecated
  943. public static boolean isTouchEventOrLeftMouseButton(Event event) {
  944. return WidgetUtil.isTouchEventOrLeftMouseButton(event);
  945. }
  946. /**
  947. * Performs a shallow comparison of the collections.
  948. *
  949. * @param collection1
  950. * The first collection
  951. * @param collection2
  952. * The second collection
  953. * @return true if the collections contain the same elements in the same
  954. * order, false otherwise
  955. */
  956. public static boolean collectionsEquals(Collection<?> collection1,
  957. Collection<?> collection2) {
  958. if (collection1 == null) {
  959. return collection2 == null;
  960. }
  961. if (collection2 == null) {
  962. return false;
  963. }
  964. if (collection1.size() != collection2.size()) {
  965. return false;
  966. }
  967. Iterator<?> collection1Iterator = collection1.iterator();
  968. Iterator<?> collection2Iterator = collection2.iterator();
  969. while (collection1Iterator.hasNext()) {
  970. Object collection1Object = collection1Iterator.next();
  971. Object collection2Object = collection2Iterator.next();
  972. if (collection1Object != collection2Object) {
  973. return false;
  974. }
  975. }
  976. return true;
  977. }
  978. public static String getConnectorString(ServerConnector p) {
  979. if (p == null) {
  980. return "null";
  981. }
  982. return getSimpleName(p) + " (" + p.getConnectorId() + ")";
  983. }
  984. /**
  985. * Resolve a relative URL to an absolute URL based on the current document's
  986. * location.
  987. *
  988. * @deprecated As of 7.4.0, use {@link WidgetUtil#getAbsoluteUrl(String)}
  989. * instead.
  990. *
  991. * @param url
  992. * a string with the relative URL to resolve
  993. * @return the corresponding absolute URL as a string
  994. */
  995. @Deprecated
  996. public static String getAbsoluteUrl(String url) {
  997. return WidgetUtil.getAbsoluteUrl(url);
  998. }
  999. /**
  1000. * Sets the selection range of an input element.
  1001. *
  1002. * We need this JSNI function to set selection range so that we can use the
  1003. * optional direction attribute to set the anchor to the end and the focus
  1004. * to the start. This makes Firefox work the same way as other browsers
  1005. * (#13477)
  1006. *
  1007. * @deprecated As of 7.4.0, use
  1008. * {@link WidgetUtil#setSelectionRange(Element, int, int, String)}
  1009. * instead.
  1010. *
  1011. * @param elem
  1012. * the html input element.
  1013. * @param pos
  1014. * the index of the first selected character.
  1015. * @param length
  1016. * the selection length.
  1017. * @param direction
  1018. * a string indicating the direction in which the selection was
  1019. * performed. This may be "forward" or "backward", or "none" if
  1020. * the direction is unknown or irrelevant.
  1021. *
  1022. * @since 7.3
  1023. */
  1024. @Deprecated
  1025. public static void setSelectionRange(Element elem, int pos, int length,
  1026. String direction) {
  1027. WidgetUtil.setSelectionRange(elem, pos, length, direction);
  1028. }
  1029. /**
  1030. * Converts a native {@link JavaScriptObject} into a {@link JsonValue}. This
  1031. * is a no-op in GWT code compiled to javascript, but needs some special
  1032. * handling to work when run in JVM.
  1033. *
  1034. * @param jso
  1035. * the java script object to represent as json
  1036. * @return the json representation
  1037. */
  1038. public static <T extends JsonValue> T jso2json(JavaScriptObject jso) {
  1039. if (GWT.isProdMode()) {
  1040. return (T) jso.<JsJsonValue> cast();
  1041. } else {
  1042. return elemental.json.Json.instance().parse(stringify(jso));
  1043. }
  1044. }
  1045. /**
  1046. * Converts a {@link JsonValue} into a native {@link JavaScriptObject}. This
  1047. * is a no-op in GWT code compiled to javascript, but needs some special
  1048. * handling to work when run in JVM.
  1049. *
  1050. * @param jsonValue
  1051. * the json value
  1052. * @return a native javascript object representation of the json value
  1053. */
  1054. public static JavaScriptObject json2jso(JsonValue jsonValue) {
  1055. if (GWT.isProdMode()) {
  1056. return ((JavaScriptObject) jsonValue.toNative()).cast();
  1057. } else {
  1058. return parse(jsonValue.toJson());
  1059. }
  1060. }
  1061. /**
  1062. * Convert a {@link JavaScriptObject} into a string representation.
  1063. *
  1064. * @param json
  1065. * a JavaScript object to be converted to a string
  1066. * @return JSON in string representation
  1067. */
  1068. private native static String stringify(JavaScriptObject json)
  1069. /*-{
  1070. return JSON.stringify(json);
  1071. }-*/;
  1072. /**
  1073. * Parse a string containing JSON into a {@link JavaScriptObject}.
  1074. *
  1075. * @param <T>
  1076. * the overlay type to expect from the parse
  1077. * @param jsonAsString
  1078. * @return a JavaScript object constructed from the parse
  1079. */
  1080. public native static <T extends JavaScriptObject> T parse(
  1081. String jsonAsString)
  1082. /*-{
  1083. return JSON.parse(jsonAsString);
  1084. }-*/;
  1085. /**
  1086. * Round {@code num} up to {@code exp} decimal positions.
  1087. *
  1088. * @since 7.6
  1089. */
  1090. public static native double round(double num, int exp)
  1091. /*-{
  1092. return +(Math.round(num + "e+" + exp) + "e-" + exp);
  1093. }-*/;
  1094. /**
  1095. * Test if we can can skip measuring this connector. We can skip the
  1096. * measurement if its parent is a ChildMeasureHintConnector and has an
  1097. * appropriate mode set.
  1098. *
  1099. * For internal use only. May be removed or replaced in the future.
  1100. */
  1101. public static boolean shouldSkipMeasurementOfConnector(
  1102. ComponentConnector candidate, ComponentConnector parent) {
  1103. Profiler.enter("skipMeasureDueLayoutHint");
  1104. boolean skip = false;
  1105. if (parent instanceof HasChildMeasurementHintConnector) {
  1106. ChildMeasurementHint measureMode = ((HasChildMeasurementHintConnector) parent)
  1107. .getChildMeasurementHint();
  1108. if (measureMode == ChildMeasurementHint.MEASURE_NEVER) {
  1109. skip = true;
  1110. } else if (measureMode == ChildMeasurementHint.MEASURE_IF_NEEDED) {
  1111. skip = canWeSkipChildMeasurement(candidate);
  1112. }
  1113. }
  1114. Profiler.leave("skipMeasureDueLayoutHint");
  1115. return skip;
  1116. }
  1117. /**
  1118. * Test if we can can skip measuring this connector. We can skip the
  1119. * measurement if its parent is a ChildMeasureHintConnector and has an
  1120. * appropriate mode set.
  1121. *
  1122. * This version of the method tries to recursively locate such a parent.
  1123. *
  1124. * For internal use only. May be removed or replaced in the future.
  1125. */
  1126. public static boolean shouldSkipMeasurementOfConnector(
  1127. ComponentConnector candidate) {
  1128. Profiler.enter("skipMeasureDueLayoutHint");
  1129. boolean skip = false;
  1130. HasChildMeasurementHintConnector parent = getPossibleChildMeasurementHintParentConnector(candidate);
  1131. if (parent != null) {
  1132. ChildMeasurementHint measureMode = parent.getChildMeasurementHint();
  1133. if (measureMode == ChildMeasurementHint.MEASURE_NEVER) {
  1134. skip = true;
  1135. } else if (measureMode == ChildMeasurementHint.MEASURE_IF_NEEDED) {
  1136. skip = canWeSkipChildMeasurement(candidate);
  1137. }
  1138. }
  1139. Profiler.leave("skipMeasureDueLayoutHint");
  1140. return skip;
  1141. }
  1142. /** For internal use only. May be removed or replaced in the future. */
  1143. private static boolean canWeSkipChildMeasurement(ComponentConnector child) {
  1144. // common cases when child measuring is possibly needed
  1145. if (child instanceof ElementResizeListener
  1146. || child instanceof ManagedLayout
  1147. || child instanceof AbstractLayoutConnector) {
  1148. return false;
  1149. }
  1150. return true;
  1151. }
  1152. /** For internal use only. May be removed or replaced in the future. */
  1153. private static HasChildMeasurementHintConnector getPossibleChildMeasurementHintParentConnector(
  1154. ComponentConnector candidate) {
  1155. ServerConnector parent = candidate.getParent();
  1156. if (parent != null
  1157. && parent instanceof HasChildMeasurementHintConnector) {
  1158. return (HasChildMeasurementHintConnector) parent;
  1159. }
  1160. return null;
  1161. }
  1162. private static Logger getLogger() {
  1163. return Logger.getLogger(Util.class.getName());
  1164. }
  1165. }