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 41KB

12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * Copyright 2000-2018 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.measureVerticalPaddingAndBorder(element,
  212. 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(
  406. com.google.gwt.dom.client.Element element) {
  407. return WidgetUtil.getRequiredWidth(element);
  408. }
  409. /**
  410. * Gets the border-box height for the given element, i.e. element height +
  411. * border + padding. Always rounds up to nearest integer.
  412. *
  413. * @deprecated As of 7.4.0, use
  414. * {@link WidgetUtil#getRequiredHeight(Element)} instead.
  415. *
  416. * @param element
  417. * The element to check
  418. * @return The border-box height for the element
  419. */
  420. @Deprecated
  421. public static int getRequiredHeight(
  422. com.google.gwt.dom.client.Element element) {
  423. return WidgetUtil.getRequiredHeight(element);
  424. }
  425. /**
  426. * @deprecated As of 7.4.0, use
  427. * {@link WidgetUtil#getRequiredWidthBoundingClientRect(Element)}
  428. * instead.
  429. */
  430. @Deprecated
  431. public int getRequiredWidthBoundingClientRect(
  432. com.google.gwt.dom.client.Element element) {
  433. return WidgetUtil.getRequiredWidthBoundingClientRect(element);
  434. }
  435. /**
  436. * @deprecated As of 7.4.0, use
  437. * {@link WidgetUtil#getRequiredHeightComputedStyle(Element)}
  438. * instead.
  439. */
  440. @Deprecated
  441. public static int getRequiredHeightComputedStyle(
  442. com.google.gwt.dom.client.Element element) {
  443. return WidgetUtil.getRequiredHeightComputedStyle(element);
  444. }
  445. /**
  446. * @deprecated As of 7.4.0, use
  447. * {@link WidgetUtil#getRequiredWidthComputedStyle(Element)}
  448. * instead.
  449. */
  450. @Deprecated
  451. public static int getRequiredWidthComputedStyle(
  452. com.google.gwt.dom.client.Element element) {
  453. return WidgetUtil.getRequiredWidthComputedStyle(element);
  454. }
  455. /**
  456. * @deprecated As of 7.4.0, use
  457. * {@link WidgetUtil#getRequiredHeightBoundingClientRect(Element)}
  458. * instead.
  459. */
  460. @Deprecated
  461. public static int getRequiredHeightBoundingClientRect(
  462. com.google.gwt.dom.client.Element element) {
  463. return WidgetUtil.getRequiredHeightBoundingClientRect(element);
  464. }
  465. /**
  466. * @deprecated As of 7.4.0, use {@link WidgetUtil#getRequiredWidth(Widget)}
  467. * instead.
  468. */
  469. @Deprecated
  470. public static int getRequiredWidth(Widget widget) {
  471. return WidgetUtil.getRequiredWidth(widget);
  472. }
  473. /**
  474. * @deprecated As of 7.4.0, use {@link WidgetUtil#getRequiredHeight(Widget)}
  475. * instead.
  476. */
  477. @Deprecated
  478. public static int getRequiredHeight(Widget widget) {
  479. return WidgetUtil.getRequiredHeight(widget);
  480. }
  481. /**
  482. * Detects what is currently the overflow style attribute in given element.
  483. *
  484. * @deprecated As of 7.4.0, use
  485. * {@link WidgetUtil#mayHaveScrollBars(Element)} instead.
  486. *
  487. * @param pe
  488. * the element to detect
  489. * @return true if auto or scroll
  490. */
  491. @Deprecated
  492. public static boolean mayHaveScrollBars(
  493. com.google.gwt.dom.client.Element pe) {
  494. return WidgetUtil.mayHaveScrollBars(pe);
  495. }
  496. /**
  497. * Locates the nested child component of <literal>parent</literal> which
  498. * contains the element <literal>element</literal>. The child component is
  499. * also returned if "element" is part of its caption. If
  500. * <literal>element</literal> is not part of any child component, null is
  501. * returned.
  502. *
  503. * This method returns the deepest nested VPaintableWidget.
  504. *
  505. * @param client
  506. * A reference to ApplicationConnection
  507. * @param parent
  508. * The widget that contains <literal>element</literal>.
  509. * @param element
  510. * An element that is a sub element of the parent
  511. * @return The VPaintableWidget which the element is a part of. Null if the
  512. * element does not belong to a child.
  513. */
  514. public static ComponentConnector getConnectorForElement(
  515. ApplicationConnection client, Widget parent, Element element) {
  516. Element browseElement = element;
  517. Element rootElement = parent.getElement();
  518. while (browseElement != null && browseElement != rootElement) {
  519. ComponentConnector connector = ConnectorMap.get(client)
  520. .getConnector(browseElement);
  521. if (connector == null) {
  522. String ownerPid = VCaption.getCaptionOwnerPid(browseElement);
  523. if (ownerPid != null) {
  524. connector = (ComponentConnector) ConnectorMap.get(client)
  525. .getConnector(ownerPid);
  526. }
  527. }
  528. if (connector != null) {
  529. // check that inside the rootElement
  530. while (browseElement != null && browseElement != rootElement) {
  531. browseElement = browseElement.getParentElement();
  532. }
  533. if (browseElement != rootElement) {
  534. return null;
  535. } else {
  536. return connector;
  537. }
  538. }
  539. browseElement = browseElement.getParentElement();
  540. }
  541. // No connector found, element is possibly inside a VOverlay
  542. // If the overlay has an owner, try to find the owner's connector
  543. VOverlay overlay = findWidget(element, VOverlay.class);
  544. if (overlay != null && overlay.getOwner() != null) {
  545. return getConnectorForElement(client,
  546. client.getUIConnector().getWidget(),
  547. overlay.getOwner().getElement());
  548. } else {
  549. return null;
  550. }
  551. }
  552. /**
  553. * Will (attempt) to focus the given DOM Element.
  554. *
  555. * @deprecated As of 7.4.0, use {@link WidgetUtil#focus(Element)} instead.
  556. *
  557. * @param el
  558. * the element to focus
  559. */
  560. @Deprecated
  561. public static void focus(Element el) {
  562. WidgetUtil.focus(el);
  563. }
  564. /**
  565. * Helper method to find the nearest parent paintable instance by traversing
  566. * the DOM upwards from given element.
  567. *
  568. * @param element
  569. * the element to start from
  570. */
  571. public static ComponentConnector findPaintable(ApplicationConnection client,
  572. Element element) {
  573. Widget widget = Util.findWidget(element, null);
  574. ConnectorMap vPaintableMap = ConnectorMap.get(client);
  575. while (widget != null && !vPaintableMap.isConnector(widget)) {
  576. widget = widget.getParent();
  577. }
  578. return vPaintableMap.getConnector(widget);
  579. }
  580. /**
  581. * Helper method to find first instance of given Widget type found by
  582. * traversing DOM upwards from given element.
  583. *
  584. * @deprecated As of 7.4.0, use
  585. * {@link WidgetUtil#findWidget(Element, Class)} instead.
  586. *
  587. * @param element
  588. * the element where to start seeking of Widget
  589. * @param class1
  590. * the Widget type to seek for
  591. */
  592. @Deprecated
  593. public static <T> T findWidget(Element element,
  594. Class<? extends Widget> class1) {
  595. return WidgetUtil.findWidget(element, class1);
  596. }
  597. /**
  598. * Force webkit to redraw an element.
  599. *
  600. * @deprecated As of 7.4.0, use
  601. * {@link WidgetUtil#forceWebkitRedraw(Element)} instead.
  602. *
  603. * @param element
  604. * The element that should be redrawn
  605. */
  606. @Deprecated
  607. public static void forceWebkitRedraw(Element element) {
  608. WidgetUtil.forceWebkitRedraw(element);
  609. }
  610. /**
  611. * Performs a hack to trigger a re-layout in the IE browser. This is usually
  612. * necessary in cases where IE "forgets" to update child elements when they
  613. * resize.
  614. *
  615. * @deprecated As of 7.4.0, use {@link WidgetUtil#forceIERedraw(Element)}
  616. * instead.
  617. *
  618. * @since 7.3
  619. * @param e
  620. * The element to perform the hack on
  621. */
  622. @Deprecated
  623. public static void forceIERedraw(Element e) {
  624. WidgetUtil.forceIERedraw(e);
  625. }
  626. /**
  627. * Detaches and re-attaches the element from its parent. The element is
  628. * reattached at the same position in the DOM as it was before.
  629. *
  630. * Does nothing if the element is not attached to the DOM.
  631. *
  632. * @deprecated As of 7.4.0, use {@link WidgetUtil#detachAttach(Element)}
  633. * instead.
  634. *
  635. * @param element
  636. * The element to detach and re-attach
  637. */
  638. @Deprecated
  639. public static void detachAttach(Element element) {
  640. WidgetUtil.detachAttach(element);
  641. }
  642. /**
  643. * @deprecated As of 7.4.0, use
  644. * {@link WidgetUtil#sinkOnloadForImages(Element)} instead.
  645. */
  646. @Deprecated
  647. public static void sinkOnloadForImages(Element element) {
  648. WidgetUtil.sinkOnloadForImages(element);
  649. }
  650. /**
  651. * Returns the index of the childElement within its parent.
  652. *
  653. * @deprecated As of 7.4.0, use
  654. * {@link WidgetUtil#getChildElementIndex(Element)} instead.
  655. *
  656. * @param subElement
  657. * @return
  658. */
  659. @Deprecated
  660. public static int getChildElementIndex(Element childElement) {
  661. return WidgetUtil.getChildElementIndex(childElement);
  662. }
  663. private static void printConnectorInvocations(
  664. List<MethodInvocation> invocations, String id,
  665. ApplicationConnection c) {
  666. ServerConnector connector = ConnectorMap.get(c).getConnector(id);
  667. if (connector != null) {
  668. getLogger().info("\t" + id + " (" + connector.getClass() + ") :");
  669. } else {
  670. getLogger().warning("\t" + id
  671. + ": Warning: no corresponding connector for id " + id);
  672. }
  673. for (MethodInvocation invocation : invocations) {
  674. getLogger().info("\t\t" + getInvocationDebugString(invocation));
  675. }
  676. }
  677. /**
  678. * Produces a string representation of a method invocation, suitable for
  679. * debug output
  680. *
  681. * @since 7.5
  682. * @param invocation
  683. * @return
  684. */
  685. private static String getInvocationDebugString(
  686. MethodInvocation invocation) {
  687. Object[] parameters = invocation.getParameters();
  688. String formattedParams = null;
  689. if (ApplicationConstants.UPDATE_VARIABLE_METHOD
  690. .equals(invocation.getMethodName()) && parameters.length == 2) {
  691. // name, value
  692. Object value = parameters[1];
  693. // TODO paintables inside lists/maps get rendered as
  694. // components in the debug console
  695. String formattedValue = value instanceof ServerConnector
  696. ? ((ServerConnector) value).getConnectorId()
  697. : String.valueOf(value);
  698. formattedParams = parameters[0] + " : " + formattedValue;
  699. }
  700. if (null == formattedParams) {
  701. formattedParams = (null != parameters) ? Arrays.toString(parameters)
  702. : null;
  703. }
  704. return invocation.getInterfaceName() + "." + invocation.getMethodName()
  705. + "(" + formattedParams + ")";
  706. }
  707. public static void logMethodInvocations(ApplicationConnection c,
  708. Collection<MethodInvocation> methodInvocations) {
  709. try {
  710. getLogger().info("RPC invocations to be sent to the server:");
  711. String curId = null;
  712. List<MethodInvocation> invocations = new ArrayList<>();
  713. for (MethodInvocation methodInvocation : methodInvocations) {
  714. String id = methodInvocation.getConnectorId();
  715. if (curId == null) {
  716. curId = id;
  717. } else if (!curId.equals(id)) {
  718. printConnectorInvocations(invocations, curId, c);
  719. invocations.clear();
  720. curId = id;
  721. }
  722. invocations.add(methodInvocation);
  723. }
  724. if (!invocations.isEmpty()) {
  725. printConnectorInvocations(invocations, curId, c);
  726. }
  727. } catch (Exception e) {
  728. getLogger().log(Level.SEVERE, "Error logging method invocations",
  729. e);
  730. }
  731. }
  732. /**
  733. * Temporarily sets the {@code styleProperty} to {@code tempValue} and then
  734. * resets it to its current value. Used mainly to work around rendering
  735. * issues in IE (and possibly in other browsers)
  736. *
  737. * @deprecated As of 7.4.0, use
  738. * {@link WidgetUtil#setStyleTemporarily(Element, String, String)}
  739. * instead.
  740. *
  741. * @param element
  742. * The target element
  743. * @param styleProperty
  744. * The name of the property to set
  745. * @param tempValue
  746. * The temporary value
  747. */
  748. @Deprecated
  749. public static void setStyleTemporarily(Element element,
  750. final String styleProperty, String tempValue) {
  751. WidgetUtil.setStyleTemporarily(element, styleProperty, tempValue);
  752. }
  753. /**
  754. * A helper method to return the client position from an event. Returns
  755. * position from either first changed touch (if touch event) or from the
  756. * event itself.
  757. *
  758. * @deprecated As of 7.4.0, use
  759. * {@link WidgetUtil#getTouchOrMouseClientX(Event)} instead.
  760. *
  761. * @param event
  762. * @return
  763. */
  764. @Deprecated
  765. public static int getTouchOrMouseClientX(Event event) {
  766. return WidgetUtil.getTouchOrMouseClientX(event);
  767. }
  768. /**
  769. * Find the element corresponding to the coordinates in the passed mouse
  770. * event. Please note that this is not always the same as the target of the
  771. * event e.g. if event capture is used.
  772. *
  773. * @deprecated As of 7.4.0, use
  774. * {@link WidgetUtil#getElementUnderMouse(NativeEvent)} instead.
  775. *
  776. * @param event
  777. * the mouse event to get coordinates from
  778. * @return the element at the coordinates of the event
  779. */
  780. @Deprecated
  781. public static com.google.gwt.user.client.Element getElementUnderMouse(
  782. NativeEvent event) {
  783. return DOM.asOld(WidgetUtil.getElementUnderMouse(event));
  784. }
  785. /**
  786. * A helper method to return the client position from an event. Returns
  787. * position from either first changed touch (if touch event) or from the
  788. * event itself.
  789. *
  790. * @deprecated As of 7.4.0, use
  791. * {@link WidgetUtil#getTouchOrMouseClientY(Event)} instead.
  792. *
  793. * @param event
  794. * @return
  795. */
  796. @Deprecated
  797. public static int getTouchOrMouseClientY(Event event) {
  798. return WidgetUtil.getTouchOrMouseClientY(event);
  799. }
  800. /**
  801. * @deprecated As of 7.4.0, use
  802. * {@link WidgetUtil#getTouchOrMouseClientY(NativeEvent)}
  803. * instead.
  804. *
  805. * @see #getTouchOrMouseClientY(Event)
  806. * @param currentGwtEvent
  807. * @return
  808. */
  809. @Deprecated
  810. public static int getTouchOrMouseClientY(NativeEvent currentGwtEvent) {
  811. return WidgetUtil.getTouchOrMouseClientY(currentGwtEvent);
  812. }
  813. /**
  814. * @deprecated As of 7.4.0, use
  815. * {@link WidgetUtil#getTouchOrMouseClientX(NativeEvent)}
  816. * instead.
  817. *
  818. * @see #getTouchOrMouseClientX(Event)
  819. *
  820. * @param event
  821. * @return
  822. */
  823. @Deprecated
  824. public static int getTouchOrMouseClientX(NativeEvent event) {
  825. return WidgetUtil.getTouchOrMouseClientX(event);
  826. }
  827. /**
  828. * @deprecated As of 7.4.0, use {@link WidgetUtil#isTouchEvent(Event)}
  829. * instead.
  830. */
  831. @Deprecated
  832. public static boolean isTouchEvent(Event event) {
  833. return WidgetUtil.isTouchEvent(event);
  834. }
  835. /**
  836. * @deprecated As of 7.4.0, use {@link WidgetUtil#isTouchEvent(NativeEvent)}
  837. * instead.
  838. */
  839. @Deprecated
  840. public static boolean isTouchEvent(NativeEvent event) {
  841. return WidgetUtil.isTouchEvent(event);
  842. }
  843. /**
  844. * @deprecated As of 7.4.0, use
  845. * {@link WidgetUtil#simulateClickFromTouchEvent(Event, Widget)}
  846. * instead.
  847. */
  848. @Deprecated
  849. public static void simulateClickFromTouchEvent(Event touchevent,
  850. Widget widget) {
  851. WidgetUtil.simulateClickFromTouchEvent(touchevent, widget);
  852. }
  853. /**
  854. * Gets the currently focused element.
  855. *
  856. * @deprecated As of 7.4.0, use {@link WidgetUtil#getFocusedElement()}
  857. * instead.
  858. *
  859. * @return The active element or null if no active element could be found.
  860. */
  861. @Deprecated
  862. public static com.google.gwt.user.client.Element getFocusedElement() {
  863. return DOM.asOld(WidgetUtil.getFocusedElement());
  864. }
  865. /**
  866. * Gets the currently focused element for Internet Explorer.
  867. *
  868. * @return The currently focused element
  869. * @deprecated Use #getFocusedElement instead
  870. */
  871. @Deprecated
  872. public static com.google.gwt.user.client.Element getIEFocusedElement() {
  873. return getFocusedElement();
  874. }
  875. /**
  876. * Gets currently focused element and checks if it's editable.
  877. *
  878. * @deprecated As of 7.4.0, use
  879. * {@link WidgetUtil#isFocusedElementEditable()} instead.
  880. *
  881. * @since 7.4
  882. *
  883. * @return true if focused element is editable
  884. */
  885. @Deprecated
  886. public static boolean isFocusedElementEditable() {
  887. return WidgetUtil.isFocusedElementEditable();
  888. }
  889. /**
  890. * Kind of stronger version of isAttached(). In addition to std isAttached,
  891. * this method checks that this widget nor any of its parents is hidden. Can
  892. * be e.g used to check whether component should react to some events or
  893. * not.
  894. *
  895. * @deprecated As of 7.4.0, use
  896. * {@link WidgetUtil#isAttachedAndDisplayed(Widget)} instead.
  897. *
  898. * @param widget
  899. * @return true if attached and displayed
  900. */
  901. @Deprecated
  902. public static boolean isAttachedAndDisplayed(Widget widget) {
  903. return WidgetUtil.isAttachedAndDisplayed(widget);
  904. }
  905. /**
  906. * Scrolls an element into view vertically only. Modified version of
  907. * Element.scrollIntoView.
  908. *
  909. * @deprecated As of 7.4.0, use
  910. * {@link WidgetUtil#scrollIntoViewVertically(Element)} instead.
  911. *
  912. * @param elem
  913. * The element to scroll into view
  914. */
  915. @Deprecated
  916. public static void scrollIntoViewVertically(Element elem) {
  917. WidgetUtil.scrollIntoViewVertically(elem);
  918. }
  919. /**
  920. * Checks if the given event is either a touch event or caused by the left
  921. * mouse button.
  922. *
  923. * @deprecated As of 7.4.0, use
  924. * {@link WidgetUtil#isTouchEventOrLeftMouseButton(Event)}
  925. * instead.
  926. *
  927. * @param event
  928. * @return true if the event is a touch event or caused by the left mouse
  929. * button, false otherwise
  930. */
  931. @Deprecated
  932. public static boolean isTouchEventOrLeftMouseButton(Event event) {
  933. return WidgetUtil.isTouchEventOrLeftMouseButton(event);
  934. }
  935. /**
  936. * Performs a shallow comparison of the collections.
  937. *
  938. * @param collection1
  939. * The first collection
  940. * @param collection2
  941. * The second collection
  942. * @return true if the collections contain the same elements in the same
  943. * order, false otherwise
  944. */
  945. public static boolean collectionsEquals(Collection<?> collection1,
  946. Collection<?> collection2) {
  947. if (collection1 == null) {
  948. return collection2 == null;
  949. }
  950. if (collection2 == null) {
  951. return false;
  952. }
  953. if (collection1.size() != collection2.size()) {
  954. return false;
  955. }
  956. Iterator<?> collection1Iterator = collection1.iterator();
  957. Iterator<?> collection2Iterator = collection2.iterator();
  958. while (collection1Iterator.hasNext()) {
  959. Object collection1Object = collection1Iterator.next();
  960. Object collection2Object = collection2Iterator.next();
  961. if (collection1Object != collection2Object) {
  962. return false;
  963. }
  964. }
  965. return true;
  966. }
  967. public static String getConnectorString(ServerConnector p) {
  968. if (p == null) {
  969. return "null";
  970. }
  971. return getSimpleName(p) + " (" + p.getConnectorId() + ")";
  972. }
  973. /**
  974. * Resolve a relative URL to an absolute URL based on the current document's
  975. * location.
  976. *
  977. * @deprecated As of 7.4.0, use {@link WidgetUtil#getAbsoluteUrl(String)}
  978. * instead.
  979. *
  980. * @param url
  981. * a string with the relative URL to resolve
  982. * @return the corresponding absolute URL as a string
  983. */
  984. @Deprecated
  985. public static String getAbsoluteUrl(String url) {
  986. return WidgetUtil.getAbsoluteUrl(url);
  987. }
  988. /**
  989. * Sets the selection range of an input element.
  990. *
  991. * We need this JSNI function to set selection range so that we can use the
  992. * optional direction attribute to set the anchor to the end and the focus
  993. * to the start. This makes Firefox work the same way as other browsers
  994. * (#13477)
  995. *
  996. * @deprecated As of 7.4.0, use
  997. * {@link WidgetUtil#setSelectionRange(Element, int, int, String)}
  998. * instead.
  999. *
  1000. * @param elem
  1001. * the html input element.
  1002. * @param pos
  1003. * the index of the first selected character.
  1004. * @param length
  1005. * the selection length.
  1006. * @param direction
  1007. * a string indicating the direction in which the selection was
  1008. * performed. This may be "forward" or "backward", or "none" if
  1009. * the direction is unknown or irrelevant.
  1010. *
  1011. * @since 7.3
  1012. */
  1013. @Deprecated
  1014. public static void setSelectionRange(Element elem, int pos, int length,
  1015. String direction) {
  1016. WidgetUtil.setSelectionRange(elem, pos, length, direction);
  1017. }
  1018. /**
  1019. * Converts a native {@link JavaScriptObject} into a {@link JsonValue}. This
  1020. * is a no-op in GWT code compiled to javascript, but needs some special
  1021. * handling to work when run in JVM.
  1022. *
  1023. * @param jso
  1024. * the java script object to represent as json
  1025. * @return the json representation
  1026. */
  1027. public static <T extends JsonValue> T jso2json(JavaScriptObject jso) {
  1028. if (GWT.isProdMode()) {
  1029. return (T) jso.<JsJsonValue> cast();
  1030. } else {
  1031. return elemental.json.Json.instance().parse(stringify(jso));
  1032. }
  1033. }
  1034. /**
  1035. * Converts a {@link JsonValue} into a native {@link JavaScriptObject}. This
  1036. * is a no-op in GWT code compiled to javascript, but needs some special
  1037. * handling to work when run in JVM.
  1038. *
  1039. * @param jsonValue
  1040. * the json value
  1041. * @return a native javascript object representation of the json value
  1042. */
  1043. public static JavaScriptObject json2jso(JsonValue jsonValue) {
  1044. if (GWT.isProdMode()) {
  1045. return ((JavaScriptObject) jsonValue.toNative()).cast();
  1046. } else {
  1047. return parse(jsonValue.toJson());
  1048. }
  1049. }
  1050. /**
  1051. * Convert a {@link JavaScriptObject} into a string representation.
  1052. *
  1053. * @param json
  1054. * a JavaScript object to be converted to a string
  1055. * @return JSON in string representation
  1056. */
  1057. private static native String stringify(JavaScriptObject json)
  1058. /*-{
  1059. return JSON.stringify(json);
  1060. }-*/;
  1061. /**
  1062. * Parse a string containing JSON into a {@link JavaScriptObject}.
  1063. *
  1064. * @param <T>
  1065. * the overlay type to expect from the parse
  1066. * @param jsonAsString
  1067. * @return a JavaScript object constructed from the parse
  1068. */
  1069. public static native <T extends JavaScriptObject> T parse(
  1070. String jsonAsString)
  1071. /*-{
  1072. return JSON.parse(jsonAsString);
  1073. }-*/;
  1074. /**
  1075. * Round {@code num} up to {@code exp} decimal positions.
  1076. *
  1077. * @since 7.6
  1078. */
  1079. public static native double round(double num, int exp)
  1080. /*-{
  1081. return +(Math.round(num + "e+" + exp) + "e-" + exp);
  1082. }-*/;
  1083. /**
  1084. * Test if we can can skip measuring this connector. We can skip the
  1085. * measurement if its parent is a ChildMeasureHintConnector and has an
  1086. * appropriate mode set.
  1087. *
  1088. * For internal use only. May be removed or replaced in the future.
  1089. */
  1090. public static boolean shouldSkipMeasurementOfConnector(
  1091. ComponentConnector candidate, ComponentConnector parent) {
  1092. Profiler.enter("skipMeasureDueLayoutHint");
  1093. boolean skip = false;
  1094. if (parent instanceof HasChildMeasurementHintConnector) {
  1095. ChildMeasurementHint measureMode = ((HasChildMeasurementHintConnector) parent)
  1096. .getChildMeasurementHint();
  1097. if (measureMode == ChildMeasurementHint.MEASURE_NEVER) {
  1098. skip = true;
  1099. } else if (measureMode == ChildMeasurementHint.MEASURE_IF_NEEDED) {
  1100. skip = canWeSkipChildMeasurement(candidate);
  1101. }
  1102. }
  1103. Profiler.leave("skipMeasureDueLayoutHint");
  1104. return skip;
  1105. }
  1106. /**
  1107. * Test if we can can skip measuring this connector. We can skip the
  1108. * measurement if its parent is a ChildMeasureHintConnector and has an
  1109. * appropriate mode set.
  1110. *
  1111. * This version of the method tries to recursively locate such a parent.
  1112. *
  1113. * For internal use only. May be removed or replaced in the future.
  1114. */
  1115. public static boolean shouldSkipMeasurementOfConnector(
  1116. ComponentConnector candidate) {
  1117. Profiler.enter("skipMeasureDueLayoutHint");
  1118. boolean skip = false;
  1119. HasChildMeasurementHintConnector parent = getPossibleChildMeasurementHintParentConnector(
  1120. candidate);
  1121. if (parent != null) {
  1122. ChildMeasurementHint measureMode = parent.getChildMeasurementHint();
  1123. if (measureMode == ChildMeasurementHint.MEASURE_NEVER) {
  1124. skip = true;
  1125. } else if (measureMode == ChildMeasurementHint.MEASURE_IF_NEEDED) {
  1126. skip = canWeSkipChildMeasurement(candidate);
  1127. }
  1128. }
  1129. Profiler.leave("skipMeasureDueLayoutHint");
  1130. return skip;
  1131. }
  1132. /** For internal use only. May be removed or replaced in the future. */
  1133. private static boolean canWeSkipChildMeasurement(ComponentConnector child) {
  1134. // common cases when child measuring is possibly needed
  1135. if (child instanceof ElementResizeListener
  1136. || child instanceof ManagedLayout
  1137. || child instanceof AbstractLayoutConnector) {
  1138. return false;
  1139. }
  1140. return true;
  1141. }
  1142. /** For internal use only. May be removed or replaced in the future. */
  1143. private static HasChildMeasurementHintConnector getPossibleChildMeasurementHintParentConnector(
  1144. ComponentConnector candidate) {
  1145. ServerConnector parent = candidate.getParent();
  1146. if (parent instanceof HasChildMeasurementHintConnector) {
  1147. return (HasChildMeasurementHintConnector) parent;
  1148. }
  1149. return null;
  1150. }
  1151. private static Logger getLogger() {
  1152. return Logger.getLogger(Util.class.getName());
  1153. }
  1154. }