Class Node<N extends Node<N>>

java.lang.Object
com.vaadin.flow.dom.Node<N>
Type Parameters:
N - The narrowed type of the node
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
Element, ShadowRoot

public abstract class Node<N extends Node<N>> extends Object implements Serializable
Represents a node in the DOM.

Contains methods for updating and querying hierarchical structure.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • Node

      protected Node(StateNode node, ElementStateProvider stateProvider)
      Private constructor for initializing with an existing node and state provider.
      Parameters:
      node - the state node, not null
      stateProvider - the state provider, not null
  • Method Details

    • getNode

      public StateNode getNode()
      Gets the node this element is connected to.

      This method is meant for internal use only.

      Returns:
      the node for this element
    • getStateProvider

      public ElementStateProvider getStateProvider()
      Gets the state provider for this element.

      This method is meant for internal use only.

      Returns:
      the state provider for this element
    • getChildCount

      public int getChildCount()
      Gets the number of child elements.
      Returns:
      the number of child elements
    • getChild

      public Element getChild(int index)
      Returns the child element at the given position.
      Parameters:
      index - the index of the child element to return
      Returns:
      the child element
    • getChildren

      public Stream<Element> getChildren()
      Gets all the children of this element.
      Returns:
      a stream of children
    • appendChild

      public N appendChild(Element... children)
      Adds the given children as the last children of this element.
      Parameters:
      children - the element(s) to add
      Returns:
      this element
    • appendChild

      public N appendChild(Collection<Element> children)
      Adds the given children as the last children of this element.
      Parameters:
      children - the element(s) to add
      Returns:
      this element
    • appendVirtualChild

      public N appendVirtualChild(Element... children)
      Appends the given children as the virtual children of the element.

      The virtual child is not really a child of the DOM element. The client-side counterpart is created in the memory, but it's not attached to the DOM tree. The resulting element is referenced via the server side Element in JS function call as usual.

      Parameters:
      children - the element(s) to add
      Returns:
      this element
    • appendVirtualChild

      public N appendVirtualChild(Collection<Element> children)
      Appends the given children as the virtual children of the element.

      The virtual child is not really a child of the DOM element. The client-side counterpart is created in the memory, but it's not attached to the DOM tree. The resulting element is referenced via the server side Element in JS function call as usual.

      Parameters:
      children - the element(s) to add
      Returns:
      this element
    • removeVirtualChild

      public N removeVirtualChild(Element... children)
      Removes the given children that have been attached as the virtual children of this element.

      The virtual child is not really a child of the DOM element. The client-side counterpart is created in the memory but it's not attached to the DOM tree. The resulting element is referenced via the server side Element in JS function call as usual. *

      Parameters:
      children - the element(s) to remove
      Returns:
      this element
    • removeVirtualChild

      public N removeVirtualChild(Collection<Element> children)
      Removes the given children that have been attached as the virtual children of this element.

      The virtual child is not really a child of the DOM element. The client-side counterpart is created in the memory but it's not attached to the DOM tree. The resulting element is referenced via the server side Element in JS function call as usual. *

      Parameters:
      children - the element(s) to remove
      Returns:
      this element
    • isVirtualChild

      public boolean isVirtualChild()
      Gets whether this element is a virtual child of its parent.
      Returns:
      true if the element has a parent and the element is a virtual child of it, false otherwise.
    • insertChild

      public N insertChild(int index, Element... children)
      Inserts the given child element(s) at the given position.
      Parameters:
      index - the position at which to insert the new child
      children - the child element(s) to insert
      Returns:
      this element
    • insertChild

      public N insertChild(int index, Collection<Element> children)
      Inserts the given child element(s) at the given position.
      Parameters:
      index - the position at which to insert the new child
      children - the child element(s) to insert
      Returns:
      this element
    • indexOfChild

      public int indexOfChild(Element child)
      Returns the index of the specified child in the children list, or -1 if this list does not contain the child.
      Parameters:
      child - the child element
      Returns:
      index of the child or -1 if it's not a child
    • setChild

      public N setChild(int index, Element child)
      Replaces the child at the given position with the given child element.
      Parameters:
      index - the position of the child element to replace
      child - the child element to insert
      Returns:
      this element
    • removeChild

      public N removeChild(Element... children)
      Removes the given child element(s).
      Parameters:
      children - the child element(s) to remove
      Returns:
      this element
    • removeChild

      public N removeChild(Collection<Element> children)
      Removes the given child element(s).
      Parameters:
      children - the child element(s) to remove
      Returns:
      this element
    • removeChild

      public N removeChild(int index)
      Removes the child at the given index.
      Parameters:
      index - the index of the child to remove
      Returns:
      this element
    • removeAllChildren

      public N removeAllChildren()
      Removes all child elements, including elements only present at the client-side.
      Returns:
      this element
    • getParentNode

      public Node getParentNode()
      Gets the parent node.
      Returns:
      the parent node or null if this element does not have a parent
    • getSelf

      protected abstract N getSelf()
      Gets the narrow typed reference to this object.
      Returns:
      this object casted to its type
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • accept

      public N accept(NodeVisitor visitor)
      Applies the visitor for the node.
      Parameters:
      visitor - the visitor to apply to the node
      Returns:
      this element
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • ensureChildHasParent

      protected void ensureChildHasParent(Element child, boolean internalCheck)
      Ensures that the child has the correct parent.

      Default implementation doesn't do anything. Subclasses may override the method to implement their own behavior.

      Parameters:
      child - the element to check for its parent
      internalCheck - whether to use assertions or throw an exception on failure