Example usage for com.google.gwt.dom.client Text getParentNode

List of usage examples for com.google.gwt.dom.client Text getParentNode

Introduction

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

Prototype

@Override
    public Node getParentNode() 

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  v a 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:org.nuxeo.ecm.platform.annotations.gwt.client.view.decorator.AnnoteaDecoratorVisitor.java

License:Apache License

private void processNode(Node node) {
    if (!(node.getNodeType() == Node.TEXT_NODE)) {
        if (node.getNodeName().equalsIgnoreCase("td")) {
            textToAnnotate -= 1;//from  w w  w  .java2 s  .c o m
        }
        return;
    }
    Text text = (Text) node;
    Node parent = text.getParentNode();
    String data = text.getData();
    processDecoratedNode(node, data, parent);
    node.getParentNode().removeChild(node);
}

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

License:Apache License

private void processToFirstNode(Node node) {
    if (!(node.getNodeType() == Node.TEXT_NODE)) {
        return;/*from  ww w .ja  va 2s.c om*/
    }
    Text text = (Text) node;
    String data = text.getData();
    if (data.length() < offset) {
        offset -= data.length();
        return;
    }
    decorating = true;
    Node parent = text.getParentNode();
    if (data.endsWith(" ")) {
        lastCharIsSpace = true;
    }
    String notInData = data.substring(0, offset);
    text.setData(notInData);
    processDecoratedNode(node, data.substring(offset), parent);
}