All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.itdraft.gwt.util.StyleUtil Maven / Gradle / Ivy

Go to download

Util.gwt is a suite of utility classes to work with DOM, HTML5 storage. Util.gwt has classes for rate limiting, better JS interop and many more.

The newest version!
package io.itdraft.gwt.util;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;

import static io.itdraft.gwt.util.Constant.PX;

public class StyleUtil {
    private StyleUtil() {
    }

    public static double getWidth(Element element) {
        return pixelValueToDouble(getComputedStyleProperty(element, "width"));
    }

    public static void copyProperty(Element from, Element to, String property) {
        copyProperty(from, to.getStyle(), property);
    }

    public static void copyProperty(Element from, Style to, String property) {
        to.setProperty(property, getComputedStyleProperty(from, property));
    }

    public static void copyTypographicStyle(Element from, Element to) {
        copyTypographicStyle(from, to.getStyle());
    }

    public static void copyTypographicStyle(Element from, Style to) {
        copyProperties(from, to, "fontFamily", "fontSize", "fontStyle", "fontVariant",
                "fontWeight");

        copyProperty(from, to, "wordSpacing");
        copyProperty(from, to, "letterSpacing");

        copyProperties(from, to, "textIndent", "textRendering", "textTransform");
    }

    public static void copyBackground(Element from, Element to) {
        copyProperties(from, to, "backgroundAttachment", "backgroundClip", "backgroundColor",
                "backgroundImage", "backgroundOrigin", "backgroundPosition", "backgroundRepeat",
                "backgroundSize");
    }

    public static void copyProperties(Element from, Element to, String... properties) {
        copyProperties(from, to.getStyle(), properties);
    }

    public static void copyProperties(Element from, Style to, String... properties) {
        for (String property : properties) {
            copyProperty(from, to, property);
        }
    }

    public static double getHorizontalPaddingAmount(Element element) {
        String paddingRight = getComputedStyleProperty(element, "paddingRight");
        String paddingLeft = getComputedStyleProperty(element, "paddingLeft");
        return pixelValueToDouble(paddingLeft) + pixelValueToDouble(paddingRight);
    }

    /**
     * @throws NumberFormatException if the string does not contain a
     *                               parsable double.
     */
    private static double pixelValueToDouble(String pixelValue) {
        return Double.parseDouble(pixelValue.replace(PX, ""));
    }

    private static native String getComputedStyleProperty(Element el, String prop)  /*-{
        var computedStyle;
        if (document.defaultView && document.defaultView.getComputedStyle) { // standart (includes ie9)
            computedStyle = document.defaultView.getComputedStyle(el, "")[prop];

        } else if (el.currentStyle) { // IE older
            computedStyle = el.currentStyle[prop];

        } else { // inline style
            computedStyle = el.style[prop];
        }
        return computedStyle || "";
    }-*/;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy