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.framework.chooser.Chooser;
37 import info.magnolia.ui.framework.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 String getTitle() {
67 return definition.getTitle() != null ? definition.getTitle() : "Configure your bird";
68 }
69
70 @Override
71 public Component asVaadinComponent() {
72 return new VerticalLayout(items.toArray(new CheckBox[]{}));
73 }
74
75 @Override
76 public void onChange(Consumer<String> callback) {
77 items.forEach(i -> i.addValueChangeListener(e -> callback.accept(this.getChoice())));
78 }
79
80 @Override
81 public String getChoice() {
82 StringBuilder result = new StringBuilder("A ");
83 this.items.stream()
84 .filter(CheckBox::getValue)
85 .forEach(checkBox -> result.append(checkBox.getCaption() + " "));
86 result.append("bird");
87 return result.toString();
88 }
89
90 @Override
91 public void setChoice(String choice) {
92
93 }
94 }