You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Binder.java 88KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  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.IdentityHashMap;
  26. import java.util.LinkedHashSet;
  27. import java.util.List;
  28. import java.util.Locale;
  29. import java.util.Map;
  30. import java.util.Objects;
  31. import java.util.Optional;
  32. import java.util.Set;
  33. import java.util.function.BiConsumer;
  34. import java.util.stream.Collectors;
  35. import java.util.stream.Stream;
  36. import com.googlecode.gentyref.GenericTypeReflector;
  37. import com.vaadin.annotations.PropertyId;
  38. import com.vaadin.data.HasValue.ValueChangeEvent;
  39. import com.vaadin.data.HasValue.ValueChangeListener;
  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 PropertySet}.
  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 PropertySet}
  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. PropertyDefinition<BEAN, ?> definition = getBinder().propertySet
  576. .getProperty(propertyName)
  577. .orElseThrow(() -> new IllegalArgumentException(
  578. "Could not resolve property name " + propertyName
  579. + " from " + getBinder().propertySet));
  580. ValueProvider<BEAN, ?> getter = definition.getGetter();
  581. Setter<BEAN, ?> setter = definition.getSetter()
  582. .orElse((bean, value) -> {
  583. // Setter ignores value
  584. });
  585. BindingBuilder<BEAN, ?> finalBinding = withConverter(
  586. createConverter(definition.getType()), false);
  587. finalBinding = getBinder().configureBinding(finalBinding,
  588. definition);
  589. try {
  590. Binding binding = ((BindingBuilder) finalBinding).bind(getter,
  591. setter);
  592. getBinder().boundProperties.put(propertyName, binding);
  593. return binding;
  594. } finally {
  595. getBinder().incompleteMemberFieldBindings.remove(getField());
  596. }
  597. }
  598. @SuppressWarnings("unchecked")
  599. private Converter<TARGET, Object> createConverter(Class<?> getterType) {
  600. return Converter.from(fieldValue -> getterType.cast(fieldValue),
  601. propertyValue -> (TARGET) propertyValue, exception -> {
  602. throw new RuntimeException(exception);
  603. });
  604. }
  605. @Override
  606. public BindingBuilder<BEAN, TARGET> withValidator(
  607. Validator<? super TARGET> validator) {
  608. checkUnbound();
  609. Objects.requireNonNull(validator, "validator cannot be null");
  610. converterValidatorChain = converterValidatorChain
  611. .chain(new ValidatorAsConverter<>(validator));
  612. return this;
  613. }
  614. @Override
  615. public <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
  616. Converter<TARGET, NEWTARGET> converter) {
  617. return withConverter(converter, true);
  618. }
  619. @Override
  620. public BindingBuilder<BEAN, TARGET> withValidationStatusHandler(
  621. BindingValidationStatusHandler handler) {
  622. checkUnbound();
  623. Objects.requireNonNull(handler, "handler cannot be null");
  624. if (isStatusHandlerChanged) {
  625. throw new IllegalStateException("A "
  626. + BindingValidationStatusHandler.class.getSimpleName()
  627. + " has already been set");
  628. }
  629. isStatusHandlerChanged = true;
  630. statusHandler = handler;
  631. return this;
  632. }
  633. @Override
  634. public BindingBuilder<BEAN, TARGET> asRequired(
  635. ErrorMessageProvider errorMessageProvider) {
  636. checkUnbound();
  637. field.setRequiredIndicatorVisible(true);
  638. return withValidator(
  639. value -> !Objects.equals(value, field.getEmptyValue()),
  640. errorMessageProvider);
  641. }
  642. /**
  643. * Implements {@link #withConverter(Converter)} method with additional
  644. * possibility to disable (reset) default null representation converter.
  645. * <p>
  646. * The method {@link #withConverter(Converter)} calls this method with
  647. * {@code true} provided as the second argument value.
  648. *
  649. * @see #withConverter(Converter)
  650. *
  651. * @param converter
  652. * the converter to use, not null
  653. * @param resetNullRepresentation
  654. * if {@code true} then default null representation will be
  655. * deactivated (if not yet), otherwise it won't be removed
  656. * @return a new binding with the appropriate type
  657. * @param <NEWTARGET>
  658. * the type to convert to
  659. * @throws IllegalStateException
  660. * if {@code bind} has already been called
  661. */
  662. protected <NEWTARGET> BindingBuilder<BEAN, NEWTARGET> withConverter(
  663. Converter<TARGET, NEWTARGET> converter,
  664. boolean resetNullRepresentation) {
  665. checkUnbound();
  666. Objects.requireNonNull(converter, "converter cannot be null");
  667. if (resetNullRepresentation) {
  668. getBinder().initialConverters.get(field).setIdentity();
  669. }
  670. return getBinder().createBinding(field,
  671. converterValidatorChain.chain(converter), statusHandler);
  672. }
  673. /**
  674. * Returns the {@code Binder} connected to this {@code Binding}
  675. * instance.
  676. *
  677. * @return the binder
  678. */
  679. protected Binder<BEAN> getBinder() {
  680. return binder;
  681. }
  682. /**
  683. * Throws if this binding is already completed and cannot be modified
  684. * anymore.
  685. *
  686. * @throws IllegalStateException
  687. * if this binding is already bound
  688. */
  689. protected void checkUnbound() {
  690. if (bound) {
  691. throw new IllegalStateException(
  692. "cannot modify binding: already bound to a property");
  693. }
  694. }
  695. @Override
  696. public HasValue<FIELDVALUE> getField() {
  697. return field;
  698. }
  699. }
  700. /**
  701. * An internal implementation of {@code Binding}.
  702. *
  703. * @param <BEAN>
  704. * the bean type, must match the Binder bean type
  705. * @param <FIELDVALUE>
  706. * the value type of the field
  707. * @param <TARGET>
  708. * the target data type of the binding, matches the field type
  709. * unless a converter has been set
  710. */
  711. protected static class BindingImpl<BEAN, FIELDVALUE, TARGET>
  712. implements Binding<BEAN, TARGET> {
  713. private final Binder<BEAN> binder;
  714. private final HasValue<FIELDVALUE> field;
  715. private final BindingValidationStatusHandler statusHandler;
  716. private final SerializableFunction<BEAN, TARGET> getter;
  717. private final Setter<BEAN, TARGET> setter;
  718. // Not final since we temporarily remove listener while changing values
  719. private Registration onValueChange;
  720. /**
  721. * Contains all converters and validators chained together in the
  722. * correct order.
  723. */
  724. private final Converter<FIELDVALUE, TARGET> converterValidatorChain;
  725. public BindingImpl(BindingBuilderImpl<BEAN, FIELDVALUE, TARGET> builder,
  726. SerializableFunction<BEAN, TARGET> getter,
  727. Setter<BEAN, TARGET> setter) {
  728. this.binder = builder.getBinder();
  729. this.field = builder.field;
  730. this.statusHandler = builder.statusHandler;
  731. converterValidatorChain = builder.converterValidatorChain;
  732. onValueChange = getField()
  733. .addValueChangeListener(this::handleFieldValueChange);
  734. this.getter = getter;
  735. this.setter = setter;
  736. }
  737. @Override
  738. public HasValue<FIELDVALUE> getField() {
  739. return field;
  740. }
  741. /**
  742. * Finds an appropriate locale to be used in conversion and validation.
  743. *
  744. * @return the found locale, not null
  745. */
  746. protected Locale findLocale() {
  747. Locale l = null;
  748. if (getField() instanceof Component) {
  749. l = ((Component) getField()).getLocale();
  750. }
  751. if (l == null && UI.getCurrent() != null) {
  752. l = UI.getCurrent().getLocale();
  753. }
  754. if (l == null) {
  755. l = Locale.getDefault();
  756. }
  757. return l;
  758. }
  759. @Override
  760. public BindingValidationStatus<TARGET> validate() {
  761. BindingValidationStatus<TARGET> status = doValidation();
  762. getBinder().getValidationStatusHandler()
  763. .statusChange(new BinderValidationStatus<>(getBinder(),
  764. Arrays.asList(status), Collections.emptyList()));
  765. getBinder().fireStatusChangeEvent(status.isError());
  766. return status;
  767. }
  768. /**
  769. * Returns the field value run through all converters and validators,
  770. * but doesn't pass the {@link BindingValidationStatus} to any status
  771. * handler.
  772. *
  773. * @return the result of the conversion
  774. */
  775. private Result<TARGET> doConversion() {
  776. FIELDVALUE fieldValue = field.getValue();
  777. return converterValidatorChain.convertToModel(fieldValue,
  778. createValueContext());
  779. }
  780. private BindingValidationStatus<TARGET> toValidationStatus(
  781. Result<TARGET> result) {
  782. return new BindingValidationStatus<>(this,
  783. result.isError()
  784. ? ValidationResult.error(result.getMessage().get())
  785. : ValidationResult.ok());
  786. }
  787. /**
  788. * Returns the field value run through all converters and validators,
  789. * but doesn't pass the {@link BindingValidationStatus} to any status
  790. * handler.
  791. *
  792. * @return the validation status
  793. */
  794. private BindingValidationStatus<TARGET> doValidation() {
  795. return toValidationStatus(doConversion());
  796. }
  797. /**
  798. * Creates a value context from the current state of the binding and its
  799. * field.
  800. *
  801. * @return the value context
  802. */
  803. protected ValueContext createValueContext() {
  804. if (field instanceof Component) {
  805. return new ValueContext((Component) field);
  806. }
  807. return new ValueContext(findLocale());
  808. }
  809. /**
  810. * Sets the field value by invoking the getter function on the given
  811. * bean. The default listener attached to the field will be removed for
  812. * the duration of this update.
  813. *
  814. * @param bean
  815. * the bean to fetch the property value from
  816. */
  817. private void initFieldValue(BEAN bean) {
  818. assert bean != null;
  819. assert onValueChange != null;
  820. onValueChange.remove();
  821. try {
  822. getField().setValue(convertDataToFieldType(bean));
  823. } finally {
  824. onValueChange = getField()
  825. .addValueChangeListener(this::handleFieldValueChange);
  826. }
  827. }
  828. private FIELDVALUE convertDataToFieldType(BEAN bean) {
  829. return converterValidatorChain.convertToPresentation(
  830. getter.apply(bean), createValueContext());
  831. }
  832. /**
  833. * Handles the value change triggered by the bound field.
  834. *
  835. * @param event
  836. */
  837. private void handleFieldValueChange(
  838. ValueChangeEvent<FIELDVALUE> event) {
  839. getBinder().setHasChanges(true);
  840. List<ValidationResult> binderValidationResults = Collections
  841. .emptyList();
  842. BindingValidationStatus<TARGET> fieldValidationStatus;
  843. if (getBinder().getBean() != null) {
  844. BEAN bean = getBinder().getBean();
  845. fieldValidationStatus = writeFieldValue(bean);
  846. if (!getBinder().bindings.stream()
  847. .map(BindingImpl::doValidation)
  848. .anyMatch(BindingValidationStatus::isError)) {
  849. binderValidationResults = getBinder().validateBean(bean);
  850. if (!binderValidationResults.stream()
  851. .anyMatch(ValidationResult::isError)) {
  852. getBinder().setHasChanges(false);
  853. }
  854. }
  855. } else {
  856. fieldValidationStatus = doValidation();
  857. }
  858. BinderValidationStatus<BEAN> status = new BinderValidationStatus<>(
  859. getBinder(), Arrays.asList(fieldValidationStatus),
  860. binderValidationResults);
  861. getBinder().getValidationStatusHandler().statusChange(status);
  862. getBinder().fireStatusChangeEvent(status.hasErrors());
  863. getBinder().fireValueChangeEvent(event);
  864. }
  865. /**
  866. * Write the field value by invoking the setter function on the given
  867. * bean, if the value passes all registered validators.
  868. *
  869. * @param bean
  870. * the bean to set the property value to
  871. */
  872. private BindingValidationStatus<TARGET> writeFieldValue(BEAN bean) {
  873. assert bean != null;
  874. Result<TARGET> result = doConversion();
  875. if (setter != null) {
  876. result.ifOk(value -> setter.accept(bean, value));
  877. }
  878. return toValidationStatus(result);
  879. }
  880. /**
  881. * Returns the {@code Binder} connected to this {@code Binding}
  882. * instance.
  883. *
  884. * @return the binder
  885. */
  886. protected Binder<BEAN> getBinder() {
  887. return binder;
  888. }
  889. private void notifyStatusHandler(BindingValidationStatus<?> status) {
  890. statusHandler.statusChange(status);
  891. }
  892. }
  893. /**
  894. * Wraps a validator as a converter.
  895. * <p>
  896. * The type of the validator must be of the same type as this converter or a
  897. * super type of it.
  898. *
  899. * @param <T>
  900. * the type of the converter
  901. */
  902. private static class ValidatorAsConverter<T> implements Converter<T, T> {
  903. private final Validator<? super T> validator;
  904. /**
  905. * Creates a new converter wrapping the given validator.
  906. *
  907. * @param validator
  908. * the validator to wrap
  909. */
  910. public ValidatorAsConverter(Validator<? super T> validator) {
  911. this.validator = validator;
  912. }
  913. @Override
  914. public Result<T> convertToModel(T value, ValueContext context) {
  915. ValidationResult validationResult = validator.apply(value, context);
  916. if (validationResult.isError()) {
  917. return Result.error(validationResult.getErrorMessage());
  918. } else {
  919. return Result.ok(value);
  920. }
  921. }
  922. @Override
  923. public T convertToPresentation(T value, ValueContext context) {
  924. return value;
  925. }
  926. }
  927. /**
  928. * Converter decorator-strategy pattern to use initially provided "delegate"
  929. * converter to execute its logic until the {@code setIdentity()} method is
  930. * called. Once the method is called the class changes its behavior to the
  931. * same as {@link Converter#identity()} behavior.
  932. */
  933. private static class ConverterDelegate<FIELDVALUE>
  934. implements Converter<FIELDVALUE, FIELDVALUE> {
  935. private Converter<FIELDVALUE, FIELDVALUE> delegate;
  936. private ConverterDelegate(Converter<FIELDVALUE, FIELDVALUE> converter) {
  937. delegate = converter;
  938. }
  939. @Override
  940. public Result<FIELDVALUE> convertToModel(FIELDVALUE value,
  941. ValueContext context) {
  942. if (delegate == null) {
  943. return Result.ok(value);
  944. } else {
  945. return delegate.convertToModel(value, context);
  946. }
  947. }
  948. @Override
  949. public FIELDVALUE convertToPresentation(FIELDVALUE value,
  950. ValueContext context) {
  951. if (delegate == null) {
  952. return value;
  953. } else {
  954. return delegate.convertToPresentation(value, context);
  955. }
  956. }
  957. void setIdentity() {
  958. delegate = null;
  959. }
  960. }
  961. private final PropertySet<BEAN> propertySet;
  962. /**
  963. * Property names that have been used for creating a binding.
  964. */
  965. private final Map<String, Binding<BEAN, ?>> boundProperties = new HashMap<>();
  966. private final Map<HasValue<?>, BindingBuilder<BEAN, ?>> incompleteMemberFieldBindings = new IdentityHashMap<>();
  967. private BEAN bean;
  968. private final Set<BindingImpl<BEAN, ?, ?>> bindings = new LinkedHashSet<>();
  969. private final Map<HasValue<?>, BindingBuilder<BEAN, ?>> incompleteBindings = new IdentityHashMap<>();
  970. private final List<Validator<? super BEAN>> validators = new ArrayList<>();
  971. private final Map<HasValue<?>, ConverterDelegate<?>> initialConverters = new IdentityHashMap<>();
  972. private EventRouter eventRouter;
  973. private Label statusLabel;
  974. private BinderValidationStatusHandler<BEAN> statusHandler;
  975. private boolean hasChanges = false;
  976. /**
  977. * Creates a binder using a custom {@link PropertySet} implementation for
  978. * finding and resolving property names for
  979. * {@link #bindInstanceFields(Object)}, {@link #bind(HasValue, String)} and
  980. * {@link BindingBuilder#bind(String)}.
  981. *
  982. * @param propertySet
  983. * the property set implementation to use, not <code>null</code>.
  984. */
  985. protected Binder(PropertySet<BEAN> propertySet) {
  986. Objects.requireNonNull(propertySet, "propertySet cannot be null");
  987. this.propertySet = propertySet;
  988. }
  989. /**
  990. * Creates a new binder that uses reflection based on the provided bean type
  991. * to resolve bean properties.
  992. *
  993. * @param beanType
  994. * the bean type to use, not <code>null</code>
  995. */
  996. public Binder(Class<BEAN> beanType) {
  997. this(BeanPropertySet.get(beanType));
  998. }
  999. /**
  1000. * Creates a new binder without support for creating bindings based on
  1001. * property names. Use an alternative constructor, such as
  1002. * {@link Binder#Binder(Class)}, to create a binder that support creating
  1003. * bindings based on instance fields through
  1004. * {@link #bindInstanceFields(Object)}, or based on a property name through
  1005. * {@link #bind(HasValue, String)} or {@link BindingBuilder#bind(String)}.
  1006. */
  1007. public Binder() {
  1008. this(new PropertySet<BEAN>() {
  1009. @Override
  1010. public Stream<PropertyDefinition<BEAN, ?>> getProperties() {
  1011. throw new IllegalStateException(
  1012. "A Binder created with the default constructor doesn't support listing properties.");
  1013. }
  1014. @Override
  1015. public Optional<PropertyDefinition<BEAN, ?>> getProperty(
  1016. String name) {
  1017. throw new IllegalStateException(
  1018. "A Binder created with the default constructor doesn't support finding properties by name.");
  1019. }
  1020. });
  1021. }
  1022. /**
  1023. * Creates a binder using a custom {@link PropertySet} implementation for
  1024. * finding and resolving property names for
  1025. * {@link #bindInstanceFields(Object)}, {@link #bind(HasValue, String)} and
  1026. * {@link BindingBuilder#bind(String)}.
  1027. * <p>
  1028. * This functionality is provided as static method instead of as a public
  1029. * constructor in order to make it possible to use a custom property set
  1030. * without creating a subclass while still leaving the public constructors
  1031. * focused on the common use cases.
  1032. *
  1033. * @see Binder#Binder()
  1034. * @see Binder#Binder(Class)
  1035. *
  1036. * @param propertySet
  1037. * the property set implementation to use, not <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. PropertySet<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 PropertySet}.
  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 PropertySet}
  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. * Adds field value change listener to all the fields in the binder.
  1609. * <p>
  1610. * Added listener is notified every time whenever any bound field value is
  1611. * changed. The same functionality can be achieved by adding a
  1612. * {@link ValueChangeListener} to all fields in the {@link Binder}.
  1613. * <p>
  1614. * The listener is added to all fields regardless of whether the method is
  1615. * invoked before or after field is bound.
  1616. *
  1617. * @see ValueChangeEvent
  1618. * @see ValueChangeListener
  1619. *
  1620. * @param listener
  1621. * a field value change listener
  1622. * @return a registration for the listener
  1623. */
  1624. public Registration addValueChangeListener(
  1625. ValueChangeListener<?> listener) {
  1626. return getEventRouter().addListener(ValueChangeEvent.class, listener,
  1627. ValueChangeListener.class.getDeclaredMethods()[0]);
  1628. }
  1629. /**
  1630. * Creates a new binding with the given field.
  1631. *
  1632. * @param <FIELDVALUE>
  1633. * the value type of the field
  1634. * @param <TARGET>
  1635. * the target data type
  1636. * @param field
  1637. * the field to bind, not null
  1638. * @param converter
  1639. * the converter for converting between FIELDVALUE and TARGET
  1640. * types, not null
  1641. * @param handler
  1642. * the handler to notify of status changes, not null
  1643. * @return the new incomplete binding
  1644. */
  1645. protected <FIELDVALUE, TARGET> BindingBuilder<BEAN, TARGET> createBinding(
  1646. HasValue<FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converter,
  1647. BindingValidationStatusHandler handler) {
  1648. BindingBuilder<BEAN, TARGET> newBinding = doCreateBinding(field,
  1649. converter, handler);
  1650. if (incompleteMemberFieldBindings.containsKey(field)) {
  1651. incompleteMemberFieldBindings.put(field, newBinding);
  1652. }
  1653. incompleteBindings.put(field, newBinding);
  1654. return newBinding;
  1655. }
  1656. protected <FIELDVALUE, TARGET> BindingBuilder<BEAN, TARGET> doCreateBinding(
  1657. HasValue<FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converter,
  1658. BindingValidationStatusHandler handler) {
  1659. return new BindingBuilderImpl<>(this, field, converter, handler);
  1660. }
  1661. /**
  1662. * Clears the error condition of the given field, if any. The default
  1663. * implementation clears the
  1664. * {@link AbstractComponent#setComponentError(ErrorMessage) component error}
  1665. * of the field if it is a Component, otherwise does nothing.
  1666. *
  1667. * @param field
  1668. * the field with an invalid value
  1669. */
  1670. protected void clearError(HasValue<?> field) {
  1671. if (field instanceof AbstractComponent) {
  1672. ((AbstractComponent) field).setComponentError(null);
  1673. }
  1674. }
  1675. /**
  1676. * Handles a validation error emitted when trying to write the value of the
  1677. * given field. The default implementation sets the
  1678. * {@link AbstractComponent#setComponentError(ErrorMessage) component error}
  1679. * of the field if it is a Component, otherwise does nothing.
  1680. *
  1681. * @param field
  1682. * the field with the invalid value
  1683. * @param error
  1684. * the error message to set
  1685. */
  1686. protected void handleError(HasValue<?> field, String error) {
  1687. if (field instanceof AbstractComponent) {
  1688. ((AbstractComponent) field).setComponentError(new UserError(error));
  1689. }
  1690. }
  1691. /**
  1692. * Default {@link BindingValidationStatusHandler} functional method
  1693. * implementation.
  1694. *
  1695. * @param status
  1696. * the validation status
  1697. */
  1698. protected void handleValidationStatus(BindingValidationStatus<?> status) {
  1699. HasValue<?> source = status.getField();
  1700. clearError(source);
  1701. if (status.isError()) {
  1702. handleError(source, status.getMessage().get());
  1703. }
  1704. }
  1705. /**
  1706. * Returns the bindings for this binder.
  1707. *
  1708. * @return a set of the bindings
  1709. */
  1710. protected Set<BindingImpl<BEAN, ?, ?>> getBindings() {
  1711. return bindings;
  1712. }
  1713. /**
  1714. * The default binder level status handler.
  1715. * <p>
  1716. * Passes all field related results to the Binding status handlers. All
  1717. * other status changes are displayed in the status label, if one has been
  1718. * set with {@link #setStatusLabel(Label)}.
  1719. *
  1720. * @param binderStatus
  1721. * status of validation results from binding and/or bean level
  1722. * validators
  1723. */
  1724. protected void handleBinderValidationStatus(
  1725. BinderValidationStatus<BEAN> binderStatus) {
  1726. // let field events go to binding status handlers
  1727. binderStatus.getFieldValidationStatuses()
  1728. .forEach(status -> ((BindingImpl<?, ?, ?>) status.getBinding())
  1729. .notifyStatusHandler(status));
  1730. // show first possible error or OK status in the label if set
  1731. if (getStatusLabel().isPresent()) {
  1732. String statusMessage = binderStatus.getBeanValidationErrors()
  1733. .stream().findFirst().map(ValidationResult::getErrorMessage)
  1734. .orElse("");
  1735. getStatusLabel().get().setValue(statusMessage);
  1736. }
  1737. }
  1738. /**
  1739. * Sets whether the values of the fields this binder is bound to have
  1740. * changed since the last explicit call to either bind, write or read.
  1741. *
  1742. * @param hasChanges
  1743. * whether this binder should be marked to have changes
  1744. */
  1745. private void setHasChanges(boolean hasChanges) {
  1746. this.hasChanges = hasChanges;
  1747. }
  1748. /**
  1749. * Check whether any of the bound fields' values have changed since last
  1750. * explicit call to {@link #setBean(Object)}, {@link #readBean(Object)},
  1751. * {@link #removeBean()}, {@link #writeBean(Object)} or
  1752. * {@link #writeBeanIfValid(Object)}. Unsuccessful write operations will not
  1753. * affect this value. Return values for each case are compiled into the
  1754. * following table:
  1755. *
  1756. * <p>
  1757. *
  1758. * <table>
  1759. * <tr>
  1760. * <td></td>
  1761. * <td>After readBean, setBean or removeBean</td>
  1762. * <td>After valid user changes</td>
  1763. * <td>After invalid user changes</td>
  1764. * <td>After successful writeBean or writeBeanIfValid</td>
  1765. * <td>After unsuccessful writeBean or writeBeanIfValid</td>
  1766. * </tr>
  1767. * <tr>
  1768. * <td>A bean is currently bound</td>
  1769. * <td>{@code false}</td>
  1770. * <td>{@code false}</td>
  1771. * <td>{@code true}</td>
  1772. * <td>{@code false}</td>
  1773. * <td>no change</td>
  1774. * </tr>
  1775. * <tr>
  1776. * <td>No bean is currently bound</td>
  1777. * <td>{@code false}</td>
  1778. * <td>{@code true}</td>
  1779. * <td>{@code true}</td>
  1780. * <td>{@code false}</td>
  1781. * <td>no change</td>
  1782. * </tr>
  1783. * </table>
  1784. *
  1785. * @return whether any bound field's value has changed since last call to
  1786. * setBean, readBean, writeBean or writeBeanIfValid
  1787. */
  1788. public boolean hasChanges() {
  1789. return hasChanges;
  1790. }
  1791. /**
  1792. * Sets the read only state to the given value for all currently bound
  1793. * fields.
  1794. * <p>
  1795. * This is just a shorthand for calling setReadOnly for all currently bound
  1796. * fields. It means that fields bound after this method call won't be set
  1797. * read-only.
  1798. *
  1799. * @param fieldsReadOnly
  1800. * true to set the fields to read only, false to set them to read
  1801. * write
  1802. */
  1803. public void setReadOnly(boolean fieldsReadOnly) {
  1804. getBindings().stream().map(BindingImpl::getField)
  1805. .forEach(field -> field.setReadOnly(fieldsReadOnly));
  1806. }
  1807. /**
  1808. * Returns the event router for this binder.
  1809. *
  1810. * @return the event router, not null
  1811. */
  1812. protected EventRouter getEventRouter() {
  1813. if (eventRouter == null) {
  1814. eventRouter = new EventRouter();
  1815. }
  1816. return eventRouter;
  1817. }
  1818. /**
  1819. * Configures the {@code binding} with the property definition
  1820. * {@code definition} before it's being bound.
  1821. *
  1822. * @param binding
  1823. * a binding to configure
  1824. * @param definition
  1825. * a property definition information
  1826. * @return the new configured binding
  1827. */
  1828. protected BindingBuilder<BEAN, ?> configureBinding(
  1829. BindingBuilder<BEAN, ?> binding,
  1830. PropertyDefinition<BEAN, ?> definition) {
  1831. return binding;
  1832. }
  1833. private void doRemoveBean(boolean fireStatusEvent) {
  1834. setHasChanges(false);
  1835. if (bean != null) {
  1836. bean = null;
  1837. }
  1838. getValidationStatusHandler().statusChange(
  1839. BinderValidationStatus.createUnresolvedStatus(this));
  1840. if (fireStatusEvent) {
  1841. fireStatusChangeEvent(false);
  1842. }
  1843. }
  1844. private void fireStatusChangeEvent(boolean hasValidationErrors) {
  1845. getEventRouter()
  1846. .fireEvent(new StatusChangeEvent(this, hasValidationErrors));
  1847. }
  1848. private <FIELDVALUE> Converter<FIELDVALUE, FIELDVALUE> createNullRepresentationAdapter(
  1849. HasValue<FIELDVALUE> field) {
  1850. Converter<FIELDVALUE, FIELDVALUE> nullRepresentationConverter = Converter
  1851. .from(fieldValue -> fieldValue,
  1852. modelValue -> Objects.isNull(modelValue)
  1853. ? field.getEmptyValue() : modelValue,
  1854. exception -> exception.getMessage());
  1855. ConverterDelegate<FIELDVALUE> converter = new ConverterDelegate<>(
  1856. nullRepresentationConverter);
  1857. initialConverters.put(field, converter);
  1858. return converter;
  1859. }
  1860. /**
  1861. * Throws if this binder has incomplete bindings.
  1862. *
  1863. * @param methodName
  1864. * name of the method where this call is originated from
  1865. * @throws IllegalStateException
  1866. * if this binder has incomplete bindings
  1867. */
  1868. private void checkBindingsCompleted(String methodName) {
  1869. if (!incompleteMemberFieldBindings.isEmpty()) {
  1870. throw new IllegalStateException(
  1871. "All bindings created with forMemberField must "
  1872. + "be completed with bindInstanceFields before calling "
  1873. + methodName);
  1874. }
  1875. if (!incompleteBindings.isEmpty()) {
  1876. throw new IllegalStateException(
  1877. "All bindings created with forField must be completed before calling "
  1878. + methodName);
  1879. }
  1880. }
  1881. /**
  1882. * Binds member fields found in the given object.
  1883. * <p>
  1884. * This method processes all (Java) member fields whose type extends
  1885. * {@link HasValue} and that can be mapped to a property id. Property name
  1886. * mapping is done based on the field name or on a @{@link PropertyId}
  1887. * annotation on the field. All non-null unbound fields for which a property
  1888. * name can be determined are bound to the property name using
  1889. * {@link BindingBuilder#bind(String)}.
  1890. * <p>
  1891. * For example:
  1892. *
  1893. * <pre>
  1894. * public class MyForm extends VerticalLayout {
  1895. * private TextField firstName = new TextField("First name");
  1896. * &#64;PropertyId("last")
  1897. * private TextField lastName = new TextField("Last name");
  1898. *
  1899. * MyForm myForm = new MyForm();
  1900. * ...
  1901. * binder.bindMemberFields(myForm);
  1902. * </pre>
  1903. *
  1904. * This binds the firstName TextField to a "firstName" property in the item,
  1905. * lastName TextField to a "last" property.
  1906. * <p>
  1907. * It's not always possible to bind a field to a property because their
  1908. * types are incompatible. E.g. custom converter is required to bind
  1909. * {@code HasValue<String>} and {@code Integer} property (that would be a
  1910. * case of "age" property). In such case {@link IllegalStateException} will
  1911. * be thrown unless the field has been configured manually before calling
  1912. * the {@link #bindInstanceFields(Object)} method.
  1913. * <p>
  1914. * It's always possible to do custom binding for any field: the
  1915. * {@link #bindInstanceFields(Object)} method doesn't override existing
  1916. * bindings.
  1917. *
  1918. * @param objectWithMemberFields
  1919. * The object that contains (Java) member fields to bind
  1920. * @throws IllegalStateException
  1921. * if there are incompatible HasValue&lt;T&gt; and property
  1922. * types
  1923. */
  1924. public void bindInstanceFields(Object objectWithMemberFields) {
  1925. Class<?> objectClass = objectWithMemberFields.getClass();
  1926. getFieldsInDeclareOrder(objectClass).stream()
  1927. .filter(memberField -> HasValue.class
  1928. .isAssignableFrom(memberField.getType()))
  1929. .forEach(memberField -> handleProperty(memberField,
  1930. objectWithMemberFields,
  1931. (property, type) -> bindProperty(objectWithMemberFields,
  1932. memberField, property, type)));
  1933. }
  1934. @SuppressWarnings("unchecked")
  1935. private BindingBuilder<BEAN, ?> getIncompleteMemberFieldBinding(
  1936. Field memberField, Object objectWithMemberFields) {
  1937. memberField.setAccessible(true);
  1938. try {
  1939. return incompleteMemberFieldBindings
  1940. .get(memberField.get(objectWithMemberFields));
  1941. } catch (IllegalArgumentException | IllegalAccessException e) {
  1942. throw new RuntimeException(e);
  1943. } finally {
  1944. memberField.setAccessible(false);
  1945. }
  1946. }
  1947. /**
  1948. * Binds {@code property} with {@code propertyType} to the field in the
  1949. * {@code objectWithMemberFields} instance using {@code memberField} as a
  1950. * reference to a member.
  1951. *
  1952. * @param objectWithMemberFields
  1953. * the object that contains (Java) member fields to build and
  1954. * bind
  1955. * @param memberField
  1956. * reference to a member field to bind
  1957. * @param property
  1958. * property name to bind
  1959. * @param propertyType
  1960. * type of the property
  1961. */
  1962. private void bindProperty(Object objectWithMemberFields, Field memberField,
  1963. String property, Class<?> propertyType) {
  1964. Type valueType = GenericTypeReflector.getTypeParameter(
  1965. memberField.getGenericType(),
  1966. HasValue.class.getTypeParameters()[0]);
  1967. if (valueType == null) {
  1968. throw new IllegalStateException(String.format(
  1969. "Unable to detect value type for the member '%s' in the "
  1970. + "class '%s'.",
  1971. memberField.getName(),
  1972. objectWithMemberFields.getClass().getName()));
  1973. }
  1974. if (propertyType.equals(GenericTypeReflector.erase(valueType))) {
  1975. HasValue<?> field;
  1976. // Get the field from the object
  1977. try {
  1978. field = (HasValue<?>) ReflectTools.getJavaFieldValue(
  1979. objectWithMemberFields, memberField, HasValue.class);
  1980. } catch (IllegalArgumentException | IllegalAccessException
  1981. | InvocationTargetException e) {
  1982. // If we cannot determine the value, just skip the field
  1983. return;
  1984. }
  1985. if (field == null) {
  1986. field = makeFieldInstance(
  1987. (Class<? extends HasValue<?>>) memberField.getType());
  1988. initializeField(objectWithMemberFields, memberField, field);
  1989. }
  1990. forField(field).bind(property);
  1991. } else {
  1992. throw new IllegalStateException(String.format(
  1993. "Property type '%s' doesn't "
  1994. + "match the field type '%s'. "
  1995. + "Binding should be configured manually using converter.",
  1996. propertyType.getName(), valueType.getTypeName()));
  1997. }
  1998. }
  1999. /**
  2000. * Makes an instance of the field type {@code fieldClass}.
  2001. * <p>
  2002. * The resulting field instance is used to bind a property to it using the
  2003. * {@link #bindInstanceFields(Object)} method.
  2004. * <p>
  2005. * The default implementation relies on the default constructor of the
  2006. * class. If there is no suitable default constructor or you want to
  2007. * configure the instantiated class then override this method and provide
  2008. * your own implementation.
  2009. *
  2010. * @see #bindInstanceFields(Object)
  2011. * @param fieldClass
  2012. * type of the field
  2013. * @return a {@code fieldClass} instance object
  2014. */
  2015. private HasValue<?> makeFieldInstance(
  2016. Class<? extends HasValue<?>> fieldClass) {
  2017. try {
  2018. return fieldClass.newInstance();
  2019. } catch (InstantiationException | IllegalAccessException e) {
  2020. throw new IllegalStateException(
  2021. String.format("Couldn't create an '%s' type instance",
  2022. fieldClass.getName()),
  2023. e);
  2024. }
  2025. }
  2026. /**
  2027. * Returns an array containing {@link Field} objects reflecting all the
  2028. * fields of the class or interface represented by this Class object. The
  2029. * elements in the array returned are sorted in declare order from sub class
  2030. * to super class.
  2031. *
  2032. * @param searchClass
  2033. * class to introspect
  2034. * @return list of all fields in the class considering hierarchy
  2035. */
  2036. private List<Field> getFieldsInDeclareOrder(Class<?> searchClass) {
  2037. ArrayList<Field> memberFieldInOrder = new ArrayList<>();
  2038. while (searchClass != null) {
  2039. memberFieldInOrder
  2040. .addAll(Arrays.asList(searchClass.getDeclaredFields()));
  2041. searchClass = searchClass.getSuperclass();
  2042. }
  2043. return memberFieldInOrder;
  2044. }
  2045. private void initializeField(Object objectWithMemberFields,
  2046. Field memberField, HasValue<?> value) {
  2047. try {
  2048. ReflectTools.setJavaFieldValue(objectWithMemberFields, memberField,
  2049. value);
  2050. } catch (IllegalArgumentException | IllegalAccessException
  2051. | InvocationTargetException e) {
  2052. throw new IllegalStateException(
  2053. String.format("Could not assign value to field '%s'",
  2054. memberField.getName()),
  2055. e);
  2056. }
  2057. }
  2058. private void handleProperty(Field field, Object objectWithMemberFields,
  2059. BiConsumer<String, Class<?>> propertyHandler) {
  2060. Optional<PropertyDefinition<BEAN, ?>> descriptor = getPropertyDescriptor(
  2061. field);
  2062. if (!descriptor.isPresent()) {
  2063. return;
  2064. }
  2065. String propertyName = descriptor.get().getName();
  2066. if (boundProperties.containsKey(propertyName)) {
  2067. return;
  2068. }
  2069. BindingBuilder<BEAN, ?> tentativeBinding = getIncompleteMemberFieldBinding(
  2070. field, objectWithMemberFields);
  2071. if (tentativeBinding != null) {
  2072. tentativeBinding.bind(propertyName);
  2073. return;
  2074. }
  2075. propertyHandler.accept(propertyName, descriptor.get().getType());
  2076. assert boundProperties.containsKey(propertyName);
  2077. }
  2078. /**
  2079. * Gets the binding for a property name. Bindings are available by property
  2080. * name if bound using {@link #bind(HasValue, String)},
  2081. * {@link BindingBuilder#bind(String)} or indirectly using
  2082. * {@link #bindInstanceFields(Object)}.
  2083. *
  2084. * @param propertyName
  2085. * the property name of the binding to get
  2086. * @return the binding corresponding to the property name, or an empty
  2087. * optional if there is no binding with that property name
  2088. */
  2089. public Optional<Binding<BEAN, ?>> getBinding(String propertyName) {
  2090. return Optional.ofNullable(boundProperties.get(propertyName));
  2091. }
  2092. private Optional<PropertyDefinition<BEAN, ?>> getPropertyDescriptor(
  2093. Field field) {
  2094. PropertyId propertyIdAnnotation = field.getAnnotation(PropertyId.class);
  2095. String propertyId;
  2096. if (propertyIdAnnotation != null) {
  2097. // @PropertyId(propertyId) always overrides property id
  2098. propertyId = propertyIdAnnotation.value();
  2099. } else {
  2100. propertyId = field.getName();
  2101. }
  2102. String minifiedFieldName = minifyFieldName(propertyId);
  2103. return propertySet.getProperties().map(PropertyDefinition::getName)
  2104. .filter(name -> minifyFieldName(name).equals(minifiedFieldName))
  2105. .findFirst().flatMap(propertySet::getProperty);
  2106. }
  2107. private String minifyFieldName(String fieldName) {
  2108. return fieldName.toLowerCase(Locale.ENGLISH).replace("_", "");
  2109. }
  2110. private <V> void fireValueChangeEvent(ValueChangeEvent<V> event) {
  2111. getEventRouter().fireEvent(event);
  2112. }
  2113. }