Project

General

Profile

Download (2.35 KB) Statistics
| Branch: | Tag: | Revision:
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
public abstract class AbstractView<P extends AbstractPresenter> extends CustomComponent
26
		implements ApplicationContextAware {
27

    
28

    
29
    public static final Logger logger = Logger.getLogger(AbstractView.class);
30

    
31
	private P presenter;
32

    
33
	private ApplicationContext applicationContext;
34

    
35
    @Autowired
36
    protected ApplicationEventPublisher eventBus;
37

    
38
	@SuppressWarnings("unchecked")
39
    @PostConstruct
40
	protected final void init() {
41
		Logger.getLogger(getClass().getSimpleName()).info("View init");
42
		if(!ApplicationView.class.isAssignableFrom(this.getClass())){
43
		    throw new RuntimeException("Any view bean must implement the ApplicationView interface: ViewBean ---> ViewInterface ---> ApplicationView");
44
		}
45

    
46
		initContent();
47

    
48
		presenter.init((ApplicationView<P>) this);
49

    
50
		onViewReady();
51
	}
52

    
53
	protected void setPresenter(P presenter) {
54
		this.presenter = presenter;
55
	}
56

    
57
    @Autowired
58
	protected final void injectPresenter(P presenter){
59
        logger.trace(this.toString() + " injecting presenter " + presenter.toString());
60
	    setPresenter(presenter);
61
	}
62

    
63
	@Override
64
	public void detach() {
65
		getPresenter().onViewExit();
66
		super.detach();
67
	}
68

    
69
	/**
70
	 * Initialize the Components of the View
71
	 */
72
	protected abstract void initContent();
73

    
74
	/**
75
	 * This method is called after the content of the view and the presenter
76
	 * are initialized and ready.
77
	 */
78
	protected void onViewReady() {
79
	    logger.trace("View ready");
80
	}
81

    
82
	protected P getPresenter() {
83
		return presenter;
84
	}
85

    
86
	@Override
87
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
88
		this.applicationContext = applicationContext;
89
	}
90

    
91
	public ApplicationEventPublisher getEventBus(){
92
	    return eventBus;
93
	}
94
}
(7-7/8)