1
|
package eu.etaxonomy.vaadin.mvp;
|
2
|
|
3
|
import javax.annotation.PostConstruct;
|
4
|
|
5
|
import org.apache.log4j.Logger;
|
6
|
import org.springframework.beans.BeansException;
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
import org.springframework.context.ApplicationContext;
|
9
|
import org.springframework.context.ApplicationContextAware;
|
10
|
import org.vaadin.spring.events.EventBus;
|
11
|
|
12
|
import com.vaadin.ui.CustomComponent;
|
13
|
|
14
|
import eu.etaxonomy.cdm.vaadin.permission.ReleasableResourcesView;
|
15
|
|
16
|
/**
|
17
|
* AbstractView is the base class of all MVP views. It takes care of finding
|
18
|
* appropriate presenter component for the view.
|
19
|
*
|
20
|
* @param
|
21
|
* <P>
|
22
|
* type of the presenter this view uses.
|
23
|
*
|
24
|
* @author Peter / Vaadin
|
25
|
* @param <V>
|
26
|
*/
|
27
|
@SuppressWarnings("serial")
|
28
|
public abstract class AbstractView<P extends AbstractPresenter> extends CustomComponent
|
29
|
implements ApplicationContextAware, ReleasableResourcesView {
|
30
|
|
31
|
|
32
|
public static final Logger logger = Logger.getLogger(AbstractView.class);
|
33
|
|
34
|
private P presenter;
|
35
|
|
36
|
private ApplicationContext applicationContext;
|
37
|
|
38
|
@Autowired
|
39
|
EventBus.ViewEventBus viewEventBus;
|
40
|
|
41
|
@SuppressWarnings("unchecked")
|
42
|
@PostConstruct
|
43
|
protected final void init() {
|
44
|
Logger.getLogger(getClass().getSimpleName()).info("View init");
|
45
|
if(!ApplicationView.class.isAssignableFrom(this.getClass())){
|
46
|
throw new RuntimeException("Any view bean must implement the ApplicationView interface: ViewBean ---> ViewInterface ---> ApplicationView");
|
47
|
}
|
48
|
|
49
|
initContent();
|
50
|
|
51
|
presenter.init((ApplicationView<P>) this);
|
52
|
|
53
|
onViewReady();
|
54
|
}
|
55
|
|
56
|
protected void setPresenter(P presenter) {
|
57
|
this.presenter = presenter;
|
58
|
}
|
59
|
|
60
|
@Autowired
|
61
|
protected final void injectPresenter(P presenter){
|
62
|
logger.trace(this.toString() + " injecting presenter " + presenter.toString());
|
63
|
setPresenter(presenter);
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public void detach() {
|
68
|
getPresenter().onViewExit();
|
69
|
super.detach();
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Initialize the Components of the View
|
74
|
*/
|
75
|
protected abstract void initContent();
|
76
|
|
77
|
/**
|
78
|
* This method is called after the content of the view and the presenter
|
79
|
* are initialized and ready.
|
80
|
*/
|
81
|
protected void onViewReady() {
|
82
|
logger.trace("View ready");
|
83
|
}
|
84
|
|
85
|
protected P getPresenter() {
|
86
|
return presenter;
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
91
|
this.applicationContext = applicationContext;
|
92
|
}
|
93
|
|
94
|
public EventBus.ViewEventBus getViewEventBus(){
|
95
|
return viewEventBus;
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public void releaseResourcesOnAccessDenied() {
|
100
|
getPresenter().onViewExit();
|
101
|
}
|
102
|
}
|