|
| 1 | +/* |
| 2 | + * Copyright 2000-2022 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.flow.component.combobox; |
| 17 | + |
| 18 | +import com.vaadin.flow.component.Tag; |
| 19 | +import com.vaadin.flow.component.dependency.JsModule; |
| 20 | +import com.vaadin.flow.component.dependency.NpmPackage; |
| 21 | +import com.vaadin.flow.data.provider.DataCommunicator; |
| 22 | +import com.vaadin.flow.data.provider.DataKeyMapper; |
| 23 | +import com.vaadin.flow.data.provider.DataProvider; |
| 24 | +import elemental.json.Json; |
| 25 | +import elemental.json.JsonArray; |
| 26 | +import elemental.json.JsonObject; |
| 27 | + |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Objects; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +/** |
| 35 | + * MultiSelectComboBox allows the user to select one or more values from a |
| 36 | + * filterable list of options presented in an overlay. Compared to |
| 37 | + * {@link ComboBox}, MultiSelectComboBox allows to select multiple values. |
| 38 | + * <p> |
| 39 | + * MultiSelectComboBox supports lazy loading. This means that when using large |
| 40 | + * data sets, items are requested from the server one "page" at a time when the |
| 41 | + * user scrolls down the overlay. The number of items in one page is by default |
| 42 | + * 50, and can be changed with {@link #setPageSize(int)}. |
| 43 | + * <p> |
| 44 | + * MultiSelectComboBox can do filtering either in the browser or in the server. |
| 45 | + * When MultiSelectComboBox has only a relatively small set of items, the |
| 46 | + * filtering will happen in the browser, allowing smooth user-experience. When |
| 47 | + * the size of the data set is larger than the {@code pageSize}, the |
| 48 | + * webcomponent doesn't necessarily have all the data available, and it will |
| 49 | + * make requests to the server to handle the filtering. Also, if you have |
| 50 | + * defined custom filtering logic, with eg. |
| 51 | + * {@link #setItems(ComboBox.ItemFilter, Collection)}, filtering will happen in |
| 52 | + * the server. To enable client-side filtering with larger data sets, you can |
| 53 | + * override the {@code pageSize} to be bigger than the size of your data set. |
| 54 | + * However, then the full data set will be sent to the client immediately, and |
| 55 | + * you will lose the benefits of lazy loading. |
| 56 | + * |
| 57 | + * @param <TItem> |
| 58 | + * the type of the items to be selectable from the combo box |
| 59 | + * @author Vaadin Ltd |
| 60 | + */ |
| 61 | +@Tag("vaadin-multi-select-combo-box") |
| 62 | +@NpmPackage(value = "@vaadin/polymer-legacy-adapter", version = "23.1.0-rc2") |
| 63 | +@JsModule("@vaadin/polymer-legacy-adapter/style-modules.js") |
| 64 | +@NpmPackage(value = "@vaadin/multi-select-combo-box", version = "23.1.0-rc2") |
| 65 | +@JsModule("@vaadin/multi-select-combo-box/src/vaadin-multi-select-combo-box.js") |
| 66 | +@JsModule("@vaadin/polymer-legacy-adapter/template-renderer.js") |
| 67 | +@JsModule("./flow-component-renderer.js") |
| 68 | +@JsModule("./comboBoxConnector.js") |
| 69 | +public class MultiSelectComboBox<TItem> |
| 70 | + extends ComboBoxBase<MultiSelectComboBox<TItem>, TItem, Set<TItem>> { |
| 71 | + |
| 72 | + /** |
| 73 | + * Default constructor. Creates an empty combo box. |
| 74 | + */ |
| 75 | + public MultiSelectComboBox() { |
| 76 | + this(50); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Creates an empty combo box with the defined page size for lazy loading. |
| 81 | + * <p> |
| 82 | + * The default page size is 50. |
| 83 | + * <p> |
| 84 | + * The page size is also the largest number of items that can support |
| 85 | + * client-side filtering. If you provide more items than the page size, the |
| 86 | + * component has to fall back to server-side filtering. |
| 87 | + * |
| 88 | + * @param pageSize |
| 89 | + * the amount of items to request at a time for lazy loading |
| 90 | + * @see #setPageSize(int) |
| 91 | + */ |
| 92 | + public MultiSelectComboBox(int pageSize) { |
| 93 | + super("selectedItems", Collections.emptySet(), JsonArray.class, |
| 94 | + MultiSelectComboBox::presentationToModel, |
| 95 | + MultiSelectComboBox::modelToPresentation); |
| 96 | + |
| 97 | + setPageSize(pageSize); |
| 98 | + setItems(new DataCommunicator.EmptyDataProvider<>()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Creates an empty combo box with the defined label. |
| 103 | + * |
| 104 | + * @param label |
| 105 | + * the label describing the combo box |
| 106 | + * @see #setLabel(String) |
| 107 | + */ |
| 108 | + public MultiSelectComboBox(String label) { |
| 109 | + this(); |
| 110 | + setLabel(label); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Creates a combo box with the defined label and populated with the items |
| 115 | + * in the collection. |
| 116 | + * |
| 117 | + * @param label |
| 118 | + * the label describing the combo box |
| 119 | + * @param items |
| 120 | + * the items to be shown in the list of the combo box |
| 121 | + * @see #setLabel(String) |
| 122 | + * @see #setItems(Collection) |
| 123 | + */ |
| 124 | + public MultiSelectComboBox(String label, Collection<TItem> items) { |
| 125 | + this(); |
| 126 | + setLabel(label); |
| 127 | + setItems(items); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Creates a combo box with the defined label and populated with the items |
| 132 | + * in the array. |
| 133 | + * |
| 134 | + * @param label |
| 135 | + * the label describing the combo box |
| 136 | + * @param items |
| 137 | + * the items to be shown in the list of the combo box |
| 138 | + * @see #setLabel(String) |
| 139 | + * @see #setItems(Object...) |
| 140 | + */ |
| 141 | + @SafeVarargs |
| 142 | + public MultiSelectComboBox(String label, TItem... items) { |
| 143 | + this(); |
| 144 | + setLabel(label); |
| 145 | + setItems(items); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Constructs a combo box with a value change listener. |
| 150 | + * |
| 151 | + * @param listener |
| 152 | + * the value change listener to add |
| 153 | + * @see #addValueChangeListener(ValueChangeListener) |
| 154 | + */ |
| 155 | + public MultiSelectComboBox( |
| 156 | + ValueChangeListener<ComponentValueChangeEvent<MultiSelectComboBox<TItem>, Set<TItem>>> listener) { |
| 157 | + this(); |
| 158 | + addValueChangeListener(listener); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Constructs a combo box with the defined label and a value change |
| 163 | + * listener. |
| 164 | + * |
| 165 | + * @param label |
| 166 | + * the label describing the combo box |
| 167 | + * @param listener |
| 168 | + * the value change listener to add |
| 169 | + * @see #setLabel(String) |
| 170 | + * @see #addValueChangeListener(ValueChangeListener) |
| 171 | + */ |
| 172 | + public MultiSelectComboBox(String label, |
| 173 | + ValueChangeListener<ComponentValueChangeEvent<MultiSelectComboBox<TItem>, Set<TItem>>> listener) { |
| 174 | + this(label); |
| 175 | + addValueChangeListener(listener); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Constructs a combo box with the defined label, a value change listener |
| 180 | + * and populated with the items in the array. |
| 181 | + * |
| 182 | + * @param label |
| 183 | + * the label describing the combo box |
| 184 | + * @param listener |
| 185 | + * the value change listener to add |
| 186 | + * @param items |
| 187 | + * the items to be shown in the list of the combo box |
| 188 | + * @see #setLabel(String) |
| 189 | + * @see #addValueChangeListener(ValueChangeListener) |
| 190 | + * @see #setItems(Object...) |
| 191 | + */ |
| 192 | + @SafeVarargs |
| 193 | + public MultiSelectComboBox(String label, |
| 194 | + ValueChangeListener<ComponentValueChangeEvent<MultiSelectComboBox<TItem>, Set<TItem>>> listener, |
| 195 | + TItem... items) { |
| 196 | + this(label, listener); |
| 197 | + setItems(items); |
| 198 | + } |
| 199 | + |
| 200 | + private static <T> Set<T> presentationToModel( |
| 201 | + MultiSelectComboBox<T> multiSelectComboBox, |
| 202 | + JsonArray presentation) { |
| 203 | + |
| 204 | + DataKeyMapper<T> keyMapper = multiSelectComboBox.getKeyMapper(); |
| 205 | + |
| 206 | + if (presentation == null || keyMapper == null) { |
| 207 | + return multiSelectComboBox.getEmptyValue(); |
| 208 | + } |
| 209 | + |
| 210 | + Set<T> set = new HashSet<>(); |
| 211 | + for (int i = 0; i < presentation.length(); i++) { |
| 212 | + String key = presentation.getObject(i).getString("key"); |
| 213 | + set.add(keyMapper.get(key)); |
| 214 | + } |
| 215 | + return set; |
| 216 | + } |
| 217 | + |
| 218 | + private static <T> JsonArray modelToPresentation( |
| 219 | + MultiSelectComboBox<T> multiSelectComboBox, Set<T> model) { |
| 220 | + JsonArray array = Json.createArray(); |
| 221 | + if (model == null || model.isEmpty()) { |
| 222 | + return array; |
| 223 | + } |
| 224 | + |
| 225 | + model.stream().map(multiSelectComboBox::generateJson) |
| 226 | + .forEach(jsonObject -> array.set(array.length(), jsonObject)); |
| 227 | + |
| 228 | + return array; |
| 229 | + } |
| 230 | + |
| 231 | + private JsonObject generateJson(TItem item) { |
| 232 | + JsonObject jsonObject = Json.createObject(); |
| 233 | + jsonObject.put("key", getKeyMapper().key(item)); |
| 234 | + getDataGenerator().generateData(item, jsonObject); |
| 235 | + return jsonObject; |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Sets the value of the component, which is a set of selected items. |
| 240 | + * <p> |
| 241 | + * Note that it is allowed to pass {@code null} as value to clear the |
| 242 | + * selection, but that an empty set will be stored as value instead. |
| 243 | + * |
| 244 | + * @param value |
| 245 | + * the new value |
| 246 | + */ |
| 247 | + @Override |
| 248 | + public void setValue(Set<TItem> value) { |
| 249 | + if (value == null) { |
| 250 | + value = Collections.emptySet(); |
| 251 | + } |
| 252 | + super.setValue(value); |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + protected void refreshValue() { |
| 257 | + Set<TItem> value = getValue(); |
| 258 | + if (value == null || value.isEmpty()) { |
| 259 | + return; |
| 260 | + } |
| 261 | + JsonArray selectedItems = modelToPresentation(this, value); |
| 262 | + getElement().setPropertyJson("selectedItems", selectedItems); |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + protected boolean isSelected(TItem item) { |
| 267 | + if (item == null) |
| 268 | + return false; |
| 269 | + |
| 270 | + DataProvider<TItem, ?> dataProvider = getDataProvider(); |
| 271 | + Object itemId = dataProvider.getId(item); |
| 272 | + |
| 273 | + return getValue().stream().anyMatch(selectedItem -> Objects |
| 274 | + .equals(itemId, dataProvider.getId(selectedItem))); |
| 275 | + } |
| 276 | +} |
0 commit comments