1
|
package com.vaadin.devday.ui.navigation;
|
2
|
|
3
|
import java.util.HashMap;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
7
|
import org.springframework.context.ApplicationEventPublisher;
|
8
|
import org.springframework.context.annotation.Lazy;
|
9
|
import org.springframework.context.event.EventListener;
|
10
|
|
11
|
import com.vaadin.devday.ui.NavigationManager;
|
12
|
import com.vaadin.devday.ui.UIInitializedEvent;
|
13
|
import com.vaadin.devday.ui.view.DoneWithPopupEvent;
|
14
|
import com.vaadin.devday.ui.view.PopupView;
|
15
|
import com.vaadin.navigator.ViewChangeListener;
|
16
|
import com.vaadin.navigator.ViewDisplay;
|
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
|
@UIScope
|
24
|
public class NavigationManagerBean extends SpringNavigator implements NavigationManager {
|
25
|
|
26
|
private static final long serialVersionUID = 6599898650948333853L;
|
27
|
|
28
|
@Autowired
|
29
|
private ViewDisplay viewDisplay;
|
30
|
|
31
|
@Autowired
|
32
|
private SpringViewProvider viewProvider;
|
33
|
|
34
|
@Autowired
|
35
|
private ViewChangeListener viewChangeListener;
|
36
|
|
37
|
private Map<PopupView, Window> popupMap;
|
38
|
|
39
|
public NavigationManagerBean() {
|
40
|
popupMap = new HashMap<>();
|
41
|
}
|
42
|
|
43
|
//@Autowired
|
44
|
private Map<String, PopupView> popupViews = null;
|
45
|
|
46
|
/*
|
47
|
* Why UriFragmentManager must be initialized lazily:
|
48
|
*
|
49
|
* when the SpringVaadinServlet usually is being instantiated the ServletUIInitHandler(UIInitHandler).getBrowserDetailsUI(VaadinRequest, VaadinSession) method is called which will
|
50
|
* 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
|
51
|
* via Page.getCurrent() which is used in the UriFragmentManager constructor. The NavigationManagerBean is initialized with the WebapplicationContext, that is when the current ui is
|
52
|
* not yet available, therefore the UriFragmentManager must be initialized lazily.
|
53
|
*/
|
54
|
@Autowired
|
55
|
@Lazy
|
56
|
private UriFragmentManager uriFragmentManager;
|
57
|
|
58
|
|
59
|
// public void setUriFragmentManager(UriFragmentManager uriFragmentManager) {
|
60
|
// this.uriFragmentManager = uriFragmentManager;
|
61
|
// }
|
62
|
|
63
|
@Autowired
|
64
|
ApplicationEventPublisher eventBus;
|
65
|
|
66
|
@EventListener
|
67
|
protected void onUIInitialized(UIInitializedEvent e) {
|
68
|
init(UI.getCurrent(), uriFragmentManager, viewDisplay);
|
69
|
addProvider(viewProvider);
|
70
|
addViewChangeListener(viewChangeListener);
|
71
|
}
|
72
|
|
73
|
public void navigateTo(String navigationState, boolean fireNavigationEvent) {
|
74
|
if (fireNavigationEvent) {
|
75
|
navigateTo(navigationState);
|
76
|
} else {
|
77
|
super.navigateTo(navigationState);
|
78
|
}
|
79
|
}
|
80
|
|
81
|
@Override
|
82
|
public void navigateTo(String navigationState) {
|
83
|
super.navigateTo(navigationState);
|
84
|
eventBus.publishEvent(new NavigationEvent(navigationState));
|
85
|
}
|
86
|
|
87
|
@EventListener
|
88
|
protected void onNavigationEvent(NavigationEvent e) {
|
89
|
navigateTo(e.getViewName(), false);
|
90
|
}
|
91
|
|
92
|
@Override
|
93
|
public <T extends PopupView> T showInPopup(Class<T> popupType) {
|
94
|
// Instance<T> instanceSelection = popupViewInstantiator.select(popupType);
|
95
|
PopupView popupContent = popupViews.get(popupType.getSimpleName()); // FIXME better scan the collection for the correct bean class
|
96
|
|
97
|
Window window = new Window();
|
98
|
window.setCaption(popupContent.getWindowCaption());
|
99
|
window.center();
|
100
|
window.setResizable(false);
|
101
|
|
102
|
window.setContent(popupContent.asComponent());
|
103
|
UI.getCurrent().addWindow(window);
|
104
|
popupContent.focusFirst();
|
105
|
|
106
|
popupMap.put(popupContent, window);
|
107
|
|
108
|
return (T) popupContent;
|
109
|
}
|
110
|
|
111
|
@EventListener
|
112
|
protected void onDoneWithTheEditor(DoneWithPopupEvent e) {
|
113
|
Window window = popupMap.get(e.getPopup());
|
114
|
if (window != null) {
|
115
|
window.close();
|
116
|
popupMap.remove(e.getPopup());
|
117
|
}
|
118
|
}
|
119
|
}
|