Project

General

Profile

Download (2.79 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.DisposableBean;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.context.ApplicationContext;
10
import org.springframework.context.ApplicationContextAware;
11
import org.vaadin.spring.events.EventBus;
12

    
13
import com.vaadin.ui.CustomComponent;
14

    
15
import eu.etaxonomy.cdm.vaadin.permission.ReleasableResourcesView;
16

    
17
/**
18
 * AbstractView is the base class of all MVP views. It takes care of finding
19
 * appropriate presenter component for the view.
20
 *
21
 * @param
22
 * 			<P>
23
 *            type of the presenter this view uses.
24
 *
25
 * @author Peter / Vaadin
26
 * @param <V>
27
 */
28
@SuppressWarnings("serial")
29
public abstract class AbstractView<P extends AbstractPresenter> extends CustomComponent
30
		implements ApplicationContextAware, ReleasableResourcesView, DisposableBean {
31

    
32

    
33
    public static final Logger logger = Logger.getLogger(AbstractView.class);
34

    
35
	private P presenter;
36

    
37
	private ApplicationContext applicationContext;
38

    
39
    @Autowired
40
    EventBus.ViewEventBus viewEventBus;
41

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

    
50
		initContent();
51

    
52
		presenter.init((ApplicationView<P>) this);
53

    
54
		onViewReady();
55
	}
56

    
57
	protected void setPresenter(P presenter) {
58
		this.presenter = presenter;
59
	}
60

    
61
    @Autowired
62
	protected final void injectPresenter(P presenter){
63
        logger.trace(this.toString() + " injecting presenter " + presenter.toString());
64
	    setPresenter(presenter);
65
	}
66

    
67
	@Override
68
	public void detach() {
69
		getPresenter().onViewExit();
70
		super.detach();
71
	}
72

    
73
	/**
74
	 * Initialize the Components of the View
75
	 */
76
	protected abstract void initContent();
77

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

    
86
	protected P getPresenter() {
87
		return presenter;
88
	}
89

    
90
	@Override
91
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
92
		this.applicationContext = applicationContext;
93
	}
94

    
95
	public EventBus.ViewEventBus getViewEventBus(){
96
	    return viewEventBus;
97
	}
98

    
99
   @Override
100
    public void releaseResourcesOnAccessDenied() {
101
        getPresenter().onViewExit();
102
    }
103

    
104
    /**
105
     * {@inheritDoc}
106
     */
107
    @Override
108
    public void destroy() throws Exception {
109
       if(presenter != null){
110
           presenter.destroy();
111
       }
112
    }
113

    
114

    
115
}
(9-9/14)