This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ComboBox.java
978 lines (867 loc) · 33.4 KB
/
ComboBox.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/*
* Copyright 2000-2018 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.combobox;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import com.vaadin.flow.component.ClientCallable;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.HasSize;
import com.vaadin.flow.component.HasValidation;
import com.vaadin.flow.component.ItemLabelGenerator;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.dependency.HtmlImport;
import com.vaadin.flow.component.dependency.JavaScript;
import com.vaadin.flow.data.binder.HasFilterableDataProvider;
import com.vaadin.flow.data.provider.ArrayUpdater;
import com.vaadin.flow.data.provider.ArrayUpdater.Update;
import com.vaadin.flow.data.provider.CallbackDataProvider;
import com.vaadin.flow.data.provider.CompositeDataGenerator;
import com.vaadin.flow.data.provider.DataCommunicator;
import com.vaadin.flow.data.provider.DataKeyMapper;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.provider.ListDataProvider;
import com.vaadin.flow.data.provider.Query;
import com.vaadin.flow.data.renderer.Renderer;
import com.vaadin.flow.data.renderer.Rendering;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.function.SerializableBiPredicate;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.function.SerializableFunction;
import com.vaadin.flow.internal.JsonUtils;
import com.vaadin.flow.shared.Registration;
import elemental.json.Json;
import elemental.json.JsonObject;
import elemental.json.JsonValue;
/**
* Server-side component for the {@code vaadin-combo-box} webcomponent. It
* contains the same features of the webcomponent, such as item filtering,
* object selection and item templating.
* <p>
* ComboBox supports lazy loading. This means that when using large data sets,
* items are requested from the server one "page" at a time when the user
* scrolls down the overlay. The number of items in one page is by default 50,
* and can be changed with {@link #setPageSize(int)}.
* <p>
* ComboBox can do filtering either in the browser or in the server. When
* ComboBox has only a relatively small set of items, the filtering will happen
* in the browser, allowing smooth user-experience. When the size of the data
* set is larger than the {@code pageSize}, the webcomponent doesn't necessarily
* have all the data available and it will make requests to the server to handle
* the filtering. Also, if you have defined custom filtering logic, with eg.
* {@link #setItems(ItemFilter, Collection)}, filtering will happen in the
* server. To enable client-side filtering with larger data sets, you can
* override the {@code pageSize} to be bigger than the size of your data set.
* However, then the full data set will be sent to the client immediately and
* you will lose the benefits of lazy loading.
*
* @param <T>
* the type of the items to be inserted in the combo box
* @author Vaadin Ltd
*/
@HtmlImport("frontend://flow-component-renderer.html")
@JavaScript("frontend://comboBoxConnector.js")
@SuppressWarnings("serial")
public class ComboBox<T> extends GeneratedVaadinComboBox<ComboBox<T>, T>
implements HasSize, HasValidation,
HasFilterableDataProvider<T, String> {
/**
* A callback method for fetching items. The callback is provided with a
* non-null string filter, offset index and limit.
*
* @param <T>
* item (bean) type in ComboBox
*/
@FunctionalInterface
public interface FetchItemsCallback<T> extends Serializable {
/**
* Returns a stream of items that match the given filter, limiting the
* results with given offset and limit.
*
* @param filter
* a non-null filter string
* @param offset
* the first index to fetch
* @param limit
* the fetched item count
* @return stream of items
*/
public Stream<T> fetchItems(String filter, int offset, int limit);
}
private class CustomValueRegistration implements Registration {
private Registration delegate;
private CustomValueRegistration(Registration delegate) {
this.delegate = delegate;
}
@Override
public void remove() {
if (delegate != null) {
delegate.remove();
customValueListenersCount--;
if (customValueListenersCount == 0) {
setAllowCustomValue(false);
}
delegate = null;
}
}
}
private final class UpdateQueue implements Update {
private List<Runnable> queue = new ArrayList<>();
private UpdateQueue(int size) {
enqueue("$connector.updateSize", size);
}
@Override
public void set(int start, List<JsonValue> items) {
enqueue("$connector.set", start,
items.stream().collect(JsonUtils.asArray()),
ComboBox.this.lastFilter);
}
@Override
public void clear(int start, int length) {
// NO-OP
}
@Override
public void commit(int updateId) {
enqueue("$connector.confirm", updateId, ComboBox.this.lastFilter);
queue.forEach(Runnable::run);
queue.clear();
ComboBox.this.lastFilter = null;
}
private void enqueue(String name, Serializable... arguments) {
queue.add(() -> getElement().callFunction(name, arguments));
}
}
/**
* Lazy loading updater, used when calling setDataProvider()
*/
private final ArrayUpdater arrayUpdater = new ArrayUpdater() {
@Override
public Update startUpdate(int sizeChange) {
return new UpdateQueue(sizeChange);
}
@Override
public void initialize() {
initConnector();
}
};
/**
* Predicate to check {@link ComboBox} items against user typed strings.
*/
@FunctionalInterface
public interface ItemFilter<T> extends SerializableBiPredicate<T, String> {
@Override
public boolean test(T item, String filterText);
}
private ItemLabelGenerator<T> itemLabelGenerator = String::valueOf;
private Renderer<T> renderer;
private boolean renderScheduled;
// Filter set by the client when requesting data. It's sent back to client
// together with the response so client may know for what filter data is
// provided.
private String lastFilter;
private DataCommunicator<T> dataCommunicator;
private final CompositeDataGenerator<T> dataGenerator = new CompositeDataGenerator<>();
private Registration dataGeneratorRegistration;
private Element template;
private int customValueListenersCount;
private SerializableConsumer<String> filterSlot = filter -> {
// Just ignore when setDataProvider has not been called
};
private enum UserProvidedFilter {
UNDECIDED, YES, NO
}
private UserProvidedFilter userProvidedFilter = UserProvidedFilter.UNDECIDED;
/**
* Creates an empty combo box with the defined page size for lazy loading.
* <p>
* The default page size is 50.
* <p>
* The page size is also the largest number of items that can support
* client-side filtering. If you provide more items than the page size, the
* component has to fall back to server-side filtering.
*
* @param pageSize
* the amount of items to request at a time for lazy loading
* @see {@link #setPageSize(int)}
*/
public ComboBox(int pageSize) {
super(null, null, String.class, ComboBox::presentationToModel,
ComboBox::modelToPresentation);
dataGenerator.addDataGenerator((item, jsonObject) -> jsonObject
.put("label", generateLabel(item)));
setItemValuePath("key");
setItemIdPath("key");
setPageSize(pageSize);
}
/**
* Default constructor. Creates an empty combo box.
*/
public ComboBox() {
this(50);
}
/**
* Creates an empty combo box with the defined label.
*
* @param label
* the label describing the combo box
*/
public ComboBox(String label) {
this();
setLabel(label);
}
/**
* Creates a combo box with the defined label and populated with the items
* in the collection.
*
* @param label
* the label describing the combo box
* @param items
* the items to be shown in the list of the combo box
* @see #setItems(Collection)
*/
public ComboBox(String label, Collection<T> items) {
this();
setLabel(label);
setItems(items);
}
/**
* Creates a combo box with the defined label and populated with the items
* in the array.
*
* @param label
* the label describing the combo box
* @param items
* the items to be shown in the list of the combo box
* @see #setItems(Object...)
*/
@SafeVarargs
public ComboBox(String label, T... items) {
this();
setLabel(label);
setItems(items);
}
private static <T> T presentationToModel(ComboBox<T> comboBox,
String presentation) {
if (presentation == null || comboBox.dataCommunicator == null) {
return comboBox.getEmptyValue();
}
return comboBox.getKeyMapper().get(presentation);
}
private static <T> String modelToPresentation(ComboBox<T> comboBox,
T model) {
if (model == null) {
return null;
}
return comboBox.getKeyMapper().key(model);
}
@Override
public void setValue(T value) {
if (dataCommunicator == null) {
if (value == null) {
return;
} else {
throw new IllegalStateException(
"Cannot set a value for a ComboBox without items. "
+ "Use setItems or setDataProvider to populate "
+ "items into the ComboBox before setting a value.");
}
}
super.setValue(value);
DataKeyMapper<T> keyMapper = getKeyMapper();
if (value != null && keyMapper.has(value)) {
value = keyMapper.get(keyMapper.key(value));
}
if (value == null) {
getElement().setProperty("selectedItem", null);
getElement().setProperty("value", "");
getElement().setProperty("_inputElementValue", "");
return;
}
// This ensures that the selection works even with lazy loading when the
// item is not yet loaded
JsonObject json = Json.createObject();
json.put("key", keyMapper.key(value));
dataGenerator.generateData(value, json);
setSelectedItem(json);
// Workaround for property not updating in certain scenario
// https://github.com/vaadin/flow/issues/4862
runBeforeClientResponse(
ui -> ui.getPage().executeJavaScript("$0.value=$1",
getElement(), getElement().getProperty("value")));
}
/**
* Sets the TemplateRenderer responsible to render the individual items in
* the list of possible choices of the ComboBox. It doesn't affect how the
* selected item is rendered - that can be configured by using
* {@link #setItemLabelGenerator(ItemLabelGenerator)}.
*
* @param renderer
* a renderer for the items in the selection list of the
* ComboBox, not <code>null</code>
*/
public void setRenderer(Renderer<T> renderer) {
Objects.requireNonNull(renderer, "The renderer must not be null");
this.renderer = renderer;
if (template == null) {
template = new Element("template");
getElement().appendChild(template);
}
scheduleRender();
}
/**
* {@inheritDoc}
* <p>
* Filtering will use a case insensitive match to show all items where the
* filter text is a substring of the label displayed for that item, which
* you can configure with
* {@link #setItemLabelGenerator(ItemLabelGenerator)}.
* <p>
* Filtering will be handled in the client-side if the size of the data set
* is less than the page size. To force client-side filtering with a larger
* data set (at the cost of increased network traffic), you can increase the
* page size with {@link #setPageSize(int)}.
*/
@Override
public void setItems(Collection<T> items) {
setDataProvider(DataProvider.ofCollection(items));
}
/**
* Sets the data items of this combo box and a filtering function for
* defining which items are displayed when user types into the combo box.
* <p>
* Note that defining a custom filter will force the component to make
* server roundtrips to handle the filtering. Otherwise it can handle
* filtering in the client-side, if the size of the data set is less than
* the {@link #setPageSize(int) pageSize}.
*
* @param itemFilter
* filter to check if an item is shown when user typed some text
* into the ComboBox
* @param items
* the data items to display
*/
public void setItems(ItemFilter<T> itemFilter, Collection<T> items) {
ListDataProvider<T> listDataProvider = DataProvider.ofCollection(items);
setDataProvider(itemFilter, listDataProvider);
}
/**
* Sets the data items of this combo box and a filtering function for
* defining which items are displayed when user types into the combo box.
* <p>
* Note that defining a custom filter will force the component to make
* server roundtrips to handle the filtering. Otherwise it can handle
* filtering in the client-side, if the size of the data set is less than
* the {@link #setPageSize(int) pageSize}.
*
* @param itemFilter
* filter to check if an item is shown when user typed some text
* into the ComboBox
* @param items
* the data items to display
*/
public void setItems(ItemFilter<T> itemFilter,
@SuppressWarnings("unchecked") T... items) {
setItems(itemFilter, Arrays.asList(items));
}
/**
* {@inheritDoc}
* <p>
* The filter-type of the given data provider must be String so that it can
* handle the filters typed into the ComboBox by users. If your data
* provider uses some other type of filter, you can provide a function which
* converts the ComboBox's filter-string into that type via
* {@link #setDataProvider(DataProvider, SerializableFunction)}. Another way
* to do the same thing is to use this method with your data provider
* converted with
* {@link DataProvider#withConvertedFilter(SerializableFunction)}.
*/
@Override
public void setDataProvider(DataProvider<T, String> dataProvider) {
setDataProvider(dataProvider, SerializableFunction.identity());
}
/**
* {@inheritDoc}
* <p>
* ComboBox triggers filtering queries based on the strings users type into
* the field. For this reason you need to provide the second parameter, a
* function which converts the filter-string typed by the user into
* filter-type used by your data provider. If your data provider already
* supports String as the filter-type, it can be used without a converter
* function via {@link #setDataProvider(DataProvider)}.
* <p>
* Using this method provides the same result as using a data provider
* wrapped with
* {@link DataProvider#withConvertedFilter(SerializableFunction)}.
*/
@Override
public <C> void setDataProvider(DataProvider<T, C> dataProvider,
SerializableFunction<String, C> filterConverter) {
Objects.requireNonNull(dataProvider,
"The data provider can not be null");
Objects.requireNonNull(filterConverter,
"filterConverter cannot be null");
if (userProvidedFilter == UserProvidedFilter.UNDECIDED) {
userProvidedFilter = UserProvidedFilter.YES;
}
runBeforeClientResponse(ui -> ui.getPage().executeJavaScript(
"window.Vaadin.Flow.comboBoxConnector.initLazy($0);",
getElement()));
if (dataCommunicator == null) {
dataCommunicator = new DataCommunicator<>(dataGenerator,
arrayUpdater, data -> getElement()
.callFunction("$connector.updateData", data),
getElement().getNode());
}
getElement().callFunction("$connector.reset");
scheduleRender();
setValue(null);
SerializableFunction<String, C> convertOrNull = filterText -> {
if (filterText == null) {
return null;
}
return filterConverter.apply(filterText);
};
SerializableConsumer<C> providerFilterSlot = dataCommunicator
.setDataProvider(dataProvider,
convertOrNull.apply(getFilterString()));
filterSlot = filter -> providerFilterSlot
.accept(convertOrNull.apply(filter));
boolean shouldForceServerSideFiltering = userProvidedFilter == UserProvidedFilter.YES;
dataProvider.addDataProviderListener(
e -> dataProviderUpdated(shouldForceServerSideFiltering));
dataProviderUpdated(shouldForceServerSideFiltering);
userProvidedFilter = UserProvidedFilter.UNDECIDED;
}
private void dataProviderUpdated(boolean forceServerSideFiltering) {
int size = getDataProvider().size(new Query<>());
setClientSideFilter(
!forceServerSideFiltering && size <= getPageSizeDouble());
reset();
}
/**
* Sets a list data provider as the data provider of this combo box.
* <p>
* Filtering will use a case insensitive match to show all items where the
* filter text is a substring of the label displayed for that item, which
* you can configure with
* {@link #setItemLabelGenerator(ItemLabelGenerator)}.
* <p>
* Filtering will be handled in the client-side if the size of the data set
* is less than the page size. To force client-side filtering with a larger
* data set (at the cost of increased network traffic), you can increase the
* page size with {@link #setPageSize(int)}.
*
* @param listDataProvider
* the list data provider to use, not <code>null</code>
*/
public void setDataProvider(ListDataProvider<T> listDataProvider) {
if (userProvidedFilter == UserProvidedFilter.UNDECIDED) {
userProvidedFilter = UserProvidedFilter.NO;
}
// Cannot use the case insensitive contains shorthand from
// ListDataProvider since it wouldn't react to locale changes
ItemFilter<T> defaultItemFilter = (item,
filterText) -> generateLabel(item).toLowerCase(getLocale())
.contains(filterText.toLowerCase(getLocale()));
setDataProvider(defaultItemFilter, listDataProvider);
}
/**
* Sets a CallbackDataProvider using the given fetch items callback and a
* size callback.
* <p>
* This method is a shorthand for making a {@link CallbackDataProvider} that
* handles a partial {@link com.vaadin.data.provider.Query Query} object.
*
* @param fetchItems
* a callback for fetching items
* @param sizeCallback
* a callback for getting the count of items
* @see CallbackDataProvider
* @see #setDataProvider(DataProvider)
*/
public void setDataProvider(FetchItemsCallback<T> fetchItems,
SerializableFunction<String, Integer> sizeCallback) {
userProvidedFilter = UserProvidedFilter.YES;
setDataProvider(new CallbackDataProvider<>(
q -> fetchItems.fetchItems(q.getFilter().orElse(""),
q.getOffset(), q.getLimit()),
q -> sizeCallback.apply(q.getFilter().orElse(""))));
}
/**
* Sets a list data provider with an item filter as the data provider of
* this combo box. The item filter is used to compare each item to the
* filter text entered by the user.
* <p>
* Note that defining a custom filter will force the component to make
* server roundtrips to handle the filtering. Otherwise it can handle
* filtering in the client-side, if the size of the data set is less than
* the {@link #setPageSize(int) pageSize}.
*
* @param itemFilter
* filter to check if an item is shown when user typed some text
* into the ComboBox
* @param listDataProvider
* the list data provider to use, not <code>null</code>
*/
public void setDataProvider(ItemFilter<T> itemFilter,
ListDataProvider<T> listDataProvider) {
Objects.requireNonNull(listDataProvider,
"List data provider cannot be null");
setDataProvider(listDataProvider,
filterText -> item -> itemFilter.test(item, filterText));
}
/**
* Gets the data provider used by this ComboBox.
*
* @return the data provider used by this ComboBox
*/
public DataProvider<T, ?> getDataProvider() {
return dataCommunicator.getDataProvider();
}
/**
* Sets the item label generator that is used to produce the strings shown
* in the combo box for each item. By default,
* {@link String#valueOf(Object)} is used.
* <p>
* When the {@link #setRenderer(Renderer)} is used, the ItemLabelGenerator
* is only used to show the selected item label.
*
* @param itemLabelGenerator
* the item label provider to use, not null
*/
public void setItemLabelGenerator(
ItemLabelGenerator<T> itemLabelGenerator) {
Objects.requireNonNull(itemLabelGenerator,
"The item label generator can not be null");
this.itemLabelGenerator = itemLabelGenerator;
reset();
}
/**
* Gets the item label generator that is used to produce the strings shown
* in the combo box for each item.
*
* @return the item label generator used, not null
*/
public ItemLabelGenerator<T> getItemLabelGenerator() {
return itemLabelGenerator;
}
/**
* Sets the page size, which is the number of items fetched at a time from
* the data provider.
* <p>
* The page size is also the largest number of items that can support
* client-side filtering. If you provide more items than the page size, the
* component has to fall back to server-side filtering.
* <p>
* Setting the page size after the ComboBox has been rendered effectively
* resets the component, and the current page(s) and sent over again.
* <p>
* The default page size is 50.
*
* @param pageSize
* the maximum number of items sent per request, should be
* greater than zero
*/
public void setPageSize(int pageSize) {
if (pageSize < 1) {
throw new IllegalArgumentException(
"Page size should be greater than zero.");
}
super.setPageSize(pageSize);
reset();
}
/**
* Gets the page size, which is the number of items fetched at a time from
* the data provider.
* <p>
* The page size is also the largest number of items that can support
* client-side filtering. If you provide more items than the page size, the
* component has to fall back to server-side filtering.
* <p>
* The default page size is 50.
*
* @return the maximum number of items sent per request
* @see {@link #setPageSize(int)}
*/
public int getPageSize() {
return getElement().getProperty("pageSize", 50);
}
@Override
public void setOpened(boolean opened) {
super.setOpened(opened);
}
/**
* Gets the states of the drop-down.
*
* @return {@code true} if the drop-down is opened, {@code false} otherwise
*/
public boolean isOpened() {
return isOpenedBoolean();
}
@Override
public void setInvalid(boolean invalid) {
super.setInvalid(invalid);
}
/**
* Gets the validity of the combobox output.
* <p>
* return true, if the value is invalid.
*
* @return the {@code validity} property from the component
*/
@Override
public boolean isInvalid() {
return isInvalidBoolean();
}
@Override
public void setErrorMessage(String errorMessage) {
super.setErrorMessage(errorMessage);
}
/**
* Gets the current error message from the combobox.
*
* @return the current error message
*/
@Override
public String getErrorMessage() {
return getErrorMessageString();
}
@Override
public void setAllowCustomValue(boolean allowCustomValue) {
super.setAllowCustomValue(allowCustomValue);
}
/**
* If {@code true}, the user can input a value that is not present in the
* items list. {@code value} property will be set to the input value in this
* case. Also, when {@code value} is set programmatically, the input value
* will be set to reflect that value.
* <p>
* This property is not synchronized automatically from the client side, so
* the returned value may not be the same as in client side.
* </p>
*
* @return the {@code allowCustomValue} property from the combobox
*/
public boolean isAllowCustomValue() {
return isAllowCustomValueBoolean();
}
/**
* Set the combobox to be input focused when the page loads.
*
* @param autofocus
* the boolean value to set
*/
@Override
public void setAutofocus(boolean autofocus) {
super.setAutofocus(autofocus);
}
/**
* Get the state for the auto-focus property of the combobox.
* <p>
* This property is not synchronized automatically from the client side, so
* the returned value may not be the same as in client side.
*
* @return the {@code autofocus} property from the combobox
*/
public boolean isAutofocus() {
return isAutofocusBoolean();
}
@Override
public void setPreventInvalidInput(boolean preventInvalidInput) {
super.setPreventInvalidInput(preventInvalidInput);
}
/**
* Determines whether preventing the user from inputing invalid value.
* <p>
* This property is not synchronized automatically from the client side, so
* the returned value may not be the same as in client side.
*
* @return the {@code preventInvalidInput} property of the combobox
*/
public boolean isPreventInvalidInput() {
return isPreventInvalidInputBoolean();
}
@Override
public void setRequired(boolean required) {
super.setRequired(required);
}
/**
* Determines whether the combobox is marked as input required.
* <p>
* This property is not synchronized automatically from the client side, so
* the returned value may not be the same as in client side.
*
* @return {@code true} if the input is required, {@code false} otherwise
*/
public boolean isRequired() {
return isRequiredBoolean();
}
@Override
public void setLabel(String label) {
super.setLabel(label);
}
/**
* Gets the label of the combobox.
*
* @return the {@code label} property of the combobox
*/
public String getLabel() {
return getLabelString();
}
@Override
public void setPlaceholder(String placeholder) {
super.setPlaceholder(placeholder);
}
/**
* Gets the placeholder of the combobox.
*
* @return the {@code placeholder} property of the combobox
*/
public String getPlaceholder() {
return getPlaceholderString();
}
@Override
public void setPattern(String pattern) {
super.setPattern(pattern);
}
/**
* Gets the valid input pattern
*
* @return the {@code pattern} property of the combobox
*/
public String getPattern() {
return getPatternString();
}
@Override
public T getEmptyValue() {
return null;
}
/**
* Adds a listener for CustomValueSetEvent which is fired when user types in
* a value that don't already exist in the ComboBox.
*
* <p>
* As a side effect makes the ComboBox allow custom values. If you don't
* want to allow a user to add new values to the list once the listener is
* added please disable it explicitly via the
* {@link #setAllowCustomValue(boolean)} method.
* </p>
*
* <p>
* The custom value becomes disallowed automatically once the last custom
* value set listener is removed.
* </p>
*
* @param listener
* the listener to be notified when a new value is filled
* @return a {@link Registration} for removing the event listener
* @see #setAllowCustomValue(boolean)
*/
@Override
public Registration addCustomValueSetListener(
ComponentEventListener<CustomValueSetEvent<ComboBox<T>>> listener) {
setAllowCustomValue(true);
customValueListenersCount++;
Registration registration = super.addCustomValueSetListener(listener);
return new CustomValueRegistration(registration);
}
@Override
public void setRequiredIndicatorVisible(boolean requiredIndicatorVisible) {
super.setRequiredIndicatorVisible(requiredIndicatorVisible);
getElement().callFunction("$connector.enableClientValidation",
!requiredIndicatorVisible);
}
CompositeDataGenerator<T> getDataGenerator() {
return dataGenerator;
}
private String generateLabel(T item) {
if (item == null) {
return "";
}
String label = getItemLabelGenerator().apply(item);
if (label == null) {
throw new IllegalStateException(String.format(
"Got 'null' as a label value for the item '%s'. "
+ "'%s' instance may not return 'null' values",
item, ItemLabelGenerator.class.getSimpleName()));
}
return label;
}
private void scheduleRender() {
if (renderScheduled || dataCommunicator == null || renderer == null) {
return;
}
renderScheduled = true;
runBeforeClientResponse(ui -> {
if (dataGeneratorRegistration != null) {
dataGeneratorRegistration.remove();
dataGeneratorRegistration = null;
}
Rendering<T> rendering = renderer.render(getElement(),
dataCommunicator.getKeyMapper(), template);
if (rendering.getDataGenerator().isPresent()) {
dataGeneratorRegistration = dataGenerator
.addDataGenerator(rendering.getDataGenerator().get());
}
reset();
});
}
@ClientCallable
private void confirmUpdate(int id) {
dataCommunicator.confirmUpdate(id);
}
@ClientCallable
private void setRequestedRange(int start, int length, String filter) {
this.lastFilter = filter;
dataCommunicator.setRequestedRange(start, length);
filterSlot.accept(filter);
}
@ClientCallable
private void resetDataCommunicator() {
dataCommunicator.reset();
}
void runBeforeClientResponse(SerializableConsumer<UI> command) {
getElement().getNode().runWhenAttached(ui -> ui
.beforeClientResponse(this, context -> command.accept(ui)));
}
private void initConnector() {
getUI().orElseThrow(() -> new IllegalStateException(
"Connector can only be initialized for an attached ComboBox"))
.getPage().executeJavaScript(
"window.Vaadin.Flow.comboBoxConnector.initLazy($0)",
getElement());
}
private DataKeyMapper<T> getKeyMapper() {
return dataCommunicator.getKeyMapper();
}
private void setClientSideFilter(boolean clientSideFilter) {
getElement().setProperty("_clientSideFilter", clientSideFilter);
}
private void reset() {
if (dataCommunicator != null) {
dataCommunicator.setRequestedRange(0, 0);
dataCommunicator.reset();
}
runBeforeClientResponse(ui -> ui.getPage().executeJavaScript(
// If-statement is needed because on the first attach this
// JavaScript is called before initializing the connector.
"if($0.$connector) $0.$connector.reset();", getElement()));
}
}