Example usage for com.google.gwt.dom.client Document getElementsByTagName

List of usage examples for com.google.gwt.dom.client Document getElementsByTagName

Introduction

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

Prototype

public NodeList<Element> getElementsByTagName(String tagName) 

Source Link

Usage

From source file:com.calclab.emite.browser.client.DomAssist.java

License:Open Source License

/**
 * Retrieves all the div on the page that matches the given class
 * /*from  w  w  w.  ja  v a 2  s  .c  o  m*/
 * @param cssClass
 *            the css class to match
 * @return a list of elements
 */
public ArrayList<Element> findElementsByClass(final String cssClass) {
    Log.debug("Finding elements div." + cssClass);
    final ArrayList<Element> elements = new ArrayList<Element>();
    final Document document = Document.get();
    final NodeList<Element> divs = document.getElementsByTagName("div");
    for (int index = 0, total = divs.getLength(); index < total; index++) {
        final Element div = divs.getItem(index);
        final String[] classes = div.getClassName().split(" ");
        for (final String className : classes) {
            if (cssClass.equals(className)) {
                elements.add(div);
            }
        }
    }
    return elements;
}

From source file:com.google.appinventor.client.editor.simple.components.MockForm.java

License:Open Source License

private static int getVerticalScrollbarWidth() {
    // We only calculate the vertical scroll bar width once, then we store it in the static field
    // verticalScrollbarWidth. If the field is non-zero, we don't need to calculate it again.
    if (verticalScrollbarWidth == 0) {
        // The following code will calculate (on the fly) the width of a vertical scroll bar.
        // We'll create two divs, one inside the other and add the outer div to the document body,
        // but off-screen where the user won't see it.
        // We'll measure the width of the inner div twice: (first) when the outer div's vertical
        // scrollbar is hidden and (second) when the outer div's vertical scrollbar is visible.
        // The width of inner div will be smaller when outer div's vertical scrollbar is visible.
        // By subtracting the two measurements, we can calculate the width of the vertical scrollbar.

        // I used code from the following websites as reference material:
        // http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php
        // http://www.fleegix.org/articles/2006-05-30-getting-the-scrollbar-width-in-pixels

        Document document = Document.get();

        // Create an outer div.
        DivElement outerDiv = document.createDivElement();
        Style outerDivStyle = outerDiv.getStyle();
        // Use absolute positioning and set the top/left so that it is off-screen.
        // We don't want the user to see anything while we do this calculation.
        outerDivStyle.setProperty("position", "absolute");
        outerDivStyle.setProperty("top", "-1000px");
        outerDivStyle.setProperty("left", "-1000px");
        // Set the width and height of the outer div to a fixed size in pixels.
        outerDivStyle.setProperty("width", "100px");
        outerDivStyle.setProperty("height", "50px");
        // Hide the outer div's scrollbar by setting the "overflow" property to "hidden".
        outerDivStyle.setProperty("overflow", "hidden");

        // Create an inner div and put it inside the outer div.
        DivElement innerDiv = document.createDivElement();
        Style innerDivStyle = innerDiv.getStyle();
        // Set the height of the inner div to be 4 times the height of the outer div so that a
        // vertical scrollbar will be necessary (but hidden for now) on the outer div.
        innerDivStyle.setProperty("height", "200px");
        outerDiv.appendChild(innerDiv);/*  w  w w  .  j  a  va 2  s .  c  o m*/

        // Temporarily add the outer div to the document body. It's off-screen so the user won't
        // actually see anything.
        Element bodyElement = document.getElementsByTagName("body").getItem(0);
        bodyElement.appendChild(outerDiv);

        // Get the width of the inner div while the outer div's vertical scrollbar is hidden.
        int widthWithoutScrollbar = innerDiv.getOffsetWidth();
        // Show the outer div's vertical scrollbar by setting the "overflow" property to "auto".
        outerDivStyle.setProperty("overflow", "auto");
        // Now, get the width of the inner div while the vertical scrollbar is visible.
        int widthWithScrollbar = innerDiv.getOffsetWidth();

        // Remove the outer div from the document body.
        bodyElement.removeChild(outerDiv);

        // Calculate the width of the vertical scrollbar by subtracting the two widths.
        verticalScrollbarWidth = widthWithoutScrollbar - widthWithScrollbar;
    }

    return verticalScrollbarWidth;
}

From source file:com.google.appinventor.client.jsonp.JsonpConnection.java

License:Open Source License

/**
 * Sends a JSONP request by embedding a SCRIPT tag into the document.
 *
 * @param id the id used for the script tag and to identify the callback
 * @param request the request to be made
 * @param parameters the parameters for the request
 * @param function a function that transforms the response into the type
 *        that the callback needs//w w w.  jav  a  2 s.  c  o m
 * @param callback the callback that should be called with the transformed
 *        response
 */
private <T> void sendJsonpRequest(String id, String request, Map<String, Object> parameters,
        final Function<String, ? extends T> function, final AsyncCallback<T> callback) {
    Preconditions.checkNotNull(id);

    // Prepare an intermediate callback that converts the String result to T.
    if (callback != null) {
        Preconditions.checkNotNull(function);
        CALLBACKS.put(id, new AsyncCallback<String>() {
            @Override
            public void onSuccess(String jsonResult) {
                T result;
                try {
                    result = function.apply(jsonResult);
                } catch (RuntimeException e) {
                    callback.onFailure(e);
                    return;
                }
                callback.onSuccess(result);
            }

            @Override
            public void onFailure(Throwable caught) {
                callback.onFailure(caught);
            }
        });
    }

    // Insert a script tag into the document.
    Document document = Document.get();
    ScriptElement script = document.createScriptElement();
    String uri = makeURI(request, parameters, id);
    script.setSrc(uri);
    script.setId(id);
    Element bodyElement = document.getElementsByTagName("body").getItem(0);
    Element previous = document.getElementById(id);
    if (previous != null) {
        bodyElement.replaceChild(script, previous);
    } else {
        bodyElement.appendChild(script);
    }
}

From source file:com.google.appinventor.client.jsonp.JsonpConnection.java

License:Open Source License

/**
 * Removes the script tag with the given id from the document.
 *//*  ww w. j  av  a  2 s  .  com*/
private static void removeScriptTag(String id) {
    Document document = Document.get();
    Element element = document.getElementById(id);
    if (element != null) {
        document.getElementsByTagName("body").getItem(0).removeChild(element);
    }
}

From source file:com.google.speedtracer.client.SourceViewer.java

License:Apache License

private static void injectStyles(IFrameElement sourceFrame, String styleText) {
    Document iframeDocument = sourceFrame.getContentDocument();
    HeadElement head = iframeDocument.getElementsByTagName("head").getItem(0).cast();
    StyleElement styleTag = iframeDocument.createStyleElement();
    styleTag.setInnerText(styleText);//from  w  w  w .  j av  a 2 s .c  om
    head.appendChild(styleTag);
}

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

License:Apache License

/**
 * Creates a new resource loader. You should generally not create you own
 * resource loader, but instead use {@link ResourceLoader#get()} to get an
 * instance./*w  ww.j ava 2s  .  c  o  m*/
 */
protected ResourceLoader() {
    Document document = Document.get();
    head = document.getElementsByTagName("head").getItem(0);

    // detect already loaded scripts and stylesheets
    NodeList<Element> scripts = document.getElementsByTagName("script");
    for (int i = 0; i < scripts.getLength(); i++) {
        ScriptElement element = ScriptElement.as(scripts.getItem(i));
        String src = element.getSrc();
        if (src != null && src.length() != 0) {
            loadedResources.add(src);
        }
    }

    NodeList<Element> links = document.getElementsByTagName("link");
    for (int i = 0; i < links.getLength(); i++) {
        LinkElement linkElement = LinkElement.as(links.getItem(i));
        String rel = linkElement.getRel();
        String href = linkElement.getHref();
        if ("stylesheet".equalsIgnoreCase(rel) && href != null && href.length() != 0) {
            loadedResources.add(href);
        }
    }
}

From source file:edu.caltech.ipac.firefly.ui.TitleFlasher.java

private static void findFavIcon() {
    Document doc = Document.get();
    NodeList<Element> eleList = doc.getElementsByTagName("link");
    for (int i = 0; (i < eleList.getLength()); i++) {
        Element e = eleList.getItem(i);
        LinkElement le = LinkElement.as(e);
        if ("image/x-icon".equals(le.getType())) {
            if (!StringUtils.isEmpty(le.getHref())) {
                favIconElement = le;/*from  w  w w. j  a  v  a2s .c o m*/
                String favIcon = le.getHref();
                break;
            }
        }
    }
}

From source file:org.chromium.distiller.extractors.embeds.TwitterExtractor.java

License:Open Source License

/**
 * Handle a Twitter embed that has already been rendered.
 * @param e The root element of the embed (should be an "iframe").
 * @return EmbeddedElement object representing the embed or null.
 *///from w  ww  .j a va2  s .  co m
private WebEmbed handleRendered(Element e) {
    // Twitter embeds are blockquote tags operated on by some javascript.
    if (!"IFRAME".equals(e.getTagName())) {
        return null;
    }
    IFrameElement iframe = IFrameElement.as(e);

    // If the iframe has no "src" attribute, explore further.
    if (!iframe.getSrc().isEmpty()) {
        return null;
    }
    Document iframeDoc = iframe.getContentDocument();
    if (iframeDoc == null) {
        return null;
    }

    // The iframe will contain a blockquote element that has information including tweet id.
    NodeList blocks = iframeDoc.getElementsByTagName("blockquote");
    if (blocks.getLength() < 1) {
        return null;
    }
    Element tweetBlock = Element.as(blocks.getItem(0));

    String id = tweetBlock.getAttribute("data-tweet-id");

    if (id.isEmpty()) {
        return null;
    }

    return new WebEmbed(e, "twitter", id, null);
}

From source file:org.cruxframework.crux.core.client.screen.DisplayHandler.java

License:Apache License

/**
 * Create a viewport meta element with specified content
 * @param content//  www  . java2  s. c  o  m
 * @param window
 */
public static void createViewport(String content, JavaScriptObject wnd) {
    Document document = getWindowDocument(wnd);
    MetaElement viewport = document.createMetaElement();
    viewport.setContent(content);
    viewport.setName("viewport");
    document.getElementsByTagName("head").getItem(0).appendChild(viewport);
    JavaScriptObject parentWindow = getParentWindow(wnd);
    if (parentWindow != null && isCruxWindow(parentWindow)) {
        createViewport(content, parentWindow);
    }
}

From source file:org.cruxframework.crux.core.client.screen.ViewPortHandlerIEMobileImpl.java

License:Apache License

@Override
public void createViewport(String content, JavaScriptObject wnd) {
    if (!DeviceAdaptiveUtils.isInternetExplorerMobile()) {
        new ViewPortHandlerImpl().createViewport(content, wnd);
        return;/*from w w  w . j a  v  a2 s  .  co  m*/
    }

    Document document = getWindowDocument(wnd);
    Element header = document.getElementsByTagName("head").getItem(0);
    StyleElement viewPortStyleElement = document.createStyleElement();

    String newProperties = handlePropertiesToCSS(content);

    viewPortStyleElement.appendChild(document.createTextNode("@-ms-viewport{" + newProperties + "}"));
    header.appendChild(viewPortStyleElement);

    JavaScriptObject parentWindow = getParentWindow(wnd);
    if (parentWindow != null && isCruxWindow(parentWindow)) {
        createViewport(content, parentWindow);
    }
}