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.

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