|
21 | 21 |
|
22 | 22 | import java.util.ArrayList; |
23 | 23 | import java.util.Arrays; |
| 24 | +import java.util.Iterator; |
24 | 25 | import java.util.List; |
25 | 26 | import java.util.Optional; |
26 | 27 | import java.util.concurrent.atomic.AtomicInteger; |
@@ -173,9 +174,44 @@ protected void onAttach(AttachEvent attachEvent) { |
173 | 174 | configure(); |
174 | 175 | } |
175 | 176 |
|
| 177 | + @Override |
| 178 | + public List<T> getValue() { |
| 179 | + return new ArrayList<>(super.getValue()); |
| 180 | + } |
| 181 | + |
176 | 182 | @Override |
177 | 183 | protected void setPresentationValue(List<T> newPresentationValue) { |
178 | | - setClientChipWithoutEvent(newPresentationValue.stream().map(itemLabelGenerator).toArray(String[]::new)); |
| 184 | + List<String> labels = new ArrayList<>(); |
| 185 | + |
| 186 | + if (isAllowAdditionalItems()) { |
| 187 | + for (T item : newPresentationValue) { |
| 188 | + String label = itemLabelGenerator.apply(item); |
| 189 | + if (!findItemByLabel(label).isPresent()) { |
| 190 | + addSelectedItemInternal(item, false); |
| 191 | + additionalItems.add(item); |
| 192 | + } |
| 193 | + labels.add(label); |
| 194 | + } |
| 195 | + } else { |
| 196 | + boolean hasChanges = false; |
| 197 | + newPresentationValue = new ArrayList<>(newPresentationValue); |
| 198 | + Iterator<T> it = newPresentationValue.iterator(); |
| 199 | + while (it.hasNext()) { |
| 200 | + T item = it.next(); |
| 201 | + String label = itemLabelGenerator.apply(item); |
| 202 | + if (findItemByLabel(label).isPresent()) { |
| 203 | + labels.add(label); |
| 204 | + } else { |
| 205 | + it.remove(); |
| 206 | + hasChanges = true; |
| 207 | + } |
| 208 | + } |
| 209 | + if (hasChanges) { |
| 210 | + setModelValue(newPresentationValue, false); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + setClientChipWithoutEvent(labels.toArray(new String[labels.size()])); |
179 | 215 | } |
180 | 216 |
|
181 | 217 | private Optional<T> findItemByLabel(String label) { |
@@ -349,8 +385,8 @@ public void removeSelectedItem(T itemToRemove) { |
349 | 385 | private void removeSelectedItem(T itemToRemove, boolean fromClient) { |
350 | 386 | List<T> value = new ArrayList<>(getValue()); |
351 | 387 | if (value.remove(itemToRemove)) { |
352 | | - setModelValue(value, fromClient); |
353 | 388 | additionalItems.retainAll(value); |
| 389 | + setModelValue(value, fromClient); |
354 | 390 | if (!fromClient) { |
355 | 391 | setPresentationValue(value); |
356 | 392 | fireEvent(new ChipRemovedEvent<>(this, fromClient, itemLabelGenerator.apply(itemToRemove))); |
|
0 commit comments