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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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 com.google.gwt.core.client.Scheduler;
  23. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  24. import com.google.gwt.dom.client.AnchorElement;
  25. import com.google.gwt.dom.client.DivElement;
  26. import com.google.gwt.dom.client.Document;
  27. import com.google.gwt.dom.client.Element;
  28. import com.google.gwt.dom.client.NativeEvent;
  29. import com.google.gwt.dom.client.Node;
  30. import com.google.gwt.dom.client.NodeList;
  31. import com.google.gwt.dom.client.Style;
  32. import com.google.gwt.dom.client.Style.Display;
  33. import com.google.gwt.dom.client.Style.Unit;
  34. import com.google.gwt.dom.client.Touch;
  35. import com.google.gwt.event.dom.client.KeyEvent;
  36. import com.google.gwt.user.client.Command;
  37. import com.google.gwt.user.client.DOM;
  38. import com.google.gwt.user.client.Event;
  39. import com.google.gwt.user.client.EventListener;
  40. import com.google.gwt.user.client.Window;
  41. import com.google.gwt.user.client.ui.HasWidgets;
  42. import com.google.gwt.user.client.ui.RootPanel;
  43. import com.google.gwt.user.client.ui.Widget;
  44. import com.vaadin.client.RenderInformation.FloatSize;
  45. import com.vaadin.client.ui.VOverlay;
  46. import com.vaadin.shared.AbstractComponentState;
  47. import com.vaadin.shared.ApplicationConstants;
  48. import com.vaadin.shared.communication.MethodInvocation;
  49. import com.vaadin.shared.ui.ComponentStateUtil;
  50. import com.vaadin.shared.util.SharedUtil;
  51. public class Util {
  52. /**
  53. * Helper method for debugging purposes.
  54. *
  55. * Stops execution on firefox browsers on a breakpoint.
  56. *
  57. */
  58. public static native void browserDebugger()
  59. /*-{
  60. if($wnd.console)
  61. debugger;
  62. }-*/;
  63. /**
  64. * Helper method for a bug fix #14041. For mozilla getKeyCode return 0 for
  65. * space bar (because space is considered as char). If return 0 use
  66. * getCharCode.
  67. *
  68. * @param event
  69. * @return return key code
  70. * @since 7.2.4
  71. */
  72. public static int getKeyCode(KeyEvent<?> event) {
  73. int keyCode = event.getNativeEvent().getKeyCode();
  74. if (keyCode == 0) {
  75. keyCode = event.getNativeEvent().getCharCode();
  76. }
  77. return keyCode;
  78. }
  79. /**
  80. *
  81. * Returns the topmost element of from given coordinates.
  82. *
  83. * TODO fix crossplat issues clientX vs pageX. See quircksmode. Not critical
  84. * for vaadin as we scroll div istead of page.
  85. *
  86. * @param x
  87. * @param y
  88. * @return the element at given coordinates
  89. */
  90. public static native com.google.gwt.user.client.Element getElementFromPoint(
  91. int clientX, int clientY)
  92. /*-{
  93. var el = $wnd.document.elementFromPoint(clientX, clientY);
  94. // Call elementFromPoint two times to make sure IE8 also returns something sensible if the application is running in an iframe
  95. el = $wnd.document.elementFromPoint(clientX, clientY);
  96. if(el != null && el.nodeType == 3) {
  97. el = el.parentNode;
  98. }
  99. return el;
  100. }-*/;
  101. /**
  102. * This helper method can be called if components size have been changed
  103. * outside rendering phase. It notifies components parent about the size
  104. * change so it can react.
  105. *
  106. * When using this method, developer should consider if size changes could
  107. * be notified lazily. If lazy flag is true, method will save widget and
  108. * wait for a moment until it notifies parents in chunks. This may vastly
  109. * optimize layout in various situation. Example: if component have a lot of
  110. * images their onload events may fire "layout phase" many times in a short
  111. * period.
  112. *
  113. * @param widget
  114. * @param lazy
  115. * run componentSizeUpdated lazyly
  116. *
  117. * @deprecated As of 7.0, use
  118. * {@link LayoutManager#setNeedsMeasure(ComponentConnector)}
  119. * instead
  120. */
  121. @Deprecated
  122. public static void notifyParentOfSizeChange(Widget widget, boolean lazy) {
  123. ComponentConnector connector = findConnectorFor(widget);
  124. if (connector != null) {
  125. connector.getLayoutManager().setNeedsMeasure(connector);
  126. if (!lazy) {
  127. connector.getLayoutManager().layoutNow();
  128. }
  129. }
  130. }
  131. public static ComponentConnector findConnectorFor(Widget widget) {
  132. List<ApplicationConnection> runningApplications = ApplicationConfiguration
  133. .getRunningApplications();
  134. for (ApplicationConnection applicationConnection : runningApplications) {
  135. ConnectorMap connectorMap = applicationConnection.getConnectorMap();
  136. ComponentConnector connector = connectorMap.getConnector(widget);
  137. if (connector == null) {
  138. continue;
  139. }
  140. if (connector.getConnection() == applicationConnection) {
  141. return connector;
  142. }
  143. }
  144. return null;
  145. }
  146. public static float parseRelativeSize(String size) {
  147. if (size == null || !size.endsWith("%")) {
  148. return -1;
  149. }
  150. try {
  151. return Float.parseFloat(size.substring(0, size.length() - 1));
  152. } catch (Exception e) {
  153. VConsole.log("Unable to parse relative size");
  154. return -1;
  155. }
  156. }
  157. private static final Element escapeHtmlHelper = DOM.createDiv();
  158. /**
  159. * Converts html entities to text.
  160. *
  161. * @param html
  162. * @return escaped string presentation of given html
  163. */
  164. public static String escapeHTML(String html) {
  165. DOM.setInnerText(escapeHtmlHelper, html);
  166. String escapedText = DOM.getInnerHTML(escapeHtmlHelper);
  167. if (BrowserInfo.get().isIE8()) {
  168. // #7478 IE8 "incorrectly" returns "<br>" for newlines set using
  169. // setInnerText. The same for " " which is converted to "&nbsp;"
  170. escapedText = escapedText.replaceAll("<(BR|br)>", "\n");
  171. escapedText = escapedText.replaceAll("&nbsp;", " ");
  172. }
  173. return escapedText;
  174. }
  175. /**
  176. * Escapes the string so it is safe to write inside an HTML attribute.
  177. *
  178. * @param attribute
  179. * The string to escape
  180. * @return An escaped version of <literal>attribute</literal>.
  181. */
  182. public static String escapeAttribute(String attribute) {
  183. if (attribute == null) {
  184. return "";
  185. }
  186. attribute = attribute.replace("\"", "&quot;");
  187. attribute = attribute.replace("'", "&#39;");
  188. attribute = attribute.replace(">", "&gt;");
  189. attribute = attribute.replace("<", "&lt;");
  190. attribute = attribute.replace("&", "&amp;");
  191. return attribute;
  192. }
  193. /**
  194. * Clones given element as in JavaScript.
  195. *
  196. * Deprecate this if there appears similar method into GWT someday.
  197. *
  198. * @param element
  199. * @param deep
  200. * clone child tree also
  201. * @return
  202. */
  203. public static native com.google.gwt.user.client.Element cloneNode(
  204. Element element, boolean deep)
  205. /*-{
  206. return element.cloneNode(deep);
  207. }-*/;
  208. public static int measureHorizontalPaddingAndBorder(Element element,
  209. int paddingGuess) {
  210. String originalWidth = DOM.getStyleAttribute(element, "width");
  211. int originalOffsetWidth = element.getOffsetWidth();
  212. int widthGuess = (originalOffsetWidth - paddingGuess);
  213. if (widthGuess < 1) {
  214. widthGuess = 1;
  215. }
  216. element.getStyle().setWidth(widthGuess, Unit.PX);
  217. int padding = element.getOffsetWidth() - widthGuess;
  218. element.getStyle().setProperty("width", originalWidth);
  219. return padding;
  220. }
  221. public static int measureVerticalPaddingAndBorder(Element element,
  222. int paddingGuess) {
  223. String originalHeight = DOM.getStyleAttribute(element, "height");
  224. int originalOffsetHeight = element.getOffsetHeight();
  225. int widthGuess = (originalOffsetHeight - paddingGuess);
  226. if (widthGuess < 1) {
  227. widthGuess = 1;
  228. }
  229. element.getStyle().setHeight(widthGuess, Unit.PX);
  230. int padding = element.getOffsetHeight() - widthGuess;
  231. element.getStyle().setProperty("height", originalHeight);
  232. return padding;
  233. }
  234. public static int measureHorizontalBorder(Element element) {
  235. int borders;
  236. if (BrowserInfo.get().isIE()) {
  237. String width = element.getStyle().getProperty("width");
  238. String height = element.getStyle().getProperty("height");
  239. int offsetWidth = element.getOffsetWidth();
  240. int offsetHeight = element.getOffsetHeight();
  241. if (offsetHeight < 1) {
  242. offsetHeight = 1;
  243. }
  244. if (offsetWidth < 1) {
  245. offsetWidth = 10;
  246. }
  247. element.getStyle().setPropertyPx("height", offsetHeight);
  248. element.getStyle().setPropertyPx("width", offsetWidth);
  249. borders = element.getOffsetWidth() - element.getClientWidth();
  250. element.getStyle().setProperty("width", width);
  251. element.getStyle().setProperty("height", height);
  252. } else {
  253. borders = element.getOffsetWidth()
  254. - element.getPropertyInt("clientWidth");
  255. }
  256. assert borders >= 0;
  257. return borders;
  258. }
  259. public static int measureVerticalBorder(Element element) {
  260. int borders;
  261. if (BrowserInfo.get().isIE()) {
  262. String width = element.getStyle().getProperty("width");
  263. String height = element.getStyle().getProperty("height");
  264. int offsetWidth = element.getOffsetWidth();
  265. int offsetHeight = element.getOffsetHeight();
  266. if (offsetHeight < 1) {
  267. offsetHeight = 1;
  268. }
  269. if (offsetWidth < 1) {
  270. offsetWidth = 10;
  271. }
  272. element.getStyle().setPropertyPx("width", offsetWidth);
  273. element.getStyle().setPropertyPx("height", offsetHeight);
  274. borders = element.getOffsetHeight()
  275. - element.getPropertyInt("clientHeight");
  276. element.getStyle().setProperty("height", height);
  277. element.getStyle().setProperty("width", width);
  278. } else {
  279. borders = element.getOffsetHeight()
  280. - element.getPropertyInt("clientHeight");
  281. }
  282. assert borders >= 0;
  283. return borders;
  284. }
  285. public static int measureMarginLeft(Element element) {
  286. return element.getAbsoluteLeft()
  287. - element.getParentElement().getAbsoluteLeft();
  288. }
  289. public static int setHeightExcludingPaddingAndBorder(Widget widget,
  290. String height, int paddingBorderGuess) {
  291. if (height.equals("")) {
  292. setHeight(widget, "");
  293. return paddingBorderGuess;
  294. } else if (height.endsWith("px")) {
  295. int pixelHeight = Integer.parseInt(height.substring(0,
  296. height.length() - 2));
  297. return setHeightExcludingPaddingAndBorder(widget.getElement(),
  298. pixelHeight, paddingBorderGuess, false);
  299. } else {
  300. // Set the height in unknown units
  301. setHeight(widget, height);
  302. // Use the offsetWidth
  303. return setHeightExcludingPaddingAndBorder(widget.getElement(),
  304. widget.getOffsetHeight(), paddingBorderGuess, true);
  305. }
  306. }
  307. private static void setWidth(Widget widget, String width) {
  308. widget.getElement().getStyle().setProperty("width", width);
  309. }
  310. private static void setHeight(Widget widget, String height) {
  311. widget.getElement().getStyle().setProperty("height", height);
  312. }
  313. public static int setWidthExcludingPaddingAndBorder(Widget widget,
  314. String width, int paddingBorderGuess) {
  315. if (width.equals("")) {
  316. setWidth(widget, "");
  317. return paddingBorderGuess;
  318. } else if (width.endsWith("px")) {
  319. int pixelWidth = Integer.parseInt(width.substring(0,
  320. width.length() - 2));
  321. return setWidthExcludingPaddingAndBorder(widget.getElement(),
  322. pixelWidth, paddingBorderGuess, false);
  323. } else {
  324. setWidth(widget, width);
  325. return setWidthExcludingPaddingAndBorder(widget.getElement(),
  326. widget.getOffsetWidth(), paddingBorderGuess, true);
  327. }
  328. }
  329. public static int setWidthExcludingPaddingAndBorder(Element element,
  330. int requestedWidth, int horizontalPaddingBorderGuess,
  331. boolean requestedWidthIncludesPaddingBorder) {
  332. int widthGuess = requestedWidth - horizontalPaddingBorderGuess;
  333. if (widthGuess < 0) {
  334. widthGuess = 0;
  335. }
  336. element.getStyle().setWidth(widthGuess, Unit.PX);
  337. int captionOffsetWidth = DOM.getElementPropertyInt(element,
  338. "offsetWidth");
  339. int actualPadding = captionOffsetWidth - widthGuess;
  340. if (requestedWidthIncludesPaddingBorder) {
  341. actualPadding += actualPadding;
  342. }
  343. if (actualPadding != horizontalPaddingBorderGuess) {
  344. int w = requestedWidth - actualPadding;
  345. if (w < 0) {
  346. // Cannot set negative width even if we would want to
  347. w = 0;
  348. }
  349. element.getStyle().setWidth(w, Unit.PX);
  350. }
  351. return actualPadding;
  352. }
  353. public static int setHeightExcludingPaddingAndBorder(Element element,
  354. int requestedHeight, int verticalPaddingBorderGuess,
  355. boolean requestedHeightIncludesPaddingBorder) {
  356. int heightGuess = requestedHeight - verticalPaddingBorderGuess;
  357. if (heightGuess < 0) {
  358. heightGuess = 0;
  359. }
  360. element.getStyle().setHeight(heightGuess, Unit.PX);
  361. int captionOffsetHeight = DOM.getElementPropertyInt(element,
  362. "offsetHeight");
  363. int actualPadding = captionOffsetHeight - heightGuess;
  364. if (requestedHeightIncludesPaddingBorder) {
  365. actualPadding += actualPadding;
  366. }
  367. if (actualPadding != verticalPaddingBorderGuess) {
  368. int h = requestedHeight - actualPadding;
  369. if (h < 0) {
  370. // Cannot set negative height even if we would want to
  371. h = 0;
  372. }
  373. element.getStyle().setHeight(h, Unit.PX);
  374. }
  375. return actualPadding;
  376. }
  377. public static String getSimpleName(Object widget) {
  378. if (widget == null) {
  379. return "(null)";
  380. }
  381. String name = widget.getClass().getName();
  382. return name.substring(name.lastIndexOf('.') + 1);
  383. }
  384. public static void setFloat(Element element, String value) {
  385. if (BrowserInfo.get().isIE()) {
  386. element.getStyle().setProperty("styleFloat", value);
  387. } else {
  388. element.getStyle().setProperty("cssFloat", value);
  389. }
  390. }
  391. private static int detectedScrollbarSize = -1;
  392. public static int getNativeScrollbarSize() {
  393. if (detectedScrollbarSize < 0) {
  394. Element scroller = DOM.createDiv();
  395. scroller.getStyle().setProperty("width", "50px");
  396. scroller.getStyle().setProperty("height", "50px");
  397. scroller.getStyle().setProperty("overflow", "scroll");
  398. scroller.getStyle().setProperty("position", "absolute");
  399. scroller.getStyle().setProperty("marginLeft", "-5000px");
  400. RootPanel.getBodyElement().appendChild(scroller);
  401. detectedScrollbarSize = scroller.getOffsetWidth()
  402. - scroller.getPropertyInt("clientWidth");
  403. RootPanel.getBodyElement().removeChild(scroller);
  404. }
  405. return detectedScrollbarSize;
  406. }
  407. /**
  408. * Defers the execution of {@link #runWebkitOverflowAutoFix(Element)}
  409. *
  410. * @since
  411. * @param elem
  412. * with overflow auto
  413. */
  414. public static void runWebkitOverflowAutoFixDeferred(final Element elem) {
  415. Scheduler.get().scheduleDeferred(new Command() {
  416. @Override
  417. public void execute() {
  418. Util.runWebkitOverflowAutoFix(elem);
  419. }
  420. });
  421. }
  422. /**
  423. * Run workaround for webkits overflow auto issue.
  424. *
  425. * See: our bug #2138 and https://bugs.webkit.org/show_bug.cgi?id=21462
  426. *
  427. * @param elem
  428. * with overflow auto
  429. */
  430. public static void runWebkitOverflowAutoFix(final Element elem) {
  431. // Add max version if fix lands sometime to Webkit
  432. // Starting from Opera 11.00, also a problem in Opera
  433. if (BrowserInfo.get().requiresOverflowAutoFix()) {
  434. final String originalOverflow = elem.getStyle().getProperty(
  435. "overflow");
  436. if ("hidden".equals(originalOverflow)) {
  437. return;
  438. }
  439. // check the scrolltop value before hiding the element
  440. final int scrolltop = elem.getScrollTop();
  441. final int scrollleft = elem.getScrollLeft();
  442. elem.getStyle().setProperty("overflow", "hidden");
  443. Scheduler.get().scheduleDeferred(new Command() {
  444. @Override
  445. public void execute() {
  446. // Dough, Safari scroll auto means actually just a moped
  447. elem.getStyle().setProperty("overflow", originalOverflow);
  448. if (scrolltop > 0 || elem.getScrollTop() > 0) {
  449. int scrollvalue = scrolltop;
  450. if (scrollvalue == 0) {
  451. // mysterious are the ways of webkits scrollbar
  452. // handling. In some cases webkit reports bad (0)
  453. // scrolltop before hiding the element temporary,
  454. // sometimes after.
  455. scrollvalue = elem.getScrollTop();
  456. }
  457. // fix another bug where scrollbar remains in wrong
  458. // position
  459. elem.setScrollTop(scrollvalue - 1);
  460. elem.setScrollTop(scrollvalue);
  461. }
  462. // fix for #6940 : Table horizontal scroll sometimes not
  463. // updated when collapsing/expanding columns
  464. // Also appeared in Safari 5.1 with webkit 534 (#7667)
  465. if ((BrowserInfo.get().isChrome() || (BrowserInfo.get()
  466. .isSafari() && BrowserInfo.get().getWebkitVersion() >= 534))
  467. && (scrollleft > 0 || elem.getScrollLeft() > 0)) {
  468. int scrollvalue = scrollleft;
  469. if (scrollvalue == 0) {
  470. // mysterious are the ways of webkits scrollbar
  471. // handling. In some cases webkit may report a bad
  472. // (0) scrollleft before hiding the element
  473. // temporary, sometimes after.
  474. scrollvalue = elem.getScrollLeft();
  475. }
  476. // fix another bug where scrollbar remains in wrong
  477. // position
  478. elem.setScrollLeft(scrollvalue - 1);
  479. elem.setScrollLeft(scrollvalue);
  480. }
  481. }
  482. });
  483. }
  484. }
  485. /**
  486. * Parses shared state and fetches the relative size of the component. If a
  487. * dimension is not specified as relative it will return -1. If the shared
  488. * state does not contain width or height specifications this will return
  489. * null.
  490. *
  491. * @param state
  492. * @return
  493. */
  494. public static FloatSize parseRelativeSize(AbstractComponentState state) {
  495. if (ComponentStateUtil.isUndefinedHeight(state)
  496. && ComponentStateUtil.isUndefinedWidth(state)) {
  497. return null;
  498. }
  499. float relativeWidth = Util.parseRelativeSize(state.width);
  500. float relativeHeight = Util.parseRelativeSize(state.height);
  501. FloatSize relativeSize = new FloatSize(relativeWidth, relativeHeight);
  502. return relativeSize;
  503. }
  504. @Deprecated
  505. public static boolean isCached(UIDL uidl) {
  506. return uidl.getBooleanAttribute("cached");
  507. }
  508. public static void alert(String string) {
  509. if (true) {
  510. Window.alert(string);
  511. }
  512. }
  513. /**
  514. * Checks if a and b are equals using {@link #equals(Object)}. Handles null
  515. * values as well. Does not ensure that objects are of the same type.
  516. * Assumes that the first object's equals method handle equals properly.
  517. *
  518. * @param a
  519. * The first value to compare
  520. * @param b
  521. * The second value to compare
  522. * @return
  523. * @deprecated As of 7.1 use {@link SharedUtil#equals(Object)} instead
  524. */
  525. @Deprecated
  526. public static boolean equals(Object a, Object b) {
  527. return SharedUtil.equals(a, b);
  528. }
  529. public static void updateRelativeChildrenAndSendSizeUpdateEvent(
  530. ApplicationConnection client, HasWidgets container, Widget widget) {
  531. notifyParentOfSizeChange(widget, false);
  532. }
  533. /**
  534. * Gets the border-box width for the given element, i.e. element width +
  535. * border + padding. Always rounds up to nearest integer.
  536. *
  537. * @param element
  538. * The element to check
  539. * @return The border-box width for the element
  540. */
  541. public static int getRequiredWidth(com.google.gwt.dom.client.Element element) {
  542. int reqWidth = getRequiredWidthBoundingClientRect(element);
  543. if (BrowserInfo.get().isIE() && !BrowserInfo.get().isIE8()) {
  544. int csSize = getRequiredWidthComputedStyle(element);
  545. if (csSize == reqWidth + 1) {
  546. // If computed style reports one pixel larger than requiredWidth
  547. // we would be rounding in the wrong direction in IE9. Round up
  548. // instead.
  549. // We do not always use csSize as it e.g. for 100% wide Labels
  550. // in GridLayouts produces senseless values (see e.g.
  551. // ThemeTestUI with Runo).
  552. return csSize;
  553. }
  554. }
  555. return reqWidth;
  556. }
  557. /**
  558. * Gets the border-box height for the given element, i.e. element height +
  559. * border + padding. Always rounds up to nearest integer.
  560. *
  561. * @param element
  562. * The element to check
  563. * @return The border-box height for the element
  564. */
  565. public static int getRequiredHeight(
  566. com.google.gwt.dom.client.Element element) {
  567. int reqHeight = getRequiredHeightBoundingClientRect(element);
  568. if (BrowserInfo.get().isIE() && !BrowserInfo.get().isIE8()) {
  569. int csSize = getRequiredHeightComputedStyle(element);
  570. if (csSize == reqHeight + 1) {
  571. // If computed style reports one pixel larger than
  572. // requiredHeight we would be rounding in the wrong direction in
  573. // IE9. Round up instead.
  574. // We do not always use csSize as it e.g. for 100% wide Labels
  575. // in GridLayouts produces senseless values (see e.g.
  576. // ThemeTestUI with Runo).
  577. return csSize;
  578. }
  579. }
  580. return reqHeight;
  581. }
  582. public static native int getRequiredWidthBoundingClientRect(
  583. com.google.gwt.dom.client.Element element)
  584. /*-{
  585. if (element.getBoundingClientRect) {
  586. var rect = element.getBoundingClientRect();
  587. return Math.ceil(rect.right - rect.left);
  588. } else {
  589. return element.offsetWidth;
  590. }
  591. }-*/;
  592. public static native int getRequiredHeightComputedStyle(
  593. com.google.gwt.dom.client.Element element)
  594. /*-{
  595. var cs = element.ownerDocument.defaultView.getComputedStyle(element);
  596. var heightPx = cs.height;
  597. if(heightPx == 'auto'){
  598. // Fallback for when IE reports auto
  599. heightPx = @com.vaadin.client.Util::getRequiredHeightBoundingClientRect(Lcom/google/gwt/dom/client/Element;)(element) + 'px';
  600. }
  601. var borderTopPx = cs.borderTop;
  602. var borderBottomPx = cs.borderBottom;
  603. var paddingTopPx = cs.paddingTop;
  604. var paddingBottomPx = cs.paddingBottom;
  605. var height = heightPx.substring(0,heightPx.length-2);
  606. var border = borderTopPx.substring(0,borderTopPx.length-2)+borderBottomPx.substring(0,borderBottomPx.length-2);
  607. var padding = paddingTopPx.substring(0,paddingTopPx.length-2)+paddingBottomPx.substring(0,paddingBottomPx.length-2);
  608. return Math.ceil(height+border+padding);
  609. }-*/;
  610. public static native int getRequiredWidthComputedStyle(
  611. com.google.gwt.dom.client.Element element)
  612. /*-{
  613. var cs = element.ownerDocument.defaultView.getComputedStyle(element);
  614. var widthPx = cs.width;
  615. if(widthPx == 'auto'){
  616. // Fallback for when IE reports auto
  617. widthPx = @com.vaadin.client.Util::getRequiredWidthBoundingClientRect(Lcom/google/gwt/dom/client/Element;)(element) + 'px';
  618. }
  619. var borderLeftPx = cs.borderLeft;
  620. var borderRightPx = cs.borderRight;
  621. var paddingLeftPx = cs.paddingLeft;
  622. var paddingRightPx = cs.paddingRight;
  623. var width = widthPx.substring(0,widthPx.length-2);
  624. var border = borderLeftPx.substring(0,borderLeftPx.length-2)+borderRightPx.substring(0,borderRightPx.length-2);
  625. var padding = paddingLeftPx.substring(0,paddingLeftPx.length-2)+paddingRightPx.substring(0,paddingRightPx.length-2);
  626. return Math.ceil(width+border+padding);
  627. }-*/;
  628. public static native int getRequiredHeightBoundingClientRect(
  629. com.google.gwt.dom.client.Element element)
  630. /*-{
  631. var height;
  632. if (element.getBoundingClientRect != null) {
  633. var rect = element.getBoundingClientRect();
  634. height = Math.ceil(rect.bottom - rect.top);
  635. } else {
  636. height = element.offsetHeight;
  637. }
  638. return height;
  639. }-*/;
  640. public static int getRequiredWidth(Widget widget) {
  641. return getRequiredWidth(widget.getElement());
  642. }
  643. public static int getRequiredHeight(Widget widget) {
  644. return getRequiredHeight(widget.getElement());
  645. }
  646. /**
  647. * Detects what is currently the overflow style attribute in given element.
  648. *
  649. * @param pe
  650. * the element to detect
  651. * @return true if auto or scroll
  652. */
  653. public static boolean mayHaveScrollBars(com.google.gwt.dom.client.Element pe) {
  654. String overflow = getComputedStyle(pe, "overflow");
  655. if (overflow != null) {
  656. if (overflow.equals("auto") || overflow.equals("scroll")) {
  657. return true;
  658. } else {
  659. return false;
  660. }
  661. } else {
  662. return false;
  663. }
  664. }
  665. /**
  666. * A simple helper method to detect "computed style" (aka style sheets +
  667. * element styles). Values returned differ a lot depending on browsers.
  668. * Always be very careful when using this.
  669. *
  670. * @param el
  671. * the element from which the style property is detected
  672. * @param p
  673. * the property to detect
  674. * @return String value of style property
  675. */
  676. private static native String getComputedStyle(
  677. com.google.gwt.dom.client.Element el, String p)
  678. /*-{
  679. try {
  680. if (el.currentStyle) {
  681. // IE
  682. return el.currentStyle[p];
  683. } else if (window.getComputedStyle) {
  684. // Sa, FF, Opera
  685. var view = el.ownerDocument.defaultView;
  686. return view.getComputedStyle(el,null).getPropertyValue(p);
  687. } else {
  688. // fall back for non IE, Sa, FF, Opera
  689. return "";
  690. }
  691. } catch (e) {
  692. return "";
  693. }
  694. }-*/;
  695. /**
  696. * Locates the nested child component of <literal>parent</literal> which
  697. * contains the element <literal>element</literal>. The child component is
  698. * also returned if "element" is part of its caption. If
  699. * <literal>element</literal> is not part of any child component, null is
  700. * returned.
  701. *
  702. * This method returns the deepest nested VPaintableWidget.
  703. *
  704. * @param client
  705. * A reference to ApplicationConnection
  706. * @param parent
  707. * The widget that contains <literal>element</literal>.
  708. * @param element
  709. * An element that is a sub element of the parent
  710. * @return The VPaintableWidget which the element is a part of. Null if the
  711. * element does not belong to a child.
  712. */
  713. public static ComponentConnector getConnectorForElement(
  714. ApplicationConnection client, Widget parent, Element element) {
  715. Element browseElement = element;
  716. Element rootElement = parent.getElement();
  717. while (browseElement != null && browseElement != rootElement) {
  718. ComponentConnector connector = ConnectorMap.get(client)
  719. .getConnector(browseElement);
  720. if (connector == null) {
  721. String ownerPid = VCaption.getCaptionOwnerPid(browseElement);
  722. if (ownerPid != null) {
  723. connector = (ComponentConnector) ConnectorMap.get(client)
  724. .getConnector(ownerPid);
  725. }
  726. }
  727. if (connector != null) {
  728. // check that inside the rootElement
  729. while (browseElement != null && browseElement != rootElement) {
  730. browseElement = browseElement.getParentElement();
  731. }
  732. if (browseElement != rootElement) {
  733. return null;
  734. } else {
  735. return connector;
  736. }
  737. }
  738. browseElement = browseElement.getParentElement();
  739. }
  740. // No connector found, element is possibly inside a VOverlay
  741. // If the overlay has an owner, try to find the owner's connector
  742. VOverlay overlay = findWidget(element, VOverlay.class);
  743. if (overlay != null && overlay.getOwner() != null) {
  744. return getConnectorForElement(client, client.getUIConnector()
  745. .getWidget(), overlay.getOwner().getElement());
  746. } else {
  747. return null;
  748. }
  749. }
  750. /**
  751. * Will (attempt) to focus the given DOM Element.
  752. *
  753. * @param el
  754. * the element to focus
  755. */
  756. public static native void focus(Element el)
  757. /*-{
  758. try {
  759. el.focus();
  760. } catch (e) {
  761. }
  762. }-*/;
  763. /**
  764. * Helper method to find the nearest parent paintable instance by traversing
  765. * the DOM upwards from given element.
  766. *
  767. * @param element
  768. * the element to start from
  769. */
  770. public static ComponentConnector findPaintable(
  771. ApplicationConnection client, Element element) {
  772. Widget widget = Util.findWidget(element, null);
  773. ConnectorMap vPaintableMap = ConnectorMap.get(client);
  774. while (widget != null && !vPaintableMap.isConnector(widget)) {
  775. widget = widget.getParent();
  776. }
  777. return vPaintableMap.getConnector(widget);
  778. }
  779. /**
  780. * Helper method to find first instance of given Widget type found by
  781. * traversing DOM upwards from given element.
  782. *
  783. * @param element
  784. * the element where to start seeking of Widget
  785. * @param class1
  786. * the Widget type to seek for
  787. */
  788. @SuppressWarnings("unchecked")
  789. public static <T> T findWidget(Element element,
  790. Class<? extends Widget> class1) {
  791. if (element != null) {
  792. /* First seek for the first EventListener (~Widget) from dom */
  793. EventListener eventListener = null;
  794. while (eventListener == null && element != null) {
  795. eventListener = Event.getEventListener(element);
  796. if (eventListener == null) {
  797. element = element.getParentElement();
  798. }
  799. }
  800. if (eventListener instanceof Widget) {
  801. /*
  802. * Then find the first widget of type class1 from widget
  803. * hierarchy
  804. */
  805. Widget w = (Widget) eventListener;
  806. while (w != null) {
  807. if (class1 == null || w.getClass() == class1) {
  808. return (T) w;
  809. }
  810. w = w.getParent();
  811. }
  812. }
  813. }
  814. return null;
  815. }
  816. /**
  817. * Force webkit to redraw an element
  818. *
  819. * @param element
  820. * The element that should be redrawn
  821. */
  822. public static void forceWebkitRedraw(Element element) {
  823. Style style = element.getStyle();
  824. String s = style.getProperty("webkitTransform");
  825. if (s == null || s.length() == 0) {
  826. style.setProperty("webkitTransform", "scale(1)");
  827. } else {
  828. style.setProperty("webkitTransform", "");
  829. }
  830. }
  831. /**
  832. * Performs a hack to trigger a re-layout in the IE8. This is usually
  833. * necessary in cases where IE8 "forgets" to update child elements when they
  834. * resize.
  835. *
  836. * @param e
  837. * The element to perform the hack on
  838. */
  839. public static final void forceIE8Redraw(Element e) {
  840. if (BrowserInfo.get().isIE8()) {
  841. forceIERedraw(e);
  842. }
  843. }
  844. /**
  845. * Performs a hack to trigger a re-layout in the IE browser. This is usually
  846. * necessary in cases where IE "forgets" to update child elements when they
  847. * resize.
  848. *
  849. * @since
  850. * @param e
  851. * The element to perform the hack on
  852. */
  853. public static void forceIERedraw(Element e) {
  854. if (BrowserInfo.get().isIE()) {
  855. setStyleTemporarily(e, "zoom", "1");
  856. }
  857. }
  858. /**
  859. * Detaches and re-attaches the element from its parent. The element is
  860. * reattached at the same position in the DOM as it was before.
  861. *
  862. * Does nothing if the element is not attached to the DOM.
  863. *
  864. * @param element
  865. * The element to detach and re-attach
  866. */
  867. public static void detachAttach(Element element) {
  868. if (element == null) {
  869. return;
  870. }
  871. Node nextSibling = element.getNextSibling();
  872. Node parent = element.getParentNode();
  873. if (parent == null) {
  874. return;
  875. }
  876. parent.removeChild(element);
  877. if (nextSibling == null) {
  878. parent.appendChild(element);
  879. } else {
  880. parent.insertBefore(element, nextSibling);
  881. }
  882. }
  883. public static void sinkOnloadForImages(Element element) {
  884. NodeList<com.google.gwt.dom.client.Element> imgElements = element
  885. .getElementsByTagName("img");
  886. for (int i = 0; i < imgElements.getLength(); i++) {
  887. DOM.sinkEvents(imgElements.getItem(i), Event.ONLOAD);
  888. }
  889. }
  890. /**
  891. * Returns the index of the childElement within its parent.
  892. *
  893. * @param subElement
  894. * @return
  895. */
  896. public static int getChildElementIndex(Element childElement) {
  897. int idx = 0;
  898. Node n = childElement;
  899. while ((n = n.getPreviousSibling()) != null) {
  900. idx++;
  901. }
  902. return idx;
  903. }
  904. private static void printConnectorInvocations(
  905. ArrayList<MethodInvocation> invocations, String id,
  906. ApplicationConnection c) {
  907. ServerConnector connector = ConnectorMap.get(c).getConnector(id);
  908. if (connector != null) {
  909. VConsole.log("\t" + id + " (" + connector.getClass() + ") :");
  910. } else {
  911. VConsole.log("\t" + id
  912. + ": Warning: no corresponding connector for id " + id);
  913. }
  914. for (MethodInvocation invocation : invocations) {
  915. Object[] parameters = invocation.getParameters();
  916. String formattedParams = null;
  917. if (ApplicationConstants.UPDATE_VARIABLE_METHOD.equals(invocation
  918. .getMethodName()) && parameters.length == 2) {
  919. // name, value
  920. Object value = parameters[1];
  921. // TODO paintables inside lists/maps get rendered as
  922. // components in the debug console
  923. String formattedValue = value instanceof ServerConnector ? ((ServerConnector) value)
  924. .getConnectorId() : String.valueOf(value);
  925. formattedParams = parameters[0] + " : " + formattedValue;
  926. }
  927. if (null == formattedParams) {
  928. formattedParams = (null != parameters) ? Arrays
  929. .toString(parameters) : null;
  930. }
  931. VConsole.log("\t\t" + invocation.getInterfaceName() + "."
  932. + invocation.getMethodName() + "(" + formattedParams + ")");
  933. }
  934. }
  935. static void logVariableBurst(ApplicationConnection c,
  936. Collection<MethodInvocation> loggedBurst) {
  937. try {
  938. VConsole.log("Variable burst to be sent to server:");
  939. String curId = null;
  940. ArrayList<MethodInvocation> invocations = new ArrayList<MethodInvocation>();
  941. for (MethodInvocation methodInvocation : loggedBurst) {
  942. String id = methodInvocation.getConnectorId();
  943. if (curId == null) {
  944. curId = id;
  945. } else if (!curId.equals(id)) {
  946. printConnectorInvocations(invocations, curId, c);
  947. invocations.clear();
  948. curId = id;
  949. }
  950. invocations.add(methodInvocation);
  951. }
  952. if (!invocations.isEmpty()) {
  953. printConnectorInvocations(invocations, curId, c);
  954. }
  955. } catch (Exception e) {
  956. VConsole.error(e);
  957. }
  958. }
  959. /**
  960. * Temporarily sets the {@code styleProperty} to {@code tempValue} and then
  961. * resets it to its current value. Used mainly to work around rendering
  962. * issues in IE (and possibly in other browsers)
  963. *
  964. * @param element
  965. * The target element
  966. * @param styleProperty
  967. * The name of the property to set
  968. * @param tempValue
  969. * The temporary value
  970. */
  971. public static void setStyleTemporarily(Element element,
  972. final String styleProperty, String tempValue) {
  973. final Style style = element.getStyle();
  974. final String currentValue = style.getProperty(styleProperty);
  975. style.setProperty(styleProperty, tempValue);
  976. element.getOffsetWidth();
  977. style.setProperty(styleProperty, currentValue);
  978. }
  979. /**
  980. * A helper method to return the client position from an event. Returns
  981. * position from either first changed touch (if touch event) or from the
  982. * event itself.
  983. *
  984. * @param event
  985. * @return
  986. */
  987. public static int getTouchOrMouseClientX(Event event) {
  988. if (isTouchEvent(event)) {
  989. return event.getChangedTouches().get(0).getClientX();
  990. } else {
  991. return event.getClientX();
  992. }
  993. }
  994. /**
  995. * Find the element corresponding to the coordinates in the passed mouse
  996. * event. Please note that this is not always the same as the target of the
  997. * event e.g. if event capture is used.
  998. *
  999. * @param event
  1000. * the mouse event to get coordinates from
  1001. * @return the element at the coordinates of the event
  1002. */
  1003. public static com.google.gwt.user.client.Element getElementUnderMouse(
  1004. NativeEvent event) {
  1005. int pageX = getTouchOrMouseClientX(event);
  1006. int pageY = getTouchOrMouseClientY(event);
  1007. return getElementFromPoint(pageX, pageY);
  1008. }
  1009. /**
  1010. * A helper method to return the client position from an event. Returns
  1011. * position from either first changed touch (if touch event) or from the
  1012. * event itself.
  1013. *
  1014. * @param event
  1015. * @return
  1016. */
  1017. public static int getTouchOrMouseClientY(Event event) {
  1018. if (isTouchEvent(event)) {
  1019. return event.getChangedTouches().get(0).getClientY();
  1020. } else {
  1021. return event.getClientY();
  1022. }
  1023. }
  1024. /**
  1025. *
  1026. * @see #getTouchOrMouseClientY(Event)
  1027. * @param currentGwtEvent
  1028. * @return
  1029. */
  1030. public static int getTouchOrMouseClientY(NativeEvent currentGwtEvent) {
  1031. return getTouchOrMouseClientY(Event.as(currentGwtEvent));
  1032. }
  1033. /**
  1034. * @see #getTouchOrMouseClientX(Event)
  1035. *
  1036. * @param event
  1037. * @return
  1038. */
  1039. public static int getTouchOrMouseClientX(NativeEvent event) {
  1040. return getTouchOrMouseClientX(Event.as(event));
  1041. }
  1042. public static boolean isTouchEvent(Event event) {
  1043. return event.getType().contains("touch");
  1044. }
  1045. public static boolean isTouchEvent(NativeEvent event) {
  1046. return isTouchEvent(Event.as(event));
  1047. }
  1048. public static void simulateClickFromTouchEvent(Event touchevent,
  1049. Widget widget) {
  1050. Touch touch = touchevent.getChangedTouches().get(0);
  1051. final NativeEvent createMouseUpEvent = Document.get()
  1052. .createMouseUpEvent(0, touch.getScreenX(), touch.getScreenY(),
  1053. touch.getClientX(), touch.getClientY(), false, false,
  1054. false, false, NativeEvent.BUTTON_LEFT);
  1055. final NativeEvent createMouseDownEvent = Document.get()
  1056. .createMouseDownEvent(0, touch.getScreenX(),
  1057. touch.getScreenY(), touch.getClientX(),
  1058. touch.getClientY(), false, false, false, false,
  1059. NativeEvent.BUTTON_LEFT);
  1060. final NativeEvent createMouseClickEvent = Document.get()
  1061. .createClickEvent(0, touch.getScreenX(), touch.getScreenY(),
  1062. touch.getClientX(), touch.getClientY(), false, false,
  1063. false, false);
  1064. /*
  1065. * Get target with element from point as we want the actual element, not
  1066. * the one that sunk the event.
  1067. */
  1068. final Element target = getElementFromPoint(touch.getClientX(),
  1069. touch.getClientY());
  1070. /*
  1071. * Fixes infocusable form fields in Safari of iOS 5.x and some Android
  1072. * browsers.
  1073. */
  1074. Widget targetWidget = findWidget(target, null);
  1075. if (targetWidget instanceof com.google.gwt.user.client.ui.Focusable) {
  1076. final com.google.gwt.user.client.ui.Focusable toBeFocusedWidget = (com.google.gwt.user.client.ui.Focusable) targetWidget;
  1077. toBeFocusedWidget.setFocus(true);
  1078. } else if (targetWidget instanceof Focusable) {
  1079. ((Focusable) targetWidget).focus();
  1080. }
  1081. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  1082. @Override
  1083. public void execute() {
  1084. try {
  1085. target.dispatchEvent(createMouseDownEvent);
  1086. target.dispatchEvent(createMouseUpEvent);
  1087. target.dispatchEvent(createMouseClickEvent);
  1088. } catch (Exception e) {
  1089. }
  1090. }
  1091. });
  1092. }
  1093. /**
  1094. * Gets the currently focused element.
  1095. *
  1096. * @return The active element or null if no active element could be found.
  1097. */
  1098. public native static com.google.gwt.user.client.Element getFocusedElement()
  1099. /*-{
  1100. if ($wnd.document.activeElement) {
  1101. return $wnd.document.activeElement;
  1102. }
  1103. return null;
  1104. }-*/;
  1105. /**
  1106. * Gets the currently focused element for Internet Explorer.
  1107. *
  1108. * @return The currently focused element
  1109. * @deprecated Use #getFocusedElement instead
  1110. */
  1111. @Deprecated
  1112. public static com.google.gwt.user.client.Element getIEFocusedElement() {
  1113. return getFocusedElement();
  1114. }
  1115. /**
  1116. * Kind of stronger version of isAttached(). In addition to std isAttached,
  1117. * this method checks that this widget nor any of its parents is hidden. Can
  1118. * be e.g used to check whether component should react to some events or
  1119. * not.
  1120. *
  1121. * @param widget
  1122. * @return true if attached and displayed
  1123. */
  1124. public static boolean isAttachedAndDisplayed(Widget widget) {
  1125. if (widget.isAttached()) {
  1126. /*
  1127. * Failfast using offset size, then by iterating the widget tree
  1128. */
  1129. boolean notZeroSized = widget.getOffsetHeight() > 0
  1130. || widget.getOffsetWidth() > 0;
  1131. return notZeroSized || checkVisibilityRecursively(widget);
  1132. } else {
  1133. return false;
  1134. }
  1135. }
  1136. private static boolean checkVisibilityRecursively(Widget widget) {
  1137. if (widget.isVisible()) {
  1138. Widget parent = widget.getParent();
  1139. if (parent == null) {
  1140. return true; // root panel
  1141. } else {
  1142. return checkVisibilityRecursively(parent);
  1143. }
  1144. } else {
  1145. return false;
  1146. }
  1147. }
  1148. /**
  1149. * Scrolls an element into view vertically only. Modified version of
  1150. * Element.scrollIntoView.
  1151. *
  1152. * @param elem
  1153. * The element to scroll into view
  1154. */
  1155. public static native void scrollIntoViewVertically(Element elem)
  1156. /*-{
  1157. var top = elem.offsetTop;
  1158. var height = elem.offsetHeight;
  1159. if (elem.parentNode != elem.offsetParent) {
  1160. top -= elem.parentNode.offsetTop;
  1161. }
  1162. var cur = elem.parentNode;
  1163. while (cur && (cur.nodeType == 1)) {
  1164. if (top < cur.scrollTop) {
  1165. cur.scrollTop = top;
  1166. }
  1167. if (top + height > cur.scrollTop + cur.clientHeight) {
  1168. cur.scrollTop = (top + height) - cur.clientHeight;
  1169. }
  1170. var offsetTop = cur.offsetTop;
  1171. if (cur.parentNode != cur.offsetParent) {
  1172. offsetTop -= cur.parentNode.offsetTop;
  1173. }
  1174. top += offsetTop - cur.scrollTop;
  1175. cur = cur.parentNode;
  1176. }
  1177. }-*/;
  1178. /**
  1179. * Checks if the given event is either a touch event or caused by the left
  1180. * mouse button
  1181. *
  1182. * @param event
  1183. * @return true if the event is a touch event or caused by the left mouse
  1184. * button, false otherwise
  1185. */
  1186. public static boolean isTouchEventOrLeftMouseButton(Event event) {
  1187. boolean touchEvent = Util.isTouchEvent(event);
  1188. return touchEvent || event.getButton() == Event.BUTTON_LEFT;
  1189. }
  1190. /**
  1191. * Performs a shallow comparison of the collections.
  1192. *
  1193. * @param collection1
  1194. * The first collection
  1195. * @param collection2
  1196. * The second collection
  1197. * @return true if the collections contain the same elements in the same
  1198. * order, false otherwise
  1199. */
  1200. public static boolean collectionsEquals(Collection collection1,
  1201. Collection collection2) {
  1202. if (collection1 == null) {
  1203. return collection2 == null;
  1204. }
  1205. if (collection2 == null) {
  1206. return false;
  1207. }
  1208. Iterator<Object> collection1Iterator = collection1.iterator();
  1209. Iterator<Object> collection2Iterator = collection2.iterator();
  1210. while (collection1Iterator.hasNext()) {
  1211. if (!collection2Iterator.hasNext()) {
  1212. return false;
  1213. }
  1214. Object collection1Object = collection1Iterator.next();
  1215. Object collection2Object = collection2Iterator.next();
  1216. if (collection1Object != collection2Object) {
  1217. return false;
  1218. }
  1219. }
  1220. if (collection2Iterator.hasNext()) {
  1221. return false;
  1222. }
  1223. return true;
  1224. }
  1225. public static String getConnectorString(ServerConnector p) {
  1226. if (p == null) {
  1227. return "null";
  1228. }
  1229. return getSimpleName(p) + " (" + p.getConnectorId() + ")";
  1230. }
  1231. /**
  1232. * Resolve a relative URL to an absolute URL based on the current document's
  1233. * location.
  1234. *
  1235. * @param url
  1236. * a string with the relative URL to resolve
  1237. * @return the corresponding absolute URL as a string
  1238. */
  1239. public static String getAbsoluteUrl(String url) {
  1240. if (BrowserInfo.get().isIE8()) {
  1241. // The hard way - must use innerHTML and attach to DOM in IE8
  1242. DivElement divElement = Document.get().createDivElement();
  1243. divElement.getStyle().setDisplay(Display.NONE);
  1244. RootPanel.getBodyElement().appendChild(divElement);
  1245. divElement.setInnerHTML("<a href='" + escapeAttribute(url)
  1246. + "' ></a>");
  1247. AnchorElement a = divElement.getChild(0).cast();
  1248. String href = a.getHref();
  1249. RootPanel.getBodyElement().removeChild(divElement);
  1250. return href;
  1251. } else {
  1252. AnchorElement a = Document.get().createAnchorElement();
  1253. a.setHref(url);
  1254. return a.getHref();
  1255. }
  1256. }
  1257. }