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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2000-2014 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.tokka.server.communication.data;
  17. import java.io.Serializable;
  18. import java.util.Collection;
  19. import java.util.LinkedHashSet;
  20. import java.util.Set;
  21. import java.util.function.BiConsumer;
  22. import java.util.function.Function;
  23. import com.vaadin.tokka.ui.components.HasValue;
  24. import com.vaadin.tokka.ui.components.Listing;
  25. // TODO: Should this class listen to changes in DataSource?
  26. public class Binder<T> implements Serializable {
  27. private Set<FieldBinding<T, ?>> bindings = new LinkedHashSet<>();
  28. private T bean;
  29. /**
  30. * Internal class for tracking field bindings with getters and setters.
  31. *
  32. * @param <T>
  33. * bean type
  34. * @param <V>
  35. * value type
  36. */
  37. private static class FieldBinding<T, V> implements Serializable {
  38. private HasValue<V> field;
  39. private Function<T, V> getter;
  40. private BiConsumer<T, V> setter;
  41. public FieldBinding(HasValue<V> field, Function<T, V> getter,
  42. BiConsumer<T, V> setter) {
  43. this.field = field;
  44. this.getter = getter;
  45. this.setter = setter;
  46. }
  47. void setFieldValue(T bean) {
  48. field.setValue(getter.apply(bean));
  49. }
  50. void storeFieldValue(T bean) {
  51. if (setter != null) {
  52. setter.accept(bean, field.getValue());
  53. }
  54. }
  55. }
  56. /**
  57. * Gets the bean that has been bound with {@link #bind}.
  58. *
  59. * @return the currently bound bean.
  60. */
  61. public T getBean() {
  62. return bean;
  63. }
  64. /**
  65. * Registers a binding for given field by using the getter and setter. If
  66. * the Binder is already bound to some bean, the new field will be also
  67. * bound to it.
  68. * <p>
  69. * Not providing a setter implicitly sets the field to be read only.
  70. * <p>
  71. * Getters and setters can be used to make conversions happen. They are also
  72. * a good place to update the state of the field. You should avoid making
  73. * any unnecessary changes after binding a field to avoid any unexpected
  74. * problems.
  75. *
  76. * @param field
  77. * editor field
  78. * @param getter
  79. * getter method to fetch data from the bean
  80. * @param setter
  81. * setter method to store data back to the bean
  82. */
  83. public <V> void addField(HasValue<V> field, Function<T, V> getter,
  84. BiConsumer<T, V> setter) {
  85. if (getter == null || field == null) {
  86. throw new IllegalArgumentException();
  87. }
  88. FieldBinding<T, V> binding = new FieldBinding<>(field, getter, setter);
  89. bindings.add(binding);
  90. if (bean != null) {
  91. binding.setFieldValue(bean);
  92. }
  93. field.onChange(value -> handleChangeEvent(field));
  94. }
  95. /**
  96. * Binds the given bean to all bound fields.
  97. *
  98. * @param bean
  99. * edited bean
  100. */
  101. public void bind(T bean) {
  102. this.bean = bean;
  103. if (bean == null) {
  104. // TODO: clean up?
  105. return;
  106. }
  107. for (FieldBinding<T, ?> binding : bindings) {
  108. binding.setFieldValue(bean);
  109. }
  110. }
  111. public <V> void addSelect(Listing<V> listing, Function<T, V> getter,
  112. BiConsumer<T, V> setter) {
  113. if (listing.getSelectionModel() instanceof SelectionModel.Single) {
  114. addField((SelectionModel.Single<V>) listing.getSelectionModel(),
  115. getter, setter);
  116. } else {
  117. throw new IllegalArgumentException(
  118. "Listing did not have a single selection model.");
  119. }
  120. }
  121. public <V> void addMultiSelect(Listing<V> listing,
  122. Function<T, Collection<V>> getter,
  123. BiConsumer<T, Collection<V>> setter) {
  124. if (listing.getSelectionModel() instanceof SelectionModel.Multi) {
  125. addField((SelectionModel.Multi<V>) listing.getSelectionModel(),
  126. getter, setter);
  127. } else {
  128. throw new IllegalArgumentException(
  129. "Listing did not have a multi selection model.");
  130. }
  131. }
  132. // TODO: Is this correct return value? How should we manage the error
  133. // messages from custom validation? Documentation?
  134. // public abstract void addValidator(ValueProvider<T, String> validator);
  135. // TODO: Needs remove method as well?
  136. /**
  137. * Saves any changes from the bound fields to the edited bean.
  138. */
  139. public void save() {
  140. if (bean == null) {
  141. return;
  142. }
  143. for (FieldBinding<T, ?> binding : bindings) {
  144. binding.storeFieldValue(bean);
  145. }
  146. // TODO: Postvalidation
  147. }
  148. /**
  149. * Resets any changes in the fields to match values from the edited bean.
  150. */
  151. // TODO: Do we need this?
  152. public void reset() {
  153. // Re-bind to refresh all fields
  154. bind(bean);
  155. }
  156. /* PROTECTED SCOPE */
  157. // Exists for the sake of making something before / after field update is
  158. // processed
  159. /**
  160. * This method does prevalidation for every changed field. By overriding
  161. * this method you can react to changes that need to happen when certain
  162. * fields are edited. e.g. re-apply conversions.
  163. *
  164. * @param field
  165. * changed field
  166. */
  167. protected <V> void handleChangeEvent(HasValue<V> field) {
  168. // TODO: pre-validation ?
  169. }
  170. /*
  171. * FIXME: Validation needs an idea. // TODO: Document protected abstract
  172. * void validate();
  173. *
  174. * // TODO: Document protected abstract void doJSRValidation();
  175. */
  176. }