1
|
package eu.etaxonomy.vaadin.ui.navigation;
|
2
|
|
3
|
import java.util.Collection;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.HashSet;
|
6
|
import java.util.Map;
|
7
|
import java.util.Optional;
|
8
|
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.context.ApplicationEventPublisher;
|
11
|
import org.springframework.context.annotation.Lazy;
|
12
|
import org.springframework.context.event.EventListener;
|
13
|
|
14
|
import com.vaadin.navigator.ViewChangeListener;
|
15
|
import com.vaadin.navigator.ViewDisplay;
|
16
|
import com.vaadin.server.Sizeable.Unit;
|
17
|
import com.vaadin.spring.annotation.UIScope;
|
18
|
import com.vaadin.spring.navigator.SpringNavigator;
|
19
|
import com.vaadin.spring.navigator.SpringViewProvider;
|
20
|
import com.vaadin.ui.UI;
|
21
|
import com.vaadin.ui.Window;
|
22
|
|
23
|
import eu.etaxonomy.vaadin.ui.UIInitializedEvent;
|
24
|
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
|
25
|
import eu.etaxonomy.vaadin.ui.view.PopupView;
|
26
|
|
27
|
@UIScope
|
28
|
public class NavigationManagerBean extends SpringNavigator implements NavigationManager {
|
29
|
|
30
|
private static final long serialVersionUID = 6599898650948333853L;
|
31
|
|
32
|
@Autowired
|
33
|
private ViewDisplay viewDisplay;
|
34
|
|
35
|
@Autowired
|
36
|
private SpringViewProvider viewProvider;
|
37
|
|
38
|
@Autowired
|
39
|
private ViewChangeListener viewChangeListener;
|
40
|
|
41
|
private Map<PopupView, Window> popupMap;
|
42
|
|
43
|
public NavigationManagerBean() {
|
44
|
popupMap = new HashMap<>();
|
45
|
}
|
46
|
|
47
|
private Collection<PopupView> popupViews = new HashSet<>();
|
48
|
|
49
|
@Lazy
|
50
|
@Autowired(required=false)
|
51
|
private void popUpViews(Collection<PopupView> popupViews){
|
52
|
this.popupViews = popupViews;
|
53
|
// popupViews.forEach(view -> this.popupViews.put(view.getClass(), view));
|
54
|
}
|
55
|
|
56
|
private <P extends PopupView> Optional<PopupView> findPopupView(Class<P> type){
|
57
|
return popupViews.stream().filter(p -> p.getClass().equals(type)).findFirst();
|
58
|
}
|
59
|
|
60
|
/*
|
61
|
* Why UriFragmentManager must be initialized lazily:
|
62
|
*
|
63
|
* when the SpringVaadinServlet usually is being instantiated the ServletUIInitHandler(UIInitHandler).getBrowserDetailsUI(VaadinRequest, VaadinSession) method is called which will
|
64
|
* first cause the WebapplicationContext being created. Once this is done the initialization of the UI classes is completed. This means that the UI classes are not readily available
|
65
|
* via Page.getCurrent() which is used in the UriFragmentManager constructor. The NavigationManagerBean is initialized with the WebapplicationContext, that is when the current ui is
|
66
|
* not yet available, therefore the UriFragmentManager must be initialized lazily.
|
67
|
*/
|
68
|
@Autowired
|
69
|
@Lazy
|
70
|
private UriFragmentManager uriFragmentManager;
|
71
|
|
72
|
|
73
|
// public void setUriFragmentManager(UriFragmentManager uriFragmentManager) {
|
74
|
// this.uriFragmentManager = uriFragmentManager;
|
75
|
// }
|
76
|
|
77
|
@Autowired
|
78
|
ApplicationEventPublisher eventBus;
|
79
|
|
80
|
@EventListener
|
81
|
protected void onUIInitialized(UIInitializedEvent e) {
|
82
|
init(UI.getCurrent(), uriFragmentManager, viewDisplay);
|
83
|
addProvider(viewProvider);
|
84
|
addViewChangeListener(viewChangeListener);
|
85
|
}
|
86
|
|
87
|
public void navigateTo(String navigationState, boolean fireNavigationEvent) {
|
88
|
if (fireNavigationEvent) {
|
89
|
navigateTo(navigationState);
|
90
|
} else {
|
91
|
super.navigateTo(navigationState);
|
92
|
}
|
93
|
}
|
94
|
|
95
|
@Override
|
96
|
public void navigateTo(String navigationState) {
|
97
|
super.navigateTo(navigationState);
|
98
|
eventBus.publishEvent(new NavigationEvent(navigationState));
|
99
|
}
|
100
|
|
101
|
@EventListener
|
102
|
protected void onNavigationEvent(NavigationEvent e) {
|
103
|
navigateTo(e.getViewName(), false);
|
104
|
}
|
105
|
|
106
|
@Override
|
107
|
public <T extends PopupView> T showInPopup(Class<T> popupType) {
|
108
|
|
109
|
PopupView popupContent = findPopupView(popupType).get(); // TODO make better use of Optional
|
110
|
|
111
|
Window window = new Window();
|
112
|
window.setCaption(popupContent.getWindowCaption());
|
113
|
window.center();
|
114
|
window.setResizable(popupContent.isResizable());
|
115
|
window.setModal(popupContent.isModal());
|
116
|
window.setCaptionAsHtml(popupContent.isWindowCaptionAsHtml());
|
117
|
window.setWidth(popupContent.getWindowPixelWidth(), Unit.PIXELS);
|
118
|
window.setContent(popupContent.asComponent());
|
119
|
UI.getCurrent().addWindow(window);
|
120
|
popupContent.focusFirst();
|
121
|
|
122
|
popupMap.put(popupContent, window);
|
123
|
|
124
|
return (T) popupContent;
|
125
|
}
|
126
|
|
127
|
@EventListener
|
128
|
protected void onDoneWithTheEditor(DoneWithPopupEvent e) {
|
129
|
Window window = popupMap.get(e.getPopup());
|
130
|
if (window != null) {
|
131
|
window.close();
|
132
|
popupMap.remove(e.getPopup());
|
133
|
}
|
134
|
}
|
135
|
}
|