Project

General

Profile

« Previous | Next » 

Revision 9b182365

Added by Andreas Kohlbecker about 7 years ago

porting the PopupEditor to spring and cdm

View differences:

src/main/java/com/vaadin/devday/ui/navigation/NavigationManagerBean.java
40 40
		popupMap = new HashMap<>();
41 41
	}
42 42

  
43
	//@Autowired
43
	@Autowired(required=false)
44 44
    private Map<String, PopupView> popupViews = null;
45 45

  
46 46
    /*
src/main/java/com/vaadin/devday/ui/view/AbstractPopupCdmEditor.java
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 com.vaadin.devday.ui.view;
10

  
11
import org.springframework.beans.factory.annotation.Autowired;
12

  
13
import com.vaadin.ui.AbstractOrderedLayout;
14

  
15
import eu.etaxonomy.cdm.api.application.CdmRepository;
16
import eu.etaxonomy.cdm.model.common.CdmBase;
17

  
18
/**
19
 * @author a.kohlbecker
20
 * @since Mar 1, 2017
21
 *
22
 */
23
public abstract class AbstractPopupCdmEditor<DTO extends CdmBase> extends AbstractPopupEditor<DTO> {
24

  
25
    private static final long serialVersionUID = -7293978747415508916L;
26

  
27
    @Autowired
28
    private CdmRepository repo;
29

  
30
    /**
31
     * @param layout
32
     * @param dtoType
33
     */
34
    public AbstractPopupCdmEditor(AbstractOrderedLayout layout, Class<DTO> dtoType) {
35
        super(layout, dtoType);
36
    }
37

  
38
    public AbstractPopupCdmEditor(Class<DTO> dtoType) {
39
        super(dtoType);
40
    }
41

  
42
}
src/main/java/com/vaadin/devday/ui/view/AbstractPopupEditor.java
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
}
src/main/java/com/vaadin/devday/ui/view/AbstractPopupEditor.java.hidden
1
package com.vaadin.devday.ui.view;
2

  
3
import javax.ejb.EJB;
4
import javax.inject.Inject;
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.service.AbstractDTOWithIdentity;
11
import com.vaadin.devday.service.DTOService;
12
import com.vaadin.devday.ui.view.DoneWithPopupEvent.Reason;
13
import com.vaadin.server.FontAwesome;
14
import com.vaadin.ui.AbstractOrderedLayout;
15
import com.vaadin.ui.Alignment;
16
import com.vaadin.ui.Button;
17
import com.vaadin.ui.CheckBox;
18
import com.vaadin.ui.Component;
19
import com.vaadin.ui.CustomComponent;
20
import com.vaadin.ui.Field;
21
import com.vaadin.ui.FormLayout;
22
import com.vaadin.ui.HorizontalLayout;
23
import com.vaadin.ui.Notification;
24
import com.vaadin.ui.Notification.Type;
25
import com.vaadin.ui.PopupDateField;
26
import com.vaadin.ui.TextField;
27
import com.vaadin.ui.VerticalLayout;
28
import com.vaadin.ui.themes.ValoTheme;
29

  
30
public abstract class AbstractPopupEditor<DTO extends AbstractDTOWithIdentity> extends CustomComponent
31
		implements PopupView {
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
	@EJB
41
	private DTOService dtoService;
42

  
43
	@Inject
44
	private javax.enterprise.event.Event<DoneWithPopupEvent> doneWithEditorEventSource;
45

  
46
	private HorizontalLayout buttonLayout;
47

  
48
	private Button save;
49

  
50
	private Button cancel;
51

  
52
	private class SaveHandler implements CommitHandler {
53
		private static final long serialVersionUID = 2047223089707080659L;
54

  
55
		@Override
56
		public void preCommit(CommitEvent commitEvent) throws CommitException {
57
		}
58

  
59
		@Override
60
		public void postCommit(CommitEvent commitEvent) throws CommitException {
61
			try {
62
				dtoService.storeDto(getBean());
63
				doneWithEditorEventSource.fire(new DoneWithPopupEvent(AbstractPopupEditor.this, Reason.SAVE));
64
			} catch (Exception e) {
65
				throw new CommitException("Failed to store data to backend", e);
66
			}
67
		}
68
	};
69

  
70
	public AbstractPopupEditor(Class<DTO> dtoType) {
71
		this(new FormLayout(), dtoType);
72
		fieldLayout.setMargin(true);
73
	}
74

  
75
	public AbstractPopupEditor(AbstractOrderedLayout layout, Class<DTO> dtoType) {
76
		setWidthUndefined();
77

  
78
		mainLayout = new VerticalLayout();
79
		mainLayout.setWidthUndefined();
80

  
81
		fieldGroup = new BeanFieldGroup<>(dtoType);
82
		fieldGroup.addCommitHandler(new SaveHandler());
83

  
84
		setCompositionRoot(mainLayout);
85

  
86
		fieldLayout = layout;
87
		fieldLayout.setWidthUndefined();
88
		fieldLayout.setSpacing(true);
89

  
90
		buttonLayout = new HorizontalLayout();
91
		buttonLayout.setStyleName(ValoTheme.WINDOW_BOTTOM_TOOLBAR);
92
		buttonLayout.setWidth(100, Unit.PERCENTAGE);
93
		buttonLayout.setSpacing(true);
94

  
95
		save = new Button("Save", FontAwesome.SAVE);
96
		save.setStyleName(ValoTheme.BUTTON_PRIMARY);
97
		save.addClickListener(e -> onSaveClicked());
98

  
99
		cancel = new Button("Cancel", FontAwesome.TRASH);
100
		cancel.addClickListener(e -> onCancelClicked());
101

  
102
		buttonLayout.addComponents(save, cancel);
103
		buttonLayout.setExpandRatio(save, 1);
104
		buttonLayout.setComponentAlignment(save, Alignment.TOP_RIGHT);
105
		buttonLayout.setComponentAlignment(cancel, Alignment.TOP_RIGHT);
106

  
107
		mainLayout.addComponents(fieldLayout, buttonLayout);
108
	}
109

  
110
	@Override
111
	public void setReadOnly(boolean readOnly) {
112
		super.setReadOnly(readOnly);
113
		save.setVisible(!readOnly);
114
		cancel.setCaption(readOnly ? "Close" : "Cancel");
115
	}
116

  
117
	protected VerticalLayout getMainLayout() {
118
		return mainLayout;
119
	}
120

  
121
	protected void addCommitHandler(CommitHandler commitHandler) {
122
		fieldGroup.addCommitHandler(commitHandler);
123
	}
124

  
125
	protected DTO getBean() {
126
		if (fieldGroup.getItemDataSource() != null) {
127
			return fieldGroup.getItemDataSource().getBean();
128
		}
129

  
130
		return null;
131
	}
132

  
133
	private void onCancelClicked() {
134
		fieldGroup.discard();
135
		doneWithEditorEventSource.fire(new DoneWithPopupEvent(this, Reason.CANCEL));
136
	}
137

  
138
	private void onSaveClicked() {
139
		try {
140
			fieldGroup.commit();
141
		} catch (CommitException e) {
142
			Notification.show("Error saving", Type.ERROR_MESSAGE);
143
		}
144
	}
145

  
146
	public void showInEditor(DTO beanToEdit) {
147
		fieldGroup.setItemDataSource(beanToEdit);
148
	}
149

  
150
	protected TextField addTextField(String caption, String propertyId) {
151
		return addField(new TextField(caption), propertyId);
152
	}
153

  
154
	protected PopupDateField addDateField(String caption, String propertyId) {
155
		return addField(new PopupDateField(caption), propertyId);
156
	}
157

  
158
	protected CheckBox addCheckBox(String caption, String propertyId) {
159
		return addField(new CheckBox(caption), propertyId);
160
	}
161

  
162
	protected <T extends Field> T addField(T field, String propertyId) {
163
		fieldGroup.bind(field, propertyId);
164
		fieldLayout.addComponent(field);
165
		return field;
166
	}
167

  
168
	protected void addComponent(Component component) {
169
		fieldLayout.addComponent(component);
170
	}
171

  
172
	protected void bindDesign(Component component) {
173
		fieldLayout.removeAllComponents();
174
		fieldGroup.bindMemberFields(component);
175
		fieldLayout.addComponent(component);
176
	}
177
}
src/main/java/eu/etaxonomy/cdm/vaadin/ui/RegistrationUI.java
17 17
import com.vaadin.annotations.Widgetset;
18 18
import com.vaadin.devday.ui.MainMenu;
19 19
import com.vaadin.devday.ui.UIInitializedEvent;
20
import com.vaadin.devday.ui.navigation.NavigationEvent;
20 21
import com.vaadin.navigator.ViewDisplay;
21 22
import com.vaadin.server.FontAwesome;
22 23
import com.vaadin.server.Responsive;
......
75 76
        mainMenu.addMenuItem(TestView2.NAME, FontAwesome.APPLE, TestView2.NAME);
76 77

  
77 78
        eventBus.publishEvent(new UIInitializedEvent());
79

  
80
        //navigate to initial view
81
        eventBus.publishEvent(new NavigationEvent(TestView1.NAME));
78 82
    }
79 83
}

Also available in: Unified diff