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.

AbstractMultiSelectConnector.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2000-2018 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.client.connectors;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Optional;
  20. import java.util.Set;
  21. import java.util.function.BiConsumer;
  22. import com.google.gwt.user.client.ui.Composite;
  23. import com.google.gwt.user.client.ui.IsWidget;
  24. import com.google.gwt.user.client.ui.Widget;
  25. import com.vaadin.client.data.DataSource;
  26. import com.vaadin.shared.Range;
  27. import com.vaadin.shared.Registration;
  28. import com.vaadin.shared.data.selection.MultiSelectServerRpc;
  29. import com.vaadin.shared.ui.ListingJsonConstants;
  30. import com.vaadin.shared.ui.abstractmultiselect.AbstractMultiSelectState;
  31. import elemental.json.JsonObject;
  32. /**
  33. * A base connector class for multiselects.
  34. * <p>
  35. * Does not care about the framework provided selection model for now, instead
  36. * just passes selection information per item.
  37. *
  38. * @author Vaadin Ltd.
  39. *
  40. * @since 8.0
  41. */
  42. public abstract class AbstractMultiSelectConnector
  43. extends AbstractListingConnector {
  44. /**
  45. * Abstraction layer to help populate different multiselect widgets based on
  46. * same JSON data.
  47. */
  48. public interface MultiSelectWidget {
  49. /**
  50. * Sets the given items to the select.
  51. *
  52. * @param items
  53. * the items for the select
  54. */
  55. void setItems(List<JsonObject> items);
  56. /**
  57. * Adds a selection change listener the select.
  58. *
  59. * @param selectionChangeListener
  60. * the listener to add, not {@code null}
  61. * @return a registration handle to remove the listener
  62. */
  63. Registration addSelectionChangeListener(
  64. BiConsumer<Set<String>, Set<String>> selectionChangeListener);
  65. /**
  66. * Returns the caption for the given item.
  67. *
  68. * @param item
  69. * the item, not {@code null}
  70. * @return caption of the item
  71. */
  72. static String getCaption(JsonObject item) {
  73. return item.getString(ListingJsonConstants.JSONKEY_ITEM_VALUE);
  74. }
  75. /**
  76. * Returns the key for the given item.
  77. *
  78. * @param item
  79. * the item, not {@code null}
  80. * @return key of the item
  81. */
  82. static String getKey(JsonObject item) {
  83. return getRowKey(item);
  84. }
  85. /**
  86. * Returns whether the given item is enabled or not.
  87. * <p>
  88. * Disabling items is not supported by all multiselects.
  89. *
  90. * @param item
  91. * the item, not {@code null}
  92. * @return {@code true} enabled, {@code false} if not
  93. */
  94. static boolean isEnabled(JsonObject item) {
  95. return !(item.hasKey(ListingJsonConstants.JSONKEY_ITEM_DISABLED)
  96. && item.getBoolean(
  97. ListingJsonConstants.JSONKEY_ITEM_DISABLED));
  98. }
  99. /**
  100. * Returns whether this item is selected or not.
  101. *
  102. * @param item
  103. * the item, not {@code null}
  104. * @return {@code true} is selected, {@code false} if not
  105. */
  106. static boolean isSelected(JsonObject item) {
  107. return item.getBoolean(ListingJsonConstants.JSONKEY_ITEM_SELECTED);
  108. }
  109. /**
  110. * Returns the optional icon URL for the given item.
  111. * <p>
  112. * Item icons are not supported by all multiselects.
  113. *
  114. * @param item
  115. * the item
  116. * @return the optional icon URL, or an empty optional if none specified
  117. */
  118. static Optional<String> getIconUrl(JsonObject item) {
  119. return Optional.ofNullable(
  120. item.getString(ListingJsonConstants.JSONKEY_ITEM_ICON));
  121. }
  122. }
  123. /**
  124. * Returns the multiselect widget for this connector.
  125. * <p>
  126. * This is used because {@link #getWidget()} returns a class
  127. * ({@link Widget}) instead of an interface ({@link IsWidget}), and most
  128. * multiselects extends {@link Composite}.
  129. *
  130. * @return the multiselect widget
  131. */
  132. public abstract MultiSelectWidget getMultiSelectWidget();
  133. @Override
  134. protected void init() {
  135. super.init();
  136. MultiSelectServerRpc rpcProxy = getRpcProxy(MultiSelectServerRpc.class);
  137. getMultiSelectWidget().addSelectionChangeListener(
  138. (addedItems, removedItems) -> rpcProxy
  139. .updateSelection(addedItems, removedItems));
  140. }
  141. @Override
  142. public AbstractMultiSelectState getState() {
  143. return (AbstractMultiSelectState) super.getState();
  144. }
  145. @Override
  146. public void setDataSource(DataSource<JsonObject> dataSource) {
  147. dataSource.addDataChangeHandler(this::onDataChange);
  148. super.setDataSource(dataSource);
  149. }
  150. /**
  151. * This method handles the parsing of the new JSON data containing the items
  152. * and the selection information.
  153. *
  154. * @param range
  155. * the updated range, never {@code null}
  156. */
  157. protected void onDataChange(Range range) {
  158. assert range.getStart() == 0
  159. && range.getEnd() == getDataSource().size() : getClass()
  160. .getSimpleName()
  161. + " only supports full updates, but got range " + range;
  162. List<JsonObject> items = new ArrayList<>(range.length());
  163. for (int i = 0; i < range.getEnd(); i++) {
  164. items.add(getDataSource().getRow(i));
  165. }
  166. getMultiSelectWidget().setItems(items);
  167. }
  168. @Override
  169. public boolean isRequiredIndicatorVisible() {
  170. return getState().required && !isReadOnly();
  171. }
  172. }