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

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

Introduction

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

Prototype

@Override
    public Node insertBefore(Node newChild, Node refChild) 

Source Link

Usage

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

License:Apache License

/**
 * Create a bounding box the size of the text between the two offsets of
 * the given textNode.  Note that this temporarily modifies the document
 * to excise the sub-text into its own span element, which is then used
 * to generate the bounding box./*from  w  w  w  .j  a va  2  s  .c o  m*/
 *
 * @param textNode Text to create bounding box around
 * @param offset1  Starting offset to get bounding box from
 * @param offset2  Ending offset to get bounding box from
 * @return a new bounding box
 */
public static HtmlBBox getBoundingBox(Text textNode, int offset1, int offset2) {
    HtmlBBox res;

    String text = textNode.getData();
    int len = text.length();
    if ((offset1 == 0) && (offset2 == len)) {
        // Shortcut
        return getBoundingBox(textNode);
    }
    if ((offset2 > len) || (offset1 > offset2)) {
        return null;
    }

    // If this is a cursor, we still need to outline one character
    boolean isCursor = (offset1 == offset2);
    boolean posRight = false;
    if (isCursor) {
        if (offset1 == len) {
            offset1--;
            posRight = true;
        } else {
            offset2++;
        }
    }

    // Create 2 or 3 spans of this text, so we can measure
    List<Element> nodes = new ArrayList<Element>(3);
    Element tmpSpan, measureSpan;
    if (offset1 > 0) {
        // First
        tmpSpan = DOM.createSpan();
        tmpSpan.setInnerHTML(text.substring(0, offset1));
        nodes.add(tmpSpan);
    }

    // Middle, the one we measure
    measureSpan = DOM.createSpan();
    measureSpan.setInnerHTML(text.substring(offset1, offset2));
    nodes.add(measureSpan);

    if (offset2 < (len - 1)) {
        // Last
        tmpSpan = DOM.createSpan();
        tmpSpan.setInnerHTML(text.substring(offset2 + 1));
        nodes.add(tmpSpan);
    }

    Node parent = textNode.getParentNode();

    for (Node node : nodes) {
        parent.insertBefore(node, textNode);
    }

    parent.removeChild(textNode);

    if (isCursor) {
        // Just make a 0-width version, depending on left or right
        res = new HtmlBBox(measureSpan.getAbsoluteLeft() + (posRight ? measureSpan.getOffsetWidth() : 0),
                measureSpan.getAbsoluteTop(), 0, measureSpan.getOffsetHeight());
    } else {
        res = new HtmlBBox(measureSpan);
    }

    parent.insertBefore(textNode, nodes.get(0));

    for (Node node : nodes) {
        parent.removeChild(node);
    }

    return res;
}

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

License:Apache License

/**
 * Move all children of this element up into its place, and remove the
 * element.//  w w w  .j  a  va  2s  .co m
 *
 * @param parent element to replace with its children
 */
public static void unSurround(Element parent) {
    Node superParent = parent.getParentNode();
    Node child;
    while ((child = parent.getFirstChild()) != null) {
        parent.removeChild(child);
        superParent.insertBefore(child, parent);
    }
    superParent.removeChild(parent);
}

From source file:com.extjs.gxt.ui.client.widget.ListView.java

License:sencha.com license

protected void onAdd(List<M> models, int index) {
    if (rendered) {
        if (all.getCount() == 0) {
            refresh();/*from  ww  w  .  jav a  2 s  .co  m*/
            return;
        }
        NodeList<Element> nodes = bufferRender(models);
        Element[] elements = Util.toElementArray(nodes);
        all.insert(elements, index);

        Element ref = index == 0 ? all.getElement(elements.length) : all.getElement(index - 1);

        for (int i = elements.length - 1; i >= 0; i--) {
            Node n = ref.getParentNode();
            if (index == 0) {
                n.insertBefore(elements[i], n.getFirstChild());
            } else {
                Node next = ref == null ? null : ref.getNextSibling();
                if (next == null) {
                    n.appendChild(elements[i]);
                } else {
                    n.insertBefore(elements[i], next);
                }
            }
            if (GXT.isAriaEnabled()) {
                elements[i].setId(XDOM.getUniqueId());
            }
        }

        updateIndexes(index, -1);
    }
}

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

License:Apache License

/**
 * Detaches and re-attaches the element from its parent. The element is
 * reattached at the same position in the DOM as it was before.
 * //from  w  w w .  ja  v  a2  s .  c o  m
 * Does nothing if the element is not attached to the DOM.
 * 
 * @param element
 *            The element to detach and re-attach
 */
public static void detachAttach(Element element) {
    if (element == null) {
        return;
    }

    Node nextSibling = element.getNextSibling();
    Node parent = element.getParentNode();
    if (parent == null) {
        return;
    }

    parent.removeChild(element);
    if (nextSibling == null) {
        parent.appendChild(element);
    } else {
        parent.insertBefore(element, nextSibling);
    }

}

From source file:de.catma.ui.client.ui.tagger.editor.TaggerEditor.java

License:Open Source License

private void addTagInstance(SpanFactory spanFactory, Node node, int originalStartOffset,
        int originalEndOffset) {

    // the whole text sequence is within one node

    int startOffset = Math.min(originalStartOffset, originalEndOffset);
    int endOffset = Math.max(originalStartOffset, originalEndOffset);
    String nodeText = node.getNodeValue();
    Node nodeParent = node.getParentNode();

    if (startOffset != 0) { // does the tagged sequence start at the beginning?
        // no, ok so we create a separate text node for the untagged part at the beginning
        Text t = Document.get().createTextNode(nodeText.substring(0, startOffset));
        nodeParent.insertBefore(t, node);
    }//from  ww w  .  j av a 2s  .c  om

    // get a list of tagged spans for every non-whitespace-containing-character-sequence 
    // and text node for the separating whitespace-sequences
    Element taggedSpan = spanFactory.createTaggedSpan(nodeText.substring(startOffset, endOffset));

    // insert tagged spans and whitespace text nodes before the old node
    nodeParent.insertBefore(taggedSpan, node);

    // does the tagged sequence stretch until the end of the whole sequence? 
    if (endOffset != nodeText.length()) {
        // no, so we create a separate text node for the untagged sequence at the end
        Text t = Document.get().createTextNode(nodeText.substring(endOffset, nodeText.length()));
        nodeParent.insertBefore(t, node);
    }

    // remove the old node which is no longer needed
    nodeParent.removeChild(node);
}

From source file:de.catma.ui.client.ui.tagger.editor.TaggerEditor.java

License:Open Source License

private void addTagInstance(SpanFactory spanFactory, Node startNode, int startOffset, Node endNode,
        int endOffset) {

    AffectedNodesFinder tw = new AffectedNodesFinder(getElement(), startNode, endNode);

    String startNodeText = startNode.getNodeValue();
    Node startNodeParent = startNode.getParentNode();
    String endNodeText = endNode.getNodeValue();
    Node endNodeParent = endNode.getParentNode();

    if (endNodeText == null) { // node is a non text node like line breaks
        VConsole.log("Found no text within the following node:");
        DebugUtil.printNode(endNode);/*from w  w  w  .java2s. c  om*/
        endNodeText = "";
    }

    // the range of unmarked text at the beginning of the start node's text range
    int unmarkedStartSeqBeginIdx = 0;
    int unmarkedStartSeqEndIdx = startOffset;

    // the marked text range of the start node
    int markedStartSeqBeginIdx = startOffset;
    int markedStartSeqEndIdx = startNodeText.length();

    // the range of umarked text at the end of the end node's text range
    int unmarkedEndSeqBeginIdx = endOffset;
    int unmarkedEndSeqEndIdx = endNodeText.length();

    // the marked text range of the end node
    int markedEndSeqBeginIdx = 0;
    int markedEndSeqEndIdx = endOffset;

    // if start node and end node are in reverse order within the tree 
    // we switch start/end of sequences accordingly
    if (!tw.isAfter()) {
        unmarkedStartSeqBeginIdx = startOffset;
        unmarkedStartSeqEndIdx = startNodeText.length();
        markedStartSeqBeginIdx = 0;
        markedStartSeqEndIdx = startOffset;

        unmarkedEndSeqBeginIdx = 0;
        unmarkedEndSeqEndIdx = endOffset;
        markedEndSeqBeginIdx = endOffset;
        markedEndSeqEndIdx = endNodeText.length();
    }

    // a text node for the unmarked start
    Text unmarkedStartSeq = Document.get()
            .createTextNode(startNodeText.substring(unmarkedStartSeqBeginIdx, unmarkedStartSeqEndIdx));

    // get a tagged span for the tagged sequence of the starting node
    Element taggedSpan = spanFactory
            .createTaggedSpan(startNodeText.substring(markedStartSeqBeginIdx, markedStartSeqEndIdx));

    if (tw.isAfter()) {
        // insert unmarked text seqence before the old node
        startNodeParent.insertBefore(unmarkedStartSeq, startNode);
        // insert tagged spans before the old node
        startNodeParent.insertBefore(taggedSpan, startNode);
        // remove the old node
        startNodeParent.removeChild(startNode);
    } else {
        // insert tagged sequences before the old node
        startNodeParent.insertBefore(taggedSpan, startNode);
        // replace the old node with a new node for the unmarked sequence
        startNodeParent.replaceChild(unmarkedStartSeq, startNode);
    }

    List<Node> affectedNodes = tw.getAffectedNodes();
    DebugUtil.printNodes("affectedNodes", affectedNodes);

    // create and insert tagged sequences for all the affected text nodes
    for (int i = 1; i < affectedNodes.size() - 1; i++) {
        Node affectedNode = affectedNodes.get(i);
        // create the tagged span ...
        taggedSpan = spanFactory.createTaggedSpan(affectedNode.getNodeValue());

        VConsole.log("affected Node and its taggedSpan:");
        DebugUtil.printNode(affectedNode);
        DebugUtil.printNode(taggedSpan);

        // ... and insert it
        affectedNode.getParentNode().insertBefore(taggedSpan, affectedNode);

        // remove the old node
        affectedNode.getParentNode().removeChild(affectedNode);
    }

    // the unmarked text sequence of the last node
    Text unmarkedEndSeq = Document.get()
            .createTextNode(endNodeText.substring(unmarkedEndSeqBeginIdx, unmarkedEndSeqEndIdx));

    // the tagged part of the last node
    taggedSpan = spanFactory.createTaggedSpan(endNodeText.substring(markedEndSeqBeginIdx, markedEndSeqEndIdx));
    if (tw.isAfter()) {
        // insert tagged part
        endNodeParent.insertBefore(taggedSpan, endNode);

        // replace old node with a text node for the unmarked part
        endNodeParent.replaceChild(unmarkedEndSeq, endNode);

    } else {

        // insert unmarked part
        endNodeParent.insertBefore(unmarkedEndSeq, endNode);

        // insert tagged part
        endNodeParent.insertBefore(taggedSpan, endNode);
        // remove old node
        endNodeParent.removeChild(endNode);
    }
}

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

License:Apache License

private void insertBefore(Node parent, Node child, Node newChild) {
    if (child == null) {
        parent.appendChild(newChild);//w  w w  .  java  2 s  .  c o  m
    } else {
        parent.insertBefore(newChild, child);
    }
}

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

License:Apache License

protected void insertBefore(Node parent, Node child, Node newChild) {
    if (child == null) {
        parent.appendChild(newChild);//w w w.j a v  a  2s. c  om
    } else {
        parent.insertBefore(newChild, child);
    }
}

From source file:org.waveprotocol.wave.client.editor.extract.Repairer.java

License:Apache License

private void reattachImplChildren(Node parentNodelet, Node nodeletBefore, Node nodeletAfter, ContentNode first,
        ContentNode last) {/*from  www  . ja v a2  s. c o  m*/
    // TODO(danilatos): Replace this hairy code with pre-order traversal
    // getters, once they exist
    while (true) {
        Node nodelet = nodeletBefore == null ? strippingView.getFirstChild(parentNodelet)
                : strippingView.getNextSibling(nodeletBefore);

        if (nodelet == null || nodelet == nodeletAfter) {
            break;
        }

        if (nodelet.getParentElement() != null) {
            nodelet.removeFromParent();
        }
    }

    for (ContentNode node = first; node != last; node = renderedView.getNextSibling(node)) {
        parentNodelet.insertBefore(node.getImplNodelet(), nodeletAfter);
    }
}

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

License:Open Source License

/**
 * Clones the node specified by its parent and its descendant, including only the left or right part of the tree
 * whose separator is the path from the given descendant to the parent of the cloned node.
 * // ww  w  .ja  v  a  2s . co m
 * @param parent The parent of the cloned node.
 * @param descendant A descendant of the cloned node.
 * @param offset The offset within the given descendant. It can be either a character index or a child index
 *            depending on the descendant node type.
 * @param left Specifies which subtree to be cloned. Left and right subtrees are delimited by the path from the
 *            given descendant to the parent of the cloned node.
 * @return The clone of the specified subtree.
 */
public Node cloneNode(Node parent, Node descendant, int offset, boolean left) {
    int delta = left ? 0 : 1;
    int index = getNodeIndex(descendant) + delta;
    Node clone = cloneNode(descendant, offset, left);
    Node node = descendant.getParentNode();
    while (node != parent) {
        Node child = clone;
        clone = cloneNode(node, index, left);
        if (left || clone.getFirstChild() == null) {
            clone.appendChild(child);
        } else {
            clone.insertBefore(child, clone.getFirstChild());
        }
        index = getNodeIndex(node) + delta;
        node = node.getParentNode();
    }
    return clone;
}