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.data.util.BeanItem;
|
13
|
import com.vaadin.event.FieldEvents.TextChangeEvent;
|
14
|
import com.vaadin.ui.Alignment;
|
15
|
import com.vaadin.ui.Component;
|
16
|
import com.vaadin.ui.CssLayout;
|
17
|
import com.vaadin.ui.CustomField;
|
18
|
import com.vaadin.ui.GridLayout;
|
19
|
import com.vaadin.ui.Label;
|
20
|
import com.vaadin.ui.TextField;
|
21
|
|
22
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
23
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
24
|
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
|
25
|
|
26
|
/**
|
27
|
* @author a.kohlbecker
|
28
|
* @since Apr 6, 2017
|
29
|
*
|
30
|
*/
|
31
|
public class TimePeriodField extends CustomField<TimePeriod> {
|
32
|
|
33
|
private static final long serialVersionUID = -7377778547595966252L;
|
34
|
|
35
|
private static final String PRIMARY_STYLE = "v-time-period-field";
|
36
|
|
37
|
private BeanFieldGroup<TimePeriod> fieldGroup = new BeanFieldGroup<>(TimePeriod.class);
|
38
|
|
39
|
TextField parseField = null;
|
40
|
|
41
|
TextField freeText = null;
|
42
|
|
43
|
Label toLabel = null;
|
44
|
|
45
|
GridLayout grid = new GridLayout(3, 3);
|
46
|
|
47
|
/**
|
48
|
*
|
49
|
*/
|
50
|
public TimePeriodField() {
|
51
|
super();
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* @param string
|
56
|
*/
|
57
|
public TimePeriodField(String string) {
|
58
|
this();
|
59
|
setCaption(string);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* {@inheritDoc}
|
64
|
*/
|
65
|
@Override
|
66
|
protected Component initContent() {
|
67
|
|
68
|
super.setPrimaryStyleName(PRIMARY_STYLE);
|
69
|
|
70
|
parseField = new TextField();
|
71
|
parseField.setWidth(100, Unit.PERCENTAGE);
|
72
|
parseField.setInputPrompt("This field will parse the entered time period");
|
73
|
parseField.addTextChangeListener(e -> parseInput(e));
|
74
|
|
75
|
PartialDateField startDate = new PartialDateField("Start");
|
76
|
startDate.setInputPrompt("dd.mm.yyy");
|
77
|
PartialDateField endDate = new PartialDateField("End");
|
78
|
endDate.setInputPrompt("dd.mm.yyy");
|
79
|
freeText = new TextField("FreeText");
|
80
|
freeText.setWidth(100, Unit.PERCENTAGE);
|
81
|
|
82
|
fieldGroup.bind(startDate, "start");
|
83
|
fieldGroup.bind(endDate, "end");
|
84
|
fieldGroup.bind(freeText, "freeText");
|
85
|
|
86
|
toLabel = new Label("\u2014"); // EM DASH : 0x2014
|
87
|
|
88
|
int row = 0;
|
89
|
grid.addComponent(parseField, 0, row, 2, row);
|
90
|
row++;
|
91
|
grid.addComponent(startDate, 0, row);
|
92
|
grid.addComponent(toLabel, 1, row);
|
93
|
grid.setComponentAlignment(toLabel, Alignment.BOTTOM_CENTER);
|
94
|
grid.addComponent(endDate, 2, row);
|
95
|
row++;
|
96
|
grid.addComponent(freeText, 0, row, 2, row);
|
97
|
|
98
|
grid.iterator().forEachRemaining(c -> c.setStyleName(getStyleName()));
|
99
|
|
100
|
CssLayout marginwrapper = new CssLayout();
|
101
|
marginwrapper.setStyleName("margin-wrapper");
|
102
|
marginwrapper.addComponent(grid);
|
103
|
|
104
|
applyDefaultStyles();
|
105
|
|
106
|
return marginwrapper;
|
107
|
}
|
108
|
|
109
|
|
110
|
|
111
|
/**
|
112
|
* @param e
|
113
|
* @return
|
114
|
*/
|
115
|
private void parseInput(TextChangeEvent e) {
|
116
|
if(!e.getText().isEmpty()){
|
117
|
TimePeriod parsedPeriod = TimePeriodParser.parseString(e.getText());
|
118
|
fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(parsedPeriod));
|
119
|
}
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
*
|
124
|
*/
|
125
|
private void applyDefaultStyles() {
|
126
|
if(parseField != null) {
|
127
|
parseField.addStyleName(RegistrationStyles.HELPER_FIELD);
|
128
|
toLabel.addStyleName("to-label");
|
129
|
}
|
130
|
|
131
|
}
|
132
|
|
133
|
@Override
|
134
|
protected void setInternalValue(TimePeriod newValue) {
|
135
|
super.setInternalValue(newValue);
|
136
|
fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(newValue));
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public void setStyleName(String style) {
|
141
|
super.setStyleName(style);
|
142
|
grid.iterator().forEachRemaining(c -> c.setStyleName(style));
|
143
|
applyDefaultStyles();
|
144
|
}
|
145
|
|
146
|
@Override
|
147
|
public void addStyleName(String style) {
|
148
|
super.addStyleName(style);
|
149
|
grid.iterator().forEachRemaining(c -> c.addStyleName(style));
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* {@inheritDoc}
|
154
|
*/
|
155
|
@Override
|
156
|
public Class<? extends TimePeriod> getType() {
|
157
|
return TimePeriod.class;
|
158
|
}
|
159
|
|
160
|
|
161
|
|
162
|
}
|