Skip to content

Commit decfa5f

Browse files
committed
refactor(demo): improve demo readibility
1 parent afb3ef1 commit decfa5f

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

src/test/java/com/flowingcode/vaadin/addons/chipfield/BinderDemo.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
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.
@@ -19,35 +19,35 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.chipfield;
2121

22+
import java.util.Arrays;
23+
2224
import com.vaadin.flow.component.button.Button;
2325
import com.vaadin.flow.component.notification.Notification;
2426
import com.vaadin.flow.component.notification.Notification.Position;
2527
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
2628
import com.vaadin.flow.data.binder.Binder;
27-
import java.util.Arrays;
28-
import java.util.stream.Collectors;
2929

3030
@SuppressWarnings("serial")
3131
public class BinderDemo extends VerticalLayout {
3232

3333
public BinderDemo() {
34+
3435
Planet p = new Planet("A new planet");
35-
ChipField<String> chf = new ChipField<>(
36-
"Choose planet features (Binder demo, try with: 'Rings', 'Moons', 'Water', etc.)");
36+
37+
ChipField<String> chf = new ChipField<>("Choose planet features (try with: 'Rings', 'Moons', 'Water', etc.)");
3738
chf.setWidthFull();
3839
chf.setItems(Arrays.asList("Rings", "Moons", "Water", "Rocks", "Lava", "Ice", "Cold", "Heat", "Atmosphere"));
3940
Binder<Planet> binder = new Binder<>();
40-
binder.bind(chf, Planet::getConfiguration, Planet::setConfiguration);
41+
binder.bind(chf, Planet::getFeatures, Planet::setFeatures);
4142
binder.setBean(p);
42-
Button show = new Button("Show planet configuration");
43-
show.addClickListener(
44-
event -> Notification.show(
45-
"Planet: " + p.getName() + ", features: "
46-
+ p.getConfiguration().stream().collect(Collectors.joining(",")),
47-
5000, Position.BOTTOM_START));
43+
4844
chf.addValueChangeListener(
49-
newItem -> Notification.show("Items: " + newItem.getValue(), 5000, Position.BOTTOM_START));
45+
newItem -> Notification.show("Current value: " + newItem.getValue(), 5000, Position.BOTTOM_START));
46+
47+
add(chf);
48+
add(new Button("Show planet features",
49+
e -> Notification.show("Features: " + p.getFeatures(), 5000, Position.BOTTOM_START)));
5050

51-
add(chf, show);
5251
}
52+
5353
}

src/test/java/com/flowingcode/vaadin/addons/chipfield/DataProviderDemo.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
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.
@@ -19,45 +19,38 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.chipfield;
2121

22+
import java.util.stream.Collectors;
23+
2224
import com.vaadin.flow.component.button.Button;
2325
import com.vaadin.flow.component.notification.Notification;
2426
import com.vaadin.flow.component.notification.Notification.Position;
2527
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
2628
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
2729
import 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")
3432
public 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
}

src/test/java/com/flowingcode/vaadin/addons/chipfield/Planet.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
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.
@@ -19,12 +19,13 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.chipfield;
2121

22-
import java.util.ArrayList;
2322
import java.util.List;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
2425

2526
public class Planet {
2627
private String name;
27-
private List<String> configuration = new ArrayList<>();
28+
private List<String> features;
2829

2930
public Planet(String name) {
3031
this.name = name;
@@ -38,12 +39,22 @@ public void setName(String name) {
3839
this.name = name;
3940
}
4041

41-
public List<String> getConfiguration() {
42-
return configuration;
42+
public List<String> getFeatures() {
43+
return features;
4344
}
4445

45-
public void setConfiguration(List<String> configuration) {
46-
this.configuration = configuration;
46+
public void setFeatures(List<String> features) {
47+
this.features = features;
4748
}
49+
}
50+
51+
public static List<Planet> all() {
52+
return Stream.of("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune").map(Planet::new).collect(Collectors.toList());
53+
}
54+
55+
public static Planet random() {
56+
return new Planet("Planet " + Integer.toString((int) Math.round(Math.random() * 36 * 36 * 36), 36).toUpperCase());
57+
}
58+
4859
}
4960

0 commit comments

Comments
 (0)