Example usage for com.google.gwt.dom.client Style getProperty

List of usage examples for com.google.gwt.dom.client Style getProperty

Introduction

In this page you can find the example usage for com.google.gwt.dom.client Style getProperty.

Prototype

public String getProperty(String name) 

Source Link

Usage

From source file:com.vaadin.client.WidgetUtil.java

License:Apache License

/**
 * Force webkit to redraw an element/* w  w  w.ja  va  2s .  c o  m*/
 * 
 * @param element
 *            The element that should be redrawn
 */
public static void forceWebkitRedraw(Element element) {
    Style style = element.getStyle();
    String s = style.getProperty("webkitTransform");
    if (s == null || s.length() == 0) {
        style.setProperty("webkitTransform", "scale(1)");
    } else {
        style.setProperty("webkitTransform", "");
    }
}

From source file:com.vaadin.client.WidgetUtil.java

License:Apache License

/**
 * Temporarily sets the {@code styleProperty} to {@code tempValue} and then
 * resets it to its current value. Used mainly to work around rendering
 * issues in IE (and possibly in other browsers)
 * //from w w w.ja va  2 s  .c  om
 * @param element
 *            The target element
 * @param styleProperty
 *            The name of the property to set
 * @param tempValue
 *            The temporary value
 */
public static void setStyleTemporarily(Element element, final String styleProperty, String tempValue) {
    final Style style = element.getStyle();
    final String currentValue = style.getProperty(styleProperty);

    style.setProperty(styleProperty, tempValue);

    // Read a style-based property to force the browser to recalculate the
    // element's dimensions with the temporary style.
    element.getOffsetWidth();

    style.setProperty(styleProperty, currentValue);
}

From source file:com.vaadin.terminal.gwt.client.ui.VTabsheet.java

License:Open Source License

private void updateDynamicWidth() {
    // Find width consumed by tabs
    TableCellElement spacerCell = ((TableElement) tb.getElement().cast()).getRows().getItem(0).getCells()
            .getItem(tb.getTabCount());/*from  www.  j a v  a 2s  .com*/

    int spacerWidth = spacerCell.getOffsetWidth();
    DivElement div = (DivElement) spacerCell.getFirstChildElement();

    int spacerMinWidth = spacerCell.getOffsetWidth() - div.getOffsetWidth();

    int tabsWidth = tb.getOffsetWidth() - spacerWidth + spacerMinWidth;

    // Find content width
    Style style = tp.getElement().getStyle();
    String overflow = style.getProperty("overflow");
    style.setProperty("overflow", "hidden");
    style.setPropertyPx("width", tabsWidth);

    boolean hasTabs = tp.getWidgetCount() > 0;

    Style wrapperstyle = null;
    if (hasTabs) {
        wrapperstyle = tp.getWidget(tp.getVisibleWidget()).getElement().getParentElement().getStyle();
        wrapperstyle.setPropertyPx("width", tabsWidth);
    }
    // Get content width from actual widget

    int contentWidth = 0;
    if (hasTabs) {
        contentWidth = tp.getWidget(tp.getVisibleWidget()).getOffsetWidth();
    }
    style.setProperty("overflow", overflow);

    // Set widths to max(tabs,content)
    if (tabsWidth < contentWidth) {
        tabsWidth = contentWidth;
    }

    int outerWidth = tabsWidth + getContentAreaBorderWidth();

    tabs.getStyle().setPropertyPx("width", outerWidth);
    style.setPropertyPx("width", tabsWidth);
    if (hasTabs) {
        wrapperstyle.setPropertyPx("width", tabsWidth);
    }

    contentNode.getStyle().setPropertyPx("width", tabsWidth);
    super.setWidth(outerWidth + "px");
    updateOpenTabSize();
}

From source file:com.vaadin.terminal.gwt.client.Util.java

License:Open Source License

/**
 * Temporarily sets the {@code styleProperty} to {@code tempValue} and then
 * resets it to its current value. Used mainly to work around rendering
 * issues in IE (and possibly in other browsers)
 * /*from  w w w  .  j  a v  a2 s  .c om*/
 * @param element
 *            The target element
 * @param styleProperty
 *            The name of the property to set
 * @param tempValue
 *            The temporary value
 */
public static void setStyleTemporarily(Element element, final String styleProperty, String tempValue) {
    final Style style = element.getStyle();
    final String currentValue = style.getProperty(styleProperty);

    style.setProperty(styleProperty, tempValue);
    element.getOffsetWidth();
    style.setProperty(styleProperty, currentValue);

}

From source file:org.chromium.distiller.webdocument.ElementAction.java

License:Open Source License

public static ElementAction getForElement(Element element) {
    Style style = DomUtil.getComputedStyle(element);
    ElementAction action = new ElementAction();
    String tagName = element.getTagName();
    switch (style.getDisplay()) {
    case "inline":
        break;//from  www .ja va 2  s  .c  o m
    case "inline-block":
    case "inline-flex":
        action.changesTagLevel = true;
        break;
    case "block":
        // Special casing for drop cap letter with "float".
        // Having style "float" would imply "display: block".
        // Ref: http://crbug.com/593128
        if (!"none".equals(style.getProperty("float")) && "SPAN".equals(tagName)) {
            break;
        }
        // Intentional fall through.
        // See http://www.w3.org/TR/CSS2/tables.html#table-display
        // and http://www.w3.org/TR/css-flexbox-1/#flex-containers
        // The default case includes the following display types:
        // list-item
        // inline-table
        // table-row
        // table-row-group
        // table-header-group
        // table-footer-group
        // table-column
        // table-column-group
        // table-cell
        // table-caption
        // flex
    default:
        action.flush = true;
        action.changesTagLevel = true;
        break;
    }

    if (!"HTML".equals(tagName) && !"BODY".equals(tagName)) {
        String className = element.getAttribute("class");
        int classCount = DomUtil.getClassList(element).length();
        String id = element.getAttribute("id");
        if ((REG_COMMENT.test(className) || REG_COMMENT.test(id)) && classCount <= MAX_CLASS_COUNT) {
            action.labels.push(DefaultLabels.STRICTLY_NOT_CONTENT);
        }

        switch (tagName) {
        case "ASIDE":
        case "NAV":
            action.labels.push(DefaultLabels.STRICTLY_NOT_CONTENT);
            break;
        case "LI":
            action.labels.push(DefaultLabels.LI);
            break;
        case "H1":
            action.labels.push(DefaultLabels.H1);
            action.labels.push(DefaultLabels.HEADING);
            break;
        case "H2":
            action.labels.push(DefaultLabels.H2);
            action.labels.push(DefaultLabels.HEADING);
            break;
        case "H3":
            action.labels.push(DefaultLabels.H3);
            action.labels.push(DefaultLabels.HEADING);
            break;
        case "A":
            // TODO(cjhopman): Anchors probably shouldn't unconditionally change the tag
            // level.
            action.changesTagLevel = true;
            if (element.hasAttribute("href")) {
                action.isAnchor = true;
            }
            break;
        }
    }
    return action;
}

From source file:org.opencms.gwt.client.util.CmsStyleSaver.java

License:Open Source License

/** 
 * Creates a new instance for an element and saves some of its style properties.<p>
 * /*from   w ww.j a  va 2s.  c  o  m*/
 * @param elem the DOM element 
 * @param properties the style properties to save 
 */
public CmsStyleSaver(Element elem, String... properties) {

    m_element = elem;
    Style style = elem.getStyle();
    for (String prop : properties) {
        String val = style.getProperty(prop);
        m_propertyValues.put(prop, val);
    }
}

From source file:org.rstudio.studio.client.workbench.views.source.editors.text.ImagePreviewer.java

License:Open Source License

private static void onPreviewImageLineWidget(final DocDisplay display, final DocUpdateSentinel sentinel,
        final String href, final String attributes, final Position position, final Range tokenRange) {
    // if we already have a line widget for this row, bail
    LineWidget lineWidget = display.getLineWidgetForRow(position.getRow());
    if (lineWidget != null)
        return;/*from   ww  w  . j  av  a 2 s.  c  o m*/

    // shared mutable state that we hide in this closure
    final Mutable<PinnedLineWidget> plw = new Mutable<PinnedLineWidget>();
    final Mutable<ChunkOutputWidget> cow = new Mutable<ChunkOutputWidget>();
    final Mutable<HandlerRegistration> docChangedHandler = new Mutable<HandlerRegistration>();
    final Mutable<HandlerRegistration> renderHandler = new Mutable<HandlerRegistration>();

    // command that ensures state is cleaned up when widget hidden
    final Command onDetach = new Command() {
        private void detach() {
            // detach chunk output widget
            cow.set(null);

            // detach pinned line widget
            if (plw.get() != null)
                plw.get().detach();
            plw.set(null);

            // detach render handler
            if (renderHandler.get() != null)
                renderHandler.get().removeHandler();
            renderHandler.set(null);

            // detach doc changed handler
            if (docChangedHandler.get() != null)
                docChangedHandler.get().removeHandler();
            docChangedHandler.set(null);
        }

        @Override
        public void execute() {
            // if the associated chunk output widget has been cleaned up,
            // make a last-ditch detach effort anyhow
            ChunkOutputWidget widget = cow.get();
            if (widget == null) {
                detach();
                return;
            }

            // fade out and then detach
            FadeOutAnimation anim = new FadeOutAnimation(widget, new Command() {
                @Override
                public void execute() {
                    detach();
                }
            });

            anim.run(400);
        }
    };

    // construct placeholder for image
    final SimplePanel container = new SimplePanel();
    container.addStyleName(RES.styles().container());
    final Label noImageLabel = new Label("(No image at path " + href + ")");

    // resize command (used by various routines that need to respond
    // to width / height change events)
    final CommandWithArg<Integer> onResize = new CommandWithArg<Integer>() {
        private int state_ = -1;

        @Override
        public void execute(Integer height) {
            // defend against missing chunk output widget (can happen if a widget
            // is closed / dismissed before image finishes loading)
            ChunkOutputWidget widget = cow.get();
            if (widget == null)
                return;

            // don't resize if the chunk widget if we were already collapsed
            int state = widget.getExpansionState();
            if (state == state_ && state == ChunkOutputWidget.COLLAPSED)
                return;

            state_ = state;
            widget.getFrame().setHeight(height + "px");
            LineWidget lw = plw.get().getLineWidget();
            lw.setPixelHeight(height);
            display.onLineWidgetChanged(lw);
        }
    };

    // construct our image
    String srcPath = imgSrcPathFromHref(sentinel, href);
    final Image image = new Image(srcPath);
    image.addStyleName(RES.styles().image());

    // parse and inject attributes
    Map<String, String> parsedAttributes = HTMLAttributesParser.parseAttributes(attributes);
    final Element imgEl = image.getElement();
    for (Map.Entry<String, String> entry : parsedAttributes.entrySet()) {
        String key = entry.getKey();
        String val = entry.getValue();
        if (StringUtil.isNullOrEmpty(key) || StringUtil.isNullOrEmpty(val))
            continue;
        imgEl.setAttribute(key, val);
    }

    // add load handlers to image
    DOM.sinkEvents(imgEl, Event.ONLOAD | Event.ONERROR);
    DOM.setEventListener(imgEl, new EventListener() {
        @Override
        public void onBrowserEvent(Event event) {
            if (DOM.eventGetType(event) == Event.ONLOAD) {
                final ImageElementEx imgEl = image.getElement().cast();

                int minWidth = Math.min(imgEl.naturalWidth(), 100);
                int maxWidth = Math.min(imgEl.naturalWidth(), 650);

                Style style = imgEl.getStyle();

                boolean hasWidth = imgEl.hasAttribute("width") || style.getProperty("width") != null;

                if (!hasWidth) {
                    style.setProperty("width", "100%");
                    style.setProperty("minWidth", minWidth + "px");
                    style.setProperty("maxWidth", maxWidth + "px");
                }

                // attach to container
                container.setWidget(image);

                // update widget
                int height = image.getOffsetHeight() + 10;
                onResize.execute(height);
            } else if (DOM.eventGetType(event) == Event.ONERROR) {
                container.setWidget(noImageLabel);
                onResize.execute(50);
            }
        }
    });

    // handle editor resize events
    final Timer renderTimer = new Timer() {
        @Override
        public void run() {
            int height = image.getOffsetHeight() + 30;
            onResize.execute(height);
        }
    };

    // initialize render handler
    renderHandler.set(display.addRenderFinishedHandler(new RenderFinishedEvent.Handler() {
        private int width_;

        @Override
        public void onRenderFinished(RenderFinishedEvent event) {
            int width = display.getBounds().getWidth();
            if (width == width_)
                return;

            width_ = width;
            renderTimer.schedule(100);
        }
    }));

    // initialize doc changed handler
    docChangedHandler.set(display.addDocumentChangedHandler(new DocumentChangedEvent.Handler() {
        private String href_ = href;
        private String attributes_ = StringUtil.notNull(attributes);

        private final Timer refreshImageTimer = new Timer() {
            @Override
            public void run() {
                // if the discovered href isn't an image link, just bail
                if (!ImagePreviewer.isImageHref(href_))
                    return;

                // set new src location (load handler will replace label as needed)
                container.setWidget(new SimplePanel());
                noImageLabel.setText("(No image at path " + href_ + ")");
                image.getElement().setAttribute("src", imgSrcPathFromHref(sentinel, href_));

                // parse and inject attributes
                Map<String, String> parsedAttributes = HTMLAttributesParser.parseAttributes(attributes_);
                final Element imgEl = image.getElement();
                for (Map.Entry<String, String> entry : parsedAttributes.entrySet()) {
                    String key = entry.getKey();
                    String val = entry.getValue();
                    if (StringUtil.isNullOrEmpty(key) || StringUtil.isNullOrEmpty(val))
                        continue;
                    imgEl.setAttribute(key, val);
                }
            }
        };

        private void onDocumentChangedImpl(DocumentChangedEvent event) {
            int row = plw.get().getRow();
            Range range = event.getEvent().getRange();
            if (range.getStart().getRow() <= row && row <= range.getEnd().getRow()) {
                String line = display.getLine(row);
                if (ImagePreviewer.isStandaloneMarkdownLink(line)) {
                    // check to see if the URL text has been updated
                    Token hrefToken = null;
                    JsArray<Token> tokens = display.getTokens(row);
                    for (Token token : JsUtil.asIterable(tokens)) {
                        if (token.hasType("href")) {
                            hrefToken = token;
                            break;
                        }
                    }

                    if (hrefToken == null)
                        return;

                    String attributes = "";
                    int startBraceIdx = line.indexOf("){");
                    int endBraceIdx = line.lastIndexOf("}");
                    if (startBraceIdx != -1 && endBraceIdx != -1 && endBraceIdx > startBraceIdx) {
                        attributes = line.substring(startBraceIdx + 2, endBraceIdx).trim();
                    }

                    // if we have the same href as before, don't update
                    // (avoid flickering + re-requests of same URL)
                    if (hrefToken.getValue().equals(href_) && attributes.equals(attributes_))
                        return;

                    // cache href and schedule refresh of image
                    href_ = hrefToken.getValue();
                    attributes_ = attributes;

                    refreshImageTimer.schedule(700);
                } else {
                    onDetach.execute();
                }
            }
        }

        @Override
        public void onDocumentChanged(final DocumentChangedEvent event) {
            // ignore 'removeLines' events as they won't mutate the actual
            // line containing the markdown link
            String action = event.getEvent().getAction();
            if (action.equals("removeLines"))
                return;

            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
                @Override
                public void execute() {
                    onDocumentChangedImpl(event);
                }
            });
        }
    }));

    ChunkOutputHost host = new ChunkOutputHost() {
        @Override
        public void onOutputRemoved(final ChunkOutputWidget widget) {
            onDetach.execute();
        }

        @Override
        public void onOutputHeightChanged(ChunkOutputWidget widget, int height, boolean ensureVisible) {
            onResize.execute(height);
        }
    };

    cow.set(new ChunkOutputWidget(sentinel.getId(), "md-image-preview-" + StringUtil.makeRandomId(8),
            RmdChunkOptions.create(), ChunkOutputWidget.EXPANDED, false, // can close
            host, ChunkOutputSize.Bare));

    ChunkOutputWidget outputWidget = cow.get();
    outputWidget.setRootWidget(container);
    outputWidget.hideSatellitePopup();
    outputWidget.getElement().getStyle().setMarginTop(4, Unit.PX);

    plw.set(new PinnedLineWidget(LINE_WIDGET_TYPE, display, outputWidget, position.getRow(), null, null));
}

From source file:org.rstudio.studio.client.workbench.views.source.editors.text.ImagePreviewPopup.java

License:Open Source License

private boolean isSmall() {
    ImageElementEx el = image_.getElement().cast();
    Style style = el.getStyle();
    return SMALL_MAX_WIDTH.equals(style.getProperty("maxWidth"))
            || SMALL_MAX_HEIGHT.equals(style.getProperty("maxHeight"));
}