Project

General

Profile

Download (3.19 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.component;
10

    
11
import com.vaadin.data.fieldgroup.BeanFieldGroup;
12
import com.vaadin.ui.Alignment;
13
import com.vaadin.ui.Component;
14
import com.vaadin.ui.CssLayout;
15
import com.vaadin.ui.CustomField;
16
import com.vaadin.ui.GridLayout;
17
import com.vaadin.ui.Label;
18
import com.vaadin.ui.PopupDateField;
19
import com.vaadin.ui.TextField;
20

    
21
import eu.etaxonomy.cdm.model.common.TimePeriod;
22
import eu.etaxonomy.cdm.vaadin.util.converter.JodaDateTimeConverter;
23

    
24
/**
25
 * @author a.kohlbecker
26
 * @since Apr 6, 2017
27
 *
28
 */
29
public class TimePeriodField extends CustomField<TimePeriod> {
30

    
31
    private static final long serialVersionUID = -7377778547595966252L;
32

    
33
    private static final String PRIMARY_STYLE = "v-time-period-field";
34

    
35
    private BeanFieldGroup<TimePeriod> fieldGroup = new BeanFieldGroup<>(TimePeriod.class);
36

    
37
    GridLayout grid = new GridLayout(4, 2);
38

    
39
    /**
40
     *
41
     */
42
    public TimePeriodField() {
43
        super();
44
    }
45

    
46
    /**
47
     * @param string
48
     */
49
    public TimePeriodField(String string) {
50
        this();
51
        setCaption(string);
52
    }
53

    
54
    /**
55
     * {@inheritDoc}
56
     */
57
    @Override
58
    protected Component initContent() {
59

    
60
        super.setPrimaryStyleName(PRIMARY_STYLE);
61

    
62
        // better use https://vaadin.com/directory#!addon/tuning-datefield ???
63
        PopupDateField startDate = new PopupDateField("Start");
64
        startDate.setConverter(new JodaDateTimeConverter());
65
        PopupDateField endDate = new PopupDateField("End");
66
        endDate.setConverter(new JodaDateTimeConverter());
67
        TextField freeText = new TextField("FreeText");
68
        freeText.setWidth(100, Unit.PERCENTAGE);
69

    
70
        fieldGroup.bind(startDate, "start");
71
        fieldGroup.bind(endDate, "end");
72
        fieldGroup.bind(freeText, "freeText");
73

    
74
        Label dashLabel = new Label("-");
75

    
76
        grid.addComponent(startDate, 0, 0);
77
        grid.addComponent(dashLabel);
78
        grid.setComponentAlignment(dashLabel, Alignment.BOTTOM_CENTER);
79
        grid.addComponent(endDate, 2, 0);
80
        grid.addComponent(freeText, 0, 1, 2, 1);
81

    
82
        grid.iterator().forEachRemaining(c -> c.setStyleName(getStyleName()));
83

    
84
        CssLayout marginwrapper = new CssLayout();
85
        marginwrapper.setStyleName("margin-wrapper");
86
        marginwrapper.addComponent(grid);
87

    
88
        return marginwrapper;
89
    }
90

    
91

    
92

    
93
    @Override
94
    protected void setInternalValue(TimePeriod newValue) {
95
        super.setInternalValue(newValue);
96
        fieldGroup.setItemDataSource(newValue);
97
    }
98

    
99
    @Override
100
    public void setStyleName(String style) {
101
        super.setStyleName(style);
102
        grid.iterator().forEachRemaining(c -> c.setStyleName(style));
103
    }
104

    
105
    @Override
106
    public void addStyleName(String style) {
107
        super.addStyleName(style);
108
        grid.iterator().forEachRemaining(c -> c.addStyleName(style));
109
    }
110

    
111
    /**
112
     * {@inheritDoc}
113
     */
114
    @Override
115
    public Class<? extends TimePeriod> getType() {
116
        return TimePeriod.class;
117
    }
118

    
119
}
(4-4/4)