Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Util.java 42KB

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