您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Binder.java 87KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.io.Serializable;
  18. import java.lang.reflect.Field;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.lang.reflect.Type;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.HashSet;
  26. import java.util.IdentityHashMap;
  27. import java.util.LinkedHashSet;
  28. import java.util.List;
  29. import java.util.Locale;
  30. import java.util.Map;
  31. import java.util.Objects;
  32. import java.util.Optional;
  33. import java.util.Set;
  34. import java.util.function.BiConsumer;
  35. import java.util.stream.Collectors;
  36. import java.util.stream.Stream;
  37. import com.googlecode.gentyref.GenericTypeReflector;
  38. import com.vaadin.annotations.PropertyId;
  39. import com.vaadin.data.HasValue.ValueChangeEvent;
  40. import com.vaadin.data.converter.StringToIntegerConverter;
  41. import com.vaadin.data.validator.BeanValidator;
  42. import com.vaadin.event.EventRouter;
  43. import com.vaadin.server.ErrorMessage;
  44. import com.vaadin.server.SerializableFunction;
  45. import com.vaadin.server.SerializablePredicate;
  46. import com.vaadin.server.Setter;
  47. import com.vaadin.server.UserError;
  48. import com.vaadin.shared.Registration;
  49. import com.vaadin.ui.AbstractComponent;
  50. import com.vaadin.ui.Component;
  51. import com.vaadin.ui.Label;
  52. import com.vaadin.ui.UI;
  53. import com.vaadin.util.ReflectTools;
  54. /**
  55. * Connects one or more {@code Field} components to properties of a backing data
  56. * type such as a bean type. With a binder, input components can be grouped
  57. * together into forms to easily create and update business objects with little
  58. * explicit logic needed to move data between the UI and the data layers of the
  59. * application.
  60. * <p>
  61. * A binder is a collection of <i>bindings</i>, each representing the mapping of
  62. * a single field, through converters and validators, to a backing property.
  63. * <p>
  64. * A binder instance can be bound to a single bean instance at a time, but can
  65. * be rebound as needed. This allows usage patterns like a <i>master-details</i>
  66. * view, where a select component is used to pick the bean to edit.
  67. * <p>
  68. * Bean level validators can be added using the
  69. * {@link #withValidator(Validator)} method and will be run on the bound bean
  70. * once it has been updated from the values of the bound fields. Bean level
  71. * validators are also run as part of {@link #writeBean(Object)} and
  72. * {@link #writeBeanIfValid(Object)} if all field level validators pass.
  73. * <p>
  74. * Note: For bean level validators, the bean must be updated before the
  75. * validators are run. If a bean level validator fails in
  76. * {@link #writeBean(Object)} or {@link #writeBeanIfValid(Object)}, the bean
  77. * will be reverted to the previous state before returning from the method. You
  78. * should ensure that the getters/setters in the bean do not have side effects.
  79. * <p>
  80. * Unless otherwise specified, {@code Binder} method arguments cannot be null.
  81. *
  82. * @author Vaadin Ltd.
  83. *
  84. * @param <BEAN>
  85. * the bean type
  86. *
  87. * @see BindingBuilder
  88. * @see Binding
  89. * @see HasValue
  90. *
  91. * @since 8.0
  92. */
  93. public class Binder<BEAN> implements Serializable {
  94. /**
  95. * Represents the binding between a field and a data property.
  96. *
  97. * @param <BEAN>
  98. * the bean type
  99. * @param <TARGET>
  100. * the target data type of the binding, matches the field type
  101. * unless a converter has been set
  102. *
  103. * @see Binder#forField(HasValue)
  104. */
  105. public interface Binding<BEAN, TARGET> extends Serializable {
  106. /**
  107. * Gets the field the binding uses.
  108. *
  109. * @return the field for the binding
  110. */
  111. public HasValue<?> getField();
  112. /**
  113. * Validates the field value and returns a {@code ValidationStatus}
  114. * instance representing the outcome of the validation.
  115. *
  116. * @see Binder#validate()
  117. * @see Validator#apply(Object, ValueContext)
  118. *
  119. * @return the validation result.
  120. */
  121. public BindingValidationStatus<TARGET> validate();
  122. }
  123. /**
  124. * Creates a binding between a field and a data property.
  125. *
  126. * @param <BEAN>
  127. * the bean type
  128. * @param <TARGET>
  129. * the target data type of the binding, matches the field type
  130. * until a converter has been set
  131. *
  132. * @see Binder#forField(HasValue)
  133. */
  134. public interface BindingBuilder<BEAN, TARGET> extends Serializable {
  135. /**
  136. * Gets the field the binding is being built for.
  137. *
  138. * @return the field this binding is being built for
  139. */
  140. public HasValue<?> getField();
  141. /**
  142. * Completes this binding using the given getter and setter functions
  143. * representing a backing bean property. The functions are used to
  144. * update the field value from the property and to store the field value
  145. * to the property, respectively.
  146. * <p>
  147. * When a bean is bound with {@link Binder#setBean(BEAN)}, the field
  148. * value is set to the return value of the given getter. The property
  149. * value is then updated via the given setter whenever the field value
  150. * changes. The setter may be null; in that case the property value is
  151. * never updated and the binding is said to be <i>read-only</i>.
  152. * <p>
  153. * If the Binder is already bound to some bean, the newly bound field is
  154. * associated with the corresponding bean property as described above.
  155. * <p>
  156. * The getter and setter can be arbitrary functions, for instance
  157. * implementing user-defined conversion or validation. However, in the
  158. * most basic use case you can simply pass a pair of method references
  159. * to this method as follows:
  160. *
  161. * <pre>
  162. * class Person {
  163. * public String getName() { ... }
  164. * public void setName(String name) { ... }
  165. * }
  166. *
  167. * TextField nameField = new TextField();
  168. * binder.forField(nameField).bind(Person::getName, Person::setName);
  169. * </pre>
  170. *
  171. * @param getter
  172. * the function to get the value of the property to the
  173. * field, not null
  174. * @param setter
  175. * the function to write the field value to the property or
  176. * null if read-only
  177. * @return the newly created binding
  178. * @throws IllegalStateException
  179. * if {@code bind} has already been called on this binding
  180. */
  181. public Binding<BEAN, TARGET> bind(ValueProvider<BEAN, TARGET> getter,
  182. Setter<BEAN, TARGET> setter);
  183. /**
  184. * Completes this binding by connecting the field to the property with
  185. * the given name. The getter and setter of the property are looked up
  186. * using a {@link BinderPropertySet}.
  187. * <p>
  188. * For a <code>Binder</code> created using the
  189. * {@link Binder#Binder(Class)} constructor, introspection will be used
  190. * to find a Java Bean property. If a JSR-303 bean validation
  191. * implementation is present on the classpath, a {@link BeanValidator}
  192. * is also added to the binding.
  193. * <p>
  194. * The property must have an accessible getter method. It need not have
  195. * an accessible setter; in that case the property value is never
  196. * updated and the binding is said to be <i>read-only</i>.
  197. *
  198. * @param propertyName
  199. * the name of the property to bind, not null
  200. * @return the newly created binding
  201. *
  202. * @throws IllegalArgumentException
  203. * if the property name is invalid
  204. * @throws IllegalArgumentException
  205. * if the property has no accessible getter
  206. * @throws IllegalStateException
  207. * if the binder is not configured with an appropriate
  208. * {@link BinderPropertySet}
  209. *
  210. * @see Binder.BindingBuilder#bind(ValueProvider, Setter)
  211. */
  212. public Binding<BEAN, TARGET> bind(String propertyName);
  213. /**
  214. * Adds a validator to this binding. Validators are applied, in
  215. * registration order, when the field value is written to the backing
  216. * property. If any validator returns a failure, the property value is
  217. * not updated.
  218. *
  219. * @see #withValidator(SerializablePredicate, String)
  220. * @see #withValidator(SerializablePredicate, ErrorMessageProvider)
  221. *
  222. * @param validator
  223. * the validator to add, not null
  224. * @return this binding, for chaining
  225. * @throws IllegalStateException
  226. * if {@code bind} has already been called
  227. */
  228. public BindingBuilder<BEAN, TARGET> withValidator(
  229. Validator<? super TARGET> validator);
  230. /**
  231. * A convenience method to add a validator to this binding using the
  232. * {@link Validator#from(SerializablePredicate, String)} factory method.
  233. * <p>
  234. * Validators are applied, in registration order, when the field value
  235. * is written to the backing property. If any validator returns a
  236. * failure, the property value is not updated.
  237. *
  238. * @see #withValidator(Validator)
  239. * @see #withValidator(SerializablePredicate, ErrorMessageProvider)
  240. * @see Validator#from(SerializablePredicate, String)
  241. *
  242. * @param predicate
  243. * the predicate performing validation, not null
  244. * @param message
  245. * the error message to report in case validation failure
  246. * @return this binding, for chaining
  247. * @throws IllegalStateException
  248. * if {@code bind} has already been called
  249. */
  250. public default BindingBuilder<BEAN, TARGET> withValidator(
  251. SerializablePredicate<? super TARGET> predicate,
  252. String message) {
  253. return withValidator(Validator.from(predicate, message));
  254. }
  255. /**
  256. * A convenience method to add a validator to this binding using the
  257. * {@link Validator#from(SerializablePredicate, ErrorMessageProvider)}
  258. * factory method.
  259. * <p>
  260. * Validators are applied, in registration order, when the field value
  261. * is written to the backing property. If any validator returns a
  262. * failure, the property value is not updated.
  263. *
  264. * @see #withValidator(Validator)
  265. * @see #withValidator(SerializablePredicate, String)
  266. * @see Validator#from(SerializablePredicate, ErrorMessageProvider)
  267. *
  268. * @param predicate
  269. * the predicate performing validation, not null
  270. * @param errorMessageProvider
  271. * the provider to generate error messages, not null
  272. * @return this binding, for chaining
  273. * @throws IllegalStateException
  274. * if {@code bind} has already been called
  275. */
  276. public default BindingBuilder<BEAN, TARGET> withValidator(
  277. SerializablePredicate<? super TARGET> predicate,
  278. ErrorMessageProvider errorMessageProvider) {
  279. return withValidator(
  280. Validator.from(predicate, errorMessageProvider));
  281. }
  282. /**
  283. * Maps the binding to another data type using the given
  284. * {@link Converter}.
  285. * <p>
  286. * A converter is capable of converting between a presentation type,
  287. * which must match the current target data type of the binding, and a
  288. * model type, which can be any data type and becomes the new target
  289. * type of the binding. When invoking
  290. * {@link #bind(ValueProvider, Setter)}, the target type of the binding
  291. * must match the getter/setter types.
  292. * <p>
  293. * For instance, a {@code TextField} can be bound to an integer-typed
  294. * property using an appropriate converter such as a
  295. * {@link StringToIntegerConverter}.
  296. *
  297. * @param <NEWTARGET>
  298. * the type to convert to
  299. * @param converter
  300. * the converter to use, not null
  301. * @return a new binding with the appropriate type
  302. * @throws IllegalStateException
  303. * if {@code bind} has already been called
  304. */
  305. public <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
  306. Converter<TARGET, NEWTARGET> converter);
  307. /**
  308. * Maps the binding to another data type using the mapping functions and
  309. * a possible exception as the error message.
  310. * <p>
  311. * The mapping functions are used to convert between a presentation
  312. * type, which must match the current target data type of the binding,
  313. * and a model type, which can be any data type and becomes the new
  314. * target type of the binding. When invoking
  315. * {@link #bind(ValueProvider, Setter)}, the target type of the binding
  316. * must match the getter/setter types.
  317. * <p>
  318. * For instance, a {@code TextField} can be bound to an integer-typed
  319. * property using appropriate functions such as:
  320. * <code>withConverter(Integer::valueOf, String::valueOf);</code>
  321. *
  322. * @param <NEWTARGET>
  323. * the type to convert to
  324. * @param toModel
  325. * the function which can convert from the old target type to
  326. * the new target type
  327. * @param toPresentation
  328. * the function which can convert from the new target type to
  329. * the old target type
  330. * @return a new binding with the appropriate type
  331. * @throws IllegalStateException
  332. * if {@code bind} has already been called
  333. */
  334. public default <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
  335. SerializableFunction<TARGET, NEWTARGET> toModel,
  336. SerializableFunction<NEWTARGET, TARGET> toPresentation) {
  337. return withConverter(Converter.from(toModel, toPresentation,
  338. exception -> exception.getMessage()));
  339. }
  340. /**
  341. * Maps the binding to another data type using the mapping functions and
  342. * the given error error message if a value cannot be converted to the
  343. * new target type.
  344. * <p>
  345. * The mapping functions are used to convert between a presentation
  346. * type, which must match the current target data type of the binding,
  347. * and a model type, which can be any data type and becomes the new
  348. * target type of the binding. When invoking
  349. * {@link #bind(ValueProvider, Setter)}, the target type of the binding
  350. * must match the getter/setter types.
  351. * <p>
  352. * For instance, a {@code TextField} can be bound to an integer-typed
  353. * property using appropriate functions such as:
  354. * <code>withConverter(Integer::valueOf, String::valueOf);</code>
  355. *
  356. * @param <NEWTARGET>
  357. * the type to convert to
  358. * @param toModel
  359. * the function which can convert from the old target type to
  360. * the new target type
  361. * @param toPresentation
  362. * the function which can convert from the new target type to
  363. * the old target type
  364. * @param errorMessage
  365. * the error message to use if conversion using
  366. * <code>toModel</code> fails
  367. * @return a new binding with the appropriate type
  368. * @throws IllegalStateException
  369. * if {@code bind} has already been called
  370. */
  371. public default <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
  372. SerializableFunction<TARGET, NEWTARGET> toModel,
  373. SerializableFunction<NEWTARGET, TARGET> toPresentation,
  374. String errorMessage) {
  375. return withConverter(Converter.from(toModel, toPresentation,
  376. exception -> errorMessage));
  377. }
  378. /**
  379. * Maps binding value {@code null} to given null representation and back
  380. * to {@code null} when converting back to model value.
  381. *
  382. * @param nullRepresentation
  383. * the value to use instead of {@code null}
  384. * @return a new binding with null representation handling.
  385. */
  386. public default BindingBuilder<BEAN, TARGET> withNullRepresentation(
  387. TARGET nullRepresentation) {
  388. return withConverter(
  389. fieldValue -> Objects.equals(fieldValue, nullRepresentation)
  390. ? null : fieldValue,
  391. modelValue -> Objects.isNull(modelValue)
  392. ? nullRepresentation : modelValue);
  393. }
  394. /**
  395. * Sets the given {@code label} to show an error message if validation
  396. * fails.
  397. * <p>
  398. * The validation state of each field is updated whenever the user
  399. * modifies the value of that field. The validation state is by default
  400. * shown using {@link AbstractComponent#setComponentError} which is used
  401. * by the layout that the field is shown in. Most built-in layouts will
  402. * show this as a red exclamation mark icon next to the component, so
  403. * that hovering or tapping the icon shows a tooltip with the message
  404. * text.
  405. * <p>
  406. * This method allows to customize the way a binder displays error
  407. * messages to get more flexibility than what
  408. * {@link AbstractComponent#setComponentError} provides (it replaces the
  409. * default behavior).
  410. * <p>
  411. * This is just a shorthand for
  412. * {@link #withValidationStatusHandler(BindingValidationStatusHandler)}
  413. * method where the handler instance hides the {@code label} if there is
  414. * no error and shows it with validation error message if validation
  415. * fails. It means that it cannot be called after
  416. * {@link #withValidationStatusHandler(BindingValidationStatusHandler)}
  417. * method call or
  418. * {@link #withValidationStatusHandler(BindingValidationStatusHandler)}
  419. * after this method call.
  420. *
  421. * @see #withValidationStatusHandler(BindingValidationStatusHandler)
  422. * @see AbstractComponent#setComponentError(ErrorMessage)
  423. * @param label
  424. * label to show validation status for the field
  425. * @return this binding, for chaining
  426. */
  427. public default BindingBuilder<BEAN, TARGET> withStatusLabel(
  428. Label label) {
  429. return withValidationStatusHandler(status -> {
  430. label.setValue(status.getMessage().orElse(""));
  431. // Only show the label when validation has failed
  432. label.setVisible(status.isError());
  433. });
  434. }
  435. /**
  436. * Sets a {@link BindingValidationStatusHandler} to track validation
  437. * status changes.
  438. * <p>
  439. * The validation state of each field is updated whenever the user
  440. * modifies the value of that field. The validation state is by default
  441. * shown using {@link AbstractComponent#setComponentError} which is used
  442. * by the layout that the field is shown in. Most built-in layouts will
  443. * show this as a red exclamation mark icon next to the component, so
  444. * that hovering or tapping the icon shows a tooltip with the message
  445. * text.
  446. * <p>
  447. * This method allows to customize the way a binder displays error
  448. * messages to get more flexibility than what
  449. * {@link AbstractComponent#setComponentError} provides (it replaces the
  450. * default behavior).
  451. * <p>
  452. * The method may be called only once. It means there is no chain unlike
  453. * {@link #withValidator(Validator)} or
  454. * {@link #withConverter(Converter)}. Also it means that the shorthand
  455. * method {@link #withStatusLabel(Label)} also may not be called after
  456. * this method.
  457. *
  458. * @see #withStatusLabel(Label)
  459. * @see AbstractComponent#setComponentError(ErrorMessage)
  460. * @param handler
  461. * status change handler
  462. * @return this binding, for chaining
  463. */
  464. public BindingBuilder<BEAN, TARGET> withValidationStatusHandler(
  465. BindingValidationStatusHandler handler);
  466. /**
  467. * Sets the field to be required. This means two things:
  468. * <ol>
  469. * <li>the required indicator is visible</li>
  470. * <li>the field value is validated for not being empty*</li>
  471. * </ol>
  472. * For localizing the error message, use
  473. * {@link #asRequired(ErrorMessageProvider)}.
  474. * <p>
  475. * *Value not being the equal to what {@link HasValue#getEmptyValue()}
  476. * returns.
  477. *
  478. * @see #asRequired(ErrorMessageProvider)
  479. * @see HasValue#setRequiredIndicatorVisible(boolean)
  480. * @see HasValue#isEmpty()
  481. * @param errorMessage
  482. * the error message to show for the invalid value
  483. * @return this binding, for chaining
  484. */
  485. public default BindingBuilder<BEAN, TARGET> asRequired(
  486. String errorMessage) {
  487. return asRequired(context -> errorMessage);
  488. }
  489. /**
  490. * Sets the field to be required. This means two things:
  491. * <ol>
  492. * <li>the required indicator is visible</li>
  493. * <li>the field value is validated for not being empty*</li>
  494. * </ol>
  495. * *Value not being the equal to what {@link HasValue#getEmptyValue()}
  496. * returns.
  497. *
  498. * @see HasValue#setRequiredIndicatorVisible(boolean)
  499. * @see HasValue#isEmpty()
  500. * @param errorMessageProvider
  501. * the provider for localized validation error message
  502. * @return this binding, for chaining
  503. */
  504. public BindingBuilder<BEAN, TARGET> asRequired(
  505. ErrorMessageProvider errorMessageProvider);
  506. }
  507. /**
  508. * An internal implementation of {@code BindingBuilder}.
  509. *
  510. * @param <BEAN>
  511. * the bean type, must match the Binder bean type
  512. * @param <FIELDVALUE>
  513. * the value type of the field
  514. * @param <TARGET>
  515. * the target data type of the binding, matches the field type
  516. * until a converter has been set
  517. */
  518. protected static class BindingBuilderImpl<BEAN, FIELDVALUE, TARGET>
  519. implements BindingBuilder<BEAN, TARGET> {
  520. private final Binder<BEAN> binder;
  521. private final HasValue<FIELDVALUE> field;
  522. private BindingValidationStatusHandler statusHandler;
  523. private boolean isStatusHandlerChanged;
  524. private boolean bound;
  525. /**
  526. * Contains all converters and validators chained together in the
  527. * correct order.
  528. */
  529. private Converter<FIELDVALUE, TARGET> converterValidatorChain;
  530. /**
  531. * Creates a new binding builder associated with the given field.
  532. * Initializes the builder with the given converter chain and status
  533. * change handler.
  534. *
  535. * @param binder
  536. * the binder this instance is connected to, not null
  537. * @param field
  538. * the field to bind, not null
  539. * @param converterValidatorChain
  540. * the converter/validator chain to use, not null
  541. * @param statusHandler
  542. * the handler to track validation status, not null
  543. */
  544. protected BindingBuilderImpl(Binder<BEAN> binder,
  545. HasValue<FIELDVALUE> field,
  546. Converter<FIELDVALUE, TARGET> converterValidatorChain,
  547. BindingValidationStatusHandler statusHandler) {
  548. this.field = field;
  549. this.binder = binder;
  550. this.converterValidatorChain = converterValidatorChain;
  551. this.statusHandler = statusHandler;
  552. }
  553. @Override
  554. public Binding<BEAN, TARGET> bind(ValueProvider<BEAN, TARGET> getter,
  555. Setter<BEAN, TARGET> setter) {
  556. checkUnbound();
  557. Objects.requireNonNull(getter, "getter cannot be null");
  558. BindingImpl<BEAN, FIELDVALUE, TARGET> binding = new BindingImpl<>(
  559. this, getter, setter);
  560. getBinder().bindings.add(binding);
  561. if (getBinder().getBean() != null) {
  562. binding.initFieldValue(getBinder().getBean());
  563. }
  564. getBinder().fireStatusChangeEvent(false);
  565. bound = true;
  566. getBinder().incompleteBindings.remove(getField());
  567. return binding;
  568. }
  569. @Override
  570. @SuppressWarnings({ "unchecked", "rawtypes" })
  571. public Binding<BEAN, TARGET> bind(String propertyName) {
  572. Objects.requireNonNull(propertyName,
  573. "Property name cannot be null");
  574. checkUnbound();
  575. BinderPropertyDefinition<BEAN, ?> definition = getBinder().propertySet
  576. .getProperty(propertyName)
  577. .orElseThrow(() -> new IllegalArgumentException(
  578. "Could not resolve property name " + propertyName
  579. + " from " + getBinder().propertySet));
  580. ValueProvider<BEAN, ?> getter = definition.getGetter();
  581. Setter<BEAN, ?> setter = definition.getSetter()
  582. .orElse((bean, value) -> {
  583. // Setter ignores value
  584. });
  585. BindingBuilder<BEAN, ?> finalBinding = withConverter(
  586. createConverter(definition.getType()), false);
  587. finalBinding = getBinder().configureBinding(finalBinding,
  588. definition);
  589. try {
  590. return ((BindingBuilder) finalBinding).bind(getter, setter);
  591. } finally {
  592. getBinder().boundProperties.add(propertyName);
  593. getBinder().incompleteMemberFieldBindings.remove(getField());
  594. }
  595. }
  596. @SuppressWarnings("unchecked")
  597. private Converter<TARGET, Object> createConverter(Class<?> getterType) {
  598. return Converter.from(fieldValue -> getterType.cast(fieldValue),
  599. propertyValue -> (TARGET) propertyValue, exception -> {
  600. throw new RuntimeException(exception);
  601. });
  602. }
  603. @Override
  604. public BindingBuilder<BEAN, TARGET> withValidator(
  605. Validator<? super TARGET> validator) {
  606. checkUnbound();
  607. Objects.requireNonNull(validator, "validator cannot be null");
  608. converterValidatorChain = converterValidatorChain
  609. .chain(new ValidatorAsConverter<>(validator));
  610. return this;
  611. }
  612. @Override
  613. public <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
  614. Converter<TARGET, NEWTARGET> converter) {
  615. return withConverter(converter, true);
  616. }
  617. @Override
  618. public BindingBuilder<BEAN, TARGET> withValidationStatusHandler(
  619. BindingValidationStatusHandler handler) {
  620. checkUnbound();
  621. Objects.requireNonNull(handler, "handler cannot be null");
  622. if (isStatusHandlerChanged) {
  623. throw new IllegalStateException("A "
  624. + BindingValidationStatusHandler.class.getSimpleName()
  625. + " has already been set");
  626. }
  627. isStatusHandlerChanged = true;
  628. statusHandler = handler;
  629. return this;
  630. }
  631. @Override
  632. public BindingBuilder<BEAN, TARGET> asRequired(
  633. ErrorMessageProvider errorMessageProvider) {
  634. checkUnbound();
  635. field.setRequiredIndicatorVisible(true);
  636. return withValidator(
  637. value -> !Objects.equals(value, field.getEmptyValue()),
  638. errorMessageProvider);
  639. }
  640. /**
  641. * Implements {@link #withConverter(Converter)} method with additional
  642. * possibility to disable (reset) default null representation converter.
  643. * <p>
  644. * The method {@link #withConverter(Converter)} calls this method with
  645. * {@code true} provided as the second argument value.
  646. *
  647. * @see #withConverter(Converter)
  648. *
  649. * @param converter
  650. * the converter to use, not null
  651. * @param resetNullRepresentation
  652. * if {@code true} then default null representation will be
  653. * deactivated (if not yet), otherwise it won't be removed
  654. * @return a new binding with the appropriate type
  655. * @param <NEWTARGET>
  656. * the type to convert to
  657. * @throws IllegalStateException
  658. * if {@code bind} has already been called
  659. */
  660. protected <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
  661. Converter<TARGET, NEWTARGET> converter,
  662. boolean resetNullRepresentation) {
  663. checkUnbound();
  664. Objects.requireNonNull(converter, "converter cannot be null");
  665. if (resetNullRepresentation) {
  666. getBinder().initialConverters.get(field).setIdentity();
  667. }
  668. return getBinder().createBinding(field,
  669. converterValidatorChain.chain(converter), statusHandler);
  670. }
  671. /**
  672. * Returns the {@code Binder} connected to this {@code Binding}
  673. * instance.
  674. *
  675. * @return the binder
  676. */
  677. protected Binder<BEAN> getBinder() {
  678. return binder;
  679. }
  680. /**
  681. * Throws if this binding is already completed and cannot be modified
  682. * anymore.
  683. *
  684. * @throws IllegalStateException
  685. * if this binding is already bound
  686. */
  687. protected void checkUnbound() {
  688. if (bound) {
  689. throw new IllegalStateException(
  690. "cannot modify binding: already bound to a property");
  691. }
  692. }
  693. @Override
  694. public HasValue<FIELDVALUE> getField() {
  695. return field;
  696. }
  697. }
  698. /**
  699. * An internal implementation of {@code Binding}.
  700. *
  701. * @param <BEAN>
  702. * the bean type, must match the Binder bean type
  703. * @param <FIELDVALUE>
  704. * the value type of the field
  705. * @param <TARGET>
  706. * the target data type of the binding, matches the field type
  707. * unless a converter has been set
  708. */
  709. protected static class BindingImpl<BEAN, FIELDVALUE, TARGET>
  710. implements Binding<BEAN, TARGET> {
  711. private final Binder<BEAN> binder;
  712. private final HasValue<FIELDVALUE> field;
  713. private final BindingValidationStatusHandler statusHandler;
  714. private final SerializableFunction<BEAN, TARGET> getter;
  715. private final Setter<BEAN, TARGET> setter;
  716. // Not final since we temporarily remove listener while changing values
  717. private Registration onValueChange;
  718. /**
  719. * Contains all converters and validators chained together in the
  720. * correct order.
  721. */
  722. private final Converter<FIELDVALUE, TARGET> converterValidatorChain;
  723. public BindingImpl(BindingBuilderImpl<BEAN, FIELDVALUE, TARGET> builder,
  724. SerializableFunction<BEAN, TARGET> getter,
  725. Setter<BEAN, TARGET> setter) {
  726. this.binder = builder.getBinder();
  727. this.field = builder.field;
  728. this.statusHandler = builder.statusHandler;
  729. converterValidatorChain = builder.converterValidatorChain;
  730. onValueChange = getField()
  731. .addValueChangeListener(this::handleFieldValueChange);
  732. this.getter = getter;
  733. this.setter = setter;
  734. }
  735. @Override
  736. public HasValue<FIELDVALUE> getField() {
  737. return field;
  738. }
  739. /**
  740. * Finds an appropriate locale to be used in conversion and validation.
  741. *
  742. * @return the found locale, not null
  743. */
  744. protected Locale findLocale() {
  745. Locale l = null;
  746. if (getField() instanceof Component) {
  747. l = ((Component) getField()).getLocale();
  748. }
  749. if (l == null && UI.getCurrent() != null) {
  750. l = UI.getCurrent().getLocale();
  751. }
  752. if (l == null) {
  753. l = Locale.getDefault();
  754. }
  755. return l;
  756. }
  757. @Override
  758. public BindingValidationStatus<TARGET> validate() {
  759. BindingValidationStatus<TARGET> status = doValidation();
  760. getBinder().getValidationStatusHandler()
  761. .statusChange(new BinderValidationStatus<>(getBinder(),
  762. Arrays.asList(status), Collections.emptyList()));
  763. getBinder().fireStatusChangeEvent(status.isError());
  764. return status;
  765. }
  766. /**
  767. * Returns the field value run through all converters and validators,
  768. * but doesn't pass the {@link BindingValidationStatus} to any status
  769. * handler.
  770. *
  771. * @return the result of the conversion
  772. */
  773. private Result<TARGET> doConversion() {
  774. FIELDVALUE fieldValue = field.getValue();
  775. return converterValidatorChain.convertToModel(fieldValue,
  776. createValueContext());
  777. }
  778. private BindingValidationStatus<TARGET> toValidationStatus(
  779. Result<TARGET> result) {
  780. return new BindingValidationStatus<>(this,
  781. result.isError()
  782. ? ValidationResult.error(result.getMessage().get())
  783. : ValidationResult.ok());
  784. }
  785. /**
  786. * Returns the field value run through all converters and validators,
  787. * but doesn't pass the {@link BindingValidationStatus} to any status
  788. * handler.
  789. *
  790. * @return the validation status
  791. */
  792. private BindingValidationStatus<TARGET> doValidation() {
  793. return toValidationStatus(doConversion());
  794. }
  795. /**
  796. * Creates a value context from the current state of the binding and its
  797. * field.
  798. *
  799. * @return the value context
  800. */
  801. protected ValueContext createValueContext() {
  802. if (field instanceof Component) {
  803. return new ValueContext((Component) field);
  804. }
  805. return new ValueContext(findLocale());
  806. }
  807. /**
  808. * Sets the field value by invoking the getter function on the given
  809. * bean. The default listener attached to the field will be removed for
  810. * the duration of this update.
  811. *
  812. * @param bean
  813. * the bean to fetch the property value from
  814. */
  815. private void initFieldValue(BEAN bean) {
  816. assert bean != null;
  817. assert onValueChange != null;
  818. onValueChange.remove();
  819. try {
  820. getField().setValue(convertDataToFieldType(bean));
  821. } finally {
  822. onValueChange = getField()
  823. .addValueChangeListener(this::handleFieldValueChange);
  824. }
  825. }
  826. private FIELDVALUE convertDataToFieldType(BEAN bean) {
  827. return converterValidatorChain.convertToPresentation(
  828. getter.apply(bean), createValueContext());
  829. }
  830. /**
  831. * Handles the value change triggered by the bound field.
  832. *
  833. * @param event
  834. */
  835. private void handleFieldValueChange(
  836. ValueChangeEvent<FIELDVALUE> event) {
  837. getBinder().setHasChanges(true);
  838. List<ValidationResult> binderValidationResults = Collections
  839. .emptyList();
  840. BindingValidationStatus<TARGET> fieldValidationStatus;
  841. if (getBinder().getBean() != null) {
  842. BEAN bean = getBinder().getBean();
  843. fieldValidationStatus = writeFieldValue(bean);
  844. if (!getBinder().bindings.stream()
  845. .map(BindingImpl::doValidation)
  846. .anyMatch(BindingValidationStatus::isError)) {
  847. binderValidationResults = getBinder().validateBean(bean);
  848. if (!binderValidationResults.stream()
  849. .anyMatch(ValidationResult::isError)) {
  850. getBinder().setHasChanges(false);
  851. }
  852. }
  853. } else {
  854. fieldValidationStatus = doValidation();
  855. }
  856. BinderValidationStatus<BEAN> status = new BinderValidationStatus<>(
  857. getBinder(), Arrays.asList(fieldValidationStatus),
  858. binderValidationResults);
  859. getBinder().getValidationStatusHandler().statusChange(status);
  860. getBinder().fireStatusChangeEvent(status.hasErrors());
  861. }
  862. /**
  863. * Write the field value by invoking the setter function on the given
  864. * bean, if the value passes all registered validators.
  865. *
  866. * @param bean
  867. * the bean to set the property value to
  868. */
  869. private BindingValidationStatus<TARGET> writeFieldValue(BEAN bean) {
  870. assert bean != null;
  871. Result<TARGET> result = doConversion();
  872. if (setter != null) {
  873. result.ifOk(value -> setter.accept(bean, value));
  874. }
  875. return toValidationStatus(result);
  876. }
  877. /**
  878. * Returns the {@code Binder} connected to this {@code Binding}
  879. * instance.
  880. *
  881. * @return the binder
  882. */
  883. protected Binder<BEAN> getBinder() {
  884. return binder;
  885. }
  886. private void notifyStatusHandler(BindingValidationStatus<?> status) {
  887. statusHandler.statusChange(status);
  888. }
  889. }
  890. /**
  891. * Wraps a validator as a converter.
  892. * <p>
  893. * The type of the validator must be of the same type as this converter or a
  894. * super type of it.
  895. *
  896. * @param <T>
  897. * the type of the converter
  898. */
  899. private static class ValidatorAsConverter<T> implements Converter<T, T> {
  900. private final Validator<? super T> validator;
  901. /**
  902. * Creates a new converter wrapping the given validator.
  903. *
  904. * @param validator
  905. * the validator to wrap
  906. */
  907. public ValidatorAsConverter(Validator<? super T> validator) {
  908. this.validator = validator;
  909. }
  910. @Override
  911. public Result<T> convertToModel(T value, ValueContext context) {
  912. ValidationResult validationResult = validator.apply(value, context);
  913. if (validationResult.isError()) {
  914. return Result.error(validationResult.getErrorMessage());
  915. } else {
  916. return Result.ok(value);
  917. }
  918. }
  919. @Override
  920. public T convertToPresentation(T value, ValueContext context) {
  921. return value;
  922. }
  923. }
  924. /**
  925. * Converter decorator-strategy pattern to use initially provided "delegate"
  926. * converter to execute its logic until the {@code setIdentity()} method is
  927. * called. Once the method is called the class changes its behavior to the
  928. * same as {@link Converter#identity()} behavior.
  929. */
  930. private static class ConverterDelegate<FIELDVALUE>
  931. implements Converter<FIELDVALUE, FIELDVALUE> {
  932. private Converter<FIELDVALUE, FIELDVALUE> delegate;
  933. private ConverterDelegate(Converter<FIELDVALUE, FIELDVALUE> converter) {
  934. delegate = converter;
  935. }
  936. @Override
  937. public Result<FIELDVALUE> convertToModel(FIELDVALUE value,
  938. ValueContext context) {
  939. if (delegate == null) {
  940. return Result.ok(value);
  941. } else {
  942. return delegate.convertToModel(value, context);
  943. }
  944. }
  945. @Override
  946. public FIELDVALUE convertToPresentation(FIELDVALUE value,
  947. ValueContext context) {
  948. if (delegate == null) {
  949. return value;
  950. } else {
  951. return delegate.convertToPresentation(value, context);
  952. }
  953. }
  954. void setIdentity() {
  955. delegate = null;
  956. }
  957. }
  958. private final BinderPropertySet<BEAN> propertySet;
  959. /**
  960. * Property names that have been used for creating a binding.
  961. */
  962. private final Set<String> boundProperties = new HashSet<>();
  963. private final Map<HasValue<?>, BindingBuilder<BEAN, ?>> incompleteMemberFieldBindings = new IdentityHashMap<>();
  964. private BEAN bean;
  965. private final Set<BindingImpl<BEAN, ?, ?>> bindings = new LinkedHashSet<>();
  966. private final Map<HasValue<?>, BindingBuilder<BEAN, ?>> incompleteBindings = new IdentityHashMap<>();
  967. private final List<Validator<? super BEAN>> validators = new ArrayList<>();
  968. private final Map<HasValue<?>, ConverterDelegate<?>> initialConverters = new IdentityHashMap<>();
  969. private EventRouter eventRouter;
  970. private Label statusLabel;
  971. private BinderValidationStatusHandler<BEAN> statusHandler;
  972. private boolean hasChanges = false;
  973. /**
  974. * Creates a binder using a custom {@link BinderPropertySet} implementation
  975. * for finding and resolving property names for
  976. * {@link #bindInstanceFields(Object)}, {@link #bind(HasValue, String)} and
  977. * {@link BindingBuilder#bind(String)}.
  978. *
  979. * @param propertySet
  980. * the binder property set implementation to use, not
  981. * <code>null</code>.
  982. */
  983. protected Binder(BinderPropertySet<BEAN> propertySet) {
  984. Objects.requireNonNull(propertySet, "propertySet cannot be null");
  985. this.propertySet = propertySet;
  986. }
  987. /**
  988. * Creates a new binder that uses reflection based on the provided bean type
  989. * to resolve bean properties.
  990. *
  991. * @param beanType
  992. * the bean type to use, not <code>null</code>
  993. */
  994. public Binder(Class<BEAN> beanType) {
  995. this(BeanBinderPropertySet.get(beanType));
  996. }
  997. /**
  998. * Creates a new binder without support for creating bindings based on
  999. * property names. Use an alternative constructor, such as
  1000. * {@link Binder#Binder(Class)}, to create a binder that support creating
  1001. * bindings based on instance fields through
  1002. * {@link #bindInstanceFields(Object)}, or based on a property name through
  1003. * {@link #bind(HasValue, String)} or {@link BindingBuilder#bind(String)}.
  1004. */
  1005. public Binder() {
  1006. this(new BinderPropertySet<BEAN>() {
  1007. @Override
  1008. public Stream<BinderPropertyDefinition<BEAN, ?>> getProperties() {
  1009. throw new IllegalStateException(
  1010. "A Binder created with the default constructor doesn't support listing properties.");
  1011. }
  1012. @Override
  1013. public Optional<BinderPropertyDefinition<BEAN, ?>> getProperty(
  1014. String name) {
  1015. throw new IllegalStateException(
  1016. "A Binder created with the default constructor doesn't support finding properties by name.");
  1017. }
  1018. });
  1019. }
  1020. /**
  1021. * Creates a binder using a custom {@link BinderPropertySet} implementation
  1022. * for finding and resolving property names for
  1023. * {@link #bindInstanceFields(Object)}, {@link #bind(HasValue, String)} and
  1024. * {@link BindingBuilder#bind(String)}.
  1025. * <p>
  1026. * This functionality is provided as static method instead of as a public
  1027. * constructor in order to make it possible to use a custom property set
  1028. * without creating a subclass while still leaving the public constructors
  1029. * focused on the common use cases.
  1030. *
  1031. * @see Binder#Binder()
  1032. * @see Binder#Binder(Class)
  1033. *
  1034. * @param propertySet
  1035. * the binder property set implementation to use, not
  1036. * <code>null</code>.
  1037. * @return a new binder using the provided property set, not
  1038. * <code>null</code>
  1039. */
  1040. public static <BEAN> Binder<BEAN> withPropertySet(
  1041. BinderPropertySet<BEAN> propertySet) {
  1042. return new Binder<>(propertySet);
  1043. }
  1044. /**
  1045. * Returns the bean that has been bound with {@link #bind}, or null if a
  1046. * bean is not currently bound.
  1047. *
  1048. * @return the currently bound bean if any
  1049. */
  1050. public BEAN getBean() {
  1051. return bean;
  1052. }
  1053. /**
  1054. * Creates a new binding for the given field. The returned builder may be
  1055. * further configured before invoking
  1056. * {@link BindingBuilder#bind(ValueProvider, Setter)} which completes the
  1057. * binding. Until {@code Binding.bind} is called, the binding has no effect.
  1058. * <p>
  1059. * <strong>Note:</strong> Not all {@link HasValue} implementations support
  1060. * passing {@code null} as the value. For these the Binder will
  1061. * automatically change {@code null} to a null representation provided by
  1062. * {@link HasValue#getEmptyValue()}. This conversion is one-way only, if you
  1063. * want to have a two-way mapping back to {@code null}, use
  1064. * {@link BindingBuilder#withNullRepresentation(Object)}.
  1065. *
  1066. * @param <FIELDVALUE>
  1067. * the value type of the field
  1068. * @param field
  1069. * the field to be bound, not null
  1070. * @return the new binding
  1071. *
  1072. * @see #bind(HasValue, ValueProvider, Setter)
  1073. */
  1074. public <FIELDVALUE> BindingBuilder<BEAN, FIELDVALUE> forField(
  1075. HasValue<FIELDVALUE> field) {
  1076. Objects.requireNonNull(field, "field cannot be null");
  1077. // clear previous errors for this field and any bean level validation
  1078. clearError(field);
  1079. getStatusLabel().ifPresent(label -> label.setValue(""));
  1080. return createBinding(field, createNullRepresentationAdapter(field),
  1081. this::handleValidationStatus);
  1082. }
  1083. /**
  1084. * Creates a new binding for the given field. The returned builder may be
  1085. * further configured before invoking {@link #bindInstanceFields(Object)}.
  1086. * Unlike with the {@link #forField(HasValue)} method, no explicit call to
  1087. * {@link BindingBuilder#bind(String)} is needed to complete this binding in
  1088. * the case that the name of the field matches a field name found in the
  1089. * bean.
  1090. *
  1091. * @param <FIELDVALUE>
  1092. * the value type of the field
  1093. * @param field
  1094. * the field to be bound, not null
  1095. * @return the new binding builder
  1096. *
  1097. * @see #forField(HasValue)
  1098. * @see #bindInstanceFields(Object)
  1099. */
  1100. public <FIELDVALUE> BindingBuilder<BEAN, FIELDVALUE> forMemberField(
  1101. HasValue<FIELDVALUE> field) {
  1102. incompleteMemberFieldBindings.put(field, null);
  1103. return forField(field);
  1104. }
  1105. /**
  1106. * Binds a field to a bean property represented by the given getter and
  1107. * setter pair. The functions are used to update the field value from the
  1108. * property and to store the field value to the property, respectively.
  1109. * <p>
  1110. * Use the {@link #forField(HasValue)} overload instead if you want to
  1111. * further configure the new binding.
  1112. * <p>
  1113. * <strong>Note:</strong> Not all {@link HasValue} implementations support
  1114. * passing {@code null} as the value. For these the Binder will
  1115. * automatically change {@code null} to a null representation provided by
  1116. * {@link HasValue#getEmptyValue()}. This conversion is one-way only, if you
  1117. * want to have a two-way mapping back to {@code null}, use
  1118. * {@link #forField(HasValue)} and
  1119. * {@link BindingBuilder#withNullRepresentation(Object)}.
  1120. * <p>
  1121. * When a bean is bound with {@link Binder#setBean(BEAN)}, the field value
  1122. * is set to the return value of the given getter. The property value is
  1123. * then updated via the given setter whenever the field value changes. The
  1124. * setter may be null; in that case the property value is never updated and
  1125. * the binding is said to be <i>read-only</i>.
  1126. * <p>
  1127. * If the Binder is already bound to some bean, the newly bound field is
  1128. * associated with the corresponding bean property as described above.
  1129. * <p>
  1130. * The getter and setter can be arbitrary functions, for instance
  1131. * implementing user-defined conversion or validation. However, in the most
  1132. * basic use case you can simply pass a pair of method references to this
  1133. * method as follows:
  1134. *
  1135. * <pre>
  1136. * class Person {
  1137. * public String getName() { ... }
  1138. * public void setName(String name) { ... }
  1139. * }
  1140. *
  1141. * TextField nameField = new TextField();
  1142. * binder.bind(nameField, Person::getName, Person::setName);
  1143. * </pre>
  1144. *
  1145. * @param <FIELDVALUE>
  1146. * the value type of the field
  1147. * @param field
  1148. * the field to bind, not null
  1149. * @param getter
  1150. * the function to get the value of the property to the field,
  1151. * not null
  1152. * @param setter
  1153. * the function to write the field value to the property or null
  1154. * if read-only
  1155. * @return the newly created binding
  1156. */
  1157. public <FIELDVALUE> Binding<BEAN, FIELDVALUE> bind(
  1158. HasValue<FIELDVALUE> field, ValueProvider<BEAN, FIELDVALUE> getter,
  1159. Setter<BEAN, FIELDVALUE> setter) {
  1160. return forField(field).bind(getter, setter);
  1161. }
  1162. /**
  1163. * Binds the given field to the property with the given name. The getter and
  1164. * setter of the property are looked up using a {@link BinderPropertySet}.
  1165. * <p>
  1166. * For a <code>Binder</code> created using the {@link Binder#Binder(Class)}
  1167. * constructor, introspection will be used to find a Java Bean property. If
  1168. * a JSR-303 bean validation implementation is present on the classpath, a
  1169. * {@link BeanValidator} is also added to the binding.
  1170. * <p>
  1171. * The property must have an accessible getter method. It need not have an
  1172. * accessible setter; in that case the property value is never updated and
  1173. * the binding is said to be <i>read-only</i>.
  1174. *
  1175. * @param <FIELDVALUE>
  1176. * the value type of the field to bind
  1177. * @param field
  1178. * the field to bind, not null
  1179. * @param propertyName
  1180. * the name of the property to bind, not null
  1181. * @return the newly created binding
  1182. *
  1183. * @throws IllegalArgumentException
  1184. * if the property name is invalid
  1185. * @throws IllegalArgumentException
  1186. * if the property has no accessible getter
  1187. * @throws IllegalStateException
  1188. * if the binder is not configured with an appropriate
  1189. * {@link BinderPropertySet}
  1190. *
  1191. * @see #bind(HasValue, ValueProvider, Setter)
  1192. */
  1193. public <FIELDVALUE> Binding<BEAN, FIELDVALUE> bind(
  1194. HasValue<FIELDVALUE> field, String propertyName) {
  1195. return forField(field).bind(propertyName);
  1196. }
  1197. /**
  1198. * Binds the given bean to all the fields added to this Binder. A
  1199. * {@code null} value removes a currently bound bean.
  1200. * <p>
  1201. * When a bean is bound, the field values are updated by invoking their
  1202. * corresponding getter functions. Any changes to field values are reflected
  1203. * back to their corresponding property values of the bean as long as the
  1204. * bean is bound.
  1205. * <p>
  1206. * Any change made in the fields also runs validation for the field
  1207. * {@link Binding} and bean level validation for this binder (bean level
  1208. * validators are added using {@link Binder#withValidator(Validator)}.
  1209. *
  1210. * @see #readBean(Object)
  1211. * @see #writeBean(Object)
  1212. * @see #writeBeanIfValid(Object)
  1213. *
  1214. * @param bean
  1215. * the bean to edit, or {@code null} to remove a currently bound
  1216. * bean
  1217. */
  1218. public void setBean(BEAN bean) {
  1219. checkBindingsCompleted("setBean");
  1220. if (bean == null) {
  1221. if (this.bean != null) {
  1222. doRemoveBean(true);
  1223. }
  1224. } else {
  1225. doRemoveBean(false);
  1226. this.bean = bean;
  1227. bindings.forEach(b -> b.initFieldValue(bean));
  1228. // if there has been field value change listeners that trigger
  1229. // validation, need to make sure the validation errors are cleared
  1230. getValidationStatusHandler().statusChange(
  1231. BinderValidationStatus.createUnresolvedStatus(this));
  1232. fireStatusChangeEvent(false);
  1233. }
  1234. }
  1235. /**
  1236. * Removes the currently set bean, if any. If there is no bound bean, does
  1237. * nothing.
  1238. * <p>
  1239. * This is a shorthand for {@link #setBean(Object)} with {@code null} bean.
  1240. */
  1241. public void removeBean() {
  1242. setBean(null);
  1243. }
  1244. /**
  1245. * Reads the bound property values from the given bean to the corresponding
  1246. * fields.
  1247. * <p>
  1248. * The bean is not otherwise associated with this binder; in particular its
  1249. * property values are not bound to the field value changes. To achieve
  1250. * that, use {@link #setBean(BEAN)}.
  1251. *
  1252. * @see #setBean(Object)
  1253. * @see #writeBeanIfValid(Object)
  1254. * @see #writeBean(Object)
  1255. *
  1256. * @param bean
  1257. * the bean whose property values to read, not null
  1258. */
  1259. public void readBean(BEAN bean) {
  1260. Objects.requireNonNull(bean, "bean cannot be null");
  1261. checkBindingsCompleted("readBean");
  1262. setHasChanges(false);
  1263. bindings.forEach(binding -> binding.initFieldValue(bean));
  1264. getValidationStatusHandler().statusChange(
  1265. BinderValidationStatus.createUnresolvedStatus(this));
  1266. fireStatusChangeEvent(false);
  1267. }
  1268. /**
  1269. * Writes changes from the bound fields to the given bean if all validators
  1270. * (binding and bean level) pass.
  1271. * <p>
  1272. * If any field binding validator fails, no values are written and a
  1273. * {@code ValidationException} is thrown.
  1274. * <p>
  1275. * If all field level validators pass, the given bean is updated and bean
  1276. * level validators are run on the updated bean. If any bean level validator
  1277. * fails, the bean updates are reverted and a {@code ValidationException} is
  1278. * thrown.
  1279. *
  1280. * @see #writeBeanIfValid(Object)
  1281. * @see #readBean(Object)
  1282. * @see #setBean(Object)
  1283. *
  1284. * @param bean
  1285. * the object to which to write the field values, not
  1286. * {@code null}
  1287. * @throws ValidationException
  1288. * if some of the bound field values fail to validate
  1289. */
  1290. public void writeBean(BEAN bean) throws ValidationException {
  1291. BinderValidationStatus<BEAN> status = doWriteIfValid(bean);
  1292. if (status.hasErrors()) {
  1293. throw new ValidationException(status.getFieldValidationErrors(),
  1294. status.getBeanValidationErrors());
  1295. }
  1296. }
  1297. /**
  1298. * Writes changes from the bound fields to the given bean if all validators
  1299. * (binding and bean level) pass.
  1300. * <p>
  1301. * If any field binding validator fails, no values are written and
  1302. * <code>false</code> is returned.
  1303. * <p>
  1304. * If all field level validators pass, the given bean is updated and bean
  1305. * level validators are run on the updated bean. If any bean level validator
  1306. * fails, the bean updates are reverted and <code>false</code> is returned.
  1307. *
  1308. * @see #writeBean(Object)
  1309. * @see #readBean(Object)
  1310. * @see #setBean(Object)
  1311. *
  1312. * @param bean
  1313. * the object to which to write the field values, not
  1314. * {@code null}
  1315. * @return {@code true} if there was no validation errors and the bean was
  1316. * updated, {@code false} otherwise
  1317. */
  1318. public boolean writeBeanIfValid(BEAN bean) {
  1319. return doWriteIfValid(bean).isOk();
  1320. }
  1321. /**
  1322. * Writes the field values into the given bean if all field level validators
  1323. * pass. Runs bean level validators on the bean after writing.
  1324. *
  1325. * @param bean
  1326. * the bean to write field values into
  1327. * @return a list of field validation errors if such occur, otherwise a list
  1328. * of bean validation errors.
  1329. */
  1330. @SuppressWarnings({ "rawtypes", "unchecked" })
  1331. private BinderValidationStatus<BEAN> doWriteIfValid(BEAN bean) {
  1332. Objects.requireNonNull(bean, "bean cannot be null");
  1333. // First run fields level validation
  1334. List<BindingValidationStatus<?>> bindingStatuses = validateBindings();
  1335. // If no validation errors then update bean
  1336. if (bindingStatuses.stream().filter(BindingValidationStatus::isError)
  1337. .findAny().isPresent()) {
  1338. fireStatusChangeEvent(true);
  1339. return new BinderValidationStatus<>(this, bindingStatuses,
  1340. Collections.emptyList());
  1341. }
  1342. // Store old bean values so we can restore them if validators fail
  1343. Map<Binding<BEAN, ?>, Object> oldValues = new HashMap<>();
  1344. bindings.forEach(
  1345. binding -> oldValues.put(binding, binding.getter.apply(bean)));
  1346. bindings.forEach(binding -> binding.writeFieldValue(bean));
  1347. // Now run bean level validation against the updated bean
  1348. List<ValidationResult> binderResults = validateBean(bean);
  1349. boolean hasErrors = binderResults.stream()
  1350. .filter(ValidationResult::isError).findAny().isPresent();
  1351. if (hasErrors) {
  1352. // Bean validator failed, revert values
  1353. bindings.forEach((BindingImpl binding) -> binding.setter
  1354. .accept(bean, oldValues.get(binding)));
  1355. } else {
  1356. // Write successful, reset hasChanges to false
  1357. setHasChanges(false);
  1358. }
  1359. fireStatusChangeEvent(hasErrors);
  1360. return new BinderValidationStatus<>(this, bindingStatuses,
  1361. binderResults);
  1362. }
  1363. /**
  1364. * Adds an bean level validator.
  1365. * <p>
  1366. * Bean level validators are applied on the bean instance after the bean is
  1367. * updated. If the validators fail, the bean instance is reverted to its
  1368. * previous state.
  1369. *
  1370. * @see #writeBean(Object)
  1371. * @see #writeBeanIfValid(Object)
  1372. * @see #withValidator(SerializablePredicate, String)
  1373. * @see #withValidator(SerializablePredicate, ErrorMessageProvider)
  1374. *
  1375. * @param validator
  1376. * the validator to add, not null
  1377. * @return this binder, for chaining
  1378. */
  1379. public Binder<BEAN> withValidator(Validator<? super BEAN> validator) {
  1380. Objects.requireNonNull(validator, "validator cannot be null");
  1381. validators.add(validator);
  1382. return this;
  1383. }
  1384. /**
  1385. * A convenience method to add a validator to this binder using the
  1386. * {@link Validator#from(SerializablePredicate, String)} factory method.
  1387. * <p>
  1388. * Bean level validators are applied on the bean instance after the bean is
  1389. * updated. If the validators fail, the bean instance is reverted to its
  1390. * previous state.
  1391. *
  1392. * @see #writeBean(Object)
  1393. * @see #writeBeanIfValid(Object)
  1394. * @see #withValidator(Validator)
  1395. * @see #withValidator(SerializablePredicate, ErrorMessageProvider)
  1396. *
  1397. * @param predicate
  1398. * the predicate performing validation, not null
  1399. * @param message
  1400. * the error message to report in case validation failure
  1401. * @return this binder, for chaining
  1402. */
  1403. public Binder<BEAN> withValidator(SerializablePredicate<BEAN> predicate,
  1404. String message) {
  1405. return withValidator(Validator.from(predicate, message));
  1406. }
  1407. /**
  1408. * A convenience method to add a validator to this binder using the
  1409. * {@link Validator#from(SerializablePredicate, ErrorMessageProvider)}
  1410. * factory method.
  1411. * <p>
  1412. * Bean level validators are applied on the bean instance after the bean is
  1413. * updated. If the validators fail, the bean instance is reverted to its
  1414. * previous state.
  1415. *
  1416. * @see #writeBean(Object)
  1417. * @see #writeBeanIfValid(Object)
  1418. * @see #withValidator(Validator)
  1419. * @see #withValidator(SerializablePredicate, String)
  1420. *
  1421. * @param predicate
  1422. * the predicate performing validation, not null
  1423. * @param errorMessageProvider
  1424. * the provider to generate error messages, not null
  1425. * @return this binder, for chaining
  1426. */
  1427. public Binder<BEAN> withValidator(SerializablePredicate<BEAN> predicate,
  1428. ErrorMessageProvider errorMessageProvider) {
  1429. return withValidator(Validator.from(predicate, errorMessageProvider));
  1430. }
  1431. /**
  1432. * Validates the values of all bound fields and returns the validation
  1433. * status.
  1434. * <p>
  1435. * If all field level validators pass, and {@link #setBean(Object)} has been
  1436. * used to bind to a bean, bean level validators are run for that bean. Bean
  1437. * level validators are ignored if there is no bound bean or if any field
  1438. * level validator fails.
  1439. * <p>
  1440. *
  1441. * @return validation status for the binder
  1442. */
  1443. public BinderValidationStatus<BEAN> validate() {
  1444. List<BindingValidationStatus<?>> bindingStatuses = validateBindings();
  1445. BinderValidationStatus<BEAN> validationStatus;
  1446. if (bindingStatuses.stream().filter(BindingValidationStatus::isError)
  1447. .findAny().isPresent() || bean == null) {
  1448. validationStatus = new BinderValidationStatus<>(this,
  1449. bindingStatuses, Collections.emptyList());
  1450. } else {
  1451. validationStatus = new BinderValidationStatus<>(this,
  1452. bindingStatuses, validateBean(bean));
  1453. }
  1454. getValidationStatusHandler().statusChange(validationStatus);
  1455. fireStatusChangeEvent(validationStatus.hasErrors());
  1456. return validationStatus;
  1457. }
  1458. /**
  1459. * Validates the bindings and returns the result of the validation as a list
  1460. * of validation statuses.
  1461. * <p>
  1462. * Does not run bean validators.
  1463. *
  1464. * @see #validateBean(Object)
  1465. *
  1466. * @return an immutable list of validation results for bindings
  1467. */
  1468. private List<BindingValidationStatus<?>> validateBindings() {
  1469. List<BindingValidationStatus<?>> results = new ArrayList<>();
  1470. for (BindingImpl<?, ?, ?> binding : bindings) {
  1471. results.add(binding.doValidation());
  1472. }
  1473. return results;
  1474. }
  1475. /**
  1476. * Validates the {@code bean} using validators added using
  1477. * {@link #withValidator(Validator)} and returns the result of the
  1478. * validation as a list of validation results.
  1479. * <p>
  1480. *
  1481. * @see #withValidator(Validator)
  1482. *
  1483. * @param bean
  1484. * the bean to validate
  1485. * @return a list of validation errors or an empty list if validation
  1486. * succeeded
  1487. */
  1488. private List<ValidationResult> validateBean(BEAN bean) {
  1489. Objects.requireNonNull(bean, "bean cannot be null");
  1490. List<ValidationResult> results = Collections.unmodifiableList(validators
  1491. .stream()
  1492. .map(validator -> validator.apply(bean, new ValueContext()))
  1493. .collect(Collectors.toList()));
  1494. return results;
  1495. }
  1496. /**
  1497. * Sets the label to show the binder level validation errors not related to
  1498. * any specific field.
  1499. * <p>
  1500. * Only the one validation error message is shown in this label at a time.
  1501. * <p>
  1502. * This is a convenience method for
  1503. * {@link #setValidationStatusHandler(BinderValidationStatusHandler)}, which
  1504. * means that this method cannot be used after the handler has been set.
  1505. * Also the handler cannot be set after this label has been set.
  1506. *
  1507. * @param statusLabel
  1508. * the status label to set
  1509. * @see #setValidationStatusHandler(BinderValidationStatusHandler)
  1510. * @see BindingBuilder#withStatusLabel(Label)
  1511. */
  1512. public void setStatusLabel(Label statusLabel) {
  1513. if (statusHandler != null) {
  1514. throw new IllegalStateException("Cannot set status label if a "
  1515. + BinderValidationStatusHandler.class.getSimpleName()
  1516. + " has already been set.");
  1517. }
  1518. this.statusLabel = statusLabel;
  1519. }
  1520. /**
  1521. * Gets the status label or an empty optional if none has been set.
  1522. *
  1523. * @return the optional status label
  1524. * @see #setStatusLabel(Label)
  1525. */
  1526. public Optional<Label> getStatusLabel() {
  1527. return Optional.ofNullable(statusLabel);
  1528. }
  1529. /**
  1530. * Sets the status handler to track form status changes.
  1531. * <p>
  1532. * Setting this handler will override the default behavior, which is to let
  1533. * fields show their validation status messages and show binder level
  1534. * validation errors or OK status in the label set with
  1535. * {@link #setStatusLabel(Label)}.
  1536. * <p>
  1537. * This handler cannot be set after the status label has been set with
  1538. * {@link #setStatusLabel(Label)}, or {@link #setStatusLabel(Label)} cannot
  1539. * be used after this handler has been set.
  1540. *
  1541. * @param statusHandler
  1542. * the status handler to set, not <code>null</code>
  1543. * @throws NullPointerException
  1544. * for <code>null</code> status handler
  1545. * @see #setStatusLabel(Label)
  1546. * @see BindingBuilder#withValidationStatusHandler(BindingValidationStatusHandler)
  1547. */
  1548. public void setValidationStatusHandler(
  1549. BinderValidationStatusHandler<BEAN> statusHandler) {
  1550. Objects.requireNonNull(statusHandler, "Cannot set a null "
  1551. + BinderValidationStatusHandler.class.getSimpleName());
  1552. if (statusLabel != null) {
  1553. throw new IllegalStateException("Cannot set "
  1554. + BinderValidationStatusHandler.class.getSimpleName()
  1555. + " if a status label has already been set.");
  1556. }
  1557. this.statusHandler = statusHandler;
  1558. }
  1559. /**
  1560. * Gets the status handler of this form.
  1561. * <p>
  1562. * If none has been set with
  1563. * {@link #setValidationStatusHandler(BinderValidationStatusHandler)}, the
  1564. * default implementation is returned.
  1565. *
  1566. * @return the status handler used, never <code>null</code>
  1567. * @see #setValidationStatusHandler(BinderValidationStatusHandler)
  1568. */
  1569. public BinderValidationStatusHandler<BEAN> getValidationStatusHandler() {
  1570. return Optional.ofNullable(statusHandler)
  1571. .orElse(this::handleBinderValidationStatus);
  1572. }
  1573. /**
  1574. * Adds status change listener to the binder.
  1575. * <p>
  1576. * The {@link Binder} status is changed whenever any of the following
  1577. * happens:
  1578. * <ul>
  1579. * <li>if it's bound and any of its bound field or select has been changed
  1580. * <li>{@link #writeBean(Object)} or {@link #writeBeanIfValid(Object)} is
  1581. * called
  1582. * <li>{@link #readBean(Object)} is called
  1583. * <li>{@link #setBean(Object)} is called
  1584. * <li>{@link #removeBean()} is called
  1585. * <li>{@link BindingBuilder#bind(ValueProvider, Setter)} is called
  1586. * <li>{@link Binder#validate()} or {@link Binding#validate()} is called
  1587. * </ul>
  1588. *
  1589. * @see #readBean(Object)
  1590. * @see #writeBean(Object)
  1591. * @see #writeBeanIfValid(Object)
  1592. * @see #setBean(Object)
  1593. * @see #removeBean()
  1594. * @see #forField(HasValue)
  1595. * @see #validate()
  1596. * @see Binding#validate()
  1597. *
  1598. * @param listener
  1599. * status change listener to add, not null
  1600. * @return a registration for the listener
  1601. */
  1602. public Registration addStatusChangeListener(StatusChangeListener listener) {
  1603. return getEventRouter().addListener(StatusChangeEvent.class, listener,
  1604. StatusChangeListener.class.getDeclaredMethods()[0]);
  1605. }
  1606. /**
  1607. * Creates a new binding with the given field.
  1608. *
  1609. * @param <FIELDVALUE>
  1610. * the value type of the field
  1611. * @param <TARGET>
  1612. * the target data type
  1613. * @param field
  1614. * the field to bind, not null
  1615. * @param converter
  1616. * the converter for converting between FIELDVALUE and TARGET
  1617. * types, not null
  1618. * @param handler
  1619. * the handler to notify of status changes, not null
  1620. * @return the new incomplete binding
  1621. */
  1622. protected <FIELDVALUE, TARGET> BindingBuilder<BEAN, TARGET> createBinding(
  1623. HasValue<FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converter,
  1624. BindingValidationStatusHandler handler) {
  1625. BindingBuilder<BEAN, TARGET> newBinding = doCreateBinding(field,
  1626. converter, handler);
  1627. if (incompleteMemberFieldBindings.containsKey(field)) {
  1628. incompleteMemberFieldBindings.put(field, newBinding);
  1629. }
  1630. incompleteBindings.put(field, newBinding);
  1631. return newBinding;
  1632. }
  1633. protected <FIELDVALUE, TARGET> BindingBuilder<BEAN, TARGET> doCreateBinding(
  1634. HasValue<FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converter,
  1635. BindingValidationStatusHandler handler) {
  1636. return new BindingBuilderImpl<>(this, field, converter, handler);
  1637. }
  1638. /**
  1639. * Clears the error condition of the given field, if any. The default
  1640. * implementation clears the
  1641. * {@link AbstractComponent#setComponentError(ErrorMessage) component error}
  1642. * of the field if it is a Component, otherwise does nothing.
  1643. *
  1644. * @param field
  1645. * the field with an invalid value
  1646. */
  1647. protected void clearError(HasValue<?> field) {
  1648. if (field instanceof AbstractComponent) {
  1649. ((AbstractComponent) field).setComponentError(null);
  1650. }
  1651. }
  1652. /**
  1653. * Handles a validation error emitted when trying to write the value of the
  1654. * given field. The default implementation sets the
  1655. * {@link AbstractComponent#setComponentError(ErrorMessage) component error}
  1656. * of the field if it is a Component, otherwise does nothing.
  1657. *
  1658. * @param field
  1659. * the field with the invalid value
  1660. * @param error
  1661. * the error message to set
  1662. */
  1663. protected void handleError(HasValue<?> field, String error) {
  1664. if (field instanceof AbstractComponent) {
  1665. ((AbstractComponent) field).setComponentError(new UserError(error));
  1666. }
  1667. }
  1668. /**
  1669. * Default {@link BindingValidationStatusHandler} functional method
  1670. * implementation.
  1671. *
  1672. * @param status
  1673. * the validation status
  1674. */
  1675. protected void handleValidationStatus(BindingValidationStatus<?> status) {
  1676. HasValue<?> source = status.getField();
  1677. clearError(source);
  1678. if (status.isError()) {
  1679. handleError(source, status.getMessage().get());
  1680. }
  1681. }
  1682. /**
  1683. * Returns the bindings for this binder.
  1684. *
  1685. * @return a set of the bindings
  1686. */
  1687. protected Set<BindingImpl<BEAN, ?, ?>> getBindings() {
  1688. return bindings;
  1689. }
  1690. /**
  1691. * The default binder level status handler.
  1692. * <p>
  1693. * Passes all field related results to the Binding status handlers. All
  1694. * other status changes are displayed in the status label, if one has been
  1695. * set with {@link #setStatusLabel(Label)}.
  1696. *
  1697. * @param binderStatus
  1698. * status of validation results from binding and/or bean level
  1699. * validators
  1700. */
  1701. protected void handleBinderValidationStatus(
  1702. BinderValidationStatus<BEAN> binderStatus) {
  1703. // let field events go to binding status handlers
  1704. binderStatus.getFieldValidationStatuses()
  1705. .forEach(status -> ((BindingImpl<?, ?, ?>) status.getBinding())
  1706. .notifyStatusHandler(status));
  1707. // show first possible error or OK status in the label if set
  1708. if (getStatusLabel().isPresent()) {
  1709. String statusMessage = binderStatus.getBeanValidationErrors()
  1710. .stream().findFirst().map(ValidationResult::getErrorMessage)
  1711. .orElse("");
  1712. getStatusLabel().get().setValue(statusMessage);
  1713. }
  1714. }
  1715. /**
  1716. * Sets whether the values of the fields this binder is bound to have
  1717. * changed since the last explicit call to either bind, write or read.
  1718. *
  1719. * @param hasChanges
  1720. * whether this binder should be marked to have changes
  1721. */
  1722. private void setHasChanges(boolean hasChanges) {
  1723. this.hasChanges = hasChanges;
  1724. }
  1725. /**
  1726. * Check whether any of the bound fields' values have changed since last
  1727. * explicit call to {@link #setBean(Object)}, {@link #readBean(Object)},
  1728. * {@link #removeBean()}, {@link #writeBean(Object)} or
  1729. * {@link #writeBeanIfValid(Object)}. Unsuccessful write operations will not
  1730. * affect this value. Return values for each case are compiled into the
  1731. * following table:
  1732. *
  1733. * <p>
  1734. *
  1735. * <table>
  1736. * <tr>
  1737. * <td></td>
  1738. * <td>After readBean, setBean or removeBean</td>
  1739. * <td>After valid user changes</td>
  1740. * <td>After invalid user changes</td>
  1741. * <td>After successful writeBean or writeBeanIfValid</td>
  1742. * <td>After unsuccessful writeBean or writeBeanIfValid</td>
  1743. * </tr>
  1744. * <tr>
  1745. * <td>A bean is currently bound</td>
  1746. * <td>{@code false}</td>
  1747. * <td>{@code false}</td>
  1748. * <td>{@code true}</td>
  1749. * <td>{@code false}</td>
  1750. * <td>no change</td>
  1751. * </tr>
  1752. * <tr>
  1753. * <td>No bean is currently bound</td>
  1754. * <td>{@code false}</td>
  1755. * <td>{@code true}</td>
  1756. * <td>{@code true}</td>
  1757. * <td>{@code false}</td>
  1758. * <td>no change</td>
  1759. * </tr>
  1760. * </table>
  1761. *
  1762. * @return whether any bound field's value has changed since last call to
  1763. * setBean, readBean, writeBean or writeBeanIfValid
  1764. */
  1765. public boolean hasChanges() {
  1766. return hasChanges;
  1767. }
  1768. /**
  1769. * Sets the read only state to the given value for all currently bound
  1770. * fields.
  1771. * <p>
  1772. * This is just a shorthand for calling setReadOnly for all currently bound
  1773. * fields. It means that fields bound after this method call won't be set
  1774. * read-only.
  1775. *
  1776. * @param fieldsReadOnly
  1777. * true to set the fields to read only, false to set them to read
  1778. * write
  1779. */
  1780. public void setReadOnly(boolean fieldsReadOnly) {
  1781. getBindings().stream().map(BindingImpl::getField)
  1782. .forEach(field -> field.setReadOnly(fieldsReadOnly));
  1783. }
  1784. /**
  1785. * Returns the event router for this binder.
  1786. *
  1787. * @return the event router, not null
  1788. */
  1789. protected EventRouter getEventRouter() {
  1790. if (eventRouter == null) {
  1791. eventRouter = new EventRouter();
  1792. }
  1793. return eventRouter;
  1794. }
  1795. /**
  1796. * Configures the {@code binding} with the property definition
  1797. * {@code definition} before it's being bound.
  1798. *
  1799. * @param binding
  1800. * a binding to configure
  1801. * @param definition
  1802. * a property definition information
  1803. * @return the new configured binding
  1804. */
  1805. protected BindingBuilder<BEAN, ?> configureBinding(
  1806. BindingBuilder<BEAN, ?> binding,
  1807. BinderPropertyDefinition<BEAN, ?> definition) {
  1808. return binding;
  1809. }
  1810. private void doRemoveBean(boolean fireStatusEvent) {
  1811. setHasChanges(false);
  1812. if (bean != null) {
  1813. bean = null;
  1814. }
  1815. getValidationStatusHandler().statusChange(
  1816. BinderValidationStatus.createUnresolvedStatus(this));
  1817. if (fireStatusEvent) {
  1818. fireStatusChangeEvent(false);
  1819. }
  1820. }
  1821. private void fireStatusChangeEvent(boolean hasValidationErrors) {
  1822. getEventRouter()
  1823. .fireEvent(new StatusChangeEvent(this, hasValidationErrors));
  1824. }
  1825. private <FIELDVALUE> Converter<FIELDVALUE, FIELDVALUE> createNullRepresentationAdapter(
  1826. HasValue<FIELDVALUE> field) {
  1827. Converter<FIELDVALUE, FIELDVALUE> nullRepresentationConverter = Converter
  1828. .from(fieldValue -> fieldValue,
  1829. modelValue -> Objects.isNull(modelValue)
  1830. ? field.getEmptyValue() : modelValue,
  1831. exception -> exception.getMessage());
  1832. ConverterDelegate<FIELDVALUE> converter = new ConverterDelegate<>(
  1833. nullRepresentationConverter);
  1834. initialConverters.put(field, converter);
  1835. return converter;
  1836. }
  1837. /**
  1838. * Throws if this binder has incomplete bindings.
  1839. *
  1840. * @param methodName
  1841. * name of the method where this call is originated from
  1842. * @throws IllegalStateException
  1843. * if this binder has incomplete bindings
  1844. */
  1845. private void checkBindingsCompleted(String methodName) {
  1846. if (!incompleteMemberFieldBindings.isEmpty()) {
  1847. throw new IllegalStateException(
  1848. "All bindings created with forMemberField must "
  1849. + "be completed with bindInstanceFields before calling "
  1850. + methodName);
  1851. }
  1852. if (!incompleteBindings.isEmpty()) {
  1853. throw new IllegalStateException(
  1854. "All bindings created with forField must be completed before calling "
  1855. + methodName);
  1856. }
  1857. }
  1858. /**
  1859. * Binds member fields found in the given object.
  1860. * <p>
  1861. * This method processes all (Java) member fields whose type extends
  1862. * {@link HasValue} and that can be mapped to a property id. Property name
  1863. * mapping is done based on the field name or on a @{@link PropertyId}
  1864. * annotation on the field. All non-null unbound fields for which a property
  1865. * name can be determined are bound to the property name using
  1866. * {@link BindingBuilder#bind(String)}.
  1867. * <p>
  1868. * For example:
  1869. *
  1870. * <pre>
  1871. * public class MyForm extends VerticalLayout {
  1872. * private TextField firstName = new TextField("First name");
  1873. * &#64;PropertyId("last")
  1874. * private TextField lastName = new TextField("Last name");
  1875. *
  1876. * MyForm myForm = new MyForm();
  1877. * ...
  1878. * binder.bindMemberFields(myForm);
  1879. * </pre>
  1880. *
  1881. * This binds the firstName TextField to a "firstName" property in the item,
  1882. * lastName TextField to a "last" property.
  1883. * <p>
  1884. * It's not always possible to bind a field to a property because their
  1885. * types are incompatible. E.g. custom converter is required to bind
  1886. * {@code HasValue<String>} and {@code Integer} property (that would be a
  1887. * case of "age" property). In such case {@link IllegalStateException} will
  1888. * be thrown unless the field has been configured manually before calling
  1889. * the {@link #bindInstanceFields(Object)} method.
  1890. * <p>
  1891. * It's always possible to do custom binding for any field: the
  1892. * {@link #bindInstanceFields(Object)} method doesn't override existing
  1893. * bindings.
  1894. *
  1895. * @param objectWithMemberFields
  1896. * The object that contains (Java) member fields to bind
  1897. * @throws IllegalStateException
  1898. * if there are incompatible HasValue&lt;T&gt; and property
  1899. * types
  1900. */
  1901. public void bindInstanceFields(Object objectWithMemberFields) {
  1902. Class<?> objectClass = objectWithMemberFields.getClass();
  1903. getFieldsInDeclareOrder(objectClass).stream()
  1904. .filter(memberField -> HasValue.class
  1905. .isAssignableFrom(memberField.getType()))
  1906. .forEach(memberField -> handleProperty(memberField,
  1907. objectWithMemberFields,
  1908. (property, type) -> bindProperty(objectWithMemberFields,
  1909. memberField, property, type)));
  1910. }
  1911. @SuppressWarnings("unchecked")
  1912. private BindingBuilder<BEAN, ?> getIncompleteMemberFieldBinding(
  1913. Field memberField, Object objectWithMemberFields) {
  1914. memberField.setAccessible(true);
  1915. try {
  1916. return incompleteMemberFieldBindings
  1917. .get(memberField.get(objectWithMemberFields));
  1918. } catch (IllegalArgumentException | IllegalAccessException e) {
  1919. throw new RuntimeException(e);
  1920. } finally {
  1921. memberField.setAccessible(false);
  1922. }
  1923. }
  1924. /**
  1925. * Binds {@code property} with {@code propertyType} to the field in the
  1926. * {@code objectWithMemberFields} instance using {@code memberField} as a
  1927. * reference to a member.
  1928. *
  1929. * @param objectWithMemberFields
  1930. * the object that contains (Java) member fields to build and
  1931. * bind
  1932. * @param memberField
  1933. * reference to a member field to bind
  1934. * @param property
  1935. * property name to bind
  1936. * @param propertyType
  1937. * type of the property
  1938. */
  1939. private void bindProperty(Object objectWithMemberFields, Field memberField,
  1940. String property, Class<?> propertyType) {
  1941. Type valueType = GenericTypeReflector.getTypeParameter(
  1942. memberField.getGenericType(),
  1943. HasValue.class.getTypeParameters()[0]);
  1944. if (valueType == null) {
  1945. throw new IllegalStateException(String.format(
  1946. "Unable to detect value type for the member '%s' in the "
  1947. + "class '%s'.",
  1948. memberField.getName(),
  1949. objectWithMemberFields.getClass().getName()));
  1950. }
  1951. if (propertyType.equals(GenericTypeReflector.erase(valueType))) {
  1952. HasValue<?> field;
  1953. // Get the field from the object
  1954. try {
  1955. field = (HasValue<?>) ReflectTools.getJavaFieldValue(
  1956. objectWithMemberFields, memberField, HasValue.class);
  1957. } catch (IllegalArgumentException | IllegalAccessException
  1958. | InvocationTargetException e) {
  1959. // If we cannot determine the value, just skip the field
  1960. return;
  1961. }
  1962. if (field == null) {
  1963. field = makeFieldInstance(
  1964. (Class<? extends HasValue<?>>) memberField.getType());
  1965. initializeField(objectWithMemberFields, memberField, field);
  1966. }
  1967. forField(field).bind(property);
  1968. } else {
  1969. throw new IllegalStateException(String.format(
  1970. "Property type '%s' doesn't "
  1971. + "match the field type '%s'. "
  1972. + "Binding should be configured manually using converter.",
  1973. propertyType.getName(), valueType.getTypeName()));
  1974. }
  1975. }
  1976. /**
  1977. * Makes an instance of the field type {@code fieldClass}.
  1978. * <p>
  1979. * The resulting field instance is used to bind a property to it using the
  1980. * {@link #bindInstanceFields(Object)} method.
  1981. * <p>
  1982. * The default implementation relies on the default constructor of the
  1983. * class. If there is no suitable default constructor or you want to
  1984. * configure the instantiated class then override this method and provide
  1985. * your own implementation.
  1986. *
  1987. * @see #bindInstanceFields(Object)
  1988. * @param fieldClass
  1989. * type of the field
  1990. * @return a {@code fieldClass} instance object
  1991. */
  1992. private HasValue<?> makeFieldInstance(
  1993. Class<? extends HasValue<?>> fieldClass) {
  1994. try {
  1995. return fieldClass.newInstance();
  1996. } catch (InstantiationException | IllegalAccessException e) {
  1997. throw new IllegalStateException(
  1998. String.format("Couldn't create an '%s' type instance",
  1999. fieldClass.getName()),
  2000. e);
  2001. }
  2002. }
  2003. /**
  2004. * Returns an array containing {@link Field} objects reflecting all the
  2005. * fields of the class or interface represented by this Class object. The
  2006. * elements in the array returned are sorted in declare order from sub class
  2007. * to super class.
  2008. *
  2009. * @param searchClass
  2010. * class to introspect
  2011. * @return list of all fields in the class considering hierarchy
  2012. */
  2013. private List<Field> getFieldsInDeclareOrder(Class<?> searchClass) {
  2014. ArrayList<Field> memberFieldInOrder = new ArrayList<>();
  2015. while (searchClass != null) {
  2016. memberFieldInOrder
  2017. .addAll(Arrays.asList(searchClass.getDeclaredFields()));
  2018. searchClass = searchClass.getSuperclass();
  2019. }
  2020. return memberFieldInOrder;
  2021. }
  2022. private void initializeField(Object objectWithMemberFields,
  2023. Field memberField, HasValue<?> value) {
  2024. try {
  2025. ReflectTools.setJavaFieldValue(objectWithMemberFields, memberField,
  2026. value);
  2027. } catch (IllegalArgumentException | IllegalAccessException
  2028. | InvocationTargetException e) {
  2029. throw new IllegalStateException(
  2030. String.format("Could not assign value to field '%s'",
  2031. memberField.getName()),
  2032. e);
  2033. }
  2034. }
  2035. private void handleProperty(Field field, Object objectWithMemberFields,
  2036. BiConsumer<String, Class<?>> propertyHandler) {
  2037. Optional<BinderPropertyDefinition<BEAN, ?>> descriptor = getPropertyDescriptor(
  2038. field);
  2039. if (!descriptor.isPresent()) {
  2040. return;
  2041. }
  2042. String propertyName = descriptor.get().getName();
  2043. if (boundProperties.contains(propertyName)) {
  2044. return;
  2045. }
  2046. BindingBuilder<BEAN, ?> tentativeBinding = getIncompleteMemberFieldBinding(
  2047. field, objectWithMemberFields);
  2048. if (tentativeBinding != null) {
  2049. tentativeBinding.bind(propertyName);
  2050. return;
  2051. }
  2052. propertyHandler.accept(propertyName, descriptor.get().getType());
  2053. boundProperties.add(propertyName);
  2054. }
  2055. private Optional<BinderPropertyDefinition<BEAN, ?>> getPropertyDescriptor(
  2056. Field field) {
  2057. PropertyId propertyIdAnnotation = field.getAnnotation(PropertyId.class);
  2058. String propertyId;
  2059. if (propertyIdAnnotation != null) {
  2060. // @PropertyId(propertyId) always overrides property id
  2061. propertyId = propertyIdAnnotation.value();
  2062. } else {
  2063. propertyId = field.getName();
  2064. }
  2065. String minifiedFieldName = minifyFieldName(propertyId);
  2066. return propertySet.getProperties()
  2067. .map(BinderPropertyDefinition::getName)
  2068. .filter(name -> minifyFieldName(name).equals(minifiedFieldName))
  2069. .findFirst().flatMap(propertySet::getProperty);
  2070. }
  2071. private String minifyFieldName(String fieldName) {
  2072. return fieldName.toLowerCase(Locale.ENGLISH).replace("_", "");
  2073. }
  2074. }