1
|
package com.vaadin.devday.ui.view;
|
2
|
|
3
|
import org.springframework.beans.factory.annotation.Autowired;
|
4
|
import org.springframework.context.ApplicationEventPublisher;
|
5
|
|
6
|
import com.vaadin.data.fieldgroup.BeanFieldGroup;
|
7
|
import com.vaadin.data.fieldgroup.FieldGroup.CommitEvent;
|
8
|
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
|
9
|
import com.vaadin.data.fieldgroup.FieldGroup.CommitHandler;
|
10
|
import com.vaadin.devday.ui.view.DoneWithPopupEvent.Reason;
|
11
|
import com.vaadin.server.FontAwesome;
|
12
|
import com.vaadin.ui.AbstractOrderedLayout;
|
13
|
import com.vaadin.ui.Alignment;
|
14
|
import com.vaadin.ui.Button;
|
15
|
import com.vaadin.ui.CheckBox;
|
16
|
import com.vaadin.ui.Component;
|
17
|
import com.vaadin.ui.CustomComponent;
|
18
|
import com.vaadin.ui.Field;
|
19
|
import com.vaadin.ui.FormLayout;
|
20
|
import com.vaadin.ui.HorizontalLayout;
|
21
|
import com.vaadin.ui.Notification;
|
22
|
import com.vaadin.ui.Notification.Type;
|
23
|
import com.vaadin.ui.PopupDateField;
|
24
|
import com.vaadin.ui.TextField;
|
25
|
import com.vaadin.ui.VerticalLayout;
|
26
|
import com.vaadin.ui.themes.ValoTheme;
|
27
|
|
28
|
public abstract class AbstractPopupEditor<DTO extends Object> extends CustomComponent
|
29
|
implements PopupView {
|
30
|
private static final long serialVersionUID = 1441816620197127918L;
|
31
|
|
32
|
private BeanFieldGroup<DTO> fieldGroup;
|
33
|
|
34
|
private VerticalLayout mainLayout;
|
35
|
|
36
|
private AbstractOrderedLayout fieldLayout;
|
37
|
|
38
|
@Autowired
|
39
|
ApplicationEventPublisher eventBus;
|
40
|
|
41
|
private HorizontalLayout buttonLayout;
|
42
|
|
43
|
private Button save;
|
44
|
|
45
|
private Button cancel;
|
46
|
|
47
|
private class SaveHandler implements CommitHandler {
|
48
|
private static final long serialVersionUID = 2047223089707080659L;
|
49
|
|
50
|
@Override
|
51
|
public void preCommit(CommitEvent commitEvent) throws CommitException {
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
public void postCommit(CommitEvent commitEvent) throws CommitException {
|
56
|
try {
|
57
|
AbstractPopupEditor.this.storeDto(getBean());
|
58
|
eventBus.publishEvent(new DoneWithPopupEvent(AbstractPopupEditor.this, Reason.SAVE));
|
59
|
} catch (Exception e) {
|
60
|
throw new CommitException("Failed to store data to backend", e);
|
61
|
}
|
62
|
}
|
63
|
}
|
64
|
|
65
|
public abstract void storeDto(DTO bean) throws CommitException;
|
66
|
|
67
|
public AbstractPopupEditor(Class<DTO> dtoType) {
|
68
|
this(new FormLayout(), dtoType);
|
69
|
fieldLayout.setMargin(true);
|
70
|
}
|
71
|
|
72
|
public AbstractPopupEditor(AbstractOrderedLayout layout, Class<DTO> dtoType) {
|
73
|
setWidthUndefined();
|
74
|
|
75
|
mainLayout = new VerticalLayout();
|
76
|
mainLayout.setWidthUndefined();
|
77
|
|
78
|
fieldGroup = new BeanFieldGroup<>(dtoType);
|
79
|
fieldGroup.addCommitHandler(new SaveHandler());
|
80
|
|
81
|
setCompositionRoot(mainLayout);
|
82
|
|
83
|
fieldLayout = layout;
|
84
|
fieldLayout.setWidthUndefined();
|
85
|
fieldLayout.setSpacing(true);
|
86
|
|
87
|
buttonLayout = new HorizontalLayout();
|
88
|
buttonLayout.setStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR);
|
89
|
buttonLayout.setWidth(100, Unit.PERCENTAGE);
|
90
|
buttonLayout.setSpacing(true);
|
91
|
|
92
|
save = new Button("Save", FontAwesome.SAVE);
|
93
|
save.setStyleName(ValoTheme.BUTTON_PRIMARY);
|
94
|
save.addClickListener(e -> onSaveClicked());
|
95
|
|
96
|
cancel = new Button("Cancel", FontAwesome.TRASH);
|
97
|
cancel.addClickListener(e -> onCancelClicked());
|
98
|
|
99
|
buttonLayout.addComponents(save, cancel);
|
100
|
buttonLayout.setExpandRatio(save, 1);
|
101
|
buttonLayout.setComponentAlignment(save, Alignment.TOP_RIGHT);
|
102
|
buttonLayout.setComponentAlignment(cancel, Alignment.TOP_RIGHT);
|
103
|
|
104
|
mainLayout.addComponents(fieldLayout, buttonLayout);
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public void setReadOnly(boolean readOnly) {
|
109
|
super.setReadOnly(readOnly);
|
110
|
save.setVisible(!readOnly);
|
111
|
cancel.setCaption(readOnly ? "Close" : "Cancel");
|
112
|
}
|
113
|
|
114
|
protected VerticalLayout getMainLayout() {
|
115
|
return mainLayout;
|
116
|
}
|
117
|
|
118
|
protected void addCommitHandler(CommitHandler commitHandler) {
|
119
|
fieldGroup.addCommitHandler(commitHandler);
|
120
|
}
|
121
|
|
122
|
protected DTO getBean() {
|
123
|
if (fieldGroup.getItemDataSource() != null) {
|
124
|
return fieldGroup.getItemDataSource().getBean();
|
125
|
}
|
126
|
|
127
|
return null;
|
128
|
}
|
129
|
|
130
|
private void onCancelClicked() {
|
131
|
fieldGroup.discard();
|
132
|
eventBus.publishEvent(new DoneWithPopupEvent(this, Reason.CANCEL));
|
133
|
}
|
134
|
|
135
|
private void onSaveClicked() {
|
136
|
try {
|
137
|
fieldGroup.commit();
|
138
|
} catch (CommitException e) {
|
139
|
Notification.show("Error saving", Type.ERROR_MESSAGE);
|
140
|
}
|
141
|
}
|
142
|
|
143
|
public void showInEditor(DTO beanToEdit) {
|
144
|
fieldGroup.setItemDataSource(beanToEdit);
|
145
|
}
|
146
|
|
147
|
protected TextField addTextField(String caption, String propertyId) {
|
148
|
return addField(new TextField(caption), propertyId);
|
149
|
}
|
150
|
|
151
|
protected PopupDateField addDateField(String caption, String propertyId) {
|
152
|
return addField(new PopupDateField(caption), propertyId);
|
153
|
}
|
154
|
|
155
|
protected CheckBox addCheckBox(String caption, String propertyId) {
|
156
|
return addField(new CheckBox(caption), propertyId);
|
157
|
}
|
158
|
|
159
|
protected <T extends Field> T addField(T field, String propertyId) {
|
160
|
fieldGroup.bind(field, propertyId);
|
161
|
fieldLayout.addComponent(field);
|
162
|
return field;
|
163
|
}
|
164
|
|
165
|
protected void addComponent(Component component) {
|
166
|
fieldLayout.addComponent(component);
|
167
|
}
|
168
|
|
169
|
protected void bindDesign(Component component) {
|
170
|
fieldLayout.removeAllComponents();
|
171
|
fieldGroup.bindMemberFields(component);
|
172
|
fieldLayout.addComponent(component);
|
173
|
}
|
174
|
}
|