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.server.FontAwesome;
|
15
|
import com.vaadin.ui.Alignment;
|
16
|
import com.vaadin.ui.Button;
|
17
|
import com.vaadin.ui.Component;
|
18
|
import com.vaadin.ui.CssLayout;
|
19
|
import com.vaadin.ui.CustomField;
|
20
|
import com.vaadin.ui.GridLayout;
|
21
|
import com.vaadin.ui.Label;
|
22
|
import com.vaadin.ui.TextField;
|
23
|
import com.vaadin.ui.themes.ValoTheme;
|
24
|
|
25
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
26
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
27
|
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
|
28
|
|
29
|
/**
|
30
|
* @author a.kohlbecker
|
31
|
* @since Apr 6, 2017
|
32
|
*
|
33
|
*/
|
34
|
public class TimePeriodField extends CustomField<TimePeriod> {
|
35
|
|
36
|
private static final long serialVersionUID = -7377778547595966252L;
|
37
|
|
38
|
private static final String PRIMARY_STYLE = "v-time-period-field";
|
39
|
|
40
|
private BeanFieldGroup<TimePeriod> fieldGroup = new BeanFieldGroup<>(TimePeriod.class);
|
41
|
|
42
|
TextField parseField = null;
|
43
|
|
44
|
TextField freeText = null;
|
45
|
|
46
|
Label toLabel = null;
|
47
|
|
48
|
GridLayout grid = new GridLayout(3, 3);
|
49
|
|
50
|
CssLayout detailsView = new CssLayout();
|
51
|
CssLayout buttonTextField = new CssLayout();
|
52
|
|
53
|
CssLayout simpleView = new CssLayout();
|
54
|
|
55
|
TextField cacheField = new TextField();
|
56
|
|
57
|
/**
|
58
|
*
|
59
|
*/
|
60
|
public TimePeriodField() {
|
61
|
super();
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* @param string
|
66
|
*/
|
67
|
public TimePeriodField(String string) {
|
68
|
this();
|
69
|
setCaption(string);
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* {@inheritDoc}
|
74
|
*/
|
75
|
@Override
|
76
|
protected Component initContent() {
|
77
|
|
78
|
super.setPrimaryStyleName(PRIMARY_STYLE);
|
79
|
|
80
|
CssLayout root = new CssLayout();
|
81
|
|
82
|
initSimpleView();
|
83
|
root.addComponent(simpleView);
|
84
|
|
85
|
initDetailsView();
|
86
|
root.addComponent(detailsView);
|
87
|
|
88
|
applyDefaultStyles();
|
89
|
|
90
|
showSimple();
|
91
|
|
92
|
return root;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
*
|
97
|
*/
|
98
|
private void initSimpleView() {
|
99
|
|
100
|
cacheField.setWidth(100, Unit.PERCENTAGE);
|
101
|
Button showDetailsButton = new Button(FontAwesome.CALENDAR);
|
102
|
showDetailsButton.addClickListener(e -> showDetails());
|
103
|
simpleView.addComponent(showDetailsButton);
|
104
|
simpleView.addComponent(cacheField);
|
105
|
simpleView.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
*
|
110
|
*/
|
111
|
private void initDetailsView() {
|
112
|
|
113
|
parseField = new TextField();
|
114
|
parseField.setWidth(100, Unit.PERCENTAGE);
|
115
|
parseField.setInputPrompt("This field will parse the entered time period");
|
116
|
parseField.addTextChangeListener(e -> parseInput(e));
|
117
|
|
118
|
Button closeDetailsButton = new Button(FontAwesome.CLOSE);
|
119
|
closeDetailsButton.addClickListener(e -> showSimple());
|
120
|
|
121
|
buttonTextField.addComponent(closeDetailsButton);
|
122
|
buttonTextField.addComponent(parseField);
|
123
|
|
124
|
PartialDateField startDate = new PartialDateField("Start");
|
125
|
startDate.setInputPrompt("dd.mm.yyy");
|
126
|
PartialDateField endDate = new PartialDateField("End");
|
127
|
endDate.setInputPrompt("dd.mm.yyy");
|
128
|
freeText = new TextField("FreeText");
|
129
|
freeText.setWidth(100, Unit.PERCENTAGE);
|
130
|
|
131
|
fieldGroup.bind(startDate, "start");
|
132
|
fieldGroup.bind(endDate, "end");
|
133
|
fieldGroup.bind(freeText, "freeText");
|
134
|
|
135
|
toLabel = new Label("\u2014"); // EM DASH : 0x2014
|
136
|
|
137
|
int row = 0;
|
138
|
grid.addComponent(buttonTextField, 0, row, 2, row);
|
139
|
row++;
|
140
|
grid.addComponent(startDate, 0, row);
|
141
|
grid.addComponent(toLabel, 1, row);
|
142
|
grid.setComponentAlignment(toLabel, Alignment.BOTTOM_CENTER);
|
143
|
grid.addComponent(endDate, 2, row);
|
144
|
row++;
|
145
|
grid.addComponent(freeText, 0, row, 2, row);
|
146
|
|
147
|
grid.iterator().forEachRemaining(c -> c.setStyleName(getStyleName()));
|
148
|
|
149
|
detailsView.setStyleName("margin-wrapper");
|
150
|
detailsView.addComponent(grid);
|
151
|
}
|
152
|
|
153
|
|
154
|
/**
|
155
|
* @return
|
156
|
*/
|
157
|
private void showSimple() {
|
158
|
detailsView.setVisible(false);
|
159
|
simpleView.setVisible(true);
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* @return
|
164
|
*/
|
165
|
private void showDetails() {
|
166
|
detailsView.setVisible(true);
|
167
|
simpleView.setVisible(false);
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* @param e
|
172
|
* @return
|
173
|
*/
|
174
|
private void parseInput(TextChangeEvent e) {
|
175
|
if(!e.getText().isEmpty()){
|
176
|
TimePeriod parsedPeriod = TimePeriodParser.parseString(e.getText());
|
177
|
fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(parsedPeriod));
|
178
|
}
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
*
|
183
|
*/
|
184
|
private void applyDefaultStyles() {
|
185
|
if(parseField != null) {
|
186
|
parseField.addStyleName(RegistrationStyles.HELPER_FIELD);
|
187
|
toLabel.addStyleName("to-label");
|
188
|
buttonTextField.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
|
189
|
}
|
190
|
|
191
|
}
|
192
|
|
193
|
@Override
|
194
|
protected void setInternalValue(TimePeriod newValue) {
|
195
|
super.setInternalValue(newValue);
|
196
|
fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(newValue));
|
197
|
|
198
|
cacheField.setReadOnly(false);
|
199
|
cacheField.setValue(newValue.toString());
|
200
|
cacheField.setReadOnly(true);
|
201
|
}
|
202
|
|
203
|
@Override
|
204
|
public void setStyleName(String style) {
|
205
|
super.setStyleName(style);
|
206
|
grid.iterator().forEachRemaining(c -> c.setStyleName(style));
|
207
|
buttonTextField.iterator().forEachRemaining(c -> c.setStyleName(style));
|
208
|
simpleView.iterator().forEachRemaining(c -> c.setStyleName(style));
|
209
|
applyDefaultStyles();
|
210
|
}
|
211
|
|
212
|
@Override
|
213
|
public void addStyleName(String style) {
|
214
|
super.addStyleName(style);
|
215
|
grid.iterator().forEachRemaining(c -> c.addStyleName(style));
|
216
|
simpleView.iterator().forEachRemaining(c -> c.addStyleName(style));
|
217
|
buttonTextField.iterator().forEachRemaining(c -> c.addStyleName(style));
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* {@inheritDoc}
|
222
|
*/
|
223
|
@Override
|
224
|
public Class<? extends TimePeriod> getType() {
|
225
|
return TimePeriod.class;
|
226
|
}
|
227
|
|
228
|
|
229
|
|
230
|
}
|