123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- package com.itmill.toolkit.terminal.gwt.client;
-
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
-
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.Element;
- import com.google.gwt.user.client.ui.RootPanel;
- import com.google.gwt.user.client.ui.Widget;
-
- public class Util {
-
- /**
- * Helper method for debugging purposes.
- *
- * Stops execution on firefox browsers on a breakpoint.
- *
- */
- public static native void browserDebugger()
- /*-{
- if($wnd.console)
- debugger;
- }-*/;
-
- /**
- * Called when the size of one or more widgets have changed during
- * rendering. Finds parent container and notifies them of the size change.
- *
- * @param widgets
- */
- public static void componentSizeUpdated(Set<Widget> widgets) {
- if (widgets.isEmpty()) {
- return;
- }
-
- Map<Container, Set<Paintable>> childWidgets = new HashMap<Container, Set<Paintable>>();
-
- for (Widget widget : widgets) {
- Widget parent = widget.getParent();
- while (parent != null && !(parent instanceof Container)) {
- parent = parent.getParent();
- }
- if (parent != null) {
- Set<Paintable> set = childWidgets.get(parent);
- if (set == null) {
- set = new HashSet<Paintable>();
- childWidgets.put((Container) parent, set);
- }
- set.add((Paintable) widget);
- }
- }
-
- Set<Widget> parentChanges = new HashSet<Widget>();
- for (Container parent : childWidgets.keySet()) {
- if (!parent.requestLayout(childWidgets.get(parent))) {
- parentChanges.add((Widget) parent);
- }
- }
-
- componentSizeUpdated(parentChanges);
- }
-
- public static float parseRelativeSize(String size) {
- if (size == null || !size.endsWith("%")) {
- return -1;
- }
-
- try {
- return Float.parseFloat(size.substring(0, size.length() - 1));
- } catch (Exception e) {
- ClientExceptionHandler.displayError(
- "Unable to parse relative size", e);
- }
-
- return -1;
- }
-
- /**
- * Returns closest parent Widget in hierarchy that implements Container
- * interface
- *
- * @param component
- * @return closest parent Container
- */
- public static Container getLayout(Widget component) {
- Widget parent = component.getParent();
- while (parent != null && !(parent instanceof Container)) {
- parent = parent.getParent();
- }
- if (parent != null) {
- assert ((Container) parent).hasChildComponent(component);
-
- return (Container) parent;
- }
- return null;
- }
-
- /**
- * Detects if current browser is IE.
- *
- * @deprecated use BrowserInfo class instead
- *
- * @return true if IE
- */
- public static boolean isIE() {
- return BrowserInfo.get().isIE();
- }
-
- /**
- * Detects if current browser is IE6.
- *
- * @deprecated use BrowserInfo class instead
- *
- * @return true if IE6
- */
- public static boolean isIE6() {
- return BrowserInfo.get().isIE6();
- }
-
- /**
- * @deprecated use BrowserInfo class instead
- * @return
- */
- public static boolean isIE7() {
- return BrowserInfo.get().isIE7();
- }
-
- /**
- * @deprecated use BrowserInfo class instead
- * @return
- */
- public static boolean isFF2() {
- return BrowserInfo.get().isFF2();
- }
-
- private static final Element escapeHtmlHelper = DOM.createDiv();
-
- /**
- * Converts html entities to text.
- *
- * @param html
- * @return escaped string presentation of given html
- */
- public static String escapeHTML(String html) {
- DOM.setInnerText(escapeHtmlHelper, html);
- return DOM.getInnerHTML(escapeHtmlHelper);
- }
-
- /**
- * Adds transparent PNG fix to image element; only use for IE6.
- *
- * @param el
- * IMG element
- * @param blankImageUrl
- * URL to transparent one-pixel gif
- */
- public native static void addPngFix(Element el, String blankImageUrl)
- /*-{
- el.attachEvent("onload", function() {
- var src = el.src;
- if (src.indexOf(".png")<1) return;
- var w = el.width||16;
- var h = el.height||16;
- el.src =blankImageUrl;
- el.style.height = h+"px";
- el.style.width = w+"px";
- el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='crop');";
- },false);
- }-*/;
-
- /**
- * Clones given element as in JavaScript.
- *
- * Deprecate this if there appears similar method into GWT someday.
- *
- * @param element
- * @param deep
- * clone child tree also
- * @return
- */
- public static native Element cloneNode(Element element, boolean deep)
- /*-{
- return element.cloneNode(deep);
- }-*/;
-
- public static int measureHorizontalPadding(Element element, int paddingGuess) {
- String originalWidth = DOM.getStyleAttribute(element, "width");
- int originalOffsetWidth = element.getOffsetWidth();
- int widthGuess = (originalOffsetWidth + paddingGuess);
- DOM.setStyleAttribute(element, "width", widthGuess + "px");
- int padding = widthGuess - element.getOffsetWidth();
-
- DOM.setStyleAttribute(element, "width", originalWidth);
- return padding;
- }
-
- public static void setWidthExcludingPadding(Element element,
- int requestedWidth, int paddingGuess) {
-
- int widthGuess = requestedWidth - paddingGuess;
- if (widthGuess < 0) {
- widthGuess = 0;
- }
-
- DOM.setStyleAttribute(element, "width", widthGuess + "px");
- int captionOffsetWidth = DOM.getElementPropertyInt(element,
- "offsetWidth");
-
- int actualPadding = captionOffsetWidth - widthGuess;
-
- if (actualPadding != paddingGuess) {
- DOM.setStyleAttribute(element, "width", requestedWidth
- - actualPadding + "px");
-
- }
-
- }
-
- private static int detectedScrollbarSize = -1;
-
- public static int getNativeScrollbarSize() {
- if (detectedScrollbarSize < 0) {
- Element scroller = DOM.createDiv();
- scroller.getStyle().setProperty("width", "50px");
- scroller.getStyle().setProperty("height", "50px");
- scroller.getStyle().setProperty("overflow", "scroll");
- scroller.getStyle().setProperty("position", "absolute");
- scroller.getStyle().setProperty("marginLeft", "-5000px");
- RootPanel.getBodyElement().appendChild(scroller);
- detectedScrollbarSize = scroller.getOffsetWidth()
- - scroller.getPropertyInt("clientWidth");
- assert detectedScrollbarSize != 0;
- RootPanel.getBodyElement().removeChild(scroller);
-
- }
- return detectedScrollbarSize;
- }
-
- }
|