Project

General

Profile

Download (3.95 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.vaadin.ui.navigation;
2

    
3
import java.util.Collection;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.context.ApplicationEventPublisher;
9
import org.springframework.context.annotation.Lazy;
10
import org.springframework.context.event.EventListener;
11

    
12
import com.vaadin.navigator.ViewChangeListener;
13
import com.vaadin.navigator.ViewDisplay;
14
import com.vaadin.spring.annotation.UIScope;
15
import com.vaadin.spring.navigator.SpringNavigator;
16
import com.vaadin.spring.navigator.SpringViewProvider;
17
import com.vaadin.ui.UI;
18
import com.vaadin.ui.Window;
19

    
20
import eu.etaxonomy.vaadin.ui.NavigationManager;
21
import eu.etaxonomy.vaadin.ui.UIInitializedEvent;
22
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
23
import eu.etaxonomy.vaadin.ui.view.PopupView;
24

    
25
@UIScope
26
public class NavigationManagerBean extends SpringNavigator implements NavigationManager {
27

    
28
	private static final long serialVersionUID = 6599898650948333853L;
29

    
30
	@Autowired
31
	private ViewDisplay viewDisplay;
32

    
33
	@Autowired
34
	private SpringViewProvider viewProvider;
35

    
36
	@Autowired
37
	private ViewChangeListener viewChangeListener;
38

    
39
	private Map<PopupView, Window> popupMap;
40

    
41
	public NavigationManagerBean() {
42
		popupMap = new HashMap<>();
43
	}
44

    
45
    private Map<String, PopupView> popupViews = new HashMap<>();
46

    
47
    @Autowired(required=false)
48
	private void popUpViews(Collection<PopupView> popupViews){
49
        popupViews.forEach(view -> this.popupViews.put(view.getClass().getSimpleName(), view));
50
	}
51

    
52
    /*
53
     * Why UriFragmentManager must be initialized lazily:
54
     *
55
     * when the SpringVaadinServlet usually is being instantiated the ServletUIInitHandler(UIInitHandler).getBrowserDetailsUI(VaadinRequest, VaadinSession) method is called which will
56
     * 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
57
     * via Page.getCurrent() which is used in the UriFragmentManager constructor. The NavigationManagerBean is initialized with the WebapplicationContext, that is when the current ui is
58
     * not yet available, therefore the UriFragmentManager must be initialized lazily.
59
     */
60
    @Autowired
61
    @Lazy
62
	private UriFragmentManager uriFragmentManager;
63

    
64

    
65
//	public void setUriFragmentManager(UriFragmentManager uriFragmentManager) {
66
//	    this.uriFragmentManager = uriFragmentManager;
67
//	}
68

    
69
	@Autowired
70
	ApplicationEventPublisher eventBus;
71

    
72
	@EventListener
73
	protected void onUIInitialized(UIInitializedEvent e) {
74
		init(UI.getCurrent(), uriFragmentManager, viewDisplay);
75
		addProvider(viewProvider);
76
		addViewChangeListener(viewChangeListener);
77
	}
78

    
79
	public void navigateTo(String navigationState, boolean fireNavigationEvent) {
80
		if (fireNavigationEvent) {
81
			navigateTo(navigationState);
82
		} else {
83
			super.navigateTo(navigationState);
84
		}
85
	}
86

    
87
	@Override
88
	public void navigateTo(String navigationState) {
89
		super.navigateTo(navigationState);
90
		eventBus.publishEvent(new NavigationEvent(navigationState));
91
	}
92

    
93
	@EventListener
94
	protected void onNavigationEvent(NavigationEvent e) {
95
		navigateTo(e.getViewName(), false);
96
	}
97

    
98
	@Override
99
	public <T extends PopupView> T showInPopup(Class<T> popupType) {
100
		// Instance<T> instanceSelection = popupViewInstantiator.select(popupType);
101
		PopupView popupContent =  popupViews.get(popupType.getSimpleName()); // FIXME better scan the collection for the correct bean class
102

    
103
		Window window = new Window();
104
		window.setCaption(popupContent.getWindowCaption());
105
		window.center();
106
		window.setResizable(false);
107

    
108
		window.setContent(popupContent.asComponent());
109
		UI.getCurrent().addWindow(window);
110
		popupContent.focusFirst();
111

    
112
		popupMap.put(popupContent, window);
113

    
114
		return (T) popupContent;
115
	}
116

    
117
    @EventListener
118
	protected void onDoneWithTheEditor(DoneWithPopupEvent e) {
119
		Window window = popupMap.get(e.getPopup());
120
		if (window != null) {
121
			window.close();
122
			popupMap.remove(e.getPopup());
123
		}
124
	}
125
}
(3-3/6)