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

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