aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/util/CheckBoxWithPropertyDataSource.java
blob: bd9df1e8f8378bd7d445b32a2969d923748ef016 (plain)
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
package com.vaadin.tests.util;

import com.vaadin.ui.CheckBox;
import com.vaadin.v7.data.Property;
import com.vaadin.v7.data.Property.ValueChangeNotifier;
import com.vaadin.v7.data.Validator.InvalidValueException;

public class CheckBoxWithPropertyDataSource extends CheckBox {

    public CheckBoxWithPropertyDataSource(String caption) {
        super(caption);
    }

    public CheckBoxWithPropertyDataSource(String caption,
            Property<Boolean> property) {
        super(caption);

        setValue(property.getValue());
        addValueChangeListener(event -> property.setValue(event.getValue()));

        if (property instanceof ValueChangeNotifier) {
            ((ValueChangeNotifier) property)
                    .addValueChangeListener(event -> setValue(
                            (Boolean) event.getProperty().getValue()));
        }
    }

    public void validate() {
        if (isRequired() && !getValue()) {
            throw new InvalidValueException(
                    "Required CheckBox should be checked");
        }
    }

}