-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoGrid.java
365 lines (327 loc) · 14.4 KB
/
AutoGrid.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package org.vaadin.addons.tatu.prototools;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.DetachEvent;
import com.vaadin.flow.component.Focusable;
import com.vaadin.flow.component.HasValidation;
import com.vaadin.flow.component.HasValue;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.KeyNotifier;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.datetimepicker.DateTimePicker;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.data.binder.BeanPropertySet;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.BindingValidationStatus;
import com.vaadin.flow.data.binder.PropertyDefinition;
import com.vaadin.flow.data.binder.PropertySet;
import com.vaadin.flow.data.converter.LocalDateTimeToDateConverter;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.shared.Registration;
import com.vaadin.flow.theme.lumo.LumoUtility;
import com.vaadin.flow.theme.lumo.LumoUtility.BoxShadow;
import com.vaadin.flow.theme.lumo.LumoUtility.Display;
import com.vaadin.flow.theme.lumo.LumoUtility.FontSize;
import com.vaadin.flow.theme.lumo.LumoUtility.Margin;
import com.vaadin.flow.theme.lumo.LumoUtility.Padding;
import com.vaadin.flow.theme.lumo.LumoUtility.TextColor;
@NpmPackage(value = "@vaadin/vaadin-lumo-styles", version = "23.2.0-alpha2")
@JsModule("@vaadin/vaadin-lumo-styles/utility.js")
@JsModule("./lumo-utility.ts")
@CssImport(value = "./picker-fixes.css", themeFor = "vaadin-combo-box-overlay")
@CssImport(value = "./time-picker-width.css", themeFor = "vaadin-date-time-picker-time-picker")
public class AutoGrid<T> extends Grid<T> {
private BeanValidationBinder<T> binder;
private String itemErrorLabel;
private Class<T> beanType;
private Registration browserListener;
private boolean responsive;
private PropertySet<T> propertySet;
private boolean editorDisabled;
private PopupEdit dialog;
private Form<T> form;
public AutoGrid(Class<T> beanType) {
this(beanType, true);
}
public AutoGrid(Class<T> beanType, boolean autoBuild) {
super(beanType, autoBuild);
this.beanType = beanType;
this.propertySet = BeanPropertySet.get(beanType);
setDetailsVisibleOnClick(false);
addItemClickListener(event -> {
if (editorDisabled)
return;
T item = event.getItem();
// If editor is open already, lets close it
if (getEditor().isOpen() && getEditor().getItem().equals(item)) {
} else {
getEditor().editItem(item);
// Tip: Find the field from the colum and focus it for better UX
Component component = event.getColumn().getEditorComponent();
if (component instanceof Focusable) {
Focusable<?> field = (Focusable<?>) component;
field.focus();
}
}
});
getStyle().set("--lumo-space-xs", "1px");
getStyle().set("--lumo-space-m", "1px");
binder = new BeanValidationBinder<>(beanType);
getEditor().setBinder(binder);
if (autoBuild) {
configureColumns(beanType);
}
setItemDetailsRenderer(new ComponentRenderer(item -> {
Label label = new Label(itemErrorLabel);
label.addClassName("text-error");
return label;
}));
form = new Form<>(null, beanType, autoBuild);
// form.setProperties(propertyNames);
dialog = new PopupEdit(Utils.formatName(beanType.getSimpleName()),
form);
form.addValueChangeListener(e -> {
getDataProvider().refreshItem(e.getValue());
});
}
public void setEditorDisabled(boolean editorDisabled) {
this.editorDisabled = editorDisabled;
}
public void setResponsive(boolean responsive) {
this.responsive = responsive;
}
public Binder<T> getBinder() {
return binder;
}
private void configureColumns(Class<T> beanType) {
propertySet.getProperties()
.filter(property -> !property.isSubProperty()).sorted((prop1,
prop2) -> prop1.getName().compareTo(prop2.getName()))
.forEach(property -> {
Component component = FieldFactory.createField(property);
configureComponent(property, component);
});
getColumns().forEach(col -> {
if (col.getEditorComponent() instanceof DateTimePicker) {
col.setWidth("220px");
}
});
}
public void setColumns(String... propertyNames) {
binder = new BeanValidationBinder<>(beanType);
getColumns().forEach(this::removeColumn);
createCompactColumn(propertyNames);
Stream.of(propertyNames).forEach(this::addColumn);
configureColumns(beanType);
getEditor().setBinder(binder);
form.setProperties(propertyNames);
}
private void createCompactColumn(String... propertyNames) {
addComponentColumn(item -> {
Div div = new Div();
div.addClassNames(Display.GRID, LumoUtility.Grid.Column.COLUMNS_2,
BoxShadow.MEDIUM, Padding.SMALL);
Stream.of(propertyNames).limit(4).forEach(propName -> {
PropertyDefinition<T, ?> property = propertySet
.getProperty(propName).get();
Object valueObj = property.getGetter().apply(item);
String valueString = (valueObj != null) ? valueObj.toString()
: "";
Label name = new Label(Utils.formatName(propName));
name.addClassName(TextColor.SECONDARY);
Span value = new Span(valueString);
div.add(name, value);
});
Icon edit = VaadinIcon.EDIT.create();
edit.addClassNames(LumoUtility.Grid.Column.COLUMN_SPAN_2,
FontSize.XSMALL, TextColor.PRIMARY, Margin.Left.AUTO);
edit.addClickListener(event -> {
form.setValue(item);
dialog.open();
});
div.add(edit);
return div;
}).setHeader(Utils.formatName(beanType.getSimpleName()))
.setVisible(false);
}
public void addListColumn(String property, Class listBeanType,
ValueProvider listBeanProvider) {
this.addListColumn(property, listBeanType, listBeanProvider, true,
null);
}
public void addListColumn(String property, Class listBeanType,
ValueProvider listBeanProvider, String... listProperties) {
this.addListColumn(property, listBeanType, listBeanProvider, false,
listProperties);
}
protected void addListColumn(String property, Class listBeanType,
ValueProvider listBeanProvider, boolean autoCreate,
String... listProperties) {
PropertySet<T> propertySet = BeanPropertySet.get(beanType);
propertySet.getProperty(property).ifPresent(prop -> {
String name = Utils.formatName(property);
PopupListEdit<?> listEdit = new PopupListEdit<>(listBeanType,
listBeanProvider, autoCreate);
addColumn(item -> {
List<?> list = (List<?>) prop.getGetter().apply(item);
String text = name + " (" + list.size() + ")";
return text;
}).setHeader(name).setEditorComponent(listEdit);
if (listProperties != null)
listEdit.setColumns(listProperties);
binder.forField(listEdit).withValidationStatusHandler(
handler -> validationStatusHandler(handler, listEdit))
.bind(property);
listEdit.setLabel(name);
});
form.addListProperty(property, listBeanType, listBeanProvider,
autoCreate, listProperties);
}
public void addBeanColumn(String property, Class listBeanType) {
this.addBeanColumn(property, listBeanType, true, null);
}
public void addBeanColumn(String property, Class listBeanType,
String... listProperties) {
this.addBeanColumn(property, listBeanType, false, listProperties);
}
protected void addBeanColumn(String property, Class beanBeanType,
boolean autoCreate, String... beanProperties) {
PropertySet<T> propertySet = BeanPropertySet.get(beanType);
propertySet.getProperty(property).ifPresent(prop -> {
addColumn(property);
PopupForm<?> form = new PopupForm<>(beanBeanType, autoCreate);
if (beanProperties != null)
form.setProperties(beanProperties);
configureComponent(prop, form);
form.setLabel(Utils.formatName(prop.getName()));
});
form.addBeanProperty(property, beanBeanType, autoCreate,
beanProperties);
}
private void configureComponent(PropertyDefinition<T, ?> property,
Component component) {
Column<T> column = getColumnByKey(property.getName());
if (column == null || component == null) {
return;
}
if (component instanceof HasValue) {
HasValue<?, ?> hasValue = (HasValue) component;
if (property.getType().isEnum()) {
Class<Enum<?>> e = (Class<Enum<?>>) property.getType();
ComboBox<String> comp = (ComboBox<String>) component;
binder.forField(comp)
.withConverter(new StringToEnumConverter(e))
.withValidationStatusHandler(
handler -> validationStatusHandler(handler,
(HasValidation) component))
.bind(property.getName());
} else if (property.getType().isAssignableFrom(Date.class)) {
DateTimePicker comp = (DateTimePicker) component;
binder.forField(comp)
.withConverter(new LocalDateTimeToDateConverter(
ZoneId.systemDefault()))
.withValidationStatusHandler(
handler -> validationStatusHandler(handler,
(HasValidation) component))
.bind(property.getName());
} else {
binder.forField(hasValue)
.withValidationStatusHandler(
handler -> validationStatusHandler(handler,
(HasValidation) component))
.bind(property.getName());
}
}
if (component instanceof KeyNotifier) {
KeyNotifier keyNotifier = (KeyNotifier) component;
keyNotifier.addKeyDownListener(Key.ENTER, event -> {
getEditor().closeEditor();
});
keyNotifier.addKeyDownListener(Key.ESCAPE, event -> {
getEditor().closeEditor();
});
}
column.setEditorComponent(component);
if (component instanceof DateTimePicker) {
DateTimePicker picker = (DateTimePicker) component;
picker.addThemeName("small");
} else {
component.getElement().getThemeList().add("small");
}
component.getElement().getStyle().set("width", "100%");
}
private void validationStatusHandler(BindingValidationStatus<?> handler,
HasValidation hasValidation) {
handler.getMessage().ifPresent(message -> itemErrorLabel = message);
setDetailsVisible(binder.getBean(), handler.isError());
hasValidation.setInvalid(handler.isError());
if (handler.isError()) {
if (hasValidation instanceof Focusable) {
Focusable<?> focusable = (Focusable<?>) hasValidation;
focusable.focus();
}
}
}
@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
if (!responsive)
return;
// Add browser window listener to observe width change
getUI().ifPresent(ui -> browserListener = ui.getPage()
.addBrowserWindowResizeListener(event -> {
adjustVisibleGridColumns(event.getWidth());
}));
// Adjust Grid according to initial width of the screen
getUI().ifPresent(
ui -> ui.getPage().retrieveExtendedClientDetails(receiver -> {
int browserWidth = receiver.getBodyClientWidth();
adjustVisibleGridColumns(browserWidth);
}));
}
private void adjustVisibleGridColumns(int width) {
// Change which columns are visible depending on browser width
if (width > 1000) {
if (dialog.isOpened()) {
dialog.close();
getEditor().editItem(form.getValue());
}
getColumns().get(0).setVisible(false);
for (int c = 1; c < getColumns().size(); c++) {
getColumns().get(c).setVisible(true);
}
} else {
if (getEditor().isOpen()) {
form.setValue(getEditor().getItem());
dialog.open();
}
for (int c = 1; c < getColumns().size(); c++) {
getColumns().get(c).setVisible(false);
}
getColumns().get(0).setVisible(true);
}
}
@Override
protected void onDetach(DetachEvent detachEvent) {
// Listener needs to be eventually removed in order to avoid resource
// leak
if (responsive && browserListener != null) {
browserListener.remove();
}
super.onDetach(detachEvent);
}
}