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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client;
  5. import java.util.HashSet;
  6. import java.util.Iterator;
  7. import java.util.Set;
  8. import com.google.gwt.user.client.DOM;
  9. import com.google.gwt.user.client.Element;
  10. import com.google.gwt.user.client.ui.HasWidgets;
  11. import com.google.gwt.user.client.ui.Widget;
  12. public class Util {
  13. /**
  14. * Helper method for debugging purposes.
  15. *
  16. * Stops execution on firefox browsers on a breakpoint.
  17. *
  18. */
  19. public static native void browserDebugger()
  20. /*-{
  21. if($wnd.console)
  22. debugger;
  23. }-*/;
  24. /**
  25. * Nulls oncontextmenu function on given element. We need to manually clear
  26. * context menu events due bad browsers memory leaks, since GWT don't
  27. * support them.
  28. *
  29. * @param el
  30. */
  31. public native static void removeContextMenuEvent(Element el)
  32. /*-{
  33. el.oncontextmenu = null;
  34. }-*/;
  35. /**
  36. * Called when the size of one or more widgets have changed during
  37. * rendering. Finds parent container and notifies them of the size change.
  38. *
  39. * @param widgets
  40. */
  41. public static void componentSizeUpdated(Set<Widget> widgets) {
  42. if (widgets.isEmpty()) {
  43. return;
  44. }
  45. Set<Container> parents = new HashSet<Container>();
  46. for (Widget widget : widgets) {
  47. /* ApplicationConnection.getConsole().log(
  48. "Size changed for widget: "
  49. + widget.toString().split(">")[0]);
  50. */
  51. Widget parent = widget.getParent();
  52. while (parent != null && !(parent instanceof Container)) {
  53. parent = parent.getParent();
  54. }
  55. if (parent != null) {
  56. parents.add((Container) parent);
  57. }
  58. }
  59. Set<Widget> parentChanges = new HashSet<Widget>();
  60. for (Container parent : parents) {
  61. if (!parent.childComponentSizesUpdated()) {
  62. parentChanges.add((Widget) parent);
  63. }
  64. }
  65. componentSizeUpdated(parentChanges);
  66. }
  67. /**
  68. * Traverses recursively ancestors until ContainerResizedListener child
  69. * widget is found. They will delegate it futher if needed.
  70. *
  71. * @param container
  72. */
  73. public static void runDescendentsLayout(HasWidgets container) {
  74. final Iterator childWidgets = container.iterator();
  75. while (childWidgets.hasNext()) {
  76. final Widget child = (Widget) childWidgets.next();
  77. if (child instanceof ContainerResizedListener) {
  78. int w = -1, h = -1;
  79. if (container instanceof WidgetSpaceAllocator) {
  80. w = ((WidgetSpaceAllocator) container)
  81. .getAllocatedWidth(child);
  82. h = ((WidgetSpaceAllocator) container)
  83. .getAllocatedHeight(child);
  84. }
  85. ((ContainerResizedListener) child).iLayout(w, h);
  86. } else if (child instanceof HasWidgets) {
  87. final HasWidgets childContainer = (HasWidgets) child;
  88. runDescendentsLayout(childContainer);
  89. }
  90. }
  91. }
  92. public interface WidgetSpaceAllocator {
  93. int getAllocatedWidth(Widget child);
  94. int getAllocatedHeight(Widget child);
  95. }
  96. /**
  97. * Returns closest parent Widget in hierarchy that implements Container
  98. * interface
  99. *
  100. * @param component
  101. * @return closest parent Container
  102. */
  103. public static Container getParentLayout(Widget component) {
  104. Widget parent = component.getParent();
  105. while (parent != null && !(parent instanceof Container)) {
  106. parent = parent.getParent();
  107. }
  108. if (parent != null && ((Container) parent).hasChildComponent(component)) {
  109. return (Container) parent;
  110. }
  111. return null;
  112. }
  113. /**
  114. * Detects if current browser is IE.
  115. *
  116. * @deprecated use BrowserInfo class instead
  117. *
  118. * @return true if IE
  119. */
  120. public static boolean isIE() {
  121. return BrowserInfo.get().isIE();
  122. }
  123. /**
  124. * Detects if current browser is IE6.
  125. *
  126. * @deprecated use BrowserInfo class instead
  127. *
  128. * @return true if IE6
  129. */
  130. public static boolean isIE6() {
  131. return BrowserInfo.get().isIE6();
  132. }
  133. /**
  134. * @deprecated use BrowserInfo class instead
  135. * @return
  136. */
  137. public static boolean isIE7() {
  138. return BrowserInfo.get().isIE7();
  139. }
  140. /**
  141. * @deprecated use BrowserInfo class instead
  142. * @return
  143. */
  144. public static boolean isFF2() {
  145. return BrowserInfo.get().isFF2();
  146. }
  147. private static final Element escapeHtmlHelper = DOM.createDiv();
  148. /**
  149. * Converts html entities to text.
  150. *
  151. * @param html
  152. * @return escaped string presentation of given html
  153. */
  154. public static String escapeHTML(String html) {
  155. DOM.setInnerText(escapeHtmlHelper, html);
  156. return DOM.getInnerHTML(escapeHtmlHelper);
  157. }
  158. /**
  159. * Adds transparent PNG fix to image element; only use for IE6.
  160. *
  161. * @param el
  162. * IMG element
  163. * @param blankImageUrl
  164. * URL to transparent one-pixel gif
  165. */
  166. public native static void addPngFix(Element el, String blankImageUrl)
  167. /*-{
  168. el.attachEvent("onload", function() {
  169. var src = el.src;
  170. if (src.indexOf(".png")<1) return;
  171. var w = el.width||16;
  172. var h = el.height||16;
  173. el.src =blankImageUrl;
  174. el.style.height = h+"px";
  175. el.style.width = w+"px";
  176. el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='crop');";
  177. },false);
  178. }-*/;
  179. /**
  180. * Clones given element as in JavaScript.
  181. *
  182. * Deprecate this if there appears similar method into GWT someday.
  183. *
  184. * @param element
  185. * @param deep
  186. * clone child tree also
  187. * @return
  188. */
  189. public static native Element cloneNode(Element element, boolean deep)
  190. /*-{
  191. return element.cloneNode(deep);
  192. }-*/;
  193. }