Project

General

Profile

Download (6.83 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 java.util.HashSet;
12
import java.util.Set;
13

    
14
import com.vaadin.data.fieldgroup.BeanFieldGroup;
15
import com.vaadin.data.util.BeanItem;
16
import com.vaadin.event.FieldEvents.TextChangeEvent;
17
import com.vaadin.server.FontAwesome;
18
import com.vaadin.ui.Alignment;
19
import com.vaadin.ui.Button;
20
import com.vaadin.ui.Component;
21
import com.vaadin.ui.CssLayout;
22
import com.vaadin.ui.CustomField;
23
import com.vaadin.ui.GridLayout;
24
import com.vaadin.ui.Label;
25
import com.vaadin.ui.TextField;
26
import com.vaadin.ui.themes.ValoTheme;
27

    
28
import eu.etaxonomy.cdm.model.common.TimePeriod;
29
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
30
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
31

    
32
/**
33
 * @author a.kohlbecker
34
 * @since Apr 6, 2017
35
 *
36
 */
37
public class TimePeriodField extends CustomField<TimePeriod> {
38

    
39
    private static final long serialVersionUID = -7377778547595966252L;
40

    
41
    private static final String PRIMARY_STYLE = "v-time-period-field";
42

    
43
    private BeanFieldGroup<TimePeriod> fieldGroup = new BeanFieldGroup<>(TimePeriod.class);
44

    
45
    TextField parseField = null;
46

    
47
    TextField freeText = null;
48

    
49
    Label toLabel = null;
50

    
51
    GridLayout grid = new GridLayout(3, 4);
52

    
53
    CssLayout detailsView = new CssLayout();
54

    
55
    //TODO implement custom button textfield which does not require a gridLayout
56
    GridLayout buttonTextField = new GridLayout(2, 1);
57
    GridLayout simpleView = new GridLayout(2, 1);
58

    
59
    TextField cacheField = new TextField();
60

    
61
    Set<Component> styledComponents = new HashSet<>();
62

    
63
    /**
64
     *
65
     */
66
    public TimePeriodField() {
67
        super();
68

    
69
    }
70

    
71
    /**
72
     * @param string
73
     */
74
    public TimePeriodField(String string) {
75
        this();
76
        setCaption(string);
77
    }
78

    
79
    /**
80
     * {@inheritDoc}
81
     */
82
    @Override
83
    protected Component initContent() {
84

    
85
        super.setPrimaryStyleName(PRIMARY_STYLE);
86

    
87
        CssLayout root = new CssLayout();
88

    
89
        initSimpleView();
90
        initDetailsView();
91

    
92
        root.addComponent(simpleView);
93
        root.addComponent(detailsView);
94

    
95
        applyDefaultStyles();
96

    
97
        showSimple();
98

    
99
        return root;
100
    }
101

    
102
    /**
103
     *
104
     */
105
    private void initSimpleView() {
106

    
107
        Button showDetailsButton = new Button(FontAwesome.CALENDAR);
108
        showDetailsButton.addClickListener(e -> showDetails());
109
        cacheField.setWidth(353, Unit.PIXELS); // FIXME 100% does not work
110

    
111
        simpleView.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
112
        simpleView.setWidth(100, Unit.PERCENTAGE);
113
        simpleView.addComponent(showDetailsButton, 0, 0);
114
        simpleView.addComponent(cacheField, 1, 0);
115
        simpleView.setColumnExpandRatio(1, 0.9f);
116
    }
117

    
118
    /**
119
     *
120
     */
121
    private void initDetailsView() {
122

    
123
        parseField = new TextField();
124
        // parseField.setWidth(100, Unit.PERCENTAGE);
125
        parseField.setInputPrompt("This field will parse the entered time period");
126
        parseField.addTextChangeListener(e -> parseInput(e));
127
        parseField.setWidth(100, Unit.PERCENTAGE);
128

    
129
        Button closeDetailsButton = new Button(FontAwesome.CLOSE);
130
        closeDetailsButton.addClickListener(e -> showSimple());
131

    
132
        buttonTextField.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
133
        buttonTextField.setWidth(100, Unit.PERCENTAGE);
134
        buttonTextField.addComponent(closeDetailsButton, 0, 0);
135
        buttonTextField.addComponent(parseField, 1, 0);
136
        buttonTextField.setColumnExpandRatio(1, 1.0f);
137

    
138
        PartialDateField startDate = new PartialDateField("Start");
139
        startDate.setInputPrompt("dd.mm.yyy");
140
        PartialDateField endDate = new PartialDateField("End");
141
        endDate.setInputPrompt("dd.mm.yyy");
142
        freeText = new TextField("FreeText");
143
        freeText.setWidth(100, Unit.PERCENTAGE);
144

    
145
        fieldGroup.bind(startDate, "start");
146
        fieldGroup.bind(endDate, "end");
147
        fieldGroup.bind(freeText, "freeText");
148

    
149
        toLabel = new Label("\u2014"); // EM DASH : 0x2014
150

    
151
        int row = 0;
152
        grid.addComponent(buttonTextField, 0, row, 2, row);
153
        row++;
154
        grid.addComponent(startDate, 0, row);
155
        grid.addComponent(toLabel, 1, row);
156
        grid.setComponentAlignment(toLabel, Alignment.BOTTOM_CENTER);
157
        grid.addComponent(endDate, 2, row);
158
        row++;
159
        grid.addComponent(freeText, 0, row, 2, row);
160

    
161
        // apply the style of the container to all child components. E.g. make all tiny
162
        addStyleName((getStyleName()));
163

    
164
        detailsView.setStyleName("margin-wrapper");
165
        detailsView.addComponent(grid);
166

    
167
    }
168

    
169

    
170
    /**
171
     * @return
172
     */
173
    private void showSimple() {
174
        detailsView.setVisible(false);
175
        simpleView.setVisible(true);
176
    }
177

    
178
    /**
179
     * @return
180
     */
181
    private void showDetails() {
182
        detailsView.setVisible(true);
183
        simpleView.setVisible(false);
184
    }
185

    
186
    /**
187
     * @param e
188
     * @return
189
     */
190
    private void parseInput(TextChangeEvent e) {
191
        if(!e.getText().isEmpty()){
192
            TimePeriod parsedPeriod = TimePeriodParser.parseString(e.getText());
193
            fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(parsedPeriod));
194
        }
195
    }
196

    
197
    /**
198
     *
199
     */
200
    private void applyDefaultStyles() {
201
        if(parseField != null) {
202
            parseField.addStyleName(RegistrationStyles.HELPER_FIELD);
203
            toLabel.addStyleName("to-label");
204
            buttonTextField.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
205
        }
206

    
207
    }
208

    
209
    @Override
210
    protected void setInternalValue(TimePeriod newValue) {
211
        super.setInternalValue(newValue);
212
        fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(newValue));
213

    
214
        cacheField.setReadOnly(false);
215
        cacheField.setValue(newValue.toString());
216
        cacheField.setReadOnly(true);
217
    }
218

    
219
    @Override
220
    public void setStyleName(String style) {
221
        super.setStyleName(style);
222
        grid.iterator().forEachRemaining(c -> c.setStyleName(style));
223
        buttonTextField.iterator().forEachRemaining(c -> c.setStyleName(style));
224
        simpleView.iterator().forEachRemaining(c -> c.setStyleName(style));
225
        applyDefaultStyles();
226
    }
227

    
228
    @Override
229
    public void addStyleName(String style) {
230
        super.addStyleName(style);
231
        grid.iterator().forEachRemaining(c -> c.addStyleName(style));
232
        simpleView.iterator().forEachRemaining(c -> {
233
            c.addStyleName(style);
234
        });
235

    
236
        buttonTextField.iterator().forEachRemaining(c -> c.addStyleName(style));
237
    }
238

    
239
    /**
240
     * {@inheritDoc}
241
     */
242
    @Override
243
    public Class<? extends TimePeriod> getType() {
244
        return TimePeriod.class;
245
    }
246

    
247

    
248

    
249
}
(6-6/6)