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 package info.magnolia.sample.app.main.chooser;
35
36 import info.magnolia.ui.chooser.Chooser;
37 import info.magnolia.ui.chooser.definition.ChooserDefinition;
38
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.function.Consumer;
42
43 import com.vaadin.ui.CheckBox;
44 import com.vaadin.ui.Component;
45 import com.vaadin.ui.VerticalLayout;
46
47
48
49
50 public class BirdAttributeChooser implements Chooser<String> {
51
52 private final List<CheckBox> items;
53 private final ChooserDefinition<String, BirdAttributeChooser> definition;
54
55 public BirdAttributeChooser(ChooserDefinition<String, BirdAttributeChooser> definition) {
56 this.definition = definition;
57
58 this.items = new ArrayList<>();
59 this.items.add(new CheckBox("stupid"));
60 this.items.add(new CheckBox("blue"));
61 this.items.add(new CheckBox("little"));
62 this.items.add(new CheckBox("old"));
63 }
64
65 @Override
66 public Component asVaadinComponent() {
67 return new VerticalLayout(items.toArray(new CheckBox[]{}));
68 }
69
70 @Override
71 public void onChange(Consumer<String> callback) {
72 items.forEach(i -> i.addValueChangeListener(e -> callback.accept(this.getChoice())));
73 }
74
75 @Override
76 public String getChoice() {
77 StringBuilder result = new StringBuilder("A ");
78 this.items.stream()
79 .filter(CheckBox::getValue)
80 .forEach(checkBox -> result.append(checkBox.getCaption() + " "));
81 result.append("bird");
82 return result.toString();
83 }
84
85 @Override
86 public void setChoice(String choice) {
87
88 }
89 }