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 eu.etaxonomy.vaadin.mvp;
|
10
|
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.context.ApplicationEventPublisher;
|
13
|
import org.springframework.context.event.EventListener;
|
14
|
|
15
|
import com.vaadin.data.fieldgroup.BeanFieldGroup;
|
16
|
|
17
|
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction;
|
18
|
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
|
19
|
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
|
20
|
import eu.etaxonomy.vaadin.mvp.event.EditorViewEvent;
|
21
|
|
22
|
/**
|
23
|
* @author a.kohlbecker
|
24
|
* @since Apr 5, 2017
|
25
|
*
|
26
|
*/
|
27
|
public abstract class AbstractEditorPresenter<DTO extends Object, V extends ApplicationView<?>> extends AbstractPresenter<V> {
|
28
|
|
29
|
|
30
|
private static final long serialVersionUID = -6677074110764145236L;
|
31
|
|
32
|
@Autowired
|
33
|
protected ApplicationEventPublisher eventBus;
|
34
|
|
35
|
@EventListener
|
36
|
public void onEditorPreSaveEvent(EditorPreSaveEvent preSaveEvent){
|
37
|
if(!isFromOwnView(preSaveEvent)){
|
38
|
return;
|
39
|
}
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
*
|
44
|
* @param saveEvent
|
45
|
*/
|
46
|
@EventListener
|
47
|
public void onEditorSaveEvent(EditorSaveEvent saveEvent){
|
48
|
if(!isFromOwnView(saveEvent)){
|
49
|
return;
|
50
|
}
|
51
|
DTO bean = ((BeanFieldGroup<DTO>)saveEvent.getCommitEvent().getFieldBinder()).getItemDataSource().getBean();
|
52
|
saveBean(bean);
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* @param saveEvent
|
57
|
* @return
|
58
|
*/
|
59
|
protected boolean isFromOwnView(EditorViewEvent saveEvent) {
|
60
|
return saveEvent.getView().equals(getView());
|
61
|
}
|
62
|
|
63
|
protected Class<V> getViewType() {
|
64
|
return (Class<V>) super.getView().getClass();
|
65
|
}
|
66
|
|
67
|
protected boolean isFromOwnView(AbstractEditorAction action){
|
68
|
return action.getSourceView() != null && getView().equals(action.getSourceView());
|
69
|
}
|
70
|
|
71
|
protected abstract void saveBean(DTO bean);
|
72
|
|
73
|
}
|