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

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

Introduction

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

Prototype

@Override
    public Element getParentElement() 

Source Link

Usage

From source file:cc.alcina.framework.gwt.client.util.WidgetUtils.java

License:Apache License

public static Element getElementForPositioning0(Element from) {
    assert tempPositioningText == null;
    if (!isVisibleAncestorChain(from)) {
        return null;
    }// w w  w.j  av a  2s . c o  m
    boolean hidden = isZeroOffsetDims(from);
    int kidCount = from.getChildCount();
    if (kidCount != 0 && !hidden) {
        return from;
    }
    Node parent = from.getParentNode();
    if (parent != null && parent.getFirstChild() == from && parent.getNodeType() == Node.ELEMENT_NODE
            && !isZeroOffsetDims((Element) parent)) {
        return (Element) parent;
    }
    ClientNodeIterator itr = new ClientNodeIterator(from, ClientNodeIterator.SHOW_ALL);
    Element fromContainingBlock = DomUtils.getContainingBlock(from);
    Node node = from;
    int insertTextIfOffsetMoreThanXChars = 100;
    while ((node = node.getPreviousSibling()) != null) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            insertTextIfOffsetMoreThanXChars -= TextUtils.normalizeWhitespaceAndTrim(node.getNodeValue())
                    .length();
            if (insertTextIfOffsetMoreThanXChars < 0) {
                // this causes a relayout - so we try and avoid. most of the
                // time, positioning elements will contain text (or be from
                // a friendly browser), or be at the start of a block elt)
                tempPositioningText = Document.get().createTextNode("---");
                from.appendChild(tempPositioningText);
                return from;
            }
        }
    }
    // give up after 50 node iterations (big tables maybe)
    int max = 50;
    while ((node = itr.nextNode()) != null && max-- > 0) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (!isZeroOffsetDims(node.getParentElement()) && node.getNodeName().equalsIgnoreCase("img")) {
                return (Element) node;
            }
            if (!UIObject.isVisible((Element) node)) {
                itr.skipChildren();
            }
        } else {
            // text
            if (!isZeroOffsetDims(node.getParentElement())
                    // we don't want the combined ancestor of everyone...
                    && (!node.getParentElement().isOrHasChild(from) ||
                    // but we do want <p><a><b>*some-text*</b></p>
                            DomUtils.getContainingBlock(node) == fromContainingBlock)) {
                return node.getParentElement();
            }
        }
    }
    return from.getParentElement();
}

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 a v  a2 s . c o  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.bfr.client.selection.HtmlBBox.java

License:Apache License

/**
 * Move a node inside of a parent element, maintaining it within the DOM
 *
 * @param toChild   Node to make into a child
 * @param newParent Element to make into a parent in its place
 *//*from  ww w .ja  v a  2  s. co  m*/
public static void surround(Node toChild, Element newParent) {
    toChild.getParentElement().insertBefore(newParent, toChild);
    newParent.appendChild(toChild);
}

From source file:com.cgxlib.xq.client.impl.SelectorEngine.java

License:Apache License

public NodeList<Element> filter(NodeList<Element> nodes, String selector, boolean filterDetached) {
    JsNodeArray res = JsNodeArray.create();
    if (selector.isEmpty()) {
        return res;
    }//  ww w  .  j  a v a  2 s . co m
    Element ghostParent = null;
    HashSet<Node> parents = new HashSet<Node>();
    HashSet<Node> elmList = new HashSet<Node>();
    for (int i = 0, l = nodes.getLength(); i < l; i++) {
        Node e = nodes.getItem(i);
        if (e == window || e == document || e.getNodeName() == null
                || "html".equalsIgnoreCase(e.getNodeName())) {
            continue;
        }
        elmList.add(e);
        if (filterDetached) {
            Element p = e.getParentElement();
            if (p == null) {
                if (ghostParent == null) {
                    ghostParent = Document.get().createDivElement();
                    parents.add(ghostParent);
                }
                p = ghostParent;
                p.appendChild(e);
            } else if (!parents.contains(p)) {
                parents.add(p);
            }
        } else if (parents.isEmpty()) {
            parents.add(document);
        }
    }
    for (Node e : parents) {
        NodeList<Element> n = select(selector, e);
        for (int i = 0, l = n.getLength(); i < l; i++) {
            Element el = n.getItem(i);
            if (elmList.remove(el)) {
                res.addNode(el);
            }
        }
    }
    if (ghostParent != null) {
        ghostParent.setInnerHTML(null);
    }
    return res;
}

From source file:com.sencha.gxt.chart.client.draw.engine.SVG.java

License:sencha.com license

/**
 * Insert or move a given {@link Sprite}'s element to the correct place in the DOM list for its zIndex.
 *
 * This should only need to be run once per set of changes, provided sprites are only rendered *after* all
 * z-index changes.//from w  w w. j  a va  2 s  .com
 * 
 * @param sprite - the {@link Sprite} to insert into the dom
 */
private void applyZIndex(Sprite sprite) {
    int index = sprites.indexOf(sprite);
    int zIndex = sprite.getZIndex();
    int leftZIndex = Integer.MIN_VALUE;
    int rightZIndex = Integer.MAX_VALUE;
    if (index >= 1) {
        leftZIndex = sprites.get(index - 1).getZIndex();
    }
    if (index < sprites.size() - 1) {
        rightZIndex = sprites.get(index + 1).getZIndex();
    }

    if (leftZIndex > zIndex || rightZIndex < zIndex) {
        // if it doesnt match either the item on the right or the left, something may be out of order, rebuild it all
        Collections.sort(sprites, zIndexComparator());

        for (int i = 0; i < sprites.size(); i++) {
            XElement elt = getElement(sprites.get(i));
            if (elt != null) {
                getSurfaceElement().appendChild(elt);
            }
        }
    } else {
        // if no reordering is needed this pass but we have a detached element, insert it into the correct position
        XElement elt = getElement(sprite);
        if (elt != null && elt.getParentElement() == null) {
            // need to insert in the correct position in the dom, three possible cases, the end, the beginning, and the middle
            if (index == sprites.size() - 1) {// fast case, last element
                getSurfaceElement().appendChild(elt);
            } else if (index == 0) {// base case, first element
                getSurfaceElement().insertChild(elt, 2);
            } else {// otherwise, make sure prev element was inserted, then insert right after it
                Sprite neighborSprite = sprites.get(index - 1);
                Node prevNeighbor = getElement(neighborSprite);
                if (prevNeighbor == null) {
                    // Force element creation - this will almost certainly invoke this method recursively, and will eventually
                    // insert at beginning, then other nodes in the stack will be appended after it.
                    // This is not presently a concern for three main reasons: first, this is how the code has worked for a long
                    // time, typically items will be drawn in order anyway, and typically items are inserted in small batches
                    // when sprite.redraw() is used individually.
                    neighborSprite.redraw();
                    prevNeighbor = getElement(neighborSprite);
                    assert prevNeighbor != null;
                }
                assert prevNeighbor.getParentElement() == getSurfaceElement() : prevNeighbor.getParentElement();
                getSurfaceElement().insertAfter(elt, prevNeighbor);
            }
        }
    }

}

From source file:com.vaadin.client.ui.dd.DDEventHandleStrategy.java

License:Apache License

/**
 * Get target element for {@code event}.
 * /*from   w w  w  .jav  a2s . c o m*/
 * @param event
 *            GWT event to find target
 * @param mediator
 *            VDragAndDropManager data accessor
 * @return target element for {@code event}
 */
public Element getTargetElement(NativePreviewEvent event, DDManagerMediator mediator) {
    NativeEvent gwtEvent = event.getNativeEvent();
    Element targetElement;
    if (WidgetUtil.isTouchEvent(gwtEvent) || mediator.getManager().getDragElement() != null) {
        int x = WidgetUtil.getTouchOrMouseClientX(gwtEvent);
        int y = WidgetUtil.getTouchOrMouseClientY(gwtEvent);
        // Util.browserDebugger();
        targetElement = WidgetUtil.getElementFromPoint(x, y);
    } else {
        Node targetNode = Node.as(gwtEvent.getEventTarget());

        if (Element.is(targetNode)) {
            targetElement = Element.as(targetNode);
        } else {
            targetElement = targetNode.getParentElement();
        }
    }
    return targetElement;
}

From source file:jetbrains.jetpad.cell.toDom.BaseCellMapper.java

License:Apache License

List<Node> divWrappedElementChildren(final Element e) {
    return new AbstractList<Node>() {
        @Override/*  w w w . jav  a 2s . c o  m*/
        public Node get(int index) {
            return e.getChild(index).getFirstChild();
        }

        @Override
        public Node set(int index, Node element) {
            if (element.getParentElement() != null) {
                throw new IllegalStateException();
            }

            Element wrapperDiv = DOM.createDiv();
            wrapperDiv.appendChild(element);

            Node child = e.getChild(index);
            e.replaceChild(child, wrapperDiv);
            return child;
        }

        @Override
        public void add(int index, Node element) {
            if (element.getParentElement() != null) {
                throw new IllegalStateException();
            }

            Element wrapperDiv = DOM.createDiv();
            wrapperDiv.appendChild(element);
            if (index == 0) {
                e.insertFirst(wrapperDiv);
            } else {
                Node prev = e.getChild(index - 1);
                e.insertAfter(wrapperDiv, prev);
            }
        }

        @Override
        public Node remove(int index) {
            Element childWrapper = (Element) e.getChild(index);
            get(index).removeFromParent();
            e.removeChild(childWrapper);
            return childWrapper;
        }

        @Override
        public int size() {
            return e.getChildCount();
        }
    };
}

From source file:jetbrains.jetpad.cell.toDom.IndentRootCellMapper.java

License:Apache License

IndentRootCellMapper(IndentCell source, CellToDomContext ctx) {
    super(source, ctx, DOM.createDiv());
    myCellMappers = createChildSet();//from w  ww.ja va  2  s. co  m

    myIndentUpdater = new IndentUpdater<Node>(getSource(), getTarget(), new IndentUpdaterTarget<Node>() {
        @Override
        public Element newLine() {
            Element result = DOM.createDiv();
            result.addClassName(CellContainerToDomMapper.CSS.horizontal());
            return result;
        }

        @Override
        public Element newIndent(int size) {
            Element result = DOM.createDiv();
            DomTextEditor editor = new DomTextEditor(result);
            editor.setText(Strings.repeat("  ", size));
            return result;
        }

        @Override
        public CellWrapper<Node> wrap(final Cell cell) {
            final BaseCellMapper<? extends Cell> mapper = getContext().apply(cell);
            CounterUtil.updateOnAdd(getSource(), cell, mapper);
            mapper.setAncestorBackground(AncestorUtil.getAncestorBackground(getSource(), cell));

            myCellMappers.add(mapper);

            return new CellWrapper<Node>() {
                @Override
                public Node item() {
                    return mapper.getTarget();
                }

                @Override
                public void remove() {
                    CounterUtil.updateOnRemove(getSource(), cell, mapper);
                    myCellMappers.remove(mapper);
                }
            };
        }

        @Override
        public List<Node> children(Node item) {
            if (item instanceof Element) {
                return divWrappedElementChildren((Element) item);
            } else {
                throw new IllegalStateException();
            }
        }

        @Override
        public Element parent(Node item) {
            if (item instanceof Element) {
                return item.getParentElement().getParentElement();
            } else {
                throw new IllegalStateException();
            }
        }
    }) {
        @Override
        protected void onVisibilityChanged(Cell cell, PropertyChangeEvent<Boolean> event) {
            myIndentUpdater.visibilityChanged(cell, event);
        }
    };
}

From source file:jetbrains.jetpad.mapper.gwt.DomUtil.java

License:Apache License

public static List<Node> nodeChildren(final Node n) {
    return new AbstractList<Node>() {
        @Override/* ww w.j  a v a2s. c om*/
        public Node get(int index) {
            return n.getChild(index);
        }

        @Override
        public Node set(int index, Node element) {
            if (element.getParentElement() != null) {
                throw new IllegalStateException();
            }

            Node child = get(index);
            n.replaceChild(child, element);
            return child;
        }

        @Override
        public void add(int index, Node element) {
            if (element.getParentElement() != null) {
                throw new IllegalStateException();
            }

            if (index == 0) {
                n.insertFirst(element);
            } else {
                Node prev = n.getChild(index - 1);
                n.insertAfter(element, prev);
            }
        }

        @Override
        public Node remove(int index) {
            Node child = n.getChild(index);
            n.removeChild(child);
            return child;
        }

        @Override
        public int size() {
            return n.getChildCount();
        }
    };
}

From source file:org.semanticsoft.vaaclipse.widgets.client.ui.stackwidget.VStackWidget.java

License:Open Source License

private List<Node> findPathToParent(Element element, Element parentElement) {
    List<Node> pathToParent = new ArrayList<Node>();
    Node parent = element.getParentElement();
    while (parent != null && parent != parentElement) {
        pathToParent.add(parent);/*from   w  w w  . j  a  v  a  2 s  .c  om*/
        parent = parent.getParentElement();
    }
    if (parent != null)
        pathToParent.add(parent);
    else
        pathToParent.clear();
    return pathToParent;
}