Revision 92d4dbe0
Added by Andreas Kohlbecker almost 7 years ago
src/main/java/eu/etaxonomy/cdm/vaadin/view/reference/ReferenceEditorPresenter.java | ||
---|---|---|
12 | 12 |
import java.util.List; |
13 | 13 |
|
14 | 14 |
import org.apache.log4j.Logger; |
15 |
import org.springframework.context.annotation.Scope; |
|
16 | 15 |
import org.springframework.context.event.EventListener; |
17 | 16 |
import org.springframework.transaction.TransactionStatus; |
18 | 17 |
|
19 | 18 |
import com.vaadin.data.Item; |
20 | 19 |
import com.vaadin.data.util.BeanItemContainer; |
21 |
import com.vaadin.spring.annotation.SpringComponent; |
|
22 | 20 |
import com.vaadin.ui.ListSelect; |
23 | 21 |
|
24 | 22 |
import eu.etaxonomy.cdm.model.reference.Reference; |
... | ... | |
35 | 33 |
* @since Apr 5, 2017 |
36 | 34 |
* |
37 | 35 |
*/ |
38 |
@SpringComponent |
|
39 |
@Scope("prototype") |
|
40 | 36 |
public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Reference, ReferencePopupEditorView> { |
41 | 37 |
|
42 | 38 |
private static final long serialVersionUID = -7926116447719010837L; |
src/main/java/eu/etaxonomy/cdm/vaadin/view/reference/ReferencePopupEditor.java | ||
---|---|---|
11 | 11 |
import java.util.Arrays; |
12 | 12 |
import java.util.Collection; |
13 | 13 |
|
14 |
import org.springframework.context.annotation.Scope; |
|
15 | 14 |
import org.springframework.security.core.GrantedAuthority; |
16 | 15 |
|
17 | 16 |
import com.vaadin.data.util.BeanItemContainer; |
18 |
import com.vaadin.spring.annotation.SpringComponent; |
|
19 | 17 |
import com.vaadin.ui.Alignment; |
20 | 18 |
import com.vaadin.ui.GridLayout; |
21 | 19 |
import com.vaadin.ui.ListSelect; |
... | ... | |
37 | 35 |
* @since Apr 4, 2017 |
38 | 36 |
* |
39 | 37 |
*/ |
40 |
|
|
41 |
@SpringComponent |
|
42 |
@Scope("prototype") |
|
43 | 38 |
public class ReferencePopupEditor extends AbstractCdmPopupEditor<Reference, ReferenceEditorPresenter> implements ReferencePopupEditorView, AccessRestrictedView { |
44 | 39 |
|
45 | 40 |
private static final long serialVersionUID = -4347633563800758815L; |
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractPresenter.java | ||
---|---|---|
9 | 9 |
import org.springframework.security.core.context.SecurityContextHolder; |
10 | 10 |
import org.springframework.transaction.TransactionStatus; |
11 | 11 |
|
12 |
import com.vaadin.spring.annotation.SpringComponent; |
|
13 |
import com.vaadin.spring.annotation.ViewScope; |
|
14 |
|
|
15 | 12 |
import eu.etaxonomy.cdm.api.application.CdmRepository; |
16 | 13 |
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager; |
17 | 14 |
import eu.etaxonomy.vaadin.ui.navigation.NavigationManagerBean; |
... | ... | |
26 | 23 |
* @param <V> |
27 | 24 |
* type of the view this presenter governs |
28 | 25 |
*/ |
29 |
@SpringComponent |
|
30 |
@ViewScope |
|
31 | 26 |
public abstract class AbstractPresenter<V extends ApplicationView> implements Serializable { |
32 | 27 |
|
33 | 28 |
|
... | ... | |
124 | 119 |
return navigationManager; |
125 | 120 |
} |
126 | 121 |
|
122 |
/** |
|
123 |
* @param repo the repo to set |
|
124 |
*/ |
|
125 |
protected void setRepo(CdmRepository repo) { |
|
126 |
this.repo = repo; |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* @param navigationManager the navigationManager to set |
|
131 |
*/ |
|
132 |
protected void setNavigationManager(NavigationManager navigationManager) { |
|
133 |
this.navigationManager = navigationManager; |
|
134 |
} |
|
135 |
|
|
136 |
|
|
137 |
|
|
127 | 138 |
} |
src/main/java/eu/etaxonomy/vaadin/ui/navigation/NavigationManagerBean.java | ||
---|---|---|
1 | 1 |
package eu.etaxonomy.vaadin.ui.navigation; |
2 | 2 |
|
3 | 3 |
import java.util.Arrays; |
4 |
import java.util.Collection; |
|
5 | 4 |
import java.util.HashMap; |
6 |
import java.util.HashSet; |
|
7 | 5 |
import java.util.List; |
8 | 6 |
import java.util.Map; |
9 |
import java.util.Optional; |
|
10 | 7 |
|
11 | 8 |
import org.apache.log4j.Logger; |
12 | 9 |
import org.springframework.beans.factory.annotation.Autowired; |
... | ... | |
30 | 27 |
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor; |
31 | 28 |
import eu.etaxonomy.vaadin.ui.UIInitializedEvent; |
32 | 29 |
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent; |
30 |
import eu.etaxonomy.vaadin.ui.view.PopupEditorFactory; |
|
33 | 31 |
import eu.etaxonomy.vaadin.ui.view.PopupView; |
34 | 32 |
|
35 | 33 |
@UIScope |
... | ... | |
51 | 49 |
@Autowired |
52 | 50 |
private PojoEventListenerManager eventListenerManager; |
53 | 51 |
|
52 |
@Autowired |
|
53 |
private PopupEditorFactory popupEditorFactory; |
|
54 |
|
|
54 | 55 |
@Autowired |
55 | 56 |
private UserHelper userHelper; |
56 | 57 |
|
... | ... | |
60 | 61 |
popupMap = new HashMap<>(); |
61 | 62 |
} |
62 | 63 |
|
63 |
private Collection<PopupView> popupViews = new HashSet<>(); |
|
64 |
|
|
65 |
@Lazy |
|
66 |
@Autowired(required=false) |
|
67 |
private void popUpViews(Collection<PopupView> popupViews){ |
|
68 |
this.popupViews = popupViews; |
|
69 |
// popupViews.forEach(view -> this.popupViews.put(view.getClass(), view)); |
|
70 |
} |
|
71 | 64 |
|
72 |
private <P extends PopupView> Optional<PopupView> findPopupView(Class<P> type){
|
|
73 |
return popupViews.stream().filter(p -> p.getClass().equals(type)).findFirst();
|
|
65 |
private <P extends PopupView> PopupView findPopupView(Class<P> popupViewClass){
|
|
66 |
return popupEditorFactory.newPopupView(popupViewClass);
|
|
74 | 67 |
} |
75 | 68 |
|
76 | 69 |
/* |
... | ... | |
122 | 115 |
@Override |
123 | 116 |
public <T extends PopupView> T showInPopup(Class<T> popupType) { |
124 | 117 |
|
125 |
PopupView popupView = findPopupView(popupType).get(); // TODO make better use of Optional
|
|
118 |
PopupView popupView = findPopupView(popupType); // TODO make better use of Optional |
|
126 | 119 |
|
127 | 120 |
if(AbstractPopupEditor.class.isAssignableFrom(popupView.getClass())){ |
128 | 121 |
AbstractEditorPresenter presenter = ((AbstractPopupEditor)popupView).presenter(); |
src/main/java/eu/etaxonomy/vaadin/ui/view/PopupEditorFactory.java | ||
---|---|---|
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.vaadin.mvp.AbstractEditorPresenter; |
|
27 |
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor; |
|
28 |
import eu.etaxonomy.vaadin.mvp.AbstractPresenter; |
|
29 |
import eu.etaxonomy.vaadin.mvp.AbstractView; |
|
30 |
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager; |
|
31 |
|
|
32 |
/** |
|
33 |
* @author a.kohlbecker |
|
34 |
* @since May 30, 2017 |
|
35 |
* |
|
36 |
*/ |
|
37 |
@SpringComponent |
|
38 |
@UIScope |
|
39 |
public class PopupEditorFactory { |
|
40 |
|
|
41 |
|
|
42 |
@Autowired |
|
43 |
protected ApplicationEventPublisher eventBus; |
|
44 |
|
|
45 |
@Autowired |
|
46 |
@Qualifier("cdmRepository") |
|
47 |
private CdmRepository repo; |
|
48 |
|
|
49 |
@Autowired |
|
50 |
@Lazy |
|
51 |
private NavigationManager navigationManager; |
|
52 |
|
|
53 |
private Field presenterRepoField; |
|
54 |
private Field presenterNavigationManagerField; |
|
55 |
private Field presenterEventBusField; |
|
56 |
|
|
57 |
private Field viewEventBusField; |
|
58 |
private Method viewInjectPresenterMethod; |
|
59 |
|
|
60 |
private Method viewInitMethod; |
|
61 |
|
|
62 |
public PopupEditorFactory(){ |
|
63 |
initFieldsAccess(); |
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
/** |
|
68 |
* |
|
69 |
*/ |
|
70 |
private void initFieldsAccess() { |
|
71 |
|
|
72 |
try { |
|
73 |
presenterRepoField = AbstractPresenter.class.getDeclaredField("repo"); |
|
74 |
presenterRepoField.setAccessible(true); |
|
75 |
|
|
76 |
presenterNavigationManagerField = AbstractPresenter.class.getDeclaredField("navigationManager"); |
|
77 |
presenterNavigationManagerField.setAccessible(true); |
|
78 |
|
|
79 |
presenterEventBusField = AbstractEditorPresenter.class.getDeclaredField("eventBus"); |
|
80 |
presenterEventBusField.setAccessible(true); |
|
81 |
|
|
82 |
viewEventBusField = AbstractView.class.getDeclaredField("eventBus"); |
|
83 |
viewEventBusField.setAccessible(true); |
|
84 |
|
|
85 |
viewInjectPresenterMethod = AbstractView.class.getDeclaredMethod("injectPresenter", AbstractPresenter.class); |
|
86 |
viewInjectPresenterMethod.setAccessible(true); |
|
87 |
|
|
88 |
viewInitMethod = AbstractView.class.getDeclaredMethod("init"); |
|
89 |
viewInitMethod.setAccessible(true); |
|
90 |
|
|
91 |
} catch (NoSuchFieldException | SecurityException | NoSuchMethodException e) { |
|
92 |
throw new RuntimeException("Severe error during initialization. Please check the classes AbstractPresenter, AbstractEditorPresenter, AbstractView for modificactions.", e); |
|
93 |
} |
|
94 |
|
|
95 |
} |
|
96 |
|
|
97 |
|
|
98 |
/** |
|
99 |
* @param popupViewClass |
|
100 |
* @return |
|
101 |
*/ |
|
102 |
public <V extends PopupView, P extends AbstractPresenter> PopupView newPopupView(Class<V> popupViewClass) { |
|
103 |
|
|
104 |
Class<? extends AbstractPresenter<?>> prestenterClass = findGenericPresenterType(popupViewClass); |
|
105 |
try { |
|
106 |
|
|
107 |
P presenter = (P) prestenterClass.newInstance(); |
|
108 |
|
|
109 |
presenterRepoField.set(presenter, repo); |
|
110 |
presenterNavigationManagerField.set(presenter, navigationManager); |
|
111 |
|
|
112 |
if(AbstractEditorPresenter.class.isAssignableFrom(prestenterClass)){ |
|
113 |
presenterEventBusField.set(presenter, eventBus); |
|
114 |
} |
|
115 |
|
|
116 |
PopupView view = popupViewClass.newInstance(); |
|
117 |
if(AbstractView.class.isAssignableFrom(popupViewClass)){ |
|
118 |
AbstractView<P> abstractView = (AbstractView<P>)view; |
|
119 |
viewEventBusField.set(abstractView, eventBus); |
|
120 |
viewInjectPresenterMethod.invoke(abstractView, presenter); |
|
121 |
// invoke the @PostConstruct method |
|
122 |
viewInitMethod.invoke(abstractView); |
|
123 |
} |
|
124 |
return view; |
|
125 |
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
|
126 |
throw new RuntimeException(String.format("Error creating the view class '%s' with presenter class '%s'", popupViewClass, prestenterClass), e); |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* @param popupViewClass |
|
132 |
* @return |
|
133 |
*/ |
|
134 |
@SuppressWarnings("unchecked") |
|
135 |
private Class<? extends AbstractPresenter<?>> findGenericPresenterType(Class<? extends PopupView> popupViewClass) { |
|
136 |
|
|
137 |
ParameterizedType genericSuperClass = (ParameterizedType)popupViewClass.getGenericSuperclass(); |
|
138 |
Type[] typeArgs = genericSuperClass.getActualTypeArguments(); |
|
139 |
if(AbstractPopupEditor.class.isAssignableFrom(popupViewClass)){ |
|
140 |
return (Class<? extends AbstractPresenter<?>>) typeArgs[1]; |
|
141 |
} else { |
|
142 |
return (Class<? extends AbstractPresenter<?>>) typeArgs[0]; |
|
143 |
} |
|
144 |
} |
|
145 |
|
|
146 |
|
|
147 |
} |
Also available in: Unified diff
fix #6673 implementing the PopupEditorFactory and making all PopEditors and presenters POJOs