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.

EditorImpl.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.ui.components.grid;
  17. import java.util.Collections;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Objects;
  22. import java.util.stream.Collectors;
  23. import java.util.stream.Stream;
  24. import com.vaadin.data.Binder;
  25. import com.vaadin.data.Binder.Binding;
  26. import com.vaadin.data.BinderValidationStatus;
  27. import com.vaadin.data.BinderValidationStatusHandler;
  28. import com.vaadin.data.PropertySet;
  29. import com.vaadin.shared.ui.grid.editor.EditorClientRpc;
  30. import com.vaadin.shared.ui.grid.editor.EditorServerRpc;
  31. import com.vaadin.shared.ui.grid.editor.EditorState;
  32. import com.vaadin.ui.Component;
  33. import com.vaadin.ui.Grid.AbstractGridExtension;
  34. import com.vaadin.ui.Grid.Column;
  35. import elemental.json.JsonObject;
  36. /**
  37. * Implementation of {@code Editor} interface.
  38. *
  39. * @param <T>
  40. * the grid bean type
  41. */
  42. public class EditorImpl<T> extends AbstractGridExtension<T>
  43. implements Editor<T> {
  44. private class EditorStatusHandler
  45. implements BinderValidationStatusHandler<T> {
  46. @Override
  47. public void statusChange(BinderValidationStatus<T> status) {
  48. boolean ok = status.isOk();
  49. if (saving) {
  50. rpc.confirmSave(ok);
  51. saving = false;
  52. }
  53. if (ok) {
  54. if (binder.getBean() != null) {
  55. refresh(binder.getBean());
  56. }
  57. rpc.setErrorMessage(null, Collections.emptyList());
  58. } else {
  59. List<Component> fields = status.getFieldValidationErrors()
  60. .stream().map(error -> error.getField())
  61. .filter(columnFields.values()::contains)
  62. .map(field -> (Component) field)
  63. .collect(Collectors.toList());
  64. Map<Component, Column<T, ?>> fieldToColumn = new HashMap<>();
  65. columnFields.entrySet().stream()
  66. .filter(entry -> fields.contains(entry.getValue()))
  67. .forEach(entry -> fieldToColumn.put(entry.getValue(),
  68. entry.getKey()));
  69. String message = errorGenerator.apply(fieldToColumn, status);
  70. List<String> columnIds = fieldToColumn.values().stream()
  71. .map(column -> getInternalIdForColumn(column))
  72. .collect(Collectors.toList());
  73. rpc.setErrorMessage(message, columnIds);
  74. }
  75. }
  76. }
  77. private Binder<T> binder;
  78. private Map<Column<T, ?>, Component> columnFields = new HashMap<>();
  79. private T edited;
  80. private boolean saving = false;
  81. private EditorClientRpc rpc;
  82. private EditorErrorGenerator<T> errorGenerator = (fieldToColumn,
  83. status) -> {
  84. String message = status.getFieldValidationErrors().stream()
  85. .filter(e -> e.getMessage().isPresent()
  86. && fieldToColumn.containsKey(e.getField()))
  87. .map(e -> fieldToColumn.get(e.getField()).getCaption() + ": "
  88. + e.getMessage().get())
  89. .collect(Collectors.joining("; "));
  90. String beanMessage = status.getBeanValidationErrors().stream()
  91. .map(e -> e.getErrorMessage())
  92. .collect(Collectors.joining("; "));
  93. message = Stream.of(message, beanMessage).filter(s -> !s.isEmpty())
  94. .collect(Collectors.joining("; "));
  95. return message;
  96. };
  97. /**
  98. * Constructor for internal implementation of the Editor.
  99. *
  100. * @param propertySet
  101. * the property set to use for configuring the default binder
  102. */
  103. public EditorImpl(PropertySet<T> propertySet) {
  104. rpc = getRpcProxy(EditorClientRpc.class);
  105. registerRpc(new EditorServerRpc() {
  106. @Override
  107. public void save() {
  108. saving = true;
  109. EditorImpl.this.save();
  110. }
  111. @Override
  112. public void cancel() {
  113. doClose();
  114. }
  115. @Override
  116. public void bind(String key) {
  117. // When in buffered mode, the editor is not allowed to move.
  118. // Binder with failed validation returns true for hasChanges.
  119. if (isOpen() && (isBuffered() || getBinder().hasChanges())) {
  120. rpc.confirmBind(false);
  121. return;
  122. }
  123. doClose();
  124. doEdit(getData(key));
  125. rpc.confirmBind(true);
  126. }
  127. });
  128. setBinder(Binder.withPropertySet(propertySet));
  129. }
  130. @Override
  131. public void generateData(T item, JsonObject jsonObject) {
  132. }
  133. @Override
  134. public Editor<T> setBinder(Binder<T> binder) {
  135. this.binder = binder;
  136. binder.setValidationStatusHandler(new EditorStatusHandler());
  137. return this;
  138. }
  139. @Override
  140. public Binder<T> getBinder() {
  141. return binder;
  142. }
  143. @Override
  144. public Editor<T> setBuffered(boolean buffered) {
  145. if (isOpen()) {
  146. throw new IllegalStateException(
  147. "Cannot modify Editor when it is open.");
  148. }
  149. getState().buffered = buffered;
  150. return this;
  151. }
  152. @Override
  153. public Editor<T> setEnabled(boolean enabled) {
  154. if (isOpen()) {
  155. throw new IllegalStateException(
  156. "Cannot modify Editor when it is open.");
  157. }
  158. getState().enabled = enabled;
  159. return this;
  160. }
  161. @Override
  162. public boolean isBuffered() {
  163. return getState(false).buffered;
  164. }
  165. @Override
  166. public boolean isEnabled() {
  167. return getState(false).enabled;
  168. }
  169. /**
  170. * Handles editor component generation and adding them to the hierarchy of
  171. * the Grid.
  172. *
  173. * @param bean
  174. * the edited item; can't be {@code null}
  175. */
  176. protected void doEdit(T bean) {
  177. Objects.requireNonNull(bean, "Editor can't edit null");
  178. if (!isEnabled()) {
  179. throw new IllegalStateException(
  180. "Editing is not allowed when Editor is disabled.");
  181. }
  182. if (!isBuffered()) {
  183. binder.setBean(bean);
  184. } else {
  185. binder.readBean(bean);
  186. }
  187. edited = bean;
  188. getParent().getColumns().stream().filter(Column::isEditable)
  189. .forEach(c -> {
  190. Binding<T, ?> binding = c.getEditorBinding();
  191. assert binding
  192. .getField() instanceof Component : "Grid should enforce that the binding field is a component";
  193. Component component = (Component) binding.getField();
  194. addComponentToGrid(component);
  195. columnFields.put(c, component);
  196. getState().columnFields.put(getInternalIdForColumn(c),
  197. component.getConnectorId());
  198. });
  199. }
  200. @Override
  201. public boolean save() {
  202. if (isOpen() && isBuffered()) {
  203. binder.validate();
  204. if (binder.writeBeanIfValid(edited)) {
  205. refresh(edited);
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. @Override
  212. public boolean isOpen() {
  213. return edited != null;
  214. }
  215. @Override
  216. public void cancel() {
  217. doClose();
  218. rpc.cancel();
  219. }
  220. /**
  221. * Handles clean up for closing the Editor.
  222. */
  223. protected void doClose() {
  224. edited = null;
  225. for (Component c : columnFields.values()) {
  226. removeComponentFromGrid(c);
  227. }
  228. columnFields.clear();
  229. getState().columnFields.clear();
  230. }
  231. @Override
  232. public Editor<T> setSaveCaption(String saveCaption) {
  233. Objects.requireNonNull(saveCaption);
  234. getState().saveCaption = saveCaption;
  235. return this;
  236. }
  237. @Override
  238. public Editor<T> setCancelCaption(String cancelCaption) {
  239. Objects.requireNonNull(cancelCaption);
  240. getState().cancelCaption = cancelCaption;
  241. return this;
  242. }
  243. @Override
  244. public String getSaveCaption() {
  245. return getState(false).saveCaption;
  246. }
  247. @Override
  248. public String getCancelCaption() {
  249. return getState(false).cancelCaption;
  250. }
  251. @Override
  252. protected EditorState getState() {
  253. return getState(true);
  254. }
  255. @Override
  256. protected EditorState getState(boolean markAsDirty) {
  257. return (EditorState) super.getState(markAsDirty);
  258. }
  259. @Override
  260. public Editor<T> setErrorGenerator(EditorErrorGenerator<T> errorGenerator) {
  261. Objects.requireNonNull(errorGenerator, "Error generator can't be null");
  262. this.errorGenerator = errorGenerator;
  263. return this;
  264. }
  265. @Override
  266. public EditorErrorGenerator<T> getErrorGenerator() {
  267. return errorGenerator;
  268. }
  269. }