aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java
blob: 14be2dbd146ef8f971b98cffe9e2924c0452c58b (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.vaadin.tests.components.textfield;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Button;
import com.vaadin.v7.data.Property;
import com.vaadin.v7.data.util.PropertyFormatter;
import com.vaadin.v7.ui.TextField;

public class TextFieldWithPropertyFormatter extends TestBase {

    private PropertyFormatter<BigDecimal> formatter;
    private Property<BigDecimal> property;

    @Override
    protected void setup() {
        /*
         * Formatter that: - formats in UK currency style - scales to 2 fraction
         * digits - rounds half up
         */
        // Property containing BigDecimal
        property = new Property<BigDecimal>() {
            private BigDecimal value;

            @Override
            public BigDecimal getValue() {
                return value;
            }

            @Override
            public void setValue(BigDecimal newValue) throws ReadOnlyException {
                value = newValue;
            }

            @Override
            public Class<BigDecimal> getType() {
                return BigDecimal.class;
            }

            @Override
            public boolean isReadOnly() {
                return false;
            }

            @Override
            public void setReadOnly(boolean newStatus) {
                // ignore
            }
        };

        formatter = new PropertyFormatter<BigDecimal>(property) {

            private final DecimalFormat df = new DecimalFormat("#,##0.00",
                    new DecimalFormatSymbols(new Locale("en", "UK")));
            {
                df.setParseBigDecimal(true);
                // df.setRoundingMode(RoundingMode.HALF_UP);
            }

            @Override
            public String format(BigDecimal value) {

                final String retVal;
                if (value == null) {
                    retVal = "";
                } else {
                    retVal = df.format(value);
                }
                return retVal;
            }

            @Override
            public BigDecimal parse(String formattedValue) throws Exception {
                if (formattedValue != null
                        && !formattedValue.trim().isEmpty()) {
                    BigDecimal value = (BigDecimal) df.parse(formattedValue);
                    value = value.setScale(2, BigDecimal.ROUND_HALF_UP);
                    return value;
                }
                return null;
            }
        };

        final TextField tf1 = new TextField();

        tf1.setPropertyDataSource(formatter);

        addComponent(tf1);

        Button b = new Button(
                "Sync (typing 12345.6789 and clicking this should format field)");
        b.addClickListener(event -> {
        });
        addComponent(b);
        b = new Button("Set '12345.6789' to textfield on the server side");
        b.addClickListener(event -> tf1.setValue("12345.6789"));
        addComponent(b);

    }

    @Override
    protected String getDescription() {
        return "Should work";
    }

    @Override
    protected Integer getTicketNumber() {
        return 4394;
    }

}