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

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

Introduction

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

Prototype

@Override
    public Node getLastChild() 

Source Link

Usage

From source file:com.bfr.client.selection.Range.java

License:Apache License

/**
 * Returns the next adjacent text node in the given direction.  Will move
 * down the hierarchy (if traversingUp is not set), then through siblings,
 * then up (but not past topMostNode), looking for the first node
 * <p/>/*from  w  w  w . jav  a 2  s.  co m*/
 * This could be non-statically included in the Node class
 *
 * @param current      An element to start the search from, can be any type
 *                     of node.
 * @param topMostNode  A node that this will traverse no higher than
 * @param forward      whether to search forward or backward
 * @param traversingUp if true, will not look at the children of this element
 * @return the next (previous) text node, or null if no more
 */
public static Text getAdjacentTextElement(Node current, Node topMostNode, boolean forward,
        boolean traversingUp) {
    Text res = null;
    Node node;

    // If traversingUp, then the children have already been processed
    if (!traversingUp) {
        if (current.getChildCount() > 0) {
            node = forward ? current.getFirstChild() : current.getLastChild();

            if (node.getNodeType() == Node.TEXT_NODE) {
                res = (Text) node;
            } else {
                // Depth first traversal, the recursive call deals with
                // siblings
                res = getAdjacentTextElement(node, topMostNode, forward, false);
            }
        }
    }

    if (res == null) {
        node = forward ? current.getNextSibling() : current.getPreviousSibling();
        // Traverse siblings
        if (node != null) {
            if (node.getNodeType() == Node.TEXT_NODE) {
                res = (Text) node;
            } else {
                // Depth first traversal, the recursive call deals with
                // siblings
                res = getAdjacentTextElement(node, topMostNode, forward, false);
            }
        }
    }

    // Go up and over if still not found
    if ((res == null) && (current != topMostNode)) {
        node = current.getParentNode();
        // Stop at document (technically could stop at "html" tag)
        if ((node != null) && (node.getNodeType() != Node.DOCUMENT_NODE)) {
            res = getAdjacentTextElement(node, topMostNode, forward, true);
        }
    }

    return res;
}

From source file:org.chromium.distiller.PageParameterParser.java

License:Open Source License

/**
 * Finds and adds the leaf node(s) closest to the given start node.
 * This recurses and keeps finding and, if necessary, adding the numeric text of valid nodes,
 * collecting the PageParamInfo.PageInfo's for the current adjacency group.
 * For backward search, i.e. nodes before start node, search terminates (i.e. recursion stops)
 * once a text node or anchor is encountered.  If the text node contains numeric text, it's
 * added to the current adjacency group.  Otherwise, a new group is created to break the
 * adjacency.//from   w  w  w .  j  ava  2s . c  om
 * For forward search, i.e. nodes after start node, search continues (i.e. recursion continues)
 * until a text node or anchor with non-numeric text is encountered.  In the process, text nodes
 * and anchors with numeric text are added to the current adjacency group.  When a non-numeric
 * text node or anchor is encountered, a new group is started to break the adjacency, and search
 * ends.
 *
 * @return true to continue search, false to stop.
 *
 * @param start node to work on.
 * @param checkStart true to check start node.  Otherwise, the previous or next sibling of the
 * start node is checked.
 * @param backward true to search backward (i.e. nodes before start node), false to search
 * forward (i.e. nodes after start node).
 * @param baseAnchor created for the current document, only needed for forward search.
 */
private boolean findAndAddClosestValidLeafNodes(Node start, boolean checkStart, boolean backward,
        AnchorElement baseAnchor) {
    Node node = checkStart ? start : (backward ? start.getPreviousSibling() : start.getNextSibling());
    if (node == null) { // No sibling, try parent.
        node = start.getParentNode();
        if (sInvalidParentWrapper == null) {
            sInvalidParentWrapper = RegExp.compile("(BODY)|(HTML)");
        }
        if (sInvalidParentWrapper.test(node.getNodeName()))
            return false;
        return findAndAddClosestValidLeafNodes(node, false, backward, baseAnchor);
    }

    checkStart = false;
    switch (node.getNodeType()) {
    case Node.TEXT_NODE:
        String text = node.getNodeValue();
        // Text must contain words.
        if (text.isEmpty() || StringUtil.countWords(text) == 0)
            break;
        boolean added = addNonLinkTextIfValid(node.getNodeValue());
        // For backward search, we're done regardless if text was added.
        // For forward search, we're done only if text was invalid, otherwise continue.
        if (backward || !added)
            return false;
        break;

    case Node.ELEMENT_NODE:
        Element e = Element.as(node);
        if (e.hasTagName("A")) {
            // For backward search, we're done because we've already processed the anchor.
            if (backward)
                return false;
            // For forward search, we're done only if link was invalid, otherwise continue.
            mNumForwardLinksProcessed++;
            if (!addLinkIfValid(AnchorElement.as(e), baseAnchor))
                return false;
            break;
        }
        // Intentionally fall through.

    default:
        // Check children nodes.
        if (!node.hasChildNodes())
            break;
        checkStart = true; // We want to check the child node.
        if (backward) {
            // Start the backward search with the rightmost child i.e. last and closest to
            // given node.
            node = node.getLastChild();
        } else {
            // Start the forward search with the leftmost child i.e. first and closest to
            // given node.
            node = node.getFirstChild();
        }
        break;
    }

    return findAndAddClosestValidLeafNodes(node, checkStart, backward, baseAnchor);
}

From source file:org.gk.ui.client.com.tree.gkTreePanelIC.java

License:Open Source License

@Override
public void bindEvent() {
    core.subscribe(evtSetBean(), new EventProcess() {

        @Override//from   w ww  . j  a  v  a  2s . co m
        public void execute(String eventId, EventObject eo) {
            setBean(eo.getInfoString());
        }
    });

    // ?
    core.subscribe(evtOnEdit(), new EventProcess() {
        @Override
        public void execute(String eventId, EventObject e) {
            TreeNode tn = (TreeNode) e.getInfoMap()
                    .get((tree.getSelectionModel().getSelectedItem().getProperties().get(PATH) + ""));
            // ??
            final Node nd = tn.getElement().getChild(0);
            // ?inputelement
            final Element input = DOM.createElement("input");
            // ?????span
            final Node childnd = nd.getLastChild();
            // ?span
            final String oldString = childnd.getFirstChild().getNodeValue();
            // ?input?span
            input.setAttribute("value", oldString);
            // inputID??
            input.setAttribute("id", "newText");
            // ?spanelement
            final Node oldNode = childnd.getFirstChild().cloneNode(true);
            // span???input?
            childnd.replaceChild(input, childnd.getFirstChild());
            // ???
            tree.disableTextSelection(false);
            // ??input
            input.focus();

            // ?inputkeydown
            DOM.sinkEvents(input,
                    com.google.gwt.user.client.Event.ONBLUR | com.google.gwt.user.client.Event.ONKEYDOWN
                            | com.google.gwt.user.client.Event.ONMOUSEOVER
                            | com.google.gwt.user.client.Event.ONMOUSEOUT);
            // ?inputenter?
            DOM.setEventListener(input, new EventListener() {
                // ?inputonblur
                boolean mouseIn = true;

                @Override
                public void onBrowserEvent(com.google.gwt.user.client.Event event) {
                    if (event.getTypeInt() == com.google.gwt.user.client.Event.ONMOUSEOUT) {
                        mouseIn = false;
                    }
                    if (event.getTypeInt() == com.google.gwt.user.client.Event.ONMOUSEOVER) {
                        mouseIn = true;
                    }
                    if (event.getTypeInt() == com.google.gwt.user.client.Event.ONBLUR
                            || event.getKeyCode() == KeyCodeEnter) {
                        if (event.getKeyCode() == KeyCodeEnter || mouseIn == false) {
                            // ?input
                            String newString = getvalue();
                            // 
                            if (newString.equals("")) {
                                input.setAttribute("value", oldString);
                            }
                            // span??input
                            oldNode.setNodeValue(newString);
                            // ?spaninput?
                            childnd.replaceChild(oldNode, input);
                            // ?treemodelDataap???
                            setModelData(newString, tree);
                            inEdit = false;
                        }
                    }
                }
            });
        }
    });
}

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

License:Apache License

/**
 * Detaches all children from a node./* w  w w. jav a 2 s .c o  m*/
 *
 * @param node
 *          Must not be null.
 * @return True iff any children were detached by this call.
 */
public static boolean removeAllChildren(final Node node) {
    final boolean hadChildren = node.getLastChild() != null;
    while (node.getLastChild() != null) {
        node.removeChild(node.getLastChild());
    }

    return hadChildren;
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.view.GwtTestTextGrabberVisitor.java

License:Apache License

public void testProcess() {
    createDocument();//www  .ja  va2 s  .c  om
    Node start = DOM.getElementById("thediv");
    assertNotNull(start);
    Node end = start.getLastChild();
    assertNotNull(end);
    assertEquals(Node.TEXT_NODE, end.getNodeType());
    Text text = (Text) end;
    assertEquals(" and other stuff", text.getNodeValue());
    TextGrabberVisitor processor = new TextGrabberVisitor();
    Visitor visitor = new Visitor(processor);
    visitor.process(start, end);
    String result = processor.getText();
    assertNotNull(result);
    assertEquals("The Da Service", result);
}

From source file:org.openelis.ui.widget.tree.Tree.java

License:Open Source License

/**
 * Private method called by all public addRow methods to handle event firing
 * and add the new row to the model.//from   w w w.ja  v a2s .co m
 * 
 * @param index
 *        Index where the new row is to be added.
 * @param row
 *        Will be null if a Tree should create a new blank Row to add
 *        otherwise the passed Row will be added.
 * @return Will return null if this action is canceled by a
 *         BeforeRowAddedHandler, otherwise the newly created Row will be
 *         returned or if a Row is passed to the method it will echoed back.
 */
private Node addNode(int index, Node parent, Node node) {
    assert (node != null);

    ArrayList<Node> children;
    int pos;

    finishEditing();

    if (!fireBeforeNodeAddedEvent(index, parent, node))
        return null;

    unselectAll();

    if (parent.isOpen) {
        if (parent == root) {
            if (index == root.getChildCount())
                pos = getRowCount();
            else
                pos = nodeIndex.get(root.getChildAt(index)).index;

        } else if (parent.getChildCount() == 0)
            pos = nodeIndex.get(parent).index + 1;
        else if (index >= parent.getChildCount())
            pos = nodeIndex.get(parent.getLastChild()).index + 1;
        else
            pos = nodeIndex.get(parent.getChildAt(index)).index;

        parent.add(node, index);

        modelView.add(pos, node);
        nodeIndex.put(node, new NodeIndex(pos));

        pos++;
        children = getDisplayedChildren(node);
        for (int i = 0; i < children.size(); i++) {
            modelView.add(pos + i, children.get(i));
            nodeIndex.put(children.get(i), new NodeIndex(pos + i));
        }

        adjustNodeIndexes(pos + children.size(), children.size() + 1);

        view.addNodes(pos - 1, pos - 1 + children.size());
    } else
        parent.add(node, index);

    fireRowAddedEvent(index, parent, node);

    return node;
}

From source file:org.waveprotocol.wave.client.clipboard.PasteBufferImplSafari.java

License:Apache License

private void maybeStripMarker(Node node, Element parent, boolean leading) {
    if (node == null) {
        logEndNotFound("node is null");
        return;/*from  w ww  .j  a v  a 2 s  .c  o  m*/
    }
    if (DomHelper.isTextNode(node)) {
        Text textNode = node.cast();
        String text = textNode.getData();
        if (!text.isEmpty()) {
            if (leading) {
                if (text.charAt(0) == MARKER_CHAR) {
                    textNode.setData(text.substring(1));
                }
            } else {
                if (text.charAt(text.length() - 1) == MARKER_CHAR) {
                    textNode.setData(text.substring(0, text.length() - 1));
                } else {
                    logEndNotFound("last character is not marker");
                }
            }
        } else {
            logEndNotFound("text node is empty");
        }
        if (textNode.getData().isEmpty()) {
            parent.removeChild(textNode);
        }
    } else {
        // In some cases, Safari will put the marker inside of a div, so this
        // traverses down the left or right side of the tree to find it.
        // For example: x<div><span>pasted</span>x</div>
        maybeStripMarker(leading ? node.getFirstChild() : node.getLastChild(), node.<Element>cast(), leading);
    }
}

From source file:org.waveprotocol.wave.client.editor.impl.HtmlViewImpl.java

License:Apache License

/** {@inheritDoc} */
public Node getLastChild(Node node) {
    return node.getLastChild();
}

From source file:org.waveprotocol.wave.client.editor.impl.StrippingHtmlView.java

License:Apache License

/** {@inheritDoc} */
@Override/*  ww  w  .j  ava 2s  . c  o m*/
public Node getLastChild(Node node) {
    Node n;
    while (maybeStrip(n = node.getLastChild())) {
    }
    return n;
}

From source file:org.xwiki.gwt.dom.client.DOMUtils.java

License:Open Source License

/**
 * @param node the root of the DOM subtree whose last leaf is returned.
 * @return the last leaf node of the DOM subtree whose root is the given node.
 *//* w w  w .  j  a va2 s  .c o m*/
public Node getLastLeaf(Node node) {
    Node descendant = node;
    while (descendant.hasChildNodes()) {
        descendant = descendant.getLastChild();
    }
    return descendant;
}