Class ContainerViewElement

java.lang.Object
com.foreach.across.modules.web.ui.elements.ContainerViewElement
All Implemented Interfaces:
MutableViewElement, ViewElement
Direct Known Subclasses:
AbstractNodeViewElement

public class ContainerViewElement extends Object implements MutableViewElement
A container is a named collection of elements (getChildren() that allows configuration of a custom template for rendering. This is a base class for every other ViewElement that supports children.

Unless a custom template is being used, a collection does not add additional output but simply renders its children in order.

Complex operations on containers (including on children that are in turn containers) can easily be done using the ContainerViewElementUtils.

Author:
Arne Vandamme
See Also:
  • Field Details

  • Constructor Details

    • ContainerViewElement

      public ContainerViewElement()
    • ContainerViewElement

      public ContainerViewElement(String name)
  • Method Details

    • elementStream

      public Stream<ViewElement> elementStream()
      Get the stream of all ViewElement instances this container represents (excluding itself). All these elements can be found using any of the find(String) methods.

      By default this is the same collection as the direct children, but that is not a requirement.

      WARNING: returning a different collection then the direct children will impact modification methods. Elements can be found but not replaced or removed if they are not a part of the children collection.

      Returns:
      stream
    • getChildren

      public List<ViewElement> getChildren()
      Get the list of direct children this container represents.
      Returns:
      list of child elements this container represents
    • addChild

      public ContainerViewElement addChild(@NonNull @NonNull ViewElement element)
      Add a child to this container.
      Parameters:
      element - to add
    • addChildren

      public ContainerViewElement addChildren(@NonNull @NonNull Collection<? extends ViewElement> elements)
      Add a collection of children to this container.
      Parameters:
      elements - to add
    • addFirstChild

      public ContainerViewElement addFirstChild(@NonNull @NonNull ViewElement element)
      Adds a child as the first one to this container.
      Parameters:
      element - to add
    • removeChild

      public boolean removeChild(ViewElement element)
      Remove a direct child from this container.
      Parameters:
      element - to remove
      Returns:
      true if child was present and has been removed
      See Also:
    • hasChildren

      public boolean hasChildren()
      Returns:
      true if this container has child elements
    • clearChildren

      public ContainerViewElement clearChildren()
      Remove all children from this container.
    • apply

      @Deprecated public ContainerViewElement apply(@NonNull @NonNull Consumer<ContainerViewElement> consumer)
      Deprecated.
      Apply a consumer to this container. Useful for providing extensions that wish to provide actions on the container itself.
      Parameters:
      consumer - to execute
    • applyUnsafe

      public <U extends ViewElement> ContainerViewElement applyUnsafe(@NonNull @NonNull Consumer<U> consumer)
      Deprecated.
      Apply a consumer to this container. Useful for providing extensions that wish to provide actions on the container itself.

      This method allows you to pass any Consumer for a generic ViewElement. Note that if you pass a consumer for a type that is not compatible with ContainerViewElement, you will receive class cast exceptions at runtime, hence the unsafe.

      Parameters:
      consumer - to execute
    • set

      Description copied from interface: ViewElement
      Execute one or more setter functions.
      Specified by:
      set in interface MutableViewElement
      Specified by:
      set in interface ViewElement
      Parameters:
      setters - to execute
      Returns:
      self
    • remove

      public ContainerViewElement remove(ViewElement.WitherRemover... functions)
      Description copied from interface: ViewElement
      Execute one or more remover functions. A remover is defined as a separate interface so implementations can both implement setter and remover at the same time.
      Specified by:
      remove in interface MutableViewElement
      Specified by:
      remove in interface ViewElement
      Parameters:
      functions - to execute
      Returns:
      self
    • find

      public Optional<ViewElement> find(String elementName)
      Find the first child element with the given name in the container. Will recursive through all children that are also ContainerViewElement implementations. Search will be top-down and the first matching element will be returned.
      Returns:
      element if found
      See Also:
    • findOrSelf

      public Optional<ViewElement> findOrSelf(String name)
      Find the first element with the given name in the container. Will first check the container itself and return it if it matches. If not, will check all children and will recursive through all children that are also ContainerViewElement implementations. Search will be top-down and the first matching element will be returned.
      Returns:
      element if found
    • find

      public <V extends ViewElement> Optional<V> find(String elementName, Class<V> requiredType)
      Find the first child element with the given name that is also of the required type. Will recursive through all children that are also ContainerViewElement implementations. Search will be top-down and the first matching element will be returned. If no element has that name or it is not of the required type, Optional.empty() will be returned.
      Returns:
      element if found
      See Also:
    • findOrSelf

      public <V extends ViewElement> Optional<V> findOrSelf(String elementName, Class<V> requiredType)
      Find the first element with the given name that is also of the required type. Will first check the container itself and return it if it matches. If not, will check all children and will recursive through all children that are also ContainerViewElement implementations. Search will be top-down and the first matching element will be returned.
      Returns:
      element if found
    • flatStream

      public Stream<ViewElement> flatStream()
      Creates a flattened stream of all elements in the container. Will recurse top-down through all children that are also ContainerViewElement implementations.
      Returns:
      flattened stream
    • findAll

      public <V extends ViewElement> Stream<V> findAll(Class<V> requiredType)
      Find all elements in the container that are of the required type. Will recurse top-down through all children that are also ContainerViewElement implementations.
      Type Parameters:
      V - type
      Parameters:
      requiredType - the elements should have
      Returns:
      stream of matching elements
    • findAll

      public <V extends ViewElement> Stream<V> findAll(Class<V> requiredType, Predicate<V> predicate)
      Find all elements in the container that are of the required type and match the additional predicate. Will recurse top-down through all children that are also ContainerViewElement implementations.
      Type Parameters:
      V - type
      Parameters:
      requiredType - the elements should have
      predicate - additional predicate the elements should match
      Returns:
      stream of matching elements
    • findAll

      public Stream<ViewElement> findAll(Predicate<ViewElement> predicate)
      Find all elements in the container that are of the required type and match the additional predicate. Will recurse top-down through all children that are also ContainerViewElement implementations.
      Parameters:
      predicate - additional predicate the elements should match
      Returns:
      stream of matching elements
    • removeAllFromTree

      public Stream<ViewElement> removeAllFromTree(String... elementNames)
      Removes all elements with the given names from the tree.
      Parameters:
      elementNames - names of the elements to remove
      Returns:
      element that has been remove
    • removeFromTree

      public Optional<ViewElement> removeFromTree(String elementName)
      Remove the first element with the given name from the container or any of its children. The container will be searched top-down recursively until an element is found. The return value will be empty if no element with the given name was present.
      Parameters:
      elementName - name of the element to remove
      Returns:
      element that has been remove
    • removeFromTree

      public boolean removeFromTree(ViewElement element)
      Remove the given element from the container. Will search the container top-down recursively.
      Parameters:
      element - to remove
      Returns:
      true if element was present and has been removed
    • getName

      public String getName()
      Description copied from interface: ViewElement
      A ViewElement can have an internal name that identifies it within a ContainerViewElement. A name is optional but when given, is preferably unique within its container as most operations work on the first element with a specific name.
      Specified by:
      getName in interface ViewElement
      Returns:
      Internal name of this element, can be null.
      See Also:
    • setName

      public ContainerViewElement setName(String name)
      Specified by:
      setName in interface MutableViewElement
      Returns:
      this.
    • getCustomTemplate

      public String getCustomTemplate()
      Specified by:
      getCustomTemplate in interface ViewElement
      Returns:
      Custom template to use when rendering this view element.
    • setCustomTemplate

      public ContainerViewElement setCustomTemplate(String customTemplate)
      Specified by:
      setCustomTemplate in interface MutableViewElement
      Returns:
      this.
    • getElementType

      public String getElementType()
      Specified by:
      getElementType in interface ViewElement
      Returns:
      Type id of this view element.
    • setElementType

      protected ContainerViewElement setElementType(String elementType)
      Returns:
      this.