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