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