/
DurationConfigField.java
235 lines (200 loc) · 7.17 KB
/
DurationConfigField.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.ui.tenantconfiguration.polling;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.hawkbit.ui.tenantconfiguration.ConfigurationItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.GridLayout;
/**
* The DurationConfigField consists of three vaadin fields. A {@link #Label}
* {@link #DurationField} and a {@link #CheckBox}. The user can then enter a
* duration in the DurationField or he can configure using the global duration
* by changing the CheckBox.
*/
public final class DurationConfigField extends GridLayout implements ConfigurationItem {
private static final long serialVersionUID = 1L;
private final List<ConfigurationItemChangeListener> configurationChangeListeners = new ArrayList<>();
private final CheckBox checkBox = new CheckBox();
private final DurationField durationField = new DurationField();
private Duration globalDuration;
private DurationConfigField(final String id) {
super(2, 2);
this.addStyleName("duration-config-field");
this.setSpacing(true);
this.setImmediate(true);
this.setColumnExpandRatio(1, 1.0F);
durationField.setId(id + ".field");
checkBox.setId(id + ".checkbox");
this.addComponent(checkBox, 0, 0);
this.setComponentAlignment(checkBox, Alignment.MIDDLE_LEFT);
this.addComponent(durationField, 1, 0);
this.setComponentAlignment(durationField, Alignment.MIDDLE_LEFT);
checkBox.addValueChangeListener(event -> checkBoxChange());
durationField.addValueChangeListener(event -> notifyConfigurationChanged());
}
private void checkBoxChange() {
durationField.setEnabled(checkBox.getValue());
if (!checkBox.getValue()) {
durationField.setDuration(globalDuration);
}
notifyConfigurationChanged();
}
/**
* has to be called before using, see Builder Implementation.
*
* @param tenantDuration
* tenant specific duration value
* @param globalDuration
* duration value which is stored in the global configuration
*/
private void init(final Duration globalDuration, final Duration tenantDuration) {
this.globalDuration = globalDuration;
this.setValue(tenantDuration);
}
private void setCheckBoxTooltip(final String label) {
checkBox.setDescription(label);
}
private void setAllowedRange(final Duration minimumDuration, final Duration maximumDuration) {
durationField.setMinimumDuration(minimumDuration);
durationField.setMaximumDuration(maximumDuration);
}
/**
* Set the value of the duration field
*
* @param tenantDuration
* duration which will be set in to the duration field, when
* {@code null} the global configuration will be used.
*/
public void setValue(final Duration tenantDuration) {
if (tenantDuration == null) {
// no tenant specific configuration
checkBox.setValue(false);
durationField.setDuration(globalDuration);
durationField.setEnabled(false);
return;
}
checkBox.setValue(true);
durationField.setDuration(tenantDuration);
durationField.setEnabled(true);
}
/**
* @return the duration of the duration field or null, when the user has
* configured to use the global value.
*/
public Duration getValue() {
if (checkBox.getValue()) {
return durationField.getDuration();
}
return null;
}
@Override
public boolean isUserInputValid() {
return !checkBox.getValue() || (durationField.isValid() && durationField.getValue() != null);
}
private void notifyConfigurationChanged() {
configurationChangeListeners.forEach(listener -> listener.configurationHasChanged());
}
@Override
public void addChangeListener(final ConfigurationItemChangeListener listener) {
configurationChangeListeners.add(listener);
}
/**
* Create a DurationConfigFieldBuilder.
*
* @return the builder
*/
public static DurationConfigFieldBuilder builder(final String id) {
return new DurationConfigFieldBuilder(id);
}
/**
* Builder for the calendar widget.
*
*/
public static final class DurationConfigFieldBuilder {
private final DurationConfigField field;
private Duration globalDuration;
private Duration tenantDuration;
private DurationConfigFieldBuilder(final String id) {
field = new DurationConfigField(id);
}
/**
* set the checkbox tooltip.
*
* @param label
* the tooltip
* @return the builder
*/
public DurationConfigFieldBuilder checkBoxTooltip(final String label) {
field.setCheckBoxTooltip(label);
return this;
}
/**
* set the global duration.
*
* @param globalDuration
* the global duration
* @return the builder
*/
public DurationConfigFieldBuilder globalDuration(final Duration globalDuration) {
this.globalDuration = globalDuration;
return this;
}
/**
* set the caption.
*
* @param caption
* the caption
* @return the builder
*/
public DurationConfigFieldBuilder caption(final String caption) {
field.setCaption(caption);
return this;
}
/**
* set the range.
*
* @param minDuration
* min duration
* @param maxDuration
* max duration
* @return the builder
*/
public DurationConfigFieldBuilder range(final Duration minDuration, final Duration maxDuration) {
field.setAllowedRange(minDuration, maxDuration);
return this;
}
/**
* set the tenant duration.
*
* @param tenantDuration
* the duration
* @return the builder
*/
public DurationConfigFieldBuilder tenantDuration(final Duration tenantDuration) {
this.tenantDuration = tenantDuration;
return this;
}
/**
* Create the {@link DurationConfigField}.
*
* @return the {@link DurationConfigField}
*/
public DurationConfigField build() {
if (globalDuration == null) {
throw new IllegalStateException(
"Cannot build DurationConfigField without a value for global duration.");
}
field.init(globalDuration, tenantDuration);
return field;
}
}
}