Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Binder.java 86KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  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 finalBinding = withConverter(
  586. createConverter(definition.getType()), false);
  587. finalBinding = definition.beforeBind(finalBinding);
  588. try {
  589. return finalBinding.bind(getter, setter);
  590. } finally {
  591. getBinder().boundProperties.add(propertyName);
  592. getBinder().incompleteMemberFieldBindings.remove(getField());
  593. }
  594. }
  595. @SuppressWarnings("unchecked")
  596. private Converter<TARGET, Object> createConverter(Class<?> getterType) {
  597. return Converter.from(fieldValue -> getterType.cast(fieldValue),
  598. propertyValue -> (TARGET) propertyValue, exception -> {
  599. throw new RuntimeException(exception);
  600. });
  601. }
  602. @Override
  603. public BindingBuilder<BEAN, TARGET> withValidator(
  604. Validator<? super TARGET> validator) {
  605. checkUnbound();
  606. Objects.requireNonNull(validator, "validator cannot be null");
  607. converterValidatorChain = converterValidatorChain
  608. .chain(new ValidatorAsConverter<>(validator));
  609. return this;
  610. }
  611. @Override
  612. public <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
  613. Converter<TARGET, NEWTARGET> converter) {
  614. return withConverter(converter, true);
  615. }
  616. @Override
  617. public BindingBuilder<BEAN, TARGET> withValidationStatusHandler(
  618. BindingValidationStatusHandler handler) {
  619. checkUnbound();
  620. Objects.requireNonNull(handler, "handler cannot be null");
  621. if (isStatusHandlerChanged) {
  622. throw new IllegalStateException("A "
  623. + BindingValidationStatusHandler.class.getSimpleName()
  624. + " has already been set");
  625. }
  626. isStatusHandlerChanged = true;
  627. statusHandler = handler;
  628. return this;
  629. }
  630. @Override
  631. public BindingBuilder<BEAN, TARGET> asRequired(
  632. ErrorMessageProvider errorMessageProvider) {
  633. checkUnbound();
  634. field.setRequiredIndicatorVisible(true);
  635. return withValidator(
  636. value -> !Objects.equals(value, field.getEmptyValue()),
  637. errorMessageProvider);
  638. }
  639. /**
  640. * Implements {@link #withConverter(Converter)} method with additional
  641. * possibility to disable (reset) default null representation converter.
  642. * <p>
  643. * The method {@link #withConverter(Converter)} calls this method with
  644. * {@code true} provided as the second argument value.
  645. *
  646. * @see #withConverter(Converter)
  647. *
  648. * @param converter
  649. * the converter to use, not null
  650. * @param resetNullRepresentation
  651. * if {@code true} then default null representation will be
  652. * deactivated (if not yet), otherwise it won't be removed
  653. * @return a new binding with the appropriate type
  654. * @param <NEWTARGET>
  655. * the type to convert to
  656. * @throws IllegalStateException
  657. * if {@code bind} has already been called
  658. */
  659. protected <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
  660. Converter<TARGET, NEWTARGET> converter,
  661. boolean resetNullRepresentation) {
  662. checkUnbound();
  663. Objects.requireNonNull(converter, "converter cannot be null");
  664. if (resetNullRepresentation) {
  665. getBinder().initialConverters.get(field).setIdentity();
  666. }
  667. return getBinder().createBinding(field,
  668. converterValidatorChain.chain(converter), statusHandler);
  669. }
  670. /**
  671. * Returns the {@code Binder} connected to this {@code Binding}
  672. * instance.
  673. *
  674. * @return the binder
  675. */
  676. protected Binder<BEAN> getBinder() {
  677. return binder;
  678. }
  679. /**
  680. * Throws if this binding is already completed and cannot be modified
  681. * anymore.
  682. *
  683. * @throws IllegalStateException
  684. * if this binding is already bound
  685. */
  686. protected void checkUnbound() {
  687. if (bound) {
  688. throw new IllegalStateException(
  689. "cannot modify binding: already bound to a property");
  690. }
  691. }
  692. @Override
  693. public HasValue<FIELDVALUE> getField() {
  694. return field;
  695. }
  696. }
  697. /**
  698. * An internal implementation of {@code Binding}.
  699. *
  700. * @param <BEAN>
  701. * the bean type, must match the Binder bean type
  702. * @param <FIELDVALUE>
  703. * the value type of the field
  704. * @param <TARGET>
  705. * the target data type of the binding, matches the field type
  706. * unless a converter has been set
  707. */
  708. protected static class BindingImpl<BEAN, FIELDVALUE, TARGET>
  709. implements Binding<BEAN, TARGET> {
  710. private final Binder<BEAN> binder;
  711. private final HasValue<FIELDVALUE> field;
  712. private final BindingValidationStatusHandler statusHandler;
  713. private final SerializableFunction<BEAN, TARGET> getter;
  714. private final Setter<BEAN, TARGET> setter;
  715. // Not final since we temporarily remove listener while changing values
  716. private Registration onValueChange;
  717. /**
  718. * Contains all converters and validators chained together in the
  719. * correct order.
  720. */
  721. private final Converter<FIELDVALUE, TARGET> converterValidatorChain;
  722. public BindingImpl(BindingBuilderImpl<BEAN, FIELDVALUE, TARGET> builder,
  723. SerializableFunction<BEAN, TARGET> getter,
  724. Setter<BEAN, TARGET> setter) {
  725. this.binder = builder.getBinder();
  726. this.field = builder.field;
  727. this.statusHandler = builder.statusHandler;
  728. converterValidatorChain = builder.converterValidatorChain;
  729. onValueChange = getField()
  730. .addValueChangeListener(this::handleFieldValueChange);
  731. this.getter = getter;
  732. this.setter = setter;
  733. }
  734. @Override
  735. public HasValue<FIELDVALUE> getField() {
  736. return field;
  737. }
  738. /**
  739. * Finds an appropriate locale to be used in conversion and validation.
  740. *
  741. * @return the found locale, not null
  742. */
  743. protected Locale findLocale() {
  744. Locale l = null;
  745. if (getField() instanceof Component) {
  746. l = ((Component) getField()).getLocale();
  747. }
  748. if (l == null && UI.getCurrent() != null) {
  749. l = UI.getCurrent().getLocale();
  750. }
  751. if (l == null) {
  752. l = Locale.getDefault();
  753. }
  754. return l;
  755. }
  756. @Override
  757. public BindingValidationStatus<TARGET> validate() {
  758. BindingValidationStatus<TARGET> status = doValidation();
  759. getBinder().getValidationStatusHandler()
  760. .statusChange(new BinderValidationStatus<>(getBinder(),
  761. Arrays.asList(status), Collections.emptyList()));
  762. getBinder().fireStatusChangeEvent(status.isError());
  763. return status;
  764. }
  765. /**
  766. * Returns the field value run through all converters and validators,
  767. * but doesn't pass the {@link BindingValidationStatus} to any status
  768. * handler.
  769. *
  770. * @return the result of the conversion
  771. */
  772. private Result<TARGET> doConversion() {
  773. FIELDVALUE fieldValue = field.getValue();
  774. return converterValidatorChain.convertToModel(fieldValue,
  775. createValueContext());
  776. }
  777. private BindingValidationStatus<TARGET> toValidationStatus(
  778. Result<TARGET> result) {
  779. return new BindingValidationStatus<>(this,
  780. result.isError()
  781. ? ValidationResult.error(result.getMessage().get())
  782. : ValidationResult.ok());
  783. }
  784. /**
  785. * Returns the field value run through all converters and validators,
  786. * but doesn't pass the {@link BindingValidationStatus} to any status
  787. * handler.
  788. *
  789. * @return the validation status
  790. */
  791. private BindingValidationStatus<TARGET> doValidation() {
  792. return toValidationStatus(doConversion());
  793. }
  794. /**
  795. * Creates a value context from the current state of the binding and its
  796. * field.
  797. *
  798. * @return the value context
  799. */
  800. protected ValueContext createValueContext() {
  801. if (field instanceof Component) {
  802. return new ValueContext((Component) field);
  803. }
  804. return new ValueContext(findLocale());
  805. }
  806. /**
  807. * Sets the field value by invoking the getter function on the given
  808. * bean. The default listener attached to the field will be removed for
  809. * the duration of this update.
  810. *
  811. * @param bean
  812. * the bean to fetch the property value from
  813. */
  814. private void initFieldValue(BEAN bean) {
  815. assert bean != null;
  816. assert onValueChange != null;
  817. onValueChange.remove();
  818. try {
  819. getField().setValue(convertDataToFieldType(bean));
  820. } finally {
  821. onValueChange = getField()
  822. .addValueChangeListener(this::handleFieldValueChange);
  823. }
  824. }
  825. private FIELDVALUE convertDataToFieldType(BEAN bean) {
  826. return converterValidatorChain.convertToPresentation(
  827. getter.apply(bean), createValueContext());
  828. }
  829. /**
  830. * Handles the value change triggered by the bound field.
  831. *
  832. * @param event
  833. */
  834. private void handleFieldValueChange(
  835. ValueChangeEvent<FIELDVALUE> event) {
  836. getBinder().setHasChanges(true);
  837. List<ValidationResult> binderValidationResults = Collections
  838. .emptyList();
  839. BindingValidationStatus<TARGET> fieldValidationStatus;
  840. if (getBinder().getBean() != null) {
  841. BEAN bean = getBinder().getBean();
  842. fieldValidationStatus = writeFieldValue(bean);
  843. if (!getBinder().bindings.stream()
  844. .map(BindingImpl::doValidation)
  845. .anyMatch(BindingValidationStatus::isError)) {
  846. binderValidationResults = getBinder().validateBean(bean);
  847. if (!binderValidationResults.stream()
  848. .anyMatch(ValidationResult::isError)) {
  849. getBinder().setHasChanges(false);
  850. }
  851. }
  852. } else {
  853. fieldValidationStatus = doValidation();
  854. }
  855. BinderValidationStatus<BEAN> status = new BinderValidationStatus<>(
  856. getBinder(), Arrays.asList(fieldValidationStatus),
  857. binderValidationResults);
  858. getBinder().getValidationStatusHandler().statusChange(status);
  859. getBinder().fireStatusChangeEvent(status.hasErrors());
  860. }
  861. /**
  862. * Write the field value by invoking the setter function on the given
  863. * bean, if the value passes all registered validators.
  864. *
  865. * @param bean
  866. * the bean to set the property value to
  867. */
  868. private BindingValidationStatus<TARGET> writeFieldValue(BEAN bean) {
  869. assert bean != null;
  870. Result<TARGET> result = doConversion();
  871. if (setter != null) {
  872. result.ifOk(value -> setter.accept(bean, value));
  873. }
  874. return toValidationStatus(result);
  875. }
  876. /**
  877. * Returns the {@code Binder} connected to this {@code Binding}
  878. * instance.
  879. *
  880. * @return the binder
  881. */
  882. protected Binder<BEAN> getBinder() {
  883. return binder;
  884. }
  885. private void notifyStatusHandler(BindingValidationStatus<?> status) {
  886. statusHandler.statusChange(status);
  887. }
  888. }
  889. /**
  890. * Wraps a validator as a converter.
  891. * <p>
  892. * The type of the validator must be of the same type as this converter or a
  893. * super type of it.
  894. *
  895. * @param <T>
  896. * the type of the converter
  897. */
  898. private static class ValidatorAsConverter<T> implements Converter<T, T> {
  899. private final Validator<? super T> validator;
  900. /**
  901. * Creates a new converter wrapping the given validator.
  902. *
  903. * @param validator
  904. * the validator to wrap
  905. */
  906. public ValidatorAsConverter(Validator<? super T> validator) {
  907. this.validator = validator;
  908. }
  909. @Override
  910. public Result<T> convertToModel(T value, ValueContext context) {
  911. ValidationResult validationResult = validator.apply(value, context);
  912. if (validationResult.isError()) {
  913. return Result.error(validationResult.getErrorMessage());
  914. } else {
  915. return Result.ok(value);
  916. }
  917. }
  918. @Override
  919. public T convertToPresentation(T value, ValueContext context) {
  920. return value;
  921. }
  922. }
  923. /**
  924. * Converter decorator-strategy pattern to use initially provided "delegate"
  925. * converter to execute its logic until the {@code setIdentity()} method is
  926. * called. Once the method is called the class changes its behavior to the
  927. * same as {@link Converter#identity()} behavior.
  928. */
  929. private static class ConverterDelegate<FIELDVALUE>
  930. implements Converter<FIELDVALUE, FIELDVALUE> {
  931. private Converter<FIELDVALUE, FIELDVALUE> delegate;
  932. private ConverterDelegate(Converter<FIELDVALUE, FIELDVALUE> converter) {
  933. delegate = converter;
  934. }
  935. @Override
  936. public Result<FIELDVALUE> convertToModel(FIELDVALUE value,
  937. ValueContext context) {
  938. if (delegate == null) {
  939. return Result.ok(value);
  940. } else {
  941. return delegate.convertToModel(value, context);
  942. }
  943. }
  944. @Override
  945. public FIELDVALUE convertToPresentation(FIELDVALUE value,
  946. ValueContext context) {
  947. if (delegate == null) {
  948. return value;
  949. } else {
  950. return delegate.convertToPresentation(value, context);
  951. }
  952. }
  953. void setIdentity() {
  954. delegate = null;
  955. }
  956. }
  957. private final BinderPropertySet<BEAN> propertySet;
  958. /**
  959. * Property names that have been used for creating a binding.
  960. */
  961. private final Set<String> boundProperties = new HashSet<>();
  962. private final Map<HasValue<?>, BindingBuilder<BEAN, ?>> incompleteMemberFieldBindings = new IdentityHashMap<>();
  963. private BEAN bean;
  964. private final Set<BindingImpl<BEAN, ?, ?>> bindings = new LinkedHashSet<>();
  965. private final Map<HasValue<?>, BindingBuilder<BEAN, ?>> incompleteBindings = new IdentityHashMap<>();
  966. private final List<Validator<? super BEAN>> validators = new ArrayList<>();
  967. private final Map<HasValue<?>, ConverterDelegate<?>> initialConverters = new IdentityHashMap<>();
  968. private EventRouter eventRouter;
  969. private Label statusLabel;
  970. private BinderValidationStatusHandler<BEAN> statusHandler;
  971. private boolean hasChanges = false;
  972. /**
  973. * Creates a binder using a custom {@link BinderPropertySet} implementation
  974. * for finding and resolving property names for
  975. * {@link #bindInstanceFields(Object)}, {@link #bind(HasValue, String)} and
  976. * {@link BindingBuilder#bind(String)}.
  977. *
  978. * @param propertySet
  979. * the binder property set implementation to use, not
  980. * <code>null</code>.
  981. */
  982. protected Binder(BinderPropertySet<BEAN> propertySet) {
  983. Objects.requireNonNull(propertySet, "propertySet cannot be null");
  984. this.propertySet = propertySet;
  985. }
  986. /**
  987. * Creates a new binder that uses reflection based on the provided bean type
  988. * to resolve bean properties. If a JSR-303 bean validation implementation
  989. * is present on the classpath, a {@link BeanValidator} is added to each
  990. * binding that is defined using a property name.
  991. *
  992. * @param beanType
  993. * the bean type to use, not <code>null</code>
  994. */
  995. public Binder(Class<BEAN> beanType) {
  996. this(BeanBinderPropertySet.get(beanType));
  997. }
  998. /**
  999. * Creates a new binder without support for creating bindings based on
  1000. * property names. Use an alternative constructor, such as
  1001. * {@link Binder#Binder(Class)}, to create a binder that support creating
  1002. * bindings based on instance fields through
  1003. * {@link #bindInstanceFields(Object)}, or based on a property name through
  1004. * {@link #bind(HasValue, String)} or {@link BindingBuilder#bind(String)}.
  1005. */
  1006. public Binder() {
  1007. this(new BinderPropertySet<BEAN>() {
  1008. @Override
  1009. public Stream<BinderPropertyDefinition<BEAN, ?>> getProperties() {
  1010. throw new IllegalStateException(
  1011. "A Binder created with the default constructor doesn't support listing properties.");
  1012. }
  1013. @Override
  1014. public Optional<BinderPropertyDefinition<BEAN, ?>> getProperty(
  1015. String name) {
  1016. throw new IllegalStateException(
  1017. "A Binder created with the default constructor doesn't support finding properties by name.");
  1018. }
  1019. });
  1020. }
  1021. /**
  1022. * Creates a binder using a custom {@link BinderPropertySet} implementation
  1023. * for finding and resolving property names for
  1024. * {@link #bindInstanceFields(Object)}, {@link #bind(HasValue, String)} and
  1025. * {@link BindingBuilder#bind(String)}.
  1026. * <p>
  1027. * This functionality is provided as static method instead of as a public
  1028. * constructor in order to make it possible to use a custom property set
  1029. * without creating a subclass while still leaving the public constructors
  1030. * focused on the common use cases.
  1031. *
  1032. * @see Binder#Binder()
  1033. * @see Binder#Binder(Class)
  1034. *
  1035. * @param propertySet
  1036. * the binder property set implementation to use, not
  1037. * <code>null</code>.
  1038. * @return a new binder using the provided property set, not
  1039. * <code>null</code>
  1040. */
  1041. public static <BEAN> Binder<BEAN> withPropertySet(
  1042. BinderPropertySet<BEAN> propertySet) {
  1043. return new Binder<>(propertySet);
  1044. }
  1045. /**
  1046. * Returns the bean that has been bound with {@link #bind}, or null if a
  1047. * bean is not currently bound.
  1048. *
  1049. * @return the currently bound bean if any
  1050. */
  1051. public BEAN getBean() {
  1052. return bean;
  1053. }
  1054. /**
  1055. * Creates a new binding for the given field. The returned builder may be
  1056. * further configured before invoking
  1057. * {@link BindingBuilder#bind(ValueProvider, Setter)} which completes the
  1058. * binding. Until {@code Binding.bind} is called, the binding has no effect.
  1059. * <p>
  1060. * <strong>Note:</strong> Not all {@link HasValue} implementations support
  1061. * passing {@code null} as the value. For these the Binder will
  1062. * automatically change {@code null} to a null representation provided by
  1063. * {@link HasValue#getEmptyValue()}. This conversion is one-way only, if you
  1064. * want to have a two-way mapping back to {@code null}, use
  1065. * {@link BindingBuilder#withNullRepresentation(Object)}.
  1066. *
  1067. * @param <FIELDVALUE>
  1068. * the value type of the field
  1069. * @param field
  1070. * the field to be bound, not null
  1071. * @return the new binding
  1072. *
  1073. * @see #bind(HasValue, ValueProvider, Setter)
  1074. */
  1075. public <FIELDVALUE> BindingBuilder<BEAN, FIELDVALUE> forField(
  1076. HasValue<FIELDVALUE> field) {
  1077. Objects.requireNonNull(field, "field cannot be null");
  1078. // clear previous errors for this field and any bean level validation
  1079. clearError(field);
  1080. getStatusLabel().ifPresent(label -> label.setValue(""));
  1081. return createBinding(field, createNullRepresentationAdapter(field),
  1082. this::handleValidationStatus);
  1083. }
  1084. /**
  1085. * Creates a new binding for the given field. The returned builder may be
  1086. * further configured before invoking {@link #bindInstanceFields(Object)}.
  1087. * Unlike with the {@link #forField(HasValue)} method, no explicit call to
  1088. * {@link BindingBuilder#bind(String)} is needed to complete this binding in
  1089. * the case that the name of the field matches a field name found in the
  1090. * bean.
  1091. *
  1092. * @param <FIELDVALUE>
  1093. * the value type of the field
  1094. * @param field
  1095. * the field to be bound, not null
  1096. * @return the new binding builder
  1097. *
  1098. * @see #forField(HasValue)
  1099. * @see #bindInstanceFields(Object)
  1100. */
  1101. public <FIELDVALUE> BindingBuilder<BEAN, FIELDVALUE> forMemberField(
  1102. HasValue<FIELDVALUE> field) {
  1103. incompleteMemberFieldBindings.put(field, null);
  1104. return forField(field);
  1105. }
  1106. /**
  1107. * Binds a field to a bean property represented by the given getter and
  1108. * setter pair. The functions are used to update the field value from the
  1109. * property and to store the field value to the property, respectively.
  1110. * <p>
  1111. * Use the {@link #forField(HasValue)} overload instead if you want to
  1112. * further configure the new binding.
  1113. * <p>
  1114. * <strong>Note:</strong> Not all {@link HasValue} implementations support
  1115. * passing {@code null} as the value. For these the Binder will
  1116. * automatically change {@code null} to a null representation provided by
  1117. * {@link HasValue#getEmptyValue()}. This conversion is one-way only, if you
  1118. * want to have a two-way mapping back to {@code null}, use
  1119. * {@link #forField(HasValue)} and
  1120. * {@link BindingBuilder#withNullRepresentation(Object)}.
  1121. * <p>
  1122. * When a bean is bound with {@link Binder#setBean(BEAN)}, the field value
  1123. * is set to the return value of the given getter. The property value is
  1124. * then updated via the given setter whenever the field value changes. The
  1125. * setter may be null; in that case the property value is never updated and
  1126. * the binding is said to be <i>read-only</i>.
  1127. * <p>
  1128. * If the Binder is already bound to some bean, the newly bound field is
  1129. * associated with the corresponding bean property as described above.
  1130. * <p>
  1131. * The getter and setter can be arbitrary functions, for instance
  1132. * implementing user-defined conversion or validation. However, in the most
  1133. * basic use case you can simply pass a pair of method references to this
  1134. * method as follows:
  1135. *
  1136. * <pre>
  1137. * class Person {
  1138. * public String getName() { ... }
  1139. * public void setName(String name) { ... }
  1140. * }
  1141. *
  1142. * TextField nameField = new TextField();
  1143. * binder.bind(nameField, Person::getName, Person::setName);
  1144. * </pre>
  1145. *
  1146. * @param <FIELDVALUE>
  1147. * the value type of the field
  1148. * @param field
  1149. * the field to bind, not null
  1150. * @param getter
  1151. * the function to get the value of the property to the field,
  1152. * not null
  1153. * @param setter
  1154. * the function to write the field value to the property or null
  1155. * if read-only
  1156. * @return the newly created binding
  1157. */
  1158. public <FIELDVALUE> Binding<BEAN, FIELDVALUE> bind(
  1159. HasValue<FIELDVALUE> field, ValueProvider<BEAN, FIELDVALUE> getter,
  1160. Setter<BEAN, FIELDVALUE> setter) {
  1161. return forField(field).bind(getter, setter);
  1162. }
  1163. /**
  1164. * Binds the given field to the property with the given name. The getter and
  1165. * setter of the property are looked up using a {@link BinderPropertySet}.
  1166. * <p>
  1167. * For a <code>Binder</code> created using the {@link Binder#Binder(Class)}
  1168. * constructor, introspection will be used to find a Java Bean property. If
  1169. * a JSR-303 bean validation implementation is present on the classpath, a
  1170. * {@link BeanValidator} is also added to the binding.
  1171. * <p>
  1172. * The property must have an accessible getter method. It need not have an
  1173. * accessible setter; in that case the property value is never updated and
  1174. * the binding is said to be <i>read-only</i>.
  1175. *
  1176. * @param <FIELDVALUE>
  1177. * the value type of the field to bind
  1178. * @param field
  1179. * the field to bind, not null
  1180. * @param propertyName
  1181. * the name of the property to bind, not null
  1182. * @return the newly created binding
  1183. *
  1184. * @throws IllegalArgumentException
  1185. * if the property name is invalid
  1186. * @throws IllegalArgumentException
  1187. * if the property has no accessible getter
  1188. * @throws IllegalStateException
  1189. * if the binder is not configured with an appropriate
  1190. * {@link BinderPropertySet}
  1191. *
  1192. * @see #bind(HasValue, ValueProvider, Setter)
  1193. */
  1194. public <FIELDVALUE> Binding<BEAN, FIELDVALUE> bind(
  1195. HasValue<FIELDVALUE> field, String propertyName) {
  1196. return forField(field).bind(propertyName);
  1197. }
  1198. /**
  1199. * Binds the given bean to all the fields added to this Binder. A
  1200. * {@code null} value removes a currently bound bean.
  1201. * <p>
  1202. * When a bean is bound, the field values are updated by invoking their
  1203. * corresponding getter functions. Any changes to field values are reflected
  1204. * back to their corresponding property values of the bean as long as the
  1205. * bean is bound.
  1206. * <p>
  1207. * Any change made in the fields also runs validation for the field
  1208. * {@link Binding} and bean level validation for this binder (bean level
  1209. * validators are added using {@link Binder#withValidator(Validator)}.
  1210. *
  1211. * @see #readBean(Object)
  1212. * @see #writeBean(Object)
  1213. * @see #writeBeanIfValid(Object)
  1214. *
  1215. * @param bean
  1216. * the bean to edit, or {@code null} to remove a currently bound
  1217. * bean
  1218. */
  1219. public void setBean(BEAN bean) {
  1220. checkBindingsCompleted("setBean");
  1221. if (bean == null) {
  1222. if (this.bean != null) {
  1223. doRemoveBean(true);
  1224. }
  1225. } else {
  1226. doRemoveBean(false);
  1227. this.bean = bean;
  1228. bindings.forEach(b -> b.initFieldValue(bean));
  1229. // if there has been field value change listeners that trigger
  1230. // validation, need to make sure the validation errors are cleared
  1231. getValidationStatusHandler().statusChange(
  1232. BinderValidationStatus.createUnresolvedStatus(this));
  1233. fireStatusChangeEvent(false);
  1234. }
  1235. }
  1236. /**
  1237. * Removes the currently set bean, if any. If there is no bound bean, does
  1238. * nothing.
  1239. * <p>
  1240. * This is a shorthand for {@link #setBean(Object)} with {@code null} bean.
  1241. */
  1242. public void removeBean() {
  1243. setBean(null);
  1244. }
  1245. /**
  1246. * Reads the bound property values from the given bean to the corresponding
  1247. * fields.
  1248. * <p>
  1249. * The bean is not otherwise associated with this binder; in particular its
  1250. * property values are not bound to the field value changes. To achieve
  1251. * that, use {@link #setBean(BEAN)}.
  1252. *
  1253. * @see #setBean(Object)
  1254. * @see #writeBeanIfValid(Object)
  1255. * @see #writeBean(Object)
  1256. *
  1257. * @param bean
  1258. * the bean whose property values to read, not null
  1259. */
  1260. public void readBean(BEAN bean) {
  1261. Objects.requireNonNull(bean, "bean cannot be null");
  1262. checkBindingsCompleted("readBean");
  1263. setHasChanges(false);
  1264. bindings.forEach(binding -> binding.initFieldValue(bean));
  1265. getValidationStatusHandler().statusChange(
  1266. BinderValidationStatus.createUnresolvedStatus(this));
  1267. fireStatusChangeEvent(false);
  1268. }
  1269. /**
  1270. * Writes changes from the bound fields to the given bean if all validators
  1271. * (binding and bean level) pass.
  1272. * <p>
  1273. * If any field binding validator fails, no values are written and a
  1274. * {@code ValidationException} is thrown.
  1275. * <p>
  1276. * If all field level validators pass, the given bean is updated and bean
  1277. * level validators are run on the updated bean. If any bean level validator
  1278. * fails, the bean updates are reverted and a {@code ValidationException} is
  1279. * thrown.
  1280. *
  1281. * @see #writeBeanIfValid(Object)
  1282. * @see #readBean(Object)
  1283. * @see #setBean(Object)
  1284. *
  1285. * @param bean
  1286. * the object to which to write the field values, not
  1287. * {@code null}
  1288. * @throws ValidationException
  1289. * if some of the bound field values fail to validate
  1290. */
  1291. public void writeBean(BEAN bean) throws ValidationException {
  1292. BinderValidationStatus<BEAN> status = doWriteIfValid(bean);
  1293. if (status.hasErrors()) {
  1294. throw new ValidationException(status.getFieldValidationErrors(),
  1295. status.getBeanValidationErrors());
  1296. }
  1297. }
  1298. /**
  1299. * Writes changes from the bound fields to the given bean if all validators
  1300. * (binding and bean level) pass.
  1301. * <p>
  1302. * If any field binding validator fails, no values are written and
  1303. * <code>false</code> is returned.
  1304. * <p>
  1305. * If all field level validators pass, the given bean is updated and bean
  1306. * level validators are run on the updated bean. If any bean level validator
  1307. * fails, the bean updates are reverted and <code>false</code> is returned.
  1308. *
  1309. * @see #writeBean(Object)
  1310. * @see #readBean(Object)
  1311. * @see #setBean(Object)
  1312. *
  1313. * @param bean
  1314. * the object to which to write the field values, not
  1315. * {@code null}
  1316. * @return {@code true} if there was no validation errors and the bean was
  1317. * updated, {@code false} otherwise
  1318. */
  1319. public boolean writeBeanIfValid(BEAN bean) {
  1320. return doWriteIfValid(bean).isOk();
  1321. }
  1322. /**
  1323. * Writes the field values into the given bean if all field level validators
  1324. * pass. Runs bean level validators on the bean after writing.
  1325. *
  1326. * @param bean
  1327. * the bean to write field values into
  1328. * @return a list of field validation errors if such occur, otherwise a list
  1329. * of bean validation errors.
  1330. */
  1331. @SuppressWarnings({ "rawtypes", "unchecked" })
  1332. private BinderValidationStatus<BEAN> doWriteIfValid(BEAN bean) {
  1333. Objects.requireNonNull(bean, "bean cannot be null");
  1334. // First run fields level validation
  1335. List<BindingValidationStatus<?>> bindingStatuses = validateBindings();
  1336. // If no validation errors then update bean
  1337. if (bindingStatuses.stream().filter(BindingValidationStatus::isError)
  1338. .findAny().isPresent()) {
  1339. fireStatusChangeEvent(true);
  1340. return new BinderValidationStatus<>(this, bindingStatuses,
  1341. Collections.emptyList());
  1342. }
  1343. // Store old bean values so we can restore them if validators fail
  1344. Map<Binding<BEAN, ?>, Object> oldValues = new HashMap<>();
  1345. bindings.forEach(
  1346. binding -> oldValues.put(binding, binding.getter.apply(bean)));
  1347. bindings.forEach(binding -> binding.writeFieldValue(bean));
  1348. // Now run bean level validation against the updated bean
  1349. List<ValidationResult> binderResults = validateBean(bean);
  1350. boolean hasErrors = binderResults.stream()
  1351. .filter(ValidationResult::isError).findAny().isPresent();
  1352. if (hasErrors) {
  1353. // Bean validator failed, revert values
  1354. bindings.forEach((BindingImpl binding) -> binding.setter
  1355. .accept(bean, oldValues.get(binding)));
  1356. } else {
  1357. // Write successful, reset hasChanges to false
  1358. setHasChanges(false);
  1359. }
  1360. fireStatusChangeEvent(hasErrors);
  1361. return new BinderValidationStatus<>(this, bindingStatuses,
  1362. binderResults);
  1363. }
  1364. /**
  1365. * Adds an bean level validator.
  1366. * <p>
  1367. * Bean level validators are applied on the bean instance after the bean is
  1368. * updated. If the validators fail, the bean instance is reverted to its
  1369. * previous state.
  1370. *
  1371. * @see #writeBean(Object)
  1372. * @see #writeBeanIfValid(Object)
  1373. * @see #withValidator(SerializablePredicate, String)
  1374. * @see #withValidator(SerializablePredicate, ErrorMessageProvider)
  1375. *
  1376. * @param validator
  1377. * the validator to add, not null
  1378. * @return this binder, for chaining
  1379. */
  1380. public Binder<BEAN> withValidator(Validator<? super BEAN> validator) {
  1381. Objects.requireNonNull(validator, "validator cannot be null");
  1382. validators.add(validator);
  1383. return this;
  1384. }
  1385. /**
  1386. * A convenience method to add a validator to this binder using the
  1387. * {@link Validator#from(SerializablePredicate, String)} factory method.
  1388. * <p>
  1389. * Bean level validators are applied on the bean instance after the bean is
  1390. * updated. If the validators fail, the bean instance is reverted to its
  1391. * previous state.
  1392. *
  1393. * @see #writeBean(Object)
  1394. * @see #writeBeanIfValid(Object)
  1395. * @see #withValidator(Validator)
  1396. * @see #withValidator(SerializablePredicate, ErrorMessageProvider)
  1397. *
  1398. * @param predicate
  1399. * the predicate performing validation, not null
  1400. * @param message
  1401. * the error message to report in case validation failure
  1402. * @return this binder, for chaining
  1403. */
  1404. public Binder<BEAN> withValidator(SerializablePredicate<BEAN> predicate,
  1405. String message) {
  1406. return withValidator(Validator.from(predicate, message));
  1407. }
  1408. /**
  1409. * A convenience method to add a validator to this binder using the
  1410. * {@link Validator#from(SerializablePredicate, ErrorMessageProvider)}
  1411. * factory method.
  1412. * <p>
  1413. * Bean level validators are applied on the bean instance after the bean is
  1414. * updated. If the validators fail, the bean instance is reverted to its
  1415. * previous state.
  1416. *
  1417. * @see #writeBean(Object)
  1418. * @see #writeBeanIfValid(Object)
  1419. * @see #withValidator(Validator)
  1420. * @see #withValidator(SerializablePredicate, String)
  1421. *
  1422. * @param predicate
  1423. * the predicate performing validation, not null
  1424. * @param errorMessageProvider
  1425. * the provider to generate error messages, not null
  1426. * @return this binder, for chaining
  1427. */
  1428. public Binder<BEAN> withValidator(SerializablePredicate<BEAN> predicate,
  1429. ErrorMessageProvider errorMessageProvider) {
  1430. return withValidator(Validator.from(predicate, errorMessageProvider));
  1431. }
  1432. /**
  1433. * Validates the values of all bound fields and returns the validation
  1434. * status.
  1435. * <p>
  1436. * If all field level validators pass, and {@link #setBean(Object)} has been
  1437. * used to bind to a bean, bean level validators are run for that bean. Bean
  1438. * level validators are ignored if there is no bound bean or if any field
  1439. * level validator fails.
  1440. * <p>
  1441. *
  1442. * @return validation status for the binder
  1443. */
  1444. public BinderValidationStatus<BEAN> validate() {
  1445. List<BindingValidationStatus<?>> bindingStatuses = validateBindings();
  1446. BinderValidationStatus<BEAN> validationStatus;
  1447. if (bindingStatuses.stream().filter(BindingValidationStatus::isError)
  1448. .findAny().isPresent() || bean == null) {
  1449. validationStatus = new BinderValidationStatus<>(this,
  1450. bindingStatuses, Collections.emptyList());
  1451. } else {
  1452. validationStatus = new BinderValidationStatus<>(this,
  1453. bindingStatuses, validateBean(bean));
  1454. }
  1455. getValidationStatusHandler().statusChange(validationStatus);
  1456. fireStatusChangeEvent(validationStatus.hasErrors());
  1457. return validationStatus;
  1458. }
  1459. /**
  1460. * Validates the bindings and returns the result of the validation as a list
  1461. * of validation statuses.
  1462. * <p>
  1463. * Does not run bean validators.
  1464. *
  1465. * @see #validateBean(Object)
  1466. *
  1467. * @return an immutable list of validation results for bindings
  1468. */
  1469. private List<BindingValidationStatus<?>> validateBindings() {
  1470. List<BindingValidationStatus<?>> results = new ArrayList<>();
  1471. for (BindingImpl<?, ?, ?> binding : bindings) {
  1472. results.add(binding.doValidation());
  1473. }
  1474. return results;
  1475. }
  1476. /**
  1477. * Validates the {@code bean} using validators added using
  1478. * {@link #withValidator(Validator)} and returns the result of the
  1479. * validation as a list of validation results.
  1480. * <p>
  1481. *
  1482. * @see #withValidator(Validator)
  1483. *
  1484. * @param bean
  1485. * the bean to validate
  1486. * @return a list of validation errors or an empty list if validation
  1487. * succeeded
  1488. */
  1489. private List<ValidationResult> validateBean(BEAN bean) {
  1490. Objects.requireNonNull(bean, "bean cannot be null");
  1491. List<ValidationResult> results = Collections.unmodifiableList(validators
  1492. .stream()
  1493. .map(validator -> validator.apply(bean, new ValueContext()))
  1494. .collect(Collectors.toList()));
  1495. return results;
  1496. }
  1497. /**
  1498. * Sets the label to show the binder level validation errors not related to
  1499. * any specific field.
  1500. * <p>
  1501. * Only the one validation error message is shown in this label at a time.
  1502. * <p>
  1503. * This is a convenience method for
  1504. * {@link #setValidationStatusHandler(BinderValidationStatusHandler)}, which
  1505. * means that this method cannot be used after the handler has been set.
  1506. * Also the handler cannot be set after this label has been set.
  1507. *
  1508. * @param statusLabel
  1509. * the status label to set
  1510. * @see #setValidationStatusHandler(BinderValidationStatusHandler)
  1511. * @see BindingBuilder#withStatusLabel(Label)
  1512. */
  1513. public void setStatusLabel(Label statusLabel) {
  1514. if (statusHandler != null) {
  1515. throw new IllegalStateException("Cannot set status label if a "
  1516. + BinderValidationStatusHandler.class.getSimpleName()
  1517. + " has already been set.");
  1518. }
  1519. this.statusLabel = statusLabel;
  1520. }
  1521. /**
  1522. * Gets the status label or an empty optional if none has been set.
  1523. *
  1524. * @return the optional status label
  1525. * @see #setStatusLabel(Label)
  1526. */
  1527. public Optional<Label> getStatusLabel() {
  1528. return Optional.ofNullable(statusLabel);
  1529. }
  1530. /**
  1531. * Sets the status handler to track form status changes.
  1532. * <p>
  1533. * Setting this handler will override the default behavior, which is to let
  1534. * fields show their validation status messages and show binder level
  1535. * validation errors or OK status in the label set with
  1536. * {@link #setStatusLabel(Label)}.
  1537. * <p>
  1538. * This handler cannot be set after the status label has been set with
  1539. * {@link #setStatusLabel(Label)}, or {@link #setStatusLabel(Label)} cannot
  1540. * be used after this handler has been set.
  1541. *
  1542. * @param statusHandler
  1543. * the status handler to set, not <code>null</code>
  1544. * @throws NullPointerException
  1545. * for <code>null</code> status handler
  1546. * @see #setStatusLabel(Label)
  1547. * @see BindingBuilder#withValidationStatusHandler(BindingValidationStatusHandler)
  1548. */
  1549. public void setValidationStatusHandler(
  1550. BinderValidationStatusHandler<BEAN> statusHandler) {
  1551. Objects.requireNonNull(statusHandler, "Cannot set a null "
  1552. + BinderValidationStatusHandler.class.getSimpleName());
  1553. if (statusLabel != null) {
  1554. throw new IllegalStateException("Cannot set "
  1555. + BinderValidationStatusHandler.class.getSimpleName()
  1556. + " if a status label has already been set.");
  1557. }
  1558. this.statusHandler = statusHandler;
  1559. }
  1560. /**
  1561. * Gets the status handler of this form.
  1562. * <p>
  1563. * If none has been set with
  1564. * {@link #setValidationStatusHandler(BinderValidationStatusHandler)}, the
  1565. * default implementation is returned.
  1566. *
  1567. * @return the status handler used, never <code>null</code>
  1568. * @see #setValidationStatusHandler(BinderValidationStatusHandler)
  1569. */
  1570. public BinderValidationStatusHandler<BEAN> getValidationStatusHandler() {
  1571. return Optional.ofNullable(statusHandler)
  1572. .orElse(this::handleBinderValidationStatus);
  1573. }
  1574. /**
  1575. * Adds status change listener to the binder.
  1576. * <p>
  1577. * The {@link Binder} status is changed whenever any of the following
  1578. * happens:
  1579. * <ul>
  1580. * <li>if it's bound and any of its bound field or select has been changed
  1581. * <li>{@link #writeBean(Object)} or {@link #writeBeanIfValid(Object)} is
  1582. * called
  1583. * <li>{@link #readBean(Object)} is called
  1584. * <li>{@link #setBean(Object)} is called
  1585. * <li>{@link #removeBean()} is called
  1586. * <li>{@link BindingBuilder#bind(ValueProvider, Setter)} is called
  1587. * <li>{@link Binder#validate()} or {@link Binding#validate()} is called
  1588. * </ul>
  1589. *
  1590. * @see #readBean(Object)
  1591. * @see #writeBean(Object)
  1592. * @see #writeBeanIfValid(Object)
  1593. * @see #setBean(Object)
  1594. * @see #removeBean()
  1595. * @see #forField(HasValue)
  1596. * @see #validate()
  1597. * @see Binding#validate()
  1598. *
  1599. * @param listener
  1600. * status change listener to add, not null
  1601. * @return a registration for the listener
  1602. */
  1603. public Registration addStatusChangeListener(StatusChangeListener listener) {
  1604. return getEventRouter().addListener(StatusChangeEvent.class, listener,
  1605. StatusChangeListener.class.getDeclaredMethods()[0]);
  1606. }
  1607. /**
  1608. * Creates a new binding with the given field.
  1609. *
  1610. * @param <FIELDVALUE>
  1611. * the value type of the field
  1612. * @param <TARGET>
  1613. * the target data type
  1614. * @param field
  1615. * the field to bind, not null
  1616. * @param converter
  1617. * the converter for converting between FIELDVALUE and TARGET
  1618. * types, not null
  1619. * @param handler
  1620. * the handler to notify of status changes, not null
  1621. * @return the new incomplete binding
  1622. */
  1623. protected <FIELDVALUE, TARGET> BindingBuilder<BEAN, TARGET> createBinding(
  1624. HasValue<FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converter,
  1625. BindingValidationStatusHandler handler) {
  1626. BindingBuilder<BEAN, TARGET> newBinding = doCreateBinding(field,
  1627. converter, handler);
  1628. if (incompleteMemberFieldBindings.containsKey(field)) {
  1629. incompleteMemberFieldBindings.put(field, newBinding);
  1630. }
  1631. incompleteBindings.put(field, newBinding);
  1632. return newBinding;
  1633. }
  1634. protected <FIELDVALUE, TARGET> BindingBuilder<BEAN, TARGET> doCreateBinding(
  1635. HasValue<FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converter,
  1636. BindingValidationStatusHandler handler) {
  1637. return new BindingBuilderImpl<>(this, field, converter, handler);
  1638. }
  1639. /**
  1640. * Clears the error condition of the given field, if any. The default
  1641. * implementation clears the
  1642. * {@link AbstractComponent#setComponentError(ErrorMessage) component error}
  1643. * of the field if it is a Component, otherwise does nothing.
  1644. *
  1645. * @param field
  1646. * the field with an invalid value
  1647. */
  1648. protected void clearError(HasValue<?> field) {
  1649. if (field instanceof AbstractComponent) {
  1650. ((AbstractComponent) field).setComponentError(null);
  1651. }
  1652. }
  1653. /**
  1654. * Handles a validation error emitted when trying to write the value of the
  1655. * given field. The default implementation sets the
  1656. * {@link AbstractComponent#setComponentError(ErrorMessage) component error}
  1657. * of the field if it is a Component, otherwise does nothing.
  1658. *
  1659. * @param field
  1660. * the field with the invalid value
  1661. * @param error
  1662. * the error message to set
  1663. */
  1664. protected void handleError(HasValue<?> field, String error) {
  1665. if (field instanceof AbstractComponent) {
  1666. ((AbstractComponent) field).setComponentError(new UserError(error));
  1667. }
  1668. }
  1669. /**
  1670. * Default {@link BindingValidationStatusHandler} functional method
  1671. * implementation.
  1672. *
  1673. * @param status
  1674. * the validation status
  1675. */
  1676. protected void handleValidationStatus(BindingValidationStatus<?> status) {
  1677. HasValue<?> source = status.getField();
  1678. clearError(source);
  1679. if (status.isError()) {
  1680. handleError(source, status.getMessage().get());
  1681. }
  1682. }
  1683. /**
  1684. * Returns the bindings for this binder.
  1685. *
  1686. * @return a set of the bindings
  1687. */
  1688. protected Set<BindingImpl<BEAN, ?, ?>> getBindings() {
  1689. return bindings;
  1690. }
  1691. /**
  1692. * The default binder level status handler.
  1693. * <p>
  1694. * Passes all field related results to the Binding status handlers. All
  1695. * other status changes are displayed in the status label, if one has been
  1696. * set with {@link #setStatusLabel(Label)}.
  1697. *
  1698. * @param binderStatus
  1699. * status of validation results from binding and/or bean level
  1700. * validators
  1701. */
  1702. protected void handleBinderValidationStatus(
  1703. BinderValidationStatus<BEAN> binderStatus) {
  1704. // let field events go to binding status handlers
  1705. binderStatus.getFieldValidationStatuses()
  1706. .forEach(status -> ((BindingImpl<?, ?, ?>) status.getBinding())
  1707. .notifyStatusHandler(status));
  1708. // show first possible error or OK status in the label if set
  1709. if (getStatusLabel().isPresent()) {
  1710. String statusMessage = binderStatus.getBeanValidationErrors()
  1711. .stream().findFirst().map(ValidationResult::getErrorMessage)
  1712. .orElse("");
  1713. getStatusLabel().get().setValue(statusMessage);
  1714. }
  1715. }
  1716. /**
  1717. * Sets whether the values of the fields this binder is bound to have
  1718. * changed since the last explicit call to either bind, write or read.
  1719. *
  1720. * @param hasChanges
  1721. * whether this binder should be marked to have changes
  1722. */
  1723. private void setHasChanges(boolean hasChanges) {
  1724. this.hasChanges = hasChanges;
  1725. }
  1726. /**
  1727. * Check whether any of the bound fields' values have changed since last
  1728. * explicit call to {@link #setBean(Object)}, {@link #readBean(Object)},
  1729. * {@link #removeBean()}, {@link #writeBean(Object)} or
  1730. * {@link #writeBeanIfValid(Object)}. Unsuccessful write operations will not
  1731. * affect this value. Return values for each case are compiled into the
  1732. * following table:
  1733. *
  1734. * <p>
  1735. *
  1736. * <table>
  1737. * <tr>
  1738. * <td></td>
  1739. * <td>After readBean, setBean or removeBean</td>
  1740. * <td>After valid user changes</td>
  1741. * <td>After invalid user changes</td>
  1742. * <td>After successful writeBean or writeBeanIfValid</td>
  1743. * <td>After unsuccessful writeBean or writeBeanIfValid</td>
  1744. * </tr>
  1745. * <tr>
  1746. * <td>A bean is currently bound</td>
  1747. * <td>{@code false}</td>
  1748. * <td>{@code false}</td>
  1749. * <td>{@code true}</td>
  1750. * <td>{@code false}</td>
  1751. * <td>no change</td>
  1752. * </tr>
  1753. * <tr>
  1754. * <td>No bean is currently bound</td>
  1755. * <td>{@code false}</td>
  1756. * <td>{@code true}</td>
  1757. * <td>{@code true}</td>
  1758. * <td>{@code false}</td>
  1759. * <td>no change</td>
  1760. * </tr>
  1761. * </table>
  1762. *
  1763. * @return whether any bound field's value has changed since last call to
  1764. * setBean, readBean, writeBean or writeBeanIfValid
  1765. */
  1766. public boolean hasChanges() {
  1767. return hasChanges;
  1768. }
  1769. /**
  1770. * Returns the event router for this binder.
  1771. *
  1772. * @return the event router, not null
  1773. */
  1774. protected EventRouter getEventRouter() {
  1775. if (eventRouter == null) {
  1776. eventRouter = new EventRouter();
  1777. }
  1778. return eventRouter;
  1779. }
  1780. private void doRemoveBean(boolean fireStatusEvent) {
  1781. setHasChanges(false);
  1782. if (bean != null) {
  1783. bean = null;
  1784. }
  1785. getValidationStatusHandler().statusChange(
  1786. BinderValidationStatus.createUnresolvedStatus(this));
  1787. if (fireStatusEvent) {
  1788. fireStatusChangeEvent(false);
  1789. }
  1790. }
  1791. private void fireStatusChangeEvent(boolean hasValidationErrors) {
  1792. getEventRouter()
  1793. .fireEvent(new StatusChangeEvent(this, hasValidationErrors));
  1794. }
  1795. private <FIELDVALUE> Converter<FIELDVALUE, FIELDVALUE> createNullRepresentationAdapter(
  1796. HasValue<FIELDVALUE> field) {
  1797. Converter<FIELDVALUE, FIELDVALUE> nullRepresentationConverter = Converter
  1798. .from(fieldValue -> fieldValue,
  1799. modelValue -> Objects.isNull(modelValue)
  1800. ? field.getEmptyValue() : modelValue,
  1801. exception -> exception.getMessage());
  1802. ConverterDelegate<FIELDVALUE> converter = new ConverterDelegate<>(
  1803. nullRepresentationConverter);
  1804. initialConverters.put(field, converter);
  1805. return converter;
  1806. }
  1807. /**
  1808. * Throws if this binder has incomplete bindings.
  1809. *
  1810. * @param methodName
  1811. * name of the method where this call is originated from
  1812. * @throws IllegalStateException
  1813. * if this binder has incomplete bindings
  1814. */
  1815. private void checkBindingsCompleted(String methodName) {
  1816. if (!incompleteMemberFieldBindings.isEmpty()) {
  1817. throw new IllegalStateException(
  1818. "All bindings created with forMemberField must "
  1819. + "be completed with bindInstanceFields before calling "
  1820. + methodName);
  1821. }
  1822. if (!incompleteBindings.isEmpty()) {
  1823. throw new IllegalStateException(
  1824. "All bindings created with forField must be completed before calling "
  1825. + methodName);
  1826. }
  1827. }
  1828. /**
  1829. * Binds member fields found in the given object.
  1830. * <p>
  1831. * This method processes all (Java) member fields whose type extends
  1832. * {@link HasValue} and that can be mapped to a property id. Property name
  1833. * mapping is done based on the field name or on a @{@link PropertyId}
  1834. * annotation on the field. All non-null unbound fields for which a property
  1835. * name can be determined are bound to the property name using
  1836. * {@link BindingBuilder#bind(String)}.
  1837. * <p>
  1838. * For example:
  1839. *
  1840. * <pre>
  1841. * public class MyForm extends VerticalLayout {
  1842. * private TextField firstName = new TextField("First name");
  1843. * &#64;PropertyId("last")
  1844. * private TextField lastName = new TextField("Last name");
  1845. *
  1846. * MyForm myForm = new MyForm();
  1847. * ...
  1848. * binder.bindMemberFields(myForm);
  1849. * </pre>
  1850. *
  1851. * This binds the firstName TextField to a "firstName" property in the item,
  1852. * lastName TextField to a "last" property.
  1853. * <p>
  1854. * It's not always possible to bind a field to a property because their
  1855. * types are incompatible. E.g. custom converter is required to bind
  1856. * {@code HasValue<String>} and {@code Integer} property (that would be a
  1857. * case of "age" property). In such case {@link IllegalStateException} will
  1858. * be thrown unless the field has been configured manually before calling
  1859. * the {@link #bindInstanceFields(Object)} method.
  1860. * <p>
  1861. * It's always possible to do custom binding for any field: the
  1862. * {@link #bindInstanceFields(Object)} method doesn't override existing
  1863. * bindings.
  1864. *
  1865. * @param objectWithMemberFields
  1866. * The object that contains (Java) member fields to bind
  1867. * @throws IllegalStateException
  1868. * if there are incompatible HasValue&lt;T&gt; and property
  1869. * types
  1870. */
  1871. public void bindInstanceFields(Object objectWithMemberFields) {
  1872. Class<?> objectClass = objectWithMemberFields.getClass();
  1873. getFieldsInDeclareOrder(objectClass).stream()
  1874. .filter(memberField -> HasValue.class
  1875. .isAssignableFrom(memberField.getType()))
  1876. .forEach(memberField -> handleProperty(memberField,
  1877. objectWithMemberFields,
  1878. (property, type) -> bindProperty(objectWithMemberFields,
  1879. memberField, property, type)));
  1880. }
  1881. @SuppressWarnings("unchecked")
  1882. private BindingBuilder<BEAN, ?> getIncompleteMemberFieldBinding(
  1883. Field memberField, Object objectWithMemberFields) {
  1884. memberField.setAccessible(true);
  1885. try {
  1886. return incompleteMemberFieldBindings
  1887. .get(memberField.get(objectWithMemberFields));
  1888. } catch (IllegalArgumentException | IllegalAccessException e) {
  1889. throw new RuntimeException(e);
  1890. } finally {
  1891. memberField.setAccessible(false);
  1892. }
  1893. }
  1894. /**
  1895. * Binds {@code property} with {@code propertyType} to the field in the
  1896. * {@code objectWithMemberFields} instance using {@code memberField} as a
  1897. * reference to a member.
  1898. *
  1899. * @param objectWithMemberFields
  1900. * the object that contains (Java) member fields to build and
  1901. * bind
  1902. * @param memberField
  1903. * reference to a member field to bind
  1904. * @param property
  1905. * property name to bind
  1906. * @param propertyType
  1907. * type of the property
  1908. */
  1909. private void bindProperty(Object objectWithMemberFields, Field memberField,
  1910. String property, Class<?> propertyType) {
  1911. Type valueType = GenericTypeReflector.getTypeParameter(
  1912. memberField.getGenericType(),
  1913. HasValue.class.getTypeParameters()[0]);
  1914. if (valueType == null) {
  1915. throw new IllegalStateException(String.format(
  1916. "Unable to detect value type for the member '%s' in the "
  1917. + "class '%s'.",
  1918. memberField.getName(),
  1919. objectWithMemberFields.getClass().getName()));
  1920. }
  1921. if (propertyType.equals(GenericTypeReflector.erase(valueType))) {
  1922. HasValue<?> field;
  1923. // Get the field from the object
  1924. try {
  1925. field = (HasValue<?>) ReflectTools.getJavaFieldValue(
  1926. objectWithMemberFields, memberField, HasValue.class);
  1927. } catch (IllegalArgumentException | IllegalAccessException
  1928. | InvocationTargetException e) {
  1929. // If we cannot determine the value, just skip the field
  1930. return;
  1931. }
  1932. if (field == null) {
  1933. field = makeFieldInstance(
  1934. (Class<? extends HasValue<?>>) memberField.getType());
  1935. initializeField(objectWithMemberFields, memberField, field);
  1936. }
  1937. forField(field).bind(property);
  1938. } else {
  1939. throw new IllegalStateException(String.format(
  1940. "Property type '%s' doesn't "
  1941. + "match the field type '%s'. "
  1942. + "Binding should be configured manually using converter.",
  1943. propertyType.getName(), valueType.getTypeName()));
  1944. }
  1945. }
  1946. /**
  1947. * Makes an instance of the field type {@code fieldClass}.
  1948. * <p>
  1949. * The resulting field instance is used to bind a property to it using the
  1950. * {@link #bindInstanceFields(Object)} method.
  1951. * <p>
  1952. * The default implementation relies on the default constructor of the
  1953. * class. If there is no suitable default constructor or you want to
  1954. * configure the instantiated class then override this method and provide
  1955. * your own implementation.
  1956. *
  1957. * @see #bindInstanceFields(Object)
  1958. * @param fieldClass
  1959. * type of the field
  1960. * @return a {@code fieldClass} instance object
  1961. */
  1962. private HasValue<?> makeFieldInstance(
  1963. Class<? extends HasValue<?>> fieldClass) {
  1964. try {
  1965. return fieldClass.newInstance();
  1966. } catch (InstantiationException | IllegalAccessException e) {
  1967. throw new IllegalStateException(
  1968. String.format("Couldn't create an '%s' type instance",
  1969. fieldClass.getName()),
  1970. e);
  1971. }
  1972. }
  1973. /**
  1974. * Returns an array containing {@link Field} objects reflecting all the
  1975. * fields of the class or interface represented by this Class object. The
  1976. * elements in the array returned are sorted in declare order from sub class
  1977. * to super class.
  1978. *
  1979. * @param searchClass
  1980. * class to introspect
  1981. * @return list of all fields in the class considering hierarchy
  1982. */
  1983. private List<Field> getFieldsInDeclareOrder(Class<?> searchClass) {
  1984. ArrayList<Field> memberFieldInOrder = new ArrayList<>();
  1985. while (searchClass != null) {
  1986. memberFieldInOrder
  1987. .addAll(Arrays.asList(searchClass.getDeclaredFields()));
  1988. searchClass = searchClass.getSuperclass();
  1989. }
  1990. return memberFieldInOrder;
  1991. }
  1992. private void initializeField(Object objectWithMemberFields,
  1993. Field memberField, HasValue<?> value) {
  1994. try {
  1995. ReflectTools.setJavaFieldValue(objectWithMemberFields, memberField,
  1996. value);
  1997. } catch (IllegalArgumentException | IllegalAccessException
  1998. | InvocationTargetException e) {
  1999. throw new IllegalStateException(
  2000. String.format("Could not assign value to field '%s'",
  2001. memberField.getName()),
  2002. e);
  2003. }
  2004. }
  2005. private void handleProperty(Field field, Object objectWithMemberFields,
  2006. BiConsumer<String, Class<?>> propertyHandler) {
  2007. Optional<BinderPropertyDefinition<BEAN, ?>> descriptor = getPropertyDescriptor(
  2008. field);
  2009. if (!descriptor.isPresent()) {
  2010. return;
  2011. }
  2012. String propertyName = descriptor.get().getName();
  2013. if (boundProperties.contains(propertyName)) {
  2014. return;
  2015. }
  2016. BindingBuilder<BEAN, ?> tentativeBinding = getIncompleteMemberFieldBinding(
  2017. field, objectWithMemberFields);
  2018. if (tentativeBinding != null) {
  2019. tentativeBinding.bind(propertyName);
  2020. return;
  2021. }
  2022. propertyHandler.accept(propertyName, descriptor.get().getType());
  2023. boundProperties.add(propertyName);
  2024. }
  2025. private Optional<BinderPropertyDefinition<BEAN, ?>> getPropertyDescriptor(
  2026. Field field) {
  2027. PropertyId propertyIdAnnotation = field.getAnnotation(PropertyId.class);
  2028. String propertyId;
  2029. if (propertyIdAnnotation != null) {
  2030. // @PropertyId(propertyId) always overrides property id
  2031. propertyId = propertyIdAnnotation.value();
  2032. } else {
  2033. propertyId = field.getName();
  2034. }
  2035. String minifiedFieldName = minifyFieldName(propertyId);
  2036. return propertySet.getProperties()
  2037. .map(BinderPropertyDefinition::getName)
  2038. .filter(name -> minifyFieldName(name).equals(minifiedFieldName))
  2039. .findFirst().flatMap(propertySet::getProperty);
  2040. }
  2041. private String minifyFieldName(String fieldName) {
  2042. return fieldName.toLowerCase(Locale.ENGLISH).replace("_", "");
  2043. }
  2044. }