Project

General

Profile

Download (5.59 KB) Statistics
| Branch: | Tag: | Revision:
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.ui.view;
10

    
11
import java.lang.reflect.Field;
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.lang.reflect.ParameterizedType;
15
import java.lang.reflect.Type;
16

    
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Qualifier;
19
import org.springframework.context.ApplicationEventPublisher;
20
import org.springframework.context.annotation.Lazy;
21

    
22
import com.vaadin.spring.annotation.SpringComponent;
23
import com.vaadin.spring.annotation.UIScope;
24

    
25
import eu.etaxonomy.cdm.api.application.CdmRepository;
26
import eu.etaxonomy.cdm.vaadin.component.SelectFieldFactory;
27
import eu.etaxonomy.vaadin.mvp.AbstractCdmPopupEditor;
28
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
29
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
30
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
31
import eu.etaxonomy.vaadin.mvp.AbstractView;
32
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
33

    
34
/**
35
 * @author a.kohlbecker
36
 * @since May 30, 2017
37
 *
38
 */
39
@SpringComponent
40
@UIScope
41
public class PopupEditorFactory {
42

    
43

    
44
    @Autowired
45
    protected ApplicationEventPublisher eventBus;
46

    
47
    @Autowired
48
    @Qualifier("cdmRepository")
49
    private CdmRepository repo;
50

    
51
    @Autowired
52
    private SelectFieldFactory selectFieldFactory;
53

    
54
    @Autowired
55
    @Lazy
56
    private NavigationManager navigationManager;
57

    
58
    private Field presenterRepoField;
59
    private Field presenterNavigationManagerField;
60
    private Field presenterEventBusField;
61

    
62
    private Field viewEventBusField;
63
    private Method viewInjectPresenterMethod;
64

    
65
    private Method viewInitMethod;
66

    
67
    private Field selectFieldFactoryField;
68

    
69
    public PopupEditorFactory(){
70
        initFieldsAccess();
71
    }
72

    
73

    
74
    /**
75
     *
76
     */
77
    private void initFieldsAccess() {
78

    
79
        try {
80
            presenterRepoField = AbstractPresenter.class.getDeclaredField("repo");
81
            presenterRepoField.setAccessible(true);
82

    
83
            presenterNavigationManagerField = AbstractPresenter.class.getDeclaredField("navigationManager");
84
            presenterNavigationManagerField.setAccessible(true);
85

    
86
            presenterEventBusField = AbstractEditorPresenter.class.getDeclaredField("eventBus");
87
            presenterEventBusField.setAccessible(true);
88

    
89
            viewEventBusField = AbstractView.class.getDeclaredField("eventBus");
90
            viewEventBusField.setAccessible(true);
91

    
92
            viewInjectPresenterMethod = AbstractView.class.getDeclaredMethod("injectPresenter", AbstractPresenter.class);
93
            viewInjectPresenterMethod.setAccessible(true);
94

    
95
            viewInitMethod = AbstractView.class.getDeclaredMethod("init");
96
            viewInitMethod.setAccessible(true);
97

    
98
            selectFieldFactoryField = AbstractCdmPopupEditor.class.getDeclaredField("selectFieldFactory");
99
            selectFieldFactoryField.setAccessible(true);
100

    
101
        } catch (NoSuchFieldException | SecurityException | NoSuchMethodException  e) {
102
            throw new RuntimeException("Severe error during initialization. Please check the classes AbstractPresenter, AbstractEditorPresenter, AbstractView for modificactions.", e);
103
        }
104

    
105
    }
106

    
107

    
108
    /**
109
     * @param popupViewClass
110
     * @return
111
     */
112
    public <V extends PopupView, P extends AbstractPresenter> PopupView newPopupView(Class<V> popupViewClass) {
113

    
114
        Class<? extends AbstractPresenter<?>> prestenterClass = findGenericPresenterType(popupViewClass);
115
        try {
116

    
117
            P presenter = (P) prestenterClass.newInstance();
118

    
119
            presenterRepoField.set(presenter, repo);
120
            presenterNavigationManagerField.set(presenter, navigationManager);
121

    
122
            if(AbstractEditorPresenter.class.isAssignableFrom(prestenterClass)){
123
                presenterEventBusField.set(presenter, eventBus);
124
            }
125

    
126
            PopupView view = popupViewClass.newInstance();
127
            if(AbstractView.class.isAssignableFrom(popupViewClass)){
128
                AbstractView<P> abstractView = (AbstractView<P>)view;
129
                viewEventBusField.set(abstractView, eventBus);
130
                viewInjectPresenterMethod.invoke(abstractView, presenter);
131
                // invoke the @PostConstruct method
132

    
133
                if(AbstractCdmPopupEditor.class.isAssignableFrom(popupViewClass)){
134
                    selectFieldFactoryField.set(view, selectFieldFactory);
135
                }
136

    
137
                viewInitMethod.invoke(abstractView);
138
            }
139
            return view;
140
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
141
            throw new RuntimeException(String.format("Error creating the view class '%s' with presenter class '%s'", popupViewClass, prestenterClass), e);
142
        }
143
    }
144

    
145
    /**
146
     * @param popupViewClass
147
     * @return
148
     */
149
    @SuppressWarnings("unchecked")
150
    private Class<? extends AbstractPresenter<?>> findGenericPresenterType(Class<?  extends PopupView> popupViewClass) {
151

    
152
        ParameterizedType genericSuperClass = (ParameterizedType)popupViewClass.getGenericSuperclass();
153
        Type[] typeArgs = genericSuperClass.getActualTypeArguments();
154
        if(AbstractPopupEditor.class.isAssignableFrom(popupViewClass)){
155
           return (Class<? extends AbstractPresenter<?>>) typeArgs[1];
156
        } else {
157
           return (Class<? extends AbstractPresenter<?>>) typeArgs[0];
158
        }
159
    }
160

    
161

    
162
}
(2-2/4)