|
| 1 | +/* |
| 2 | + * Copyright © 2024 XDEV Software (https://xdev.software) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package software.xdev.vaadin.builder; |
| 17 | + |
| 18 | +import com.vaadin.flow.function.ValueProvider; |
| 19 | +import software.xdev.vaadin.model.FilterField; |
| 20 | +import software.xdev.vaadin.model.FilterFieldEnumExtension; |
| 21 | +import software.xdev.vaadin.model.FilterProvider; |
| 22 | + |
| 23 | +import java.time.LocalDate; |
| 24 | +import java.time.LocalDateTime; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +/** |
| 29 | + * Used to create a customizable FilterField. |
| 30 | + * |
| 31 | + * @see FilterField |
| 32 | + */ |
| 33 | +public final class CustomizableFilterBuilder |
| 34 | +{ |
| 35 | + private CustomizableFilterBuilder() |
| 36 | + { |
| 37 | + } |
| 38 | + |
| 39 | + public static CustomizableFilterBuilder builder() |
| 40 | + { |
| 41 | + return new CustomizableFilterBuilder(); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * Is used to create a new field for filtering. |
| 47 | + * |
| 48 | + * @param provider The field of the bean with type String. |
| 49 | + * @param description Is used to set the description of the selectable field. |
| 50 | + * @param <T> Type of the field. |
| 51 | + * @return Returns a field for filtering. |
| 52 | + */ |
| 53 | + public <T> FilterField<T, String> withValueProvider(final FilterProvider.StringProvider<T> provider, |
| 54 | + final String description) |
| 55 | + { |
| 56 | + return this.withValueProvider(provider, description, String.class); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Is used to create a new field for filtering. |
| 61 | + * |
| 62 | + * @param provider The field of the bean with type Number. |
| 63 | + * @param description Is used to set the description of the selectable field. |
| 64 | + * @param <T> Type of the field. |
| 65 | + * @return Returns a field for filtering. |
| 66 | + */ |
| 67 | + public <T> FilterField<T, Number> withValueProvider(final FilterProvider.NumberProvider<T> provider, |
| 68 | + final String description) |
| 69 | + { |
| 70 | + return this.withValueProvider(provider, description, Number.class); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Is used to create a new field for filtering. |
| 75 | + * |
| 76 | + * @param provider The field of the bean with type LocalDate. |
| 77 | + * @param description Is used to set the description of the selectable field. |
| 78 | + * @param <T> Type of the field. |
| 79 | + * @return Returns a field for filtering. |
| 80 | + */ |
| 81 | + public <T> FilterField<T, LocalDate> withValueProvider( |
| 82 | + final FilterProvider.LocalDateProvider<T> provider, |
| 83 | + final String description) |
| 84 | + { |
| 85 | + return this.withValueProvider(provider, description, LocalDate.class); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Is used to create a new field for filtering. |
| 90 | + * |
| 91 | + * @param provider The field of the bean with type LocalDateTime. |
| 92 | + * @param description Is used to set the description of the selectable field. |
| 93 | + * @param <T> Type of the field. |
| 94 | + * @return Returns a field for filtering. |
| 95 | + */ |
| 96 | + public <T> FilterField<T, LocalDateTime> withValueProvider( |
| 97 | + final FilterProvider.LocalDateTimeProvider<T> provider, |
| 98 | + final String description) |
| 99 | + { |
| 100 | + return this.withValueProvider(provider, description, LocalDateTime.class); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Is used to create a new field for filtering. |
| 105 | + * |
| 106 | + * @param provider The field of the bean with type Boolean. |
| 107 | + * @param description Is used to set the description of the selectable field. |
| 108 | + * @param <T> Type of the field. |
| 109 | + * @return Returns a field for filtering. |
| 110 | + */ |
| 111 | + public <T> FilterField<T, Boolean> withValueProvider( |
| 112 | + final FilterProvider.BooleanProvider<T> provider, |
| 113 | + final String description) |
| 114 | + { |
| 115 | + return this.withValueProvider(provider, description, Boolean.class); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Is used to create a new field for filtering. |
| 120 | + * |
| 121 | + * @param provider The field of the bean with type Enum. |
| 122 | + * @param description Is used to set the description of the selectable field. |
| 123 | + * @param <T> Type of the field. |
| 124 | + * @return Returns a field for filtering. |
| 125 | + */ |
| 126 | + public <T> FilterField<T, Enum> withValueProvider(final FilterProvider.EnumProvider<T> provider, |
| 127 | + final String description, |
| 128 | + final Enum<?>[] enumValues) |
| 129 | + { |
| 130 | + return new FilterFieldEnumExtension<>(provider, description, Enum.class, new ArrayList<>(), enumValues); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Is used to create a new field for filtering. |
| 135 | + * |
| 136 | + * @param provider The field of the bean. |
| 137 | + * @param description The name of the selectable field. |
| 138 | + * @param type The class of the field. |
| 139 | + * @param <T> The bean. |
| 140 | + * @param <X> The type of the field. |
| 141 | + * @return Returns a new filterField. |
| 142 | + */ |
| 143 | + public <T, X> FilterField<T, X> withValueProvider(final ValueProvider<T, X> provider, |
| 144 | + final String description, |
| 145 | + final Class<X> type) |
| 146 | + { |
| 147 | + Objects.requireNonNull(provider); |
| 148 | + Objects.requireNonNull(description); |
| 149 | + |
| 150 | + return new FilterField<>(provider, description, type, new ArrayList<>()); |
| 151 | + } |
| 152 | +} |
0 commit comments