Project

General

Profile

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

    
3
import java.util.Arrays;
4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Map;
7

    
8
import org.apache.log4j.Logger;
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
import org.springframework.context.event.PojoEventListenerManager;
14

    
15
import com.vaadin.navigator.ViewChangeListener;
16
import com.vaadin.navigator.ViewDisplay;
17
import com.vaadin.server.Sizeable.Unit;
18
import com.vaadin.spring.annotation.SpringView;
19
import com.vaadin.spring.annotation.UIScope;
20
import com.vaadin.spring.navigator.SpringNavigator;
21
import com.vaadin.spring.navigator.SpringViewProvider;
22
import com.vaadin.ui.UI;
23
import com.vaadin.ui.Window;
24

    
25
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
26
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
27
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
28
import eu.etaxonomy.vaadin.ui.UIInitializedEvent;
29
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
30
import eu.etaxonomy.vaadin.ui.view.PopupEditorFactory;
31
import eu.etaxonomy.vaadin.ui.view.PopupView;
32

    
33
@UIScope
34
public class NavigationManagerBean extends SpringNavigator implements NavigationManager {
35

    
36
	private static final long serialVersionUID = 6599898650948333853L;
37

    
38
	private final static Logger logger = Logger.getLogger(NavigationManagerBean.class);
39

    
40
	@Autowired
41
	private ViewDisplay viewDisplay;
42

    
43
	@Autowired
44
	private SpringViewProvider viewProvider;
45

    
46
	@Autowired
47
	private ViewChangeListener viewChangeListener;
48

    
49
	@Autowired
50
	private PojoEventListenerManager eventListenerManager;
51

    
52
	@Autowired
53
	private PopupEditorFactory popupEditorFactory;
54

    
55
	@Autowired
56
    private UserHelper userHelper;
57

    
58
	private Map<PopupView, Window> popupMap;
59

    
60
	public NavigationManagerBean() {
61
		popupMap = new HashMap<>();
62
	}
63

    
64

    
65
    private <P extends PopupView> PopupView findPopupView(Class<P> popupViewClass){
66
        return popupEditorFactory.newPopupView(popupViewClass);
67
    }
68

    
69
    /*
70
     * Why UriFragmentManager must be initialized lazily:
71
     *
72
     * when the SpringVaadinServlet usually is being instantiated the ServletUIInitHandler(UIInitHandler).getBrowserDetailsUI(VaadinRequest, VaadinSession) method is called which will
73
     * 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
74
     * via Page.getCurrent() which is used in the UriFragmentManager constructor. The NavigationManagerBean is initialized with the WebapplicationContext, that is when the current ui is
75
     * not yet available, therefore the UriFragmentManager must be initialized lazily.
76
     */
77
    @Autowired
78
    @Lazy
79
	private UriFragmentManager uriFragmentManager;
80

    
81

    
82
//	public void setUriFragmentManager(UriFragmentManager uriFragmentManager) {
83
//	    this.uriFragmentManager = uriFragmentManager;
84
//	}
85

    
86
	@Autowired
87
	ApplicationEventPublisher eventBus;
88

    
89
	@EventListener
90
	protected void onUIInitialized(UIInitializedEvent e) {
91
		init(UI.getCurrent(), uriFragmentManager, viewDisplay);
92
		addProvider(viewProvider);
93
		addViewChangeListener(viewChangeListener);
94
	}
95

    
96
	public void navigateTo(String navigationState, boolean fireNavigationEvent) {
97
		if (fireNavigationEvent) {
98
			navigateTo(navigationState);
99
		} else {
100
			super.navigateTo(navigationState);
101
		}
102
	}
103

    
104
	@Override
105
	public void navigateTo(String navigationState) {
106
		super.navigateTo(navigationState);
107
		eventBus.publishEvent(new NavigationEvent(navigationState));
108
	}
109

    
110
	@EventListener
111
	protected void onNavigationEvent(NavigationEvent e) {
112
		navigateTo(e.getViewName(), false);
113
	}
114

    
115
	@Override
116
	public <T extends PopupView> T showInPopup(Class<T> popupType) {
117

    
118
	    PopupView popupView =  findPopupView(popupType); // TODO make better use of Optional
119

    
120
	    if(AbstractPopupEditor.class.isAssignableFrom(popupView.getClass())){
121
	        AbstractEditorPresenter presenter = ((AbstractPopupEditor)popupView).presenter();
122
	        eventListenerManager.addEventListeners(presenter);
123
	    }
124

    
125
		Window window = new Window();
126
		window.setCaption(popupView.getWindowCaption());
127
		window.center();
128
		window.setResizable(popupView.isResizable());
129
		// due to issue #6673 (https://dev.e-taxonomy.eu/redmine/issues/6673) popup editors must be modal!
130
		//window.setModal(popupView.isModal());
131
		window.setModal(true);
132
		window.setCaptionAsHtml(popupView.isWindowCaptionAsHtml());
133
		window.setWidth(popupView.getWindowPixelWidth(), Unit.PIXELS);
134
		window.setContent(popupView.asComponent());
135
		window.addCloseListener(e -> popupView.cancel());
136
		UI.getCurrent().addWindow(window);
137
		popupView.viewEntered();
138
		popupView.focusFirst();
139

    
140
		popupMap.put(popupView, window);
141

    
142
		return (T) popupView;
143
	}
144

    
145
    @EventListener
146
	protected void onDoneWithTheEditor(DoneWithPopupEvent e) {
147

    
148
		PopupView popup = e.getPopup();
149
        Window window = popupMap.get(popup);
150
		if (window != null) {
151
			window.close();
152
			popupMap.remove(popup);
153
		}
154
		if(AbstractPopupEditor.class.isAssignableFrom(popup.getClass())){
155
		    AbstractEditorPresenter presenter = ((AbstractPopupEditor)popup).presenter();
156
		    eventListenerManager.removeEventListeners(presenter);
157
		}
158
	}
159

    
160
    /**
161
     * {@inheritDoc}
162
     */
163
    @Override
164
    public void reloadCurrentView() {
165
        if(logger.isTraceEnabled()){
166
            logger.trace("reloading " + getState());
167
        }
168
        navigateTo(getState(), false);
169
    }
170

    
171
    /**
172
     * This method requires that the {@SpringView} annotation is used to ser the name of the <code>View</code>.
173
     *
174
     * @return the current view name or <code>null</code>
175
     */
176
    public String getCurrentViewName() {
177
        SpringView springViewAnnotation = getCurrentView().getClass().getAnnotation(SpringView.class);
178
        if(springViewAnnotation != null){
179
            return springViewAnnotation.name();
180
        }
181
        return null;
182
    }
183

    
184
    @Override
185
    public List<String> getCurrentViewParameters(){
186
        String substate = getState();
187
        String currentViewName = getCurrentViewName();
188
        if(currentViewName != null){
189
            substate = substate.replaceAll("^" + currentViewName + "/?", "");
190

    
191
        }
192
        return Arrays.asList(substate.split("/"));
193
    }
194

    
195
    /**
196
     * {@inheritDoc}
197
     */
198
    @Override
199
    public List<AbstractEditorPresenter<?, ?>> getPopupEditorPresenters() {
200
        // TODO Auto-generated method stub
201
        return null;
202
    }
203
}
(4-4/7)