Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Util.java 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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. getLogger().info("\t\t" + getInvocationDebugString(invocation));
  684. }
  685. }
  686. /**
  687. * Produces a string representation of a method invocation, suitable for
  688. * debug output
  689. *
  690. * @since 7.5
  691. * @param invocation
  692. * @return
  693. */
  694. private static String getInvocationDebugString(MethodInvocation invocation) {
  695. Object[] parameters = invocation.getParameters();
  696. String formattedParams = null;
  697. if (ApplicationConstants.UPDATE_VARIABLE_METHOD.equals(invocation
  698. .getMethodName()) && parameters.length == 2) {
  699. // name, value
  700. Object value = parameters[1];
  701. // TODO paintables inside lists/maps get rendered as
  702. // components in the debug console
  703. String formattedValue = value instanceof ServerConnector ? ((ServerConnector) value)
  704. .getConnectorId() : String.valueOf(value);
  705. formattedParams = parameters[0] + " : " + formattedValue;
  706. }
  707. if (null == formattedParams) {
  708. formattedParams = (null != parameters) ? Arrays
  709. .toString(parameters) : null;
  710. }
  711. return invocation.getInterfaceName() + "." + invocation.getMethodName()
  712. + "(" + formattedParams + ")";
  713. }
  714. public static void logMethodInvocations(ApplicationConnection c,
  715. Collection<MethodInvocation> methodInvocations) {
  716. try {
  717. getLogger().info("RPC invocations to be sent to the server:");
  718. String curId = null;
  719. ArrayList<MethodInvocation> invocations = new ArrayList<MethodInvocation>();
  720. for (MethodInvocation methodInvocation : methodInvocations) {
  721. String id = methodInvocation.getConnectorId();
  722. if (curId == null) {
  723. curId = id;
  724. } else if (!curId.equals(id)) {
  725. printConnectorInvocations(invocations, curId, c);
  726. invocations.clear();
  727. curId = id;
  728. }
  729. invocations.add(methodInvocation);
  730. }
  731. if (!invocations.isEmpty()) {
  732. printConnectorInvocations(invocations, curId, c);
  733. }
  734. } catch (Exception e) {
  735. getLogger()
  736. .log(Level.SEVERE, "Error logging method invocations", e);
  737. }
  738. }
  739. /**
  740. * Temporarily sets the {@code styleProperty} to {@code tempValue} and then
  741. * resets it to its current value. Used mainly to work around rendering
  742. * issues in IE (and possibly in other browsers)
  743. *
  744. * @deprecated As of 7.4.0, use
  745. * {@link WidgetUtil#setStyleTemporarily(Element, String, String)}
  746. * instead.
  747. *
  748. * @param element
  749. * The target element
  750. * @param styleProperty
  751. * The name of the property to set
  752. * @param tempValue
  753. * The temporary value
  754. */
  755. @Deprecated
  756. public static void setStyleTemporarily(Element element,
  757. final String styleProperty, String tempValue) {
  758. WidgetUtil.setStyleTemporarily(element, styleProperty, tempValue);
  759. }
  760. /**
  761. * A helper method to return the client position from an event. Returns
  762. * position from either first changed touch (if touch event) or from the
  763. * event itself.
  764. *
  765. * @deprecated As of 7.4.0, use
  766. * {@link WidgetUtil#getTouchOrMouseClientX(Event)} instead.
  767. *
  768. * @param event
  769. * @return
  770. */
  771. @Deprecated
  772. public static int getTouchOrMouseClientX(Event event) {
  773. return WidgetUtil.getTouchOrMouseClientX(event);
  774. }
  775. /**
  776. * Find the element corresponding to the coordinates in the passed mouse
  777. * event. Please note that this is not always the same as the target of the
  778. * event e.g. if event capture is used.
  779. *
  780. * @deprecated As of 7.4.0, use
  781. * {@link WidgetUtil#getElementUnderMouse(NativeEvent)} instead.
  782. *
  783. * @param event
  784. * the mouse event to get coordinates from
  785. * @return the element at the coordinates of the event
  786. */
  787. @Deprecated
  788. public static com.google.gwt.user.client.Element getElementUnderMouse(
  789. NativeEvent event) {
  790. return DOM.asOld(WidgetUtil.getElementUnderMouse(event));
  791. }
  792. /**
  793. * A helper method to return the client position from an event. Returns
  794. * position from either first changed touch (if touch event) or from the
  795. * event itself.
  796. *
  797. * @deprecated As of 7.4.0, use
  798. * {@link WidgetUtil#getTouchOrMouseClientY(Event)} instead.
  799. *
  800. * @param event
  801. * @return
  802. */
  803. @Deprecated
  804. public static int getTouchOrMouseClientY(Event event) {
  805. return WidgetUtil.getTouchOrMouseClientY(event);
  806. }
  807. /**
  808. * @deprecated As of 7.4.0, use
  809. * {@link WidgetUtil#getTouchOrMouseClientY(NativeEvent)}
  810. * instead.
  811. *
  812. * @see #getTouchOrMouseClientY(Event)
  813. * @param currentGwtEvent
  814. * @return
  815. */
  816. @Deprecated
  817. public static int getTouchOrMouseClientY(NativeEvent currentGwtEvent) {
  818. return WidgetUtil.getTouchOrMouseClientY(currentGwtEvent);
  819. }
  820. /**
  821. * @deprecated As of 7.4.0, use
  822. * {@link WidgetUtil#getTouchOrMouseClientX(NativeEvent)}
  823. * instead.
  824. *
  825. * @see #getTouchOrMouseClientX(Event)
  826. *
  827. * @param event
  828. * @return
  829. */
  830. @Deprecated
  831. public static int getTouchOrMouseClientX(NativeEvent event) {
  832. return WidgetUtil.getTouchOrMouseClientX(event);
  833. }
  834. /**
  835. * @deprecated As of 7.4.0, use {@link WidgetUtil#isTouchEvent(Event)}
  836. * instead.
  837. */
  838. @Deprecated
  839. public static boolean isTouchEvent(Event event) {
  840. return WidgetUtil.isTouchEvent(event);
  841. }
  842. /**
  843. * @deprecated As of 7.4.0, use {@link WidgetUtil#isTouchEvent(NativeEvent)}
  844. * instead.
  845. */
  846. @Deprecated
  847. public static boolean isTouchEvent(NativeEvent event) {
  848. return WidgetUtil.isTouchEvent(event);
  849. }
  850. /**
  851. * @deprecated As of 7.4.0, use
  852. * {@link WidgetUtil#simulateClickFromTouchEvent(Event, Widget)}
  853. * instead.
  854. */
  855. @Deprecated
  856. public static void simulateClickFromTouchEvent(Event touchevent,
  857. Widget widget) {
  858. WidgetUtil.simulateClickFromTouchEvent(touchevent, widget);
  859. }
  860. /**
  861. * Gets the currently focused element.
  862. *
  863. * @deprecated As of 7.4.0, use {@link WidgetUtil#getFocusedElement()}
  864. * instead.
  865. *
  866. * @return The active element or null if no active element could be found.
  867. */
  868. @Deprecated
  869. public static com.google.gwt.user.client.Element getFocusedElement() {
  870. return DOM.asOld(WidgetUtil.getFocusedElement());
  871. }
  872. /**
  873. * Gets the currently focused element for Internet Explorer.
  874. *
  875. * @return The currently focused element
  876. * @deprecated Use #getFocusedElement instead
  877. */
  878. @Deprecated
  879. public static com.google.gwt.user.client.Element getIEFocusedElement() {
  880. return getFocusedElement();
  881. }
  882. /**
  883. * Gets currently focused element and checks if it's editable
  884. *
  885. * @deprecated As of 7.4.0, use
  886. * {@link WidgetUtil#isFocusedElementEditable()} instead.
  887. *
  888. * @since 7.4
  889. *
  890. * @return true if focused element is editable
  891. */
  892. @Deprecated
  893. public static boolean isFocusedElementEditable() {
  894. return WidgetUtil.isFocusedElementEditable();
  895. }
  896. /**
  897. * Kind of stronger version of isAttached(). In addition to std isAttached,
  898. * this method checks that this widget nor any of its parents is hidden. Can
  899. * be e.g used to check whether component should react to some events or
  900. * not.
  901. *
  902. * @deprecated As of 7.4.0, use
  903. * {@link WidgetUtil#isAttachedAndDisplayed(Widget)} instead.
  904. *
  905. * @param widget
  906. * @return true if attached and displayed
  907. */
  908. @Deprecated
  909. public static boolean isAttachedAndDisplayed(Widget widget) {
  910. return WidgetUtil.isAttachedAndDisplayed(widget);
  911. }
  912. /**
  913. * Scrolls an element into view vertically only. Modified version of
  914. * Element.scrollIntoView.
  915. *
  916. * @deprecated As of 7.4.0, use
  917. * {@link WidgetUtil#scrollIntoViewVertically(Element)} instead.
  918. *
  919. * @param elem
  920. * The element to scroll into view
  921. */
  922. @Deprecated
  923. public static void scrollIntoViewVertically(Element elem) {
  924. WidgetUtil.scrollIntoViewVertically(elem);
  925. }
  926. /**
  927. * Checks if the given event is either a touch event or caused by the left
  928. * mouse button
  929. *
  930. * @deprecated As of 7.4.0, use
  931. * {@link WidgetUtil#isTouchEventOrLeftMouseButton(Event)}
  932. * instead.
  933. *
  934. * @param event
  935. * @return true if the event is a touch event or caused by the left mouse
  936. * button, false otherwise
  937. */
  938. @Deprecated
  939. public static boolean isTouchEventOrLeftMouseButton(Event event) {
  940. return WidgetUtil.isTouchEventOrLeftMouseButton(event);
  941. }
  942. /**
  943. * Performs a shallow comparison of the collections.
  944. *
  945. * @param collection1
  946. * The first collection
  947. * @param collection2
  948. * The second collection
  949. * @return true if the collections contain the same elements in the same
  950. * order, false otherwise
  951. */
  952. public static boolean collectionsEquals(Collection<?> collection1,
  953. Collection<?> collection2) {
  954. if (collection1 == null) {
  955. return collection2 == null;
  956. }
  957. if (collection2 == null) {
  958. return false;
  959. }
  960. if (collection1.size() != collection2.size()) {
  961. return false;
  962. }
  963. Iterator<?> collection1Iterator = collection1.iterator();
  964. Iterator<?> collection2Iterator = collection2.iterator();
  965. while (collection1Iterator.hasNext()) {
  966. Object collection1Object = collection1Iterator.next();
  967. Object collection2Object = collection2Iterator.next();
  968. if (collection1Object != collection2Object) {
  969. return false;
  970. }
  971. }
  972. return true;
  973. }
  974. public static String getConnectorString(ServerConnector p) {
  975. if (p == null) {
  976. return "null";
  977. }
  978. return getSimpleName(p) + " (" + p.getConnectorId() + ")";
  979. }
  980. /**
  981. * Resolve a relative URL to an absolute URL based on the current document's
  982. * location.
  983. *
  984. * @deprecated As of 7.4.0, use {@link WidgetUtil#getAbsoluteUrl(String)}
  985. * instead.
  986. *
  987. * @param url
  988. * a string with the relative URL to resolve
  989. * @return the corresponding absolute URL as a string
  990. */
  991. @Deprecated
  992. public static String getAbsoluteUrl(String url) {
  993. return WidgetUtil.getAbsoluteUrl(url);
  994. }
  995. /**
  996. * Sets the selection range of an input element.
  997. *
  998. * We need this JSNI function to set selection range so that we can use the
  999. * optional direction attribute to set the anchor to the end and the focus
  1000. * to the start. This makes Firefox work the same way as other browsers
  1001. * (#13477)
  1002. *
  1003. * @deprecated As of 7.4.0, use
  1004. * {@link WidgetUtil#setSelectionRange(Element, int, int, String)}
  1005. * instead.
  1006. *
  1007. * @param elem
  1008. * the html input element.
  1009. * @param pos
  1010. * the index of the first selected character.
  1011. * @param length
  1012. * the selection length.
  1013. * @param direction
  1014. * a string indicating the direction in which the selection was
  1015. * performed. This may be "forward" or "backward", or "none" if
  1016. * the direction is unknown or irrelevant.
  1017. *
  1018. * @since 7.3
  1019. */
  1020. @Deprecated
  1021. public static void setSelectionRange(Element elem, int pos, int length,
  1022. String direction) {
  1023. WidgetUtil.setSelectionRange(elem, pos, length, direction);
  1024. }
  1025. /**
  1026. * Converts a native {@link JavaScriptObject} into a {@link JsonValue}. This
  1027. * is a no-op in GWT code compiled to javascript, but needs some special
  1028. * handling to work when run in JVM.
  1029. *
  1030. * @param jso
  1031. * the java script object to represent as json
  1032. * @return the json representation
  1033. */
  1034. public static <T extends JsonValue> T jso2json(JavaScriptObject jso) {
  1035. if (GWT.isProdMode()) {
  1036. return (T) jso.<JsJsonValue> cast();
  1037. } else {
  1038. return elemental.json.Json.instance().parse(stringify(jso));
  1039. }
  1040. }
  1041. /**
  1042. * Converts a {@link JsonValue} into a native {@link JavaScriptObject}. This
  1043. * is a no-op in GWT code compiled to javascript, but needs some special
  1044. * handling to work when run in JVM.
  1045. *
  1046. * @param jsonValue
  1047. * the json value
  1048. * @return a native javascript object representation of the json value
  1049. */
  1050. public static JavaScriptObject json2jso(JsonValue jsonValue) {
  1051. if (GWT.isProdMode()) {
  1052. return ((JavaScriptObject) jsonValue.toNative()).cast();
  1053. } else {
  1054. return parse(jsonValue.toJson());
  1055. }
  1056. }
  1057. /**
  1058. * Convert a {@link JavaScriptObject} into a string representation.
  1059. *
  1060. * @param json
  1061. * a JavaScript object to be converted to a string
  1062. * @return JSON in string representation
  1063. */
  1064. private native static String stringify(JavaScriptObject json)
  1065. /*-{
  1066. return JSON.stringify(json);
  1067. }-*/;
  1068. /**
  1069. * Parse a string containing JSON into a {@link JavaScriptObject}.
  1070. *
  1071. * @param <T>
  1072. * the overlay type to expect from the parse
  1073. * @param jsonAsString
  1074. * @return a JavaScript object constructed from the parse
  1075. */
  1076. public native static <T extends JavaScriptObject> T parse(
  1077. String jsonAsString)
  1078. /*-{
  1079. return JSON.parse(jsonAsString);
  1080. }-*/;
  1081. private static Logger getLogger() {
  1082. return Logger.getLogger(Util.class.getName());
  1083. }
  1084. }