Example usage for com.google.gwt.dom.client NodeList getLength

List of usage examples for com.google.gwt.dom.client NodeList getLength

Introduction

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

Prototype

public int getLength() 

Source Link

Usage

From source file:org.jboss.errai.common.client.dom.DOMUtil.java

License:Apache License

/**
 * @param element/*from w  ww . j  av  a 2s .c om*/
 *          Must not be null.
 * @return If the given element has any child elements, return an optional containing the last child element.
 *         Otherwise return an empty optional.
 */
public static Optional<Element> getLastChildElement(final Element element) {
    final NodeList children = element.getChildNodes();
    for (int i = children.getLength() - 1; i > -1; i--) {
        if (isElement(children.item(i))) {
            return Optional.ofNullable((Element) children.item(i));
        }
    }

    return Optional.empty();
}

From source file:org.jboss.errai.common.client.dom.DOMUtil.java

License:Apache License

/**
 * @param nodeList/*from   w w w .  j  a  v a  2 s  . co m*/
 *          Must not be null.
 * @return An iterator for the given node list.
 */
public static Iterator<Node> nodeIterator(final NodeList nodeList) {
    return new Iterator<Node>() {
        int index = 0;

        @Override
        public boolean hasNext() {
            return index < nodeList.getLength();
        }

        @Override
        public Node next() {
            if (hasNext()) {
                return nodeList.item(index++);
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.jboss.errai.common.client.dom.DOMUtil.java

License:Apache License

/**
 * @param nodeList//from  w ww .  j a v  a  2 s .  co m
 *          Must not be null.
 * @return An iterator for the given node list that ignores non-element nodes.
 */
public static Iterator<Element> elementIterator(final NodeList nodeList) {
    return new Iterator<Element>() {

        int i = 0;

        @Override
        public boolean hasNext() {
            while (i < nodeList.getLength() && !isElement(nodeList.item(i))) {
                i++;
            }
            return i < nodeList.getLength();
        }

        @Override
        public Element next() {
            if (hasNext()) {
                return (Element) nodeList.item(i++);
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.kaaproject.avro.ui.gwt.client.widget.ExtendedValueListBox.java

License:Apache License

private void updateOptionsStyle() {
    if (Utils.isNotBlank(promptText)) {
        SelectElement select = getSelectElement();
        int index = select.getSelectedIndex();
        if (index > -1) {
            OptionElement selectedOption = getOptionElement(index);
            if (Utils.isBlank(selectedOption.getValue())) {
                selectedOption.setClassName(style.prompt());
                selectedOption.setText(promptText);
                addStyleName(style.prompt());
                NodeList<OptionElement> options = getSelectElement().getOptions();
                for (int i = 0; i < options.getLength(); i++) {
                    if (index != i) {
                        OptionElement option = options.getItem(i);
                        option.setClassName(style.noPrompt());
                    }/*from  www.j a v a2  s. co m*/
                }
            } else {
                NodeList<OptionElement> options = getSelectElement().getOptions();
                for (int i = 0; i < options.getLength(); i++) {
                    OptionElement option = options.getItem(i);
                    if (Utils.isBlank(option.getValue())) {
                        option.setClassName("");
                        option.setText("");
                    }
                }
                removeStyleName(style.prompt());
            }
        }
    }
}

From source file:org.kaaproject.avro.ui.gwt.client.widget.grid.AvroUiDataGrid.java

License:Apache License

@Override
protected boolean resetFocusOnCell() {
    boolean focused = false;
    if (hasFilterHeaders() && filterFocusedCellColumn > -1 && filterFocusedCellRow > -1) {
        TableSectionElement thead = getTableHeadElement();
        NodeList<TableRowElement> rows = thead.getRows();
        if (filterFocusedCellRow < rows.getLength()) {
            TableRowElement row = rows.getItem(filterFocusedCellRow);
            NodeList<TableCellElement> cells = row.getCells();
            if (filterFocusedCellColumn < cells.getLength()) {
                TableCellElement cell = cells.getItem(filterFocusedCellColumn);
                if (getHeaderBuilder().isHeader(cell)) {
                    Header<?> header = getHeaderBuilder().getHeader(cell);
                    Context context = new Context(0, 0, header.getKey());
                    focused = resetFocusOnFilterCellImpl(context, header, cell);
                }/*from   w  w w .ja  va 2  s. c  o m*/
            }
        }
    }
    if (!focused) {
        focused = super.resetFocusOnCell();
    }
    return focused;
}

From source file:org.kuali.student.common.ui.client.widgets.KSLightBox.java

License:Educational Community License

private void grabFocus() {
    Widget mainContent = contentPanel.getWidget();
    NodeList<Element> nodeList = mainContent.getElement().getElementsByTagName("*");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element e = nodeList.getItem(i);
        if (FOCUSABLE_TAGS.contains(e.getTagName().toUpperCase())) {
            e.focus();// ww  w.java 2 s. com
            return;
        }
    }

}

From source file:org.lightframe.components.client.ui.VWindowManager.java

License:Apache License

public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
    // This call should be made first. Ensure correct implementation,
    // and let the containing layout manage caption, etc.
    if (client.updateComponent(this, uidl, true)) {
        return;/*from w w  w .  j a va 2  s. c  o m*/
    }

    // Save reference to server connection object to be able to send
    // user interaction later
    this.client = client;

    // Save the UIDL identifier for the component
    uidlId = uidl.getId();

    if (uidl.hasAttribute(ATTRIBUTE_SC_WINDOW_NAMES)) {
        // ugly way to clear all elements.
        getElement().setInnerHTML("");

        final String[] names = uidl.getStringArrayAttribute(ATTRIBUTE_SC_WINDOW_NAMES);
        for (final String name : names) {
            final Element windowBox = DOM.createDiv();

            windowBox.setInnerHTML(name);
            windowBox.setClassName(WINDOWBOX_CLASSNAME);
            DOM.sinkEvents(windowBox, Event.ONCLICK | Event.ONCONTEXTMENU);

            DOM.appendChild(getElement(), windowBox);
        }

        windowCount = names.length;

    }

    if (uidl.hasAttribute(ATTRIBUTE_SC_WINDOW_MINIMZED)) {
        final int[] minimized = uidl.getIntArrayAttribute(ATTRIBUTE_SC_WINDOW_MINIMZED);
        final NodeList<Node> windowBoxes = getElement().getChildNodes();

        for (int i = 0; i < windowBoxes.getLength(); i++) {
            final Element windowBox = (Element) windowBoxes.getItem(i);
            windowBox.setClassName(minimized[i] == 1 ? WINDOWBOX_CLASSNAME_MINIMIZED : WINDOWBOX_CLASSNAME);
        }
    }
}

From source file:org.lightframe.components.client.ui.VWindowManager.java

License:Apache License

private int getWindowBoxIndex(Element windowBox) {
    final NodeList<Node> siblings = windowBox.getParentNode().getChildNodes();

    for (int i = 0; i < siblings.getLength(); i++) {
        final Node sibling = siblings.getItem(i);
        if (sibling.equals(windowBox)) {
            return i;
        }//from ww  w .j  a  v a2  s  . co  m
    }

    return -1;
}

From source file:org.nsesa.editor.gwt.core.client.ui.document.sourcefile.content.ContentController.java

License:EUPL

/**
 * Get the root element node(s) that is/are set on the
 * {@link org.nsesa.editor.gwt.core.client.ui.document.sourcefile.content.ContentView#getContentElement()}
 *
 * @return the root element//from  w w  w . j a  va2s.  c o  m
 */
public Element[] getContentElements() {
    if (!contentLoaded) {
        throw new RuntimeException("Content not yet available.");
    }
    final List<Element> elements = new ArrayList<Element>();
    final NodeList<Node> childNodes = view.getContentElement().getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.getItem(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            elements.add(Element.as(node));
        }
    }
    return elements.toArray(new Element[elements.size()]);
}

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

License:EUPL

/**
 * This method is in facto responsible with the xml generation. For each child of the given
 * overlay widget ({@link org.nsesa.editor.gwt.core.client.ui.overlay.document.OverlayWidget#getChildOverlayWidgets()})
 * it tries to find out the corresponding browser element node and to extract from there the text content and the
 * css attributes values. The process continue then recursively for all descendants of the original overlay widget.
 *
 * @param widget     The overlay widget that will be processed
 * @param namespaces The map of all namespaces used in overlay widget and its descendants
 * @param rootNode   True if the/*  w w  w.j  a va2 s  . c o m*/
 * @param depth
 * @return Xml representation of overlay widget as String
 */
public String toXMLElement(final OverlayWidget widget, final Map<String, String> namespaces,
        final boolean rootNode, int depth) {
    final StringBuilder sb = new StringBuilder();
    final String indent = withIndentation ? TextUtils.repeat(depth, "  ") : "";
    sb.append(indent).append("<");
    if (rootNode) {
        sb.append(widget.getType());
        for (final Map.Entry<String, String> entry : namespaces.entrySet()) {
            if (DEFAULT_NAMESPACE.equals(entry.getValue())) {
                // this is the default namespace
                sb.append(" xmlns=\"").append(widget.getNamespaceURI()).append("\"");
            } else {
                // prefixed namespace
                sb.append(" xmlns:").append(entry.getValue()).append("=\"").append(entry.getKey()).append("\"");
            }
        }
    } else {
        final String prefix = namespaces.get(widget.getNamespaceURI());
        if (!DEFAULT_NAMESPACE.equals(prefix)) {
            sb.append(prefix).append(":");
        }
        sb.append(widget.getType());
    }
    //get the attributes
    final LinkedHashMap<String, String> attrs = widget.getAttributes();
    if (!attrs.isEmpty()) {
        for (final Map.Entry<String, String> entry : attrs.entrySet()) {
            // do not add the class attribute
            if (!"class".equalsIgnoreCase(entry.getKey())) {
                if (entry.getValue() != null && entry.getValue().length() > 0) {
                    sb.append(" ").append(entry.getKey()).append("=").append("\"").append(entry.getValue())
                            .append("\"");
                }
            }
        }
    }
    sb.append(">");
    Element element = widget.getOverlayElement();
    NodeList<Node> nodes = element.getChildNodes();
    int length = nodes.getLength();
    if (length == 0) {
        // the root is all the time a new one
        // apply xml transformation for children
        for (final OverlayWidget child : widget.getChildOverlayWidgets()) {
            sb.append(toXMLElement(child, namespaces, false, depth + 1).trim());
        }
    } else {
        for (int i = 0; i < length; i++) {
            final short nodeType = nodes.getItem(i).getNodeType();
            Element childElement = nodes.getItem(i).cast();
            switch (nodeType) {
            case Node.ELEMENT_NODE:
                // get the amendable widget corresponding to this child and apply xml transformation
                // hopefully there is one amendable widget linked to this node
                OverlayWidget child = null;
                // try to find out a amendable widget linked to childElement
                for (OverlayWidget aw : widget.getChildOverlayWidgets()) {
                    if (aw.getOverlayElement().equals(childElement)) {
                        child = aw;
                        break;
                    }
                }
                if (child != null) {
                    sb.append(toXMLElement(child, namespaces, false, depth + 1));
                } else {
                    LOG.warning("No amendable child widget found for element " + childElement.getInnerHTML());
                }
                break;
            case Node.TEXT_NODE:
                sb.append(TextUtils.escapeXML(nodes.getItem(i).getNodeValue()));
                //sb.append(nodes.getItem(i).getNodeValue().trim());
                break;
            case Node.DOCUMENT_NODE:
                LOG.log(Level.WARNING, "There should be no document node here for " + element.getInnerHTML());
                break;
            }
        }
    }
    sb.append("</");
    final String prefix = namespaces.get(widget.getNamespaceURI());
    if (!DEFAULT_NAMESPACE.equals(prefix)) {
        sb.append(prefix).append(":");
    }
    sb.append(widget.getType()).append(">");
    return sb.toString();
}