Class ContainerViewElement
java.lang.Object
com.foreach.across.modules.web.ui.elements.ContainerViewElement
- All Implemented Interfaces:
MutableViewElement
,ViewElement
- Direct Known Subclasses:
AbstractNodeViewElement
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.foreach.across.modules.web.ui.MutableViewElement
MutableViewElement.Functions
Nested classes/interfaces inherited from interface com.foreach.across.modules.web.ui.ViewElement
ViewElement.Wither<T extends ViewElement>, ViewElement.WitherGetter<T extends ViewElement,
U>, ViewElement.WitherRemover<T extends ViewElement>, ViewElement.WitherSetter<T extends ViewElement> -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddChild
(@NonNull ViewElement element) Add a child to this container.addChildren
(@NonNull Collection<? extends ViewElement> elements) Add a collection of children to this container.addFirstChild
(@NonNull ViewElement element) Adds a child as the first one to this container.apply
(@NonNull Consumer<ContainerViewElement> consumer) Deprecated.<U extends ViewElement>
ContainerViewElementapplyUnsafe
(@NonNull Consumer<U> consumer) Deprecated.useViewElement.with(ViewElement)
insteadRemove all children from this container.Get the stream of allViewElement
instances this container represents (excluding itself).Find the first child element with the given name in the container.<V extends ViewElement>
Optional<V>Find the first child element with the given name that is also of the required type.<V extends ViewElement>
Stream<V>Find all elements in the container that are of the required type.<V extends ViewElement>
Stream<V>Find all elements in the container that are of the required type and match the additional predicate.findAll
(Predicate<ViewElement> predicate) Find all elements in the container that are of the required type and match the additional predicate.findOrSelf
(String name) Find the first element with the given name in the container.<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.Creates a flattened stream of all elements in the container.Get the list of direct children this container represents.getName()
A ViewElement can have an internal name that identifies it within aContainerViewElement
.boolean
remove
(ViewElement.WitherRemover... functions) Execute one or more remover functions.removeAllFromTree
(String... elementNames) Removes all elements with the given names from the tree.boolean
removeChild
(ViewElement element) Remove a direct child from this container.boolean
removeFromTree
(ViewElement element) Remove the given element from the container.removeFromTree
(String elementName) Remove the first element with the given name from the container or any of its children.set
(ViewElement.WitherSetter... setters) Execute one or more setter functions.setCustomTemplate
(String customTemplate) protected ContainerViewElement
setElementType
(String elementType) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.foreach.across.modules.web.ui.ViewElement
get, matches
-
Field Details
-
ELEMENT_TYPE
- See Also:
-
-
Constructor Details
-
ContainerViewElement
public ContainerViewElement() -
ContainerViewElement
-
-
Method Details
-
elementStream
Get the stream of allViewElement
instances this container represents (excluding itself). All these elements can be found using any of thefind(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
Get the list of direct children this container represents.- Returns:
- list of child elements this container represents
-
addChild
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
Adds a child as the first one to this container.- Parameters:
element
- to add
-
removeChild
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
Remove all children from this container. -
apply
@Deprecated public ContainerViewElement apply(@NonNull @NonNull Consumer<ContainerViewElement> consumer) Deprecated.useViewElement.with(ViewElement)
insteadApply 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.useViewElement.with(ViewElement)
insteadApply a consumer to this container. Useful for providing extensions that wish to provide actions on the container itself. This method allows you to pass anyConsumer
for a genericViewElement
. Note that if you pass a consumer for a type that is not compatible withContainerViewElement
, 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 interfaceMutableViewElement
- Specified by:
set
in interfaceViewElement
- Parameters:
setters
- to execute- Returns:
- self
-
remove
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 interfaceMutableViewElement
- Specified by:
remove
in interfaceViewElement
- Parameters:
functions
- to execute- Returns:
- self
-
find
Find the first child element with the given name in the container. Will recursive through all children that are alsoContainerViewElement
implementations. Search will be top-down and the first matching element will be returned.- Returns:
- element if found
- See Also:
-
findOrSelf
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 alsoContainerViewElement
implementations. Search will be top-down and the first matching element will be returned.- Returns:
- element if found
-
find
Find the first child element with the given name that is also of the required type. Will recursive through all children that are alsoContainerViewElement
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
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 alsoContainerViewElement
implementations. Search will be top-down and the first matching element will be returned.- Returns:
- element if found
-
flatStream
Creates a flattened stream of all elements in the container. Will recurse top-down through all children that are alsoContainerViewElement
implementations.- Returns:
- flattened stream
-
findAll
Find all elements in the container that are of the required type. Will recurse top-down through all children that are alsoContainerViewElement
implementations.- Type Parameters:
V
- type- Parameters:
requiredType
- the elements should have- Returns:
- stream of matching elements
-
findAll
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 alsoContainerViewElement
implementations.- Type Parameters:
V
- type- Parameters:
requiredType
- the elements should havepredicate
- additional predicate the elements should match- Returns:
- stream of matching elements
-
findAll
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 alsoContainerViewElement
implementations.- Parameters:
predicate
- additional predicate the elements should match- Returns:
- stream of matching elements
-
removeAllFromTree
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
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
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
Description copied from interface:ViewElement
A ViewElement can have an internal name that identifies it within aContainerViewElement
. 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 interfaceViewElement
- Returns:
- Internal name of this element, can be null.
- See Also:
-
setName
- Specified by:
setName
in interfaceMutableViewElement
- Returns:
this
.
-
getCustomTemplate
- Specified by:
getCustomTemplate
in interfaceViewElement
- Returns:
- Custom template to use when rendering this view element.
-
setCustomTemplate
- Specified by:
setCustomTemplate
in interfaceMutableViewElement
- Returns:
this
.
-
getElementType
- Specified by:
getElementType
in interfaceViewElement
- Returns:
- Type id of this view element.
-
setElementType
- Returns:
this
.
-
ViewElement.with(ViewElement)
instead