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

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