You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Util.java 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * Copyright 2000-2016 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 IE8. This is usually
  612. * necessary in cases where IE8 "forgets" to update child elements when they
  613. * resize.
  614. *
  615. * @deprecated As of 7.4.0, use {@link WidgetUtil#forceIE8Redraw(Element)}
  616. * instead.
  617. *
  618. * @param e
  619. * The element to perform the hack on
  620. */
  621. @Deprecated
  622. public static final void forceIE8Redraw(Element e) {
  623. WidgetUtil.forceIE8Redraw(e);
  624. }
  625. /**
  626. * Performs a hack to trigger a re-layout in the IE browser. This is usually
  627. * necessary in cases where IE "forgets" to update child elements when they
  628. * resize.
  629. *
  630. * @deprecated As of 7.4.0, use {@link WidgetUtil#forceIERedraw(Element)}
  631. * instead.
  632. *
  633. * @since 7.3
  634. * @param e
  635. * The element to perform the hack on
  636. */
  637. @Deprecated
  638. public static void forceIERedraw(Element e) {
  639. WidgetUtil.forceIERedraw(e);
  640. }
  641. /**
  642. * Detaches and re-attaches the element from its parent. The element is
  643. * reattached at the same position in the DOM as it was before.
  644. *
  645. * Does nothing if the element is not attached to the DOM.
  646. *
  647. * @deprecated As of 7.4.0, use {@link WidgetUtil#detachAttach(Element)}
  648. * instead.
  649. *
  650. * @param element
  651. * The element to detach and re-attach
  652. */
  653. @Deprecated
  654. public static void detachAttach(Element element) {
  655. WidgetUtil.detachAttach(element);
  656. }
  657. /**
  658. * @deprecated As of 7.4.0, use
  659. * {@link WidgetUtil#sinkOnloadForImages(Element)} instead.
  660. */
  661. @Deprecated
  662. public static void sinkOnloadForImages(Element element) {
  663. WidgetUtil.sinkOnloadForImages(element);
  664. }
  665. /**
  666. * Returns the index of the childElement within its parent.
  667. *
  668. * @deprecated As of 7.4.0, use
  669. * {@link WidgetUtil#getChildElementIndex(Element)} instead.
  670. *
  671. * @param subElement
  672. * @return
  673. */
  674. @Deprecated
  675. public static int getChildElementIndex(Element childElement) {
  676. return WidgetUtil.getChildElementIndex(childElement);
  677. }
  678. private static void printConnectorInvocations(
  679. ArrayList<MethodInvocation> invocations, String id,
  680. ApplicationConnection c) {
  681. ServerConnector connector = ConnectorMap.get(c).getConnector(id);
  682. if (connector != null) {
  683. getLogger().info("\t" + id + " (" + connector.getClass() + ") :");
  684. } else {
  685. getLogger().warning("\t" + id
  686. + ": Warning: no corresponding connector for id " + id);
  687. }
  688. for (MethodInvocation invocation : invocations) {
  689. getLogger().info("\t\t" + getInvocationDebugString(invocation));
  690. }
  691. }
  692. /**
  693. * Produces a string representation of a method invocation, suitable for
  694. * debug output
  695. *
  696. * @since 7.5
  697. * @param invocation
  698. * @return
  699. */
  700. private static String getInvocationDebugString(
  701. MethodInvocation invocation) {
  702. Object[] parameters = invocation.getParameters();
  703. String formattedParams = null;
  704. if (ApplicationConstants.UPDATE_VARIABLE_METHOD
  705. .equals(invocation.getMethodName()) && parameters.length == 2) {
  706. // name, value
  707. Object value = parameters[1];
  708. // TODO paintables inside lists/maps get rendered as
  709. // components in the debug console
  710. String formattedValue = value instanceof ServerConnector
  711. ? ((ServerConnector) value).getConnectorId()
  712. : String.valueOf(value);
  713. formattedParams = parameters[0] + " : " + formattedValue;
  714. }
  715. if (null == formattedParams) {
  716. formattedParams = (null != parameters) ? Arrays.toString(parameters)
  717. : null;
  718. }
  719. return invocation.getInterfaceName() + "." + invocation.getMethodName()
  720. + "(" + formattedParams + ")";
  721. }
  722. public static void logMethodInvocations(ApplicationConnection c,
  723. Collection<MethodInvocation> methodInvocations) {
  724. try {
  725. getLogger().info("RPC invocations to be sent to the server:");
  726. String curId = null;
  727. ArrayList<MethodInvocation> invocations = new ArrayList<MethodInvocation>();
  728. for (MethodInvocation methodInvocation : methodInvocations) {
  729. String id = methodInvocation.getConnectorId();
  730. if (curId == null) {
  731. curId = id;
  732. } else if (!curId.equals(id)) {
  733. printConnectorInvocations(invocations, curId, c);
  734. invocations.clear();
  735. curId = id;
  736. }
  737. invocations.add(methodInvocation);
  738. }
  739. if (!invocations.isEmpty()) {
  740. printConnectorInvocations(invocations, curId, c);
  741. }
  742. } catch (Exception e) {
  743. getLogger().log(Level.SEVERE, "Error logging method invocations",
  744. e);
  745. }
  746. }
  747. /**
  748. * Temporarily sets the {@code styleProperty} to {@code tempValue} and then
  749. * resets it to its current value. Used mainly to work around rendering
  750. * issues in IE (and possibly in other browsers)
  751. *
  752. * @deprecated As of 7.4.0, use
  753. * {@link WidgetUtil#setStyleTemporarily(Element, String, String)}
  754. * instead.
  755. *
  756. * @param element
  757. * The target element
  758. * @param styleProperty
  759. * The name of the property to set
  760. * @param tempValue
  761. * The temporary value
  762. */
  763. @Deprecated
  764. public static void setStyleTemporarily(Element element,
  765. final String styleProperty, String tempValue) {
  766. WidgetUtil.setStyleTemporarily(element, styleProperty, tempValue);
  767. }
  768. /**
  769. * A helper method to return the client position from an event. Returns
  770. * position from either first changed touch (if touch event) or from the
  771. * event itself.
  772. *
  773. * @deprecated As of 7.4.0, use
  774. * {@link WidgetUtil#getTouchOrMouseClientX(Event)} instead.
  775. *
  776. * @param event
  777. * @return
  778. */
  779. @Deprecated
  780. public static int getTouchOrMouseClientX(Event event) {
  781. return WidgetUtil.getTouchOrMouseClientX(event);
  782. }
  783. /**
  784. * Find the element corresponding to the coordinates in the passed mouse
  785. * event. Please note that this is not always the same as the target of the
  786. * event e.g. if event capture is used.
  787. *
  788. * @deprecated As of 7.4.0, use
  789. * {@link WidgetUtil#getElementUnderMouse(NativeEvent)} instead.
  790. *
  791. * @param event
  792. * the mouse event to get coordinates from
  793. * @return the element at the coordinates of the event
  794. */
  795. @Deprecated
  796. public static com.google.gwt.user.client.Element getElementUnderMouse(
  797. NativeEvent event) {
  798. return DOM.asOld(WidgetUtil.getElementUnderMouse(event));
  799. }
  800. /**
  801. * A helper method to return the client position from an event. Returns
  802. * position from either first changed touch (if touch event) or from the
  803. * event itself.
  804. *
  805. * @deprecated As of 7.4.0, use
  806. * {@link WidgetUtil#getTouchOrMouseClientY(Event)} instead.
  807. *
  808. * @param event
  809. * @return
  810. */
  811. @Deprecated
  812. public static int getTouchOrMouseClientY(Event event) {
  813. return WidgetUtil.getTouchOrMouseClientY(event);
  814. }
  815. /**
  816. * @deprecated As of 7.4.0, use
  817. * {@link WidgetUtil#getTouchOrMouseClientY(NativeEvent)}
  818. * instead.
  819. *
  820. * @see #getTouchOrMouseClientY(Event)
  821. * @param currentGwtEvent
  822. * @return
  823. */
  824. @Deprecated
  825. public static int getTouchOrMouseClientY(NativeEvent currentGwtEvent) {
  826. return WidgetUtil.getTouchOrMouseClientY(currentGwtEvent);
  827. }
  828. /**
  829. * @deprecated As of 7.4.0, use
  830. * {@link WidgetUtil#getTouchOrMouseClientX(NativeEvent)}
  831. * instead.
  832. *
  833. * @see #getTouchOrMouseClientX(Event)
  834. *
  835. * @param event
  836. * @return
  837. */
  838. @Deprecated
  839. public static int getTouchOrMouseClientX(NativeEvent event) {
  840. return WidgetUtil.getTouchOrMouseClientX(event);
  841. }
  842. /**
  843. * @deprecated As of 7.4.0, use {@link WidgetUtil#isTouchEvent(Event)}
  844. * instead.
  845. */
  846. @Deprecated
  847. public static boolean isTouchEvent(Event event) {
  848. return WidgetUtil.isTouchEvent(event);
  849. }
  850. /**
  851. * @deprecated As of 7.4.0, use {@link WidgetUtil#isTouchEvent(NativeEvent)}
  852. * instead.
  853. */
  854. @Deprecated
  855. public static boolean isTouchEvent(NativeEvent event) {
  856. return WidgetUtil.isTouchEvent(event);
  857. }
  858. /**
  859. * @deprecated As of 7.4.0, use
  860. * {@link WidgetUtil#simulateClickFromTouchEvent(Event, Widget)}
  861. * instead.
  862. */
  863. @Deprecated
  864. public static void simulateClickFromTouchEvent(Event touchevent,
  865. Widget widget) {
  866. WidgetUtil.simulateClickFromTouchEvent(touchevent, widget);
  867. }
  868. /**
  869. * Gets the currently focused element.
  870. *
  871. * @deprecated As of 7.4.0, use {@link WidgetUtil#getFocusedElement()}
  872. * instead.
  873. *
  874. * @return The active element or null if no active element could be found.
  875. */
  876. @Deprecated
  877. public static com.google.gwt.user.client.Element getFocusedElement() {
  878. return DOM.asOld(WidgetUtil.getFocusedElement());
  879. }
  880. /**
  881. * Gets the currently focused element for Internet Explorer.
  882. *
  883. * @return The currently focused element
  884. * @deprecated Use #getFocusedElement instead
  885. */
  886. @Deprecated
  887. public static com.google.gwt.user.client.Element getIEFocusedElement() {
  888. return getFocusedElement();
  889. }
  890. /**
  891. * Gets currently focused element and checks if it's editable
  892. *
  893. * @deprecated As of 7.4.0, use
  894. * {@link WidgetUtil#isFocusedElementEditable()} instead.
  895. *
  896. * @since 7.4
  897. *
  898. * @return true if focused element is editable
  899. */
  900. @Deprecated
  901. public static boolean isFocusedElementEditable() {
  902. return WidgetUtil.isFocusedElementEditable();
  903. }
  904. /**
  905. * Kind of stronger version of isAttached(). In addition to std isAttached,
  906. * this method checks that this widget nor any of its parents is hidden. Can
  907. * be e.g used to check whether component should react to some events or
  908. * not.
  909. *
  910. * @deprecated As of 7.4.0, use
  911. * {@link WidgetUtil#isAttachedAndDisplayed(Widget)} instead.
  912. *
  913. * @param widget
  914. * @return true if attached and displayed
  915. */
  916. @Deprecated
  917. public static boolean isAttachedAndDisplayed(Widget widget) {
  918. return WidgetUtil.isAttachedAndDisplayed(widget);
  919. }
  920. /**
  921. * Scrolls an element into view vertically only. Modified version of
  922. * Element.scrollIntoView.
  923. *
  924. * @deprecated As of 7.4.0, use
  925. * {@link WidgetUtil#scrollIntoViewVertically(Element)} instead.
  926. *
  927. * @param elem
  928. * The element to scroll into view
  929. */
  930. @Deprecated
  931. public static void scrollIntoViewVertically(Element elem) {
  932. WidgetUtil.scrollIntoViewVertically(elem);
  933. }
  934. /**
  935. * Checks if the given event is either a touch event or caused by the left
  936. * mouse button
  937. *
  938. * @deprecated As of 7.4.0, use
  939. * {@link WidgetUtil#isTouchEventOrLeftMouseButton(Event)}
  940. * instead.
  941. *
  942. * @param event
  943. * @return true if the event is a touch event or caused by the left mouse
  944. * button, false otherwise
  945. */
  946. @Deprecated
  947. public static boolean isTouchEventOrLeftMouseButton(Event event) {
  948. return WidgetUtil.isTouchEventOrLeftMouseButton(event);
  949. }
  950. /**
  951. * Performs a shallow comparison of the collections.
  952. *
  953. * @param collection1
  954. * The first collection
  955. * @param collection2
  956. * The second collection
  957. * @return true if the collections contain the same elements in the same
  958. * order, false otherwise
  959. */
  960. public static boolean collectionsEquals(Collection<?> collection1,
  961. Collection<?> collection2) {
  962. if (collection1 == null) {
  963. return collection2 == null;
  964. }
  965. if (collection2 == null) {
  966. return false;
  967. }
  968. if (collection1.size() != collection2.size()) {
  969. return false;
  970. }
  971. Iterator<?> collection1Iterator = collection1.iterator();
  972. Iterator<?> collection2Iterator = collection2.iterator();
  973. while (collection1Iterator.hasNext()) {
  974. Object collection1Object = collection1Iterator.next();
  975. Object collection2Object = collection2Iterator.next();
  976. if (collection1Object != collection2Object) {
  977. return false;
  978. }
  979. }
  980. return true;
  981. }
  982. public static String getConnectorString(ServerConnector p) {
  983. if (p == null) {
  984. return "null";
  985. }
  986. return getSimpleName(p) + " (" + p.getConnectorId() + ")";
  987. }
  988. /**
  989. * Resolve a relative URL to an absolute URL based on the current document's
  990. * location.
  991. *
  992. * @deprecated As of 7.4.0, use {@link WidgetUtil#getAbsoluteUrl(String)}
  993. * instead.
  994. *
  995. * @param url
  996. * a string with the relative URL to resolve
  997. * @return the corresponding absolute URL as a string
  998. */
  999. @Deprecated
  1000. public static String getAbsoluteUrl(String url) {
  1001. return WidgetUtil.getAbsoluteUrl(url);
  1002. }
  1003. /**
  1004. * Sets the selection range of an input element.
  1005. *
  1006. * We need this JSNI function to set selection range so that we can use the
  1007. * optional direction attribute to set the anchor to the end and the focus
  1008. * to the start. This makes Firefox work the same way as other browsers
  1009. * (#13477)
  1010. *
  1011. * @deprecated As of 7.4.0, use
  1012. * {@link WidgetUtil#setSelectionRange(Element, int, int, String)}
  1013. * instead.
  1014. *
  1015. * @param elem
  1016. * the html input element.
  1017. * @param pos
  1018. * the index of the first selected character.
  1019. * @param length
  1020. * the selection length.
  1021. * @param direction
  1022. * a string indicating the direction in which the selection was
  1023. * performed. This may be "forward" or "backward", or "none" if
  1024. * the direction is unknown or irrelevant.
  1025. *
  1026. * @since 7.3
  1027. */
  1028. @Deprecated
  1029. public static void setSelectionRange(Element elem, int pos, int length,
  1030. String direction) {
  1031. WidgetUtil.setSelectionRange(elem, pos, length, direction);
  1032. }
  1033. /**
  1034. * Converts a native {@link JavaScriptObject} into a {@link JsonValue}. This
  1035. * is a no-op in GWT code compiled to javascript, but needs some special
  1036. * handling to work when run in JVM.
  1037. *
  1038. * @param jso
  1039. * the java script object to represent as json
  1040. * @return the json representation
  1041. */
  1042. public static <T extends JsonValue> T jso2json(JavaScriptObject jso) {
  1043. if (GWT.isProdMode()) {
  1044. return (T) jso.<JsJsonValue> cast();
  1045. } else {
  1046. return elemental.json.Json.instance().parse(stringify(jso));
  1047. }
  1048. }
  1049. /**
  1050. * Converts a {@link JsonValue} into a native {@link JavaScriptObject}. This
  1051. * is a no-op in GWT code compiled to javascript, but needs some special
  1052. * handling to work when run in JVM.
  1053. *
  1054. * @param jsonValue
  1055. * the json value
  1056. * @return a native javascript object representation of the json value
  1057. */
  1058. public static JavaScriptObject json2jso(JsonValue jsonValue) {
  1059. if (GWT.isProdMode()) {
  1060. return ((JavaScriptObject) jsonValue.toNative()).cast();
  1061. } else {
  1062. return parse(jsonValue.toJson());
  1063. }
  1064. }
  1065. /**
  1066. * Convert a {@link JavaScriptObject} into a string representation.
  1067. *
  1068. * @param json
  1069. * a JavaScript object to be converted to a string
  1070. * @return JSON in string representation
  1071. */
  1072. private native static String stringify(JavaScriptObject json)
  1073. /*-{
  1074. return JSON.stringify(json);
  1075. }-*/;
  1076. /**
  1077. * Parse a string containing JSON into a {@link JavaScriptObject}.
  1078. *
  1079. * @param <T>
  1080. * the overlay type to expect from the parse
  1081. * @param jsonAsString
  1082. * @return a JavaScript object constructed from the parse
  1083. */
  1084. public native static <T extends JavaScriptObject> T parse(
  1085. String jsonAsString)
  1086. /*-{
  1087. return JSON.parse(jsonAsString);
  1088. }-*/;
  1089. /**
  1090. * Round {@code num} up to {@code exp} decimal positions.
  1091. *
  1092. * @since 7.6
  1093. */
  1094. public static native double round(double num, int exp)
  1095. /*-{
  1096. return +(Math.round(num + "e+" + exp) + "e-" + exp);
  1097. }-*/;
  1098. /**
  1099. * Test if we can can skip measuring this connector. We can skip the
  1100. * measurement if its parent is a ChildMeasureHintConnector and has an
  1101. * appropriate mode set.
  1102. *
  1103. * For internal use only. May be removed or replaced in the future.
  1104. */
  1105. public static boolean shouldSkipMeasurementOfConnector(
  1106. ComponentConnector candidate, ComponentConnector parent) {
  1107. Profiler.enter("skipMeasureDueLayoutHint");
  1108. boolean skip = false;
  1109. if (parent instanceof HasChildMeasurementHintConnector) {
  1110. ChildMeasurementHint measureMode = ((HasChildMeasurementHintConnector) parent)
  1111. .getChildMeasurementHint();
  1112. if (measureMode == ChildMeasurementHint.MEASURE_NEVER) {
  1113. skip = true;
  1114. } else if (measureMode == ChildMeasurementHint.MEASURE_IF_NEEDED) {
  1115. skip = canWeSkipChildMeasurement(candidate);
  1116. }
  1117. }
  1118. Profiler.leave("skipMeasureDueLayoutHint");
  1119. return skip;
  1120. }
  1121. /**
  1122. * Test if we can can skip measuring this connector. We can skip the
  1123. * measurement if its parent is a ChildMeasureHintConnector and has an
  1124. * appropriate mode set.
  1125. *
  1126. * This version of the method tries to recursively locate such a parent.
  1127. *
  1128. * For internal use only. May be removed or replaced in the future.
  1129. */
  1130. public static boolean shouldSkipMeasurementOfConnector(
  1131. ComponentConnector candidate) {
  1132. Profiler.enter("skipMeasureDueLayoutHint");
  1133. boolean skip = false;
  1134. HasChildMeasurementHintConnector parent = getPossibleChildMeasurementHintParentConnector(
  1135. candidate);
  1136. if (parent != null) {
  1137. ChildMeasurementHint measureMode = parent.getChildMeasurementHint();
  1138. if (measureMode == ChildMeasurementHint.MEASURE_NEVER) {
  1139. skip = true;
  1140. } else if (measureMode == ChildMeasurementHint.MEASURE_IF_NEEDED) {
  1141. skip = canWeSkipChildMeasurement(candidate);
  1142. }
  1143. }
  1144. Profiler.leave("skipMeasureDueLayoutHint");
  1145. return skip;
  1146. }
  1147. /** For internal use only. May be removed or replaced in the future. */
  1148. private static boolean canWeSkipChildMeasurement(ComponentConnector child) {
  1149. // common cases when child measuring is possibly needed
  1150. if (child instanceof ElementResizeListener
  1151. || child instanceof ManagedLayout
  1152. || child instanceof AbstractLayoutConnector) {
  1153. return false;
  1154. }
  1155. return true;
  1156. }
  1157. /** For internal use only. May be removed or replaced in the future. */
  1158. private static HasChildMeasurementHintConnector getPossibleChildMeasurementHintParentConnector(
  1159. ComponentConnector candidate) {
  1160. ServerConnector parent = candidate.getParent();
  1161. if (parent != null
  1162. && parent instanceof HasChildMeasurementHintConnector) {
  1163. return (HasChildMeasurementHintConnector) parent;
  1164. }
  1165. return null;
  1166. }
  1167. private static Logger getLogger() {
  1168. return Logger.getLogger(Util.class.getName());
  1169. }
  1170. }