77 * Licensed under the Apache License, Version 2.0 (the "License");
88 * you may not use this file except in compliance with the License.
99 * You may obtain a copy of the License at
10- *
10+ *
1111 * http://www.apache.org/licenses/LICENSE-2.0
12- *
12+ *
1313 * Unless required by applicable law or agreed to in writing, software
1414 * distributed under the License is distributed on an "AS IS" BASIS,
1515 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1919 */
2020package com .flowingcode .vaadin .addons .chipfield ;
2121
22+ import java .util .stream .Collectors ;
23+
2224import com .vaadin .flow .component .button .Button ;
2325import com .vaadin .flow .component .notification .Notification ;
2426import com .vaadin .flow .component .notification .Notification .Position ;
2527import com .vaadin .flow .component .orderedlayout .HorizontalLayout ;
2628import com .vaadin .flow .component .orderedlayout .VerticalLayout ;
2729import com .vaadin .flow .data .provider .ListDataProvider ;
28- import java .util .ArrayList ;
29- import java .util .Arrays ;
30- import java .util .List ;
31- import java .util .stream .Collectors ;
3230
3331@ SuppressWarnings ("serial" )
3432public class DataProviderDemo extends VerticalLayout {
3533
3634 public DataProviderDemo () {
37- List <Planet > availablePlanets = new ArrayList <>(
38- Arrays .asList (new Planet ("Mercury" ), new Planet ("Venus" ), new Planet ("Earth" ), new Planet ("Mars" ),
39- new Planet ("Jupiter" ), new Planet ("Saturn" ), new Planet ("Uranus" ), new Planet ("Neptune" )));
40- ListDataProvider <Planet > ldp = new ListDataProvider <>(availablePlanets );
4135
42- ChipField <Planet > chf = new ChipField <>("Select some planets (Mercury, Venus, Earth, etc.)" ,
43- planet -> planet .getName ());
36+ ListDataProvider <Planet > ldp = new ListDataProvider <>(Planet .all ());
37+
38+ ChipField <Planet > chf = new ChipField <>("Select some planets (Mercury, Venus, Earth, etc.)" , planet -> planet .getName ());
4439 chf .setWidthFull ();
4540 chf .setDataProvider (ldp );
4641 chf .setClosable (true );
4742 chf .setNewItemHandler (label -> new Planet (label ));
4843
49- Button b = new Button ("Obtain selected planets" );
50- b .addClickListener (event -> Notification .show (
51- "Planets: " + chf .getValue ().stream ().map (planet -> planet .getName ()).collect (Collectors .joining ("," )),
52- 5000 , Position .BOTTOM_START ));
44+ HorizontalLayout buttons = new HorizontalLayout ();
45+ buttons .add (new Button ("Obtain selected planets" , ev -> Notification
46+ .show ("Planets: " + chf .getValue ().stream ().map (Planet ::getName ).collect (Collectors .joining ("," )), 5000 , Position .BOTTOM_START )));
5347
54- Button b2 = new Button ("Add random planet" );
55- b2 .addClickListener (event -> {
56- Planet p = new Planet ("Planet" + Math .round (Math .random () * 10000 ));
57- availablePlanets .add (p );
48+ buttons .add (new Button ("Add random planet" , ev -> {
49+ Planet newPlanet = Planet .random ();
50+ ldp .getItems ().add (newPlanet );
5851 ldp .refreshAll ();
59- chf .addSelectedItem (p );
60- });
52+ chf .addSelectedItem (newPlanet );
53+ })) ;
6154
6255 chf .addChipCreatedListener (
6356 ev -> Notification .show ("Chip: " + ev .getChipLabel () + " Created by client: " + ev .isFromClient () + "!" ,
@@ -68,7 +61,7 @@ public DataProviderDemo() {
6861 chf .addChipClickedListener (
6962 ev -> Notification .show ("Chip: " + ev .getChipLabel () + " Clicked!" , 5000 , Position .BOTTOM_END ));
7063
71- VerticalLayout vl = new VerticalLayout (chf , new HorizontalLayout (b , b2 ));
72- add (vl );
64+ add (new VerticalLayout (chf , buttons ));
7365 }
66+
7467}
0 commit comments