Project

General

Profile

Download (7.64 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.common;
10

    
11
import java.util.HashSet;
12
import java.util.Set;
13

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

    
29
import eu.etaxonomy.cdm.model.common.TimePeriod;
30
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
31
import eu.etaxonomy.cdm.vaadin.component.PartialDateField;
32
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
33
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
34
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
35

    
36
/**
37
 * @author a.kohlbecker
38
 * @since Apr 6, 2017
39
 *
40
 */
41
public class TimePeriodField extends CustomField<TimePeriod> {
42

    
43
    private static final long serialVersionUID = -7377778547595966252L;
44

    
45
    private static final String PRIMARY_STYLE = "v-time-period-field";
46

    
47
    private BeanFieldGroup<TimePeriod> fieldGroup = new BeanFieldGroup<>(TimePeriod.class);
48

    
49
    TextField parseField = null;
50

    
51
    TextField freeText = null;
52

    
53
    Label toLabel = null;
54

    
55
    GridLayout grid = new GridLayout(3, 4);
56

    
57
    CssLayout detailsView = new CssLayout();
58

    
59
    //TODO implement custom button textfield which does not require a gridLayout
60
    GridLayout buttonTextField = new GridLayout(2, 1);
61
    GridLayout simpleView = new GridLayout(2, 1);
62

    
63
    TextField cacheField = new TextField();
64

    
65
    Set<Component> styledComponents = new HashSet<>();
66

    
67
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
68

    
69
    /**
70
     *
71
     */
72
    public TimePeriodField() {
73
        super();
74

    
75
    }
76

    
77
    /**
78
     * @param string
79
     */
80
    public TimePeriodField(String string) {
81
        this();
82
        setCaption(string);
83
    }
84

    
85
    /**
86
     * {@inheritDoc}
87
     */
88
    @Override
89
    protected Component initContent() {
90

    
91
        super.setPrimaryStyleName(PRIMARY_STYLE);
92

    
93
        CssLayout root = new CssLayout();
94

    
95
        initSimpleView();
96
        initDetailsView();
97

    
98
        root.addComponent(simpleView);
99
        root.addComponent(detailsView);
100

    
101
        applyDefaultStyles();
102

    
103
        showSimple();
104

    
105
        return root;
106
    }
107

    
108
    /**
109
     *
110
     */
111
    private void initSimpleView() {
112

    
113
        Button showDetailsButton = new Button(FontAwesome.CALENDAR);
114
        showDetailsButton.addClickListener(e -> showDetails());
115
        cacheField.setWidth(353, Unit.PIXELS); // FIXME 100% does not work
116

    
117
        simpleView.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
118
        simpleView.setWidth(100, Unit.PERCENTAGE);
119
        simpleView.addComponent(showDetailsButton, 0, 0);
120
        simpleView.addComponent(cacheField, 1, 0);
121
        simpleView.setColumnExpandRatio(1, 0.9f);
122
    }
123

    
124
    /**
125
     *
126
     */
127
    private void initDetailsView() {
128

    
129
        parseField = new TextField();
130
        // parseField.setWidth(100, Unit.PERCENTAGE);
131
        parseField.setInputPrompt("This field will parse the entered time period");
132
        parseField.addTextChangeListener(e -> parseInput(e));
133
        parseField.setWidth(100, Unit.PERCENTAGE);
134

    
135
        Button closeDetailsButton = new Button(FontAwesome.CLOSE);
136
        closeDetailsButton.addClickListener(e -> {
137
            try {
138
                fieldGroup.commit();
139
            } catch (CommitException e1) {
140
                // TODO Auto-generated catch block
141
                e1.printStackTrace();
142
            }
143
            updateCacheField();
144
            showSimple();
145
        });
146

    
147
        buttonTextField.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
148
        buttonTextField.setWidth(100, Unit.PERCENTAGE);
149
        buttonTextField.addComponent(closeDetailsButton, 0, 0);
150
        buttonTextField.addComponent(parseField, 1, 0);
151
        buttonTextField.setColumnExpandRatio(1, 1.0f);
152

    
153
        PartialDateField startDate = new PartialDateField("Start");
154
        startDate.setInputPrompt("dd.mm.yyyy");
155
        PartialDateField endDate = new PartialDateField("End");
156
        endDate.setInputPrompt("dd.mm.yyyy");
157
        freeText = new TextField("FreeText");
158
        freeText.setWidth(100, Unit.PERCENTAGE);
159

    
160
        fieldGroup.bind(startDate, "start");
161
        fieldGroup.bind(endDate, "end");
162
        fieldGroup.bind(freeText, "freeText");
163

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

    
166
        int row = 0;
167
        grid.addComponent(buttonTextField, 0, row, 2, row);
168
        row++;
169
        grid.addComponent(startDate, 0, row);
170
        grid.addComponent(toLabel, 1, row);
171
        grid.setComponentAlignment(toLabel, Alignment.BOTTOM_CENTER);
172
        grid.addComponent(endDate, 2, row);
173
        row++;
174
        grid.addComponent(freeText, 0, row, 2, row);
175

    
176
        // apply the style of the container to all child components. E.g. make all tiny
177
        addStyleName((getStyleName()));
178

    
179
        detailsView.setStyleName("margin-wrapper");
180
        detailsView.addComponent(grid);
181

    
182
    }
183

    
184

    
185
    /**
186
     * @return
187
     */
188
    private void showSimple() {
189
        detailsView.setVisible(false);
190
        simpleView.setVisible(true);
191
    }
192

    
193
    /**
194
     * @return
195
     */
196
    private void showDetails() {
197
        detailsView.setVisible(true);
198
        simpleView.setVisible(false);
199
    }
200

    
201
    /**
202
     * @param e
203
     * @return
204
     */
205
    private void parseInput(TextChangeEvent e) {
206
        if(!e.getText().isEmpty()){
207
            TimePeriod parsedPeriod = TimePeriodParser.parseString(e.getText());
208
            fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(parsedPeriod));
209
        }
210
    }
211

    
212
    /**
213
     *
214
     */
215
    private void applyDefaultStyles() {
216
        if(parseField != null) {
217
            parseField.addStyleName(RegistrationStyles.HELPER_FIELD);
218
            toLabel.addStyleName("to-label");
219
            buttonTextField.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
220
        }
221

    
222
    }
223

    
224
    @Override
225
    protected void setInternalValue(TimePeriod newValue) {
226
        super.setInternalValue(newValue);
227
        fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(newValue));
228
        updateCacheField();
229
    }
230

    
231
    /**
232
     * @param newValue
233
     */
234
    private void updateCacheField() {
235
        TimePeriod newValue = fieldGroup.getItemDataSource().getBean();
236
        cacheField.setReadOnly(false);
237
        cacheField.setValue(timePeriodFormatter.print(newValue));
238
        cacheField.setReadOnly(true);
239
    }
240

    
241
    @Override
242
    public void setStyleName(String style) {
243
        super.setStyleName(style);
244
        grid.iterator().forEachRemaining(c -> c.setStyleName(style));
245
        buttonTextField.iterator().forEachRemaining(c -> c.setStyleName(style));
246
        simpleView.iterator().forEachRemaining(c -> c.setStyleName(style));
247
        applyDefaultStyles();
248
    }
249

    
250
    @Override
251
    public void addStyleName(String style) {
252
        super.addStyleName(style);
253
        grid.iterator().forEachRemaining(c -> c.addStyleName(style));
254
        simpleView.iterator().forEachRemaining(c -> {
255
            c.addStyleName(style);
256
        });
257

    
258
        buttonTextField.iterator().forEachRemaining(c -> c.addStyleName(style));
259
    }
260

    
261
    /**
262
     * {@inheritDoc}
263
     */
264
    @Override
265
    public Class<? extends TimePeriod> getType() {
266
        return TimePeriod.class;
267
    }
268

    
269

    
270

    
271
}
(3-3/3)