Example usage for com.google.gwt.dom.client Node cast

List of usage examples for com.google.gwt.dom.client Node cast

Introduction

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

Prototype

@Override
    public abstract <T extends JavascriptObjectEquivalent> T cast();

Source Link

Usage

From source file:ch.bergturbenthal.hs485.frontend.gwtfrontend.client.svg.SVGProcessor.java

License:Open Source License

public static int normalizeIds(final OMSVGElement srcSvg) {
    docId++;//  w w w  . ja  va 2  s.c  om
    // Collect all the original element ids and replace them with a
    // normalized id
    int idIndex = 0;
    final Map<String, Element> idToElement = new HashMap<String, Element>();
    final Map<String, String> idToNormalizedId = new HashMap<String, String>();
    final List<Element> queue = new ArrayList<Element>();
    queue.add(srcSvg.getElement());
    while (queue.size() > 0) {
        final Element element = queue.remove(0);
        final String id = element.getId();
        if (id != null) {
            idToElement.put(id, element);
            final String normalizedId = "d" + docId + "_" + idIndex++;
            idToNormalizedId.put(id, normalizedId);
            element.setId(normalizedId);
        }
        final NodeList<Node> childNodes = element.getChildNodes();
        for (int i = 0, length = childNodes.getLength(); i < length; i++) {
            final Node childNode = childNodes.getItem(i);
            if (childNode.getNodeType() == Node.ELEMENT_NODE)
                queue.add((Element) childNode.cast());
        }
    }

    // Change all the attributes which are URI references
    final Set<String> attNames = new HashSet<String>(Arrays.asList(new String[] { "clip-path", "mask",
            "marker-start", "marker-mid", "marker-end", "fill", "stroke", "filter", "cursor", "style" }));
    queue.add(srcSvg.getElement());
    final IdRefTokenizer tokenizer = GWT.create(IdRefTokenizer.class);
    while (queue.size() > 0) {
        final Element element = queue.remove(0);
        if (DOMHelper.hasAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI,
                SVGConstants.XLINK_HREF_ATTRIBUTE)) {
            final String idRef = DOMHelper.getAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI,
                    SVGConstants.XLINK_HREF_ATTRIBUTE).substring(1);
            final String normalizeIdRef = idToNormalizedId.get(idRef);
            DOMHelper.setAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI,
                    SVGConstants.XLINK_HREF_ATTRIBUTE, "#" + normalizeIdRef);
        }
        final NamedNodeMap<Attr> attrs = DOMHelper.getAttributes(element);
        for (int i = 0, length = attrs.getLength(); i < length; i++) {
            final Attr attr = attrs.item(i);
            if (attNames.contains(attr.getName())) {
                final StringBuilder builder = new StringBuilder();
                tokenizer.tokenize(attr.getValue());
                IdRefTokenizer.IdRefToken token;
                while ((token = tokenizer.nextToken()) != null) {
                    String value = token.getValue();
                    if (token.getKind() == IdRefTokenizer.IdRefToken.DATA)
                        builder.append(value);
                    else {
                        value = idToNormalizedId.get(value);
                        builder.append(value == null ? token.getValue() : value);
                    }
                }
                attr.setValue(builder.toString());
            }
        }
        final NodeList<Node> childNodes = element.getChildNodes();
        for (int i = 0, length = childNodes.getLength(); i < length; i++) {
            final Node childNode = childNodes.getItem(i);
            if (childNode.getNodeType() == Node.ELEMENT_NODE)
                queue.add((Element) childNode.cast());
        }
    }
    return docId;
}

From source file:com.arcbees.gquery.elastic.client.ElasticImpl.java

License:Apache License

private void bind() {
    resizeHandlerRegistration = Window.addResizeHandler(new ResizeHandler() {
        @Override/*from w  w w . j  av a 2s .  co m*/
        public void onResize(ResizeEvent event) {
            if (options.isAutoResize()) {
                layout();
            }
        }
    });

    if (MutationObserver.isSupported()) {
        mutationObserver = new MutationObserver(new DomMutationCallback() {
            @Override
            public void onNodesRemoved(JsArray<Node> removedNodes) {
                onItemsRemoved();
            }

            @Override
            public void onNodesInserted(JsArray<Node> addedNodes, Node nextSibling) {
                onItemsInserted(toElementList(addedNodes));
            }

            @Override
            public void onNodesAppended(JsArray<Node> addedNodes) {
                onItemsAppended(toElementList(addedNodes));
            }
        });

        mutationObserver.observe(container);
    } else {
        // try old api with DomMutationEvent
        $(container).on("DOMNodeInserted", new Function() {
            @Override
            public boolean f(Event event) {
                Node node = event.getEventTarget().cast();

                if (node.getNodeType() != Node.ELEMENT_NODE || node.getParentElement() != container) {
                    return false;
                }

                final Element element = node.cast();
                Element prevSibling = element.getPreviousSiblingElement();
                Element nextSibling = element.getNextSiblingElement();

                if (prevSibling != null && getStyleInfo(prevSibling) != null
                        && (nextSibling == null || getStyleInfo(nextSibling) == null)) {
                    onItemsAppended(new ArrayList<Element>() {
                        {
                            this.add(element);
                        }
                    });
                } else {
                    onItemsInserted(new ArrayList<Element>() {
                        {
                            this.add(element);
                        }
                    });
                }
                return false;
            }
        }).on("DOMNodeRemoved", new Function() {
            @Override
            public boolean f(Event event) {
                Node node = event.getEventTarget().cast();

                if (node.getNodeType() != Node.ELEMENT_NODE || node.getParentElement() != container) {
                    return false;
                }

                onItemsRemoved();
                return false;
            }
        });
    }
}

From source file:com.sencha.gxt.dnd.core.client.DropTarget.java

License:sencha.com license

private XElement elementFromPoint(XElement element, int x, int y) {
    if (!element.getBounds().contains(x, y)) {
        return null;
    }/* w  w  w. j av a  2s  .  c  om*/
    if (!element.hasChildNodes()) {
        return element;
    }

    NodeList<Node> childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.getItem(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            XElement child = node.cast();
            if (child.getBounds().contains(x, y)) {
                return elementFromPoint(child, x, y);
            }
        }
    }

    // children are not within bounds, return this
    return element;
}

From source file:com.vaadin.client.widgets.JsniWorkaround.java

License:Apache License

/**
 * Returns the widget from a cell node or <code>null</code> if there is no
 * widget in the cell//from  ww w.j a v a 2  s . c o m
 * 
 * @param cellNode
 *            The cell node
 */
static Widget getWidgetFromCell(Node cellNode) {
    Node possibleWidgetNode = cellNode.getFirstChild();
    if (possibleWidgetNode != null && possibleWidgetNode.getNodeType() == Node.ELEMENT_NODE) {
        @SuppressWarnings("deprecation")
        com.google.gwt.user.client.Element castElement = (com.google.gwt.user.client.Element) possibleWidgetNode
                .cast();
        Widget w = WidgetUtil.findWidget(castElement, null);

        // Ensure findWidget did not traverse past the cell element in the
        // DOM hierarchy
        if (cellNode.isOrHasChild(w.getElement())) {
            return w;
        }
    }
    return null;
}

From source file:de.uni_koeln.spinfo.maalr.webapp.ui.user.client.User.java

License:Apache License

private void updateLanguageLinks(Element element) {
    String attribute = null;//from  www .  java  2 s.  c  o  m
    try {
        attribute = element.getAttribute("class");
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    if (attribute != null && attribute.contains("lang_select")) {
        final Anchor anchor = Anchor.wrap(element);
        final String langParam = anchor.getHref().substring(anchor.getHref().lastIndexOf('?'));
        HiJax.hijackAnchor(anchor, new Command() {

            @Override
            public void execute() {
                String url = Window.Location.getPath() + langParam + "#" + History.getToken();
                Window.Location.assign(url);
            }
        });
    } else {
        for (int i = 0; i < element.getChildCount(); i++) {
            Node child = element.getChild(i);
            Element e = child.cast();
            updateLanguageLinks(e);
        }
    }
}

From source file:nz.co.doltech.gwt.sdm.SuperDevCompiler.java

License:Apache License

public Element getDevModeOnScriptElement() {
    HeadElement head = Document.get().getHead();
    NodeList<Node> childNodes = head.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.getItem(i);
        if (Element.is(childNode)) {
            Element child = childNode.cast();
            if (ScriptElement.is(child)) {
                ScriptElement scriptElement = ScriptElement.as(child);
                String scriptSrc = scriptElement.getSrc();
                if (scriptSrc != null && scriptSrc.contains("dev_mode_on.js")) {
                    return child;
                }/*  ww  w.  j a  v  a 2 s  .co  m*/
            }
        }
    }
    return null;
}

From source file:org.eclipse.che.ide.console.OutputConsoleViewImpl.java

License:Open Source License

@Override
public String getText() {
    String text = "";
    NodeList<Node> nodes = consoleLines.getElement().getChildNodes();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.getItem(i);
        Element element = node.cast();
        text += element.getInnerText() + "\r\n";
    }/*from   w  ww. jav  a 2  s  . c  o  m*/

    return text;
}

From source file:org.mklab.taskit.client.ui.cell.SelectCell.java

License:Apache License

private static SelectElement getSelectElement(Element parent) {
    Node child = parent;
    do {/*from  ww w.j  ava2  s . co m*/
        if (child.getNodeName().toLowerCase().equals("select")) //$NON-NLS-1$
            return child.cast();
    } while ((child = child.getFirstChild()) != null);
    return null;
}

From source file:org.nsesa.editor.gwt.core.client.ui.overlay.document.OverlayStrategySupport.java

License:EUPL

public final Element getElementByTag(Element element, final String tag) {
    for (int i = 0; i < element.getChildCount(); i++) {
        final Node node = element.getChild(i);
        if (node.getNodeName().equalsIgnoreCase(tag)) {
            return node.cast();
        }//from w w  w.  j av a2  s  . co m
    }
    return null;
}

From source file:org.nsesa.editor.gwt.core.client.ui.overlay.document.OverlayStrategySupport.java

License:EUPL

public final Element getElementByAttribute(Element element, final String attributeName,
        final String attributeValue) {
    for (int i = 0; i < element.getChildCount(); i++) {
        final Node node = element.getChild(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            final Element el = node.cast();
            if (getAttribute(el, attributeName).equalsIgnoreCase(attributeValue)) {
                return el;
            }//from  w  w  w.  j a  v  a  2 s .com
        }
    }
    return null;
}