Class SimplePropertyComponentBuilder
- java.lang.Object
-
- org.vaadin.miki.superfields.object.builder.SimplePropertyComponentBuilder
-
- All Implemented Interfaces:
Serializable,PropertyComponentBuilder
public class SimplePropertyComponentBuilder extends Object implements PropertyComponentBuilder
Reference implementation ofPropertyComponentBuilder. In general, it allows building components based onPredicate<Property> and provides some helper methods for common use cases.- Since:
- 2022-06-06
- Author:
- miki
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static FieldBuilder<?>DEFAULT_BUILDERDefault field builder used when no registered predicate matches given property.
-
Constructor Summary
Constructors Constructor Description SimplePropertyComponentBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <P,C extends com.vaadin.flow.component.Component & com.vaadin.flow.component.HasValue<?,P>>
Optional<C>buildPropertyField(Property<?,P> property)Builds a component for a given property.FieldBuilder<?>getDefaultBuilder()Returns the current defaultFieldBuilder, i.e.Map<Predicate<Property<?,?>>,FieldBuilder<?>>getRegisteredBuilders()Returns the builders registered so far.booleanisDefaultLabel()Checks if the produced component should have its label set to default.static Predicate<Property<?,?>>isExactlyType(Class<?> type)Returns aPredicatethat checks if a property is exactly of given type.static Predicate<Property<?,?>>isOfType(Class<?> type)Returns aPredicatethat checks if a property is of the given type (i.e.<P> voidregisterBuilder(Class<P> valueType, FieldBuilder<P> builder)Registers a givenbuilderwith a predicate that checks for a given (not exact) type.<P> voidregisterBuilder(Predicate<Property<?,P>> predicate, FieldBuilder<P> builder)Registers a givenPredicateand its matchingFieldBuilder.<P,C extends com.vaadin.flow.component.Component & com.vaadin.flow.component.HasValue<?,P>>
voidregisterType(Class<P> valueType, com.vaadin.flow.function.SerializableSupplier<C> componentSupplier)Registers a builder for a givenComponentfor properties of exactly the givenvalueType.voidsetDefaultBuilder(FieldBuilder<?> defaultBuilder)Sets a new defaultFieldBuilder, i.e.voidsetDefaultLabel(boolean defaultLabel)Controls whether the produced component should have its label set to default.SimplePropertyComponentBuilderwithDefaultBuilder(FieldBuilder<?> builder)ChainssetDefaultBuilder(FieldBuilder)and returns itself.SimplePropertyComponentBuilderwithDefaultLabel()ChainssetDefaultLabel(boolean)withtrueand returns itself.SimplePropertyComponentBuilderwithDefaultLabel(boolean state)ChainssetDefaultLabel(boolean)and returns itself.SimplePropertyComponentBuilderwithoutDefaultLabel()ChainssetDefaultLabel(boolean)withfalseand returns itself.<P> SimplePropertyComponentBuilderwithRegisteredBuilder(Class<P> valueType, FieldBuilder<P> builder)ChainsregisterBuilder(Class, FieldBuilder)and returns itself.<P> SimplePropertyComponentBuilderwithRegisteredBuilder(Predicate<Property<?,P>> predicate, FieldBuilder<P> builder)ChainsregisterBuilder(Predicate, FieldBuilder)and returns itself.<P,C extends com.vaadin.flow.component.Component & com.vaadin.flow.component.HasValue<?,P>>
SimplePropertyComponentBuilderwithRegisteredType(Class<P> valueType, com.vaadin.flow.function.SerializableSupplier<C> componentSupplier)ChainsregisterType(Class, SerializableSupplier)and returns itself.
-
-
-
Field Detail
-
DEFAULT_BUILDER
public static final FieldBuilder<?> DEFAULT_BUILDER
Default field builder used when no registered predicate matches given property. It always produces a result.
-
-
Method Detail
-
isExactlyType
public static Predicate<Property<?,?>> isExactlyType(Class<?> type)
Returns aPredicatethat checks if a property is exactly of given type.- Parameters:
type- Type to check.- Returns:
- A
Predicatethat checks if the giventypeis equal to the property's type.
-
isOfType
public static Predicate<Property<?,?>> isOfType(Class<?> type)
Returns aPredicatethat checks if a property is of the given type (i.e. is a subclass or implementation of it).- Parameters:
type- Type to check.- Returns:
- A
Predicatethat evaluates totype.isAssignableFrom(property.getType()).
-
buildPropertyField
public <P,C extends com.vaadin.flow.component.Component & com.vaadin.flow.component.HasValue<?,P>> Optional<C> buildPropertyField(Property<?,P> property)
Description copied from interface:PropertyComponentBuilderBuilds a component for a given property.- Specified by:
buildPropertyFieldin interfacePropertyComponentBuilder- Type Parameters:
P- Type of the value of the property.C- Component to be returned.- Parameters:
property- Property to build a component for.- Returns:
- A component capable of displaying the value of the given property, if any.
-
getRegisteredBuilders
public Map<Predicate<Property<?,?>>,FieldBuilder<?>> getRegisteredBuilders()
Returns the builders registered so far. Modifications of the returned map will affect this object.- Returns:
- A non-
null, but possibly empty,Mapof registeredFieldBuilders with matchingPredicates.
-
registerType
public <P,C extends com.vaadin.flow.component.Component & com.vaadin.flow.component.HasValue<?,P>> void registerType(Class<P> valueType, com.vaadin.flow.function.SerializableSupplier<C> componentSupplier)
Registers a builder for a givenComponentfor properties of exactly the givenvalueType.- Type Parameters:
P- Value type.C- Component type.- Parameters:
valueType- Type of the property.componentSupplier- Supplier of component capable of displaying value of the given type.
-
withRegisteredType
public final <P,C extends com.vaadin.flow.component.Component & com.vaadin.flow.component.HasValue<?,P>> SimplePropertyComponentBuilder withRegisteredType(Class<P> valueType, com.vaadin.flow.function.SerializableSupplier<C> componentSupplier)
ChainsregisterType(Class, SerializableSupplier)and returns itself.- Type Parameters:
P- Value type.C- Component type.- Parameters:
valueType- Type of the property.componentSupplier- Supplier of component capable of displaying value of the given type.- Returns:
- This.
- See Also:
registerType(Class, SerializableSupplier)
-
registerBuilder
public <P> void registerBuilder(Class<P> valueType, FieldBuilder<P> builder)
Registers a givenbuilderwith a predicate that checks for a given (not exact) type.- Type Parameters:
P- Property value type.- Parameters:
valueType- Type to check.builder- Builder to register.
-
registerBuilder
public <P> void registerBuilder(Predicate<Property<?,P>> predicate, FieldBuilder<P> builder)
Registers a givenPredicateand its matchingFieldBuilder.- Type Parameters:
P- Property value type.- Parameters:
predicate- Predicate.builder- Builder.
-
withRegisteredBuilder
public final <P> SimplePropertyComponentBuilder withRegisteredBuilder(Class<P> valueType, FieldBuilder<P> builder)
ChainsregisterBuilder(Class, FieldBuilder)and returns itself.- Type Parameters:
P- Property value type.- Parameters:
valueType- Value type to check.builder- Builder.- Returns:
- This.
- See Also:
registerBuilder(Class, FieldBuilder)
-
withRegisteredBuilder
public final <P> SimplePropertyComponentBuilder withRegisteredBuilder(Predicate<Property<?,P>> predicate, FieldBuilder<P> builder)
ChainsregisterBuilder(Predicate, FieldBuilder)and returns itself.- Type Parameters:
P- Property value type.- Parameters:
predicate- Predicate to check.builder- Builder.- Returns:
- This.
- See Also:
registerBuilder(Predicate, FieldBuilder)
-
isDefaultLabel
public boolean isDefaultLabel()
Checks if the produced component should have its label set to default.- Returns:
- When
true(default), the created component will have its label set to human-readable version of the property name, if it is possible.
-
setDefaultLabel
public void setDefaultLabel(boolean defaultLabel)
Controls whether the produced component should have its label set to default.- Parameters:
defaultLabel- Whentrue, the created component will have its label set to human-readable version of the property name, if it is possible.
-
withDefaultLabel
public final SimplePropertyComponentBuilder withDefaultLabel()
ChainssetDefaultLabel(boolean)withtrueand returns itself.- Returns:
- This.
- See Also:
setDefaultLabel(boolean),withoutDefaultLabel(),withDefaultLabel(boolean)
-
withoutDefaultLabel
public final SimplePropertyComponentBuilder withoutDefaultLabel()
ChainssetDefaultLabel(boolean)withfalseand returns itself.- Returns:
- This.
- See Also:
setDefaultLabel(boolean),withDefaultLabel(),withDefaultLabel(boolean)
-
withDefaultLabel
public final SimplePropertyComponentBuilder withDefaultLabel(boolean state)
ChainssetDefaultLabel(boolean)and returns itself.- Parameters:
state- Whentrue, the created component will have its label set to human-readable version of the property name, if it is possible.- Returns:
- This.
-
setDefaultBuilder
public void setDefaultBuilder(FieldBuilder<?> defaultBuilder)
Sets a new defaultFieldBuilder, i.e. the builder used when none of the registered builders are suitable.- Parameters:
defaultBuilder- New builder. Whennullis passed,DEFAULT_BUILDERwill be used instead.
-
getDefaultBuilder
public final FieldBuilder<?> getDefaultBuilder()
Returns the current defaultFieldBuilder, i.e. the builder used when none of the registered builders are suitable.- Returns:
- The current default
FieldBuilder. Nevernull.
-
withDefaultBuilder
public final SimplePropertyComponentBuilder withDefaultBuilder(FieldBuilder<?> builder)
ChainssetDefaultBuilder(FieldBuilder)and returns itself.- Parameters:
builder- Builder to use.- Returns:
- This.
- See Also:
setDefaultBuilder(FieldBuilder)
-
-