Project

General

Profile

Download (7.96 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.PermissionDebugUtils;
26
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
27
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
28
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
29
import eu.etaxonomy.vaadin.ui.UIInitializedEvent;
30
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
31
import eu.etaxonomy.vaadin.ui.view.PopupEditorFactory;
32
import eu.etaxonomy.vaadin.ui.view.PopupView;
33

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

    
37
	private static final long serialVersionUID = 6599898650948333853L;
38

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

    
41
	@Autowired
42
	private ViewDisplay viewDisplay;
43

    
44
	@Autowired
45
	private SpringViewProvider viewProvider;
46

    
47
	@Autowired
48
	private ViewChangeListener viewChangeListener;
49

    
50
	@Autowired
51
	private PojoEventListenerManager eventListenerManager;
52

    
53
	@Autowired
54
	private PopupEditorFactory popupEditorFactory;
55

    
56
	/**
57
	 * This reference will cause the scoped UserHelper being initialized
58
	 * It is not used in this class but attaches itself to the vaadin session
59
	 * from where it will be accessible via UserHelper.fromSession()
60
	 */
61
	@Autowired
62
    private UserHelper userHelper;
63

    
64
    /**
65
     * This reference will cause the scoped PermissionDebugUtils being initialized.
66
     * It is not used in this class but attaches itself to the vaadin session
67
     * from where it will be accessible via UserHelper.fromSession()
68
     *
69
     * <b>NOTE:</b> PermissionDebugUtils is only available if the spring profile "debug" is active,
70
     * See
71
     */
72
    @Autowired(required=false)
73
    private PermissionDebugUtils permissionDebugUtils;
74

    
75
	private Map<PopupView, Window> popupMap;
76

    
77
	private String defaultViewName = null;
78

    
79
    /*
80
     * Why UriFragmentManager must be initialized lazily:
81
     *
82
     * when the SpringVaadinServlet usually is being instantiated the ServletUIInitHandler(UIInitHandler).getBrowserDetailsUI(VaadinRequest, VaadinSession) method is called which will
83
     * 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
84
     * via Page.getCurrent() which is used in the UriFragmentManager constructor. The NavigationManagerBean is initialized with the WebapplicationContext, that is when the current ui is
85
     * not yet available, therefore the UriFragmentManager must be initialized lazily.
86
     */
87
    @Autowired
88
    @Lazy
89
	private UriFragmentManager uriFragmentManager;
90

    
91

    
92
//	public void setUriFragmentManager(UriFragmentManager uriFragmentManager) {
93
//	    this.uriFragmentManager = uriFragmentManager;
94
//	}
95

    
96
	@Autowired
97
	ApplicationEventPublisher eventBus;
98

    
99

    
100
	public NavigationManagerBean() {
101
	    popupMap = new HashMap<>();
102
	}
103

    
104

    
105
	private <P extends PopupView> PopupView findPopupView(Class<P> popupViewClass){
106
	    return popupEditorFactory.newPopupView(popupViewClass);
107
	}
108

    
109
	@EventListener
110
	protected void onUIInitialized(UIInitializedEvent e) {
111
		init(UI.getCurrent(), uriFragmentManager, viewDisplay);
112
		addViewChangeListener(viewChangeListener);
113
	}
114

    
115
	public void navigateTo(String navigationState, boolean fireNavigationEvent) {
116
	    if(navigationState == null){
117
            navigationState = defaultViewName;
118
        }
119
		if (fireNavigationEvent) {
120
			navigateTo(navigationState);
121
		} else {
122
			super.navigateTo(navigationState);
123
		}
124
	}
125

    
126
	@Override
127
	public void navigateTo(String navigationState) {
128
	    if(navigationState == null){
129
	        navigationState = defaultViewName;
130
	    }
131
		super.navigateTo(navigationState);
132
		//eventBus.publishEvent(new NavigationEvent(navigationState));
133
	}
134

    
135
	@EventListener
136
	protected void onNavigationEvent(NavigationEvent e) {
137
		navigateTo(e.getViewName(), false);
138
	}
139

    
140
	@Override
141
	public <T extends PopupView> T showInPopup(Class<T> popupType) {
142

    
143
	    PopupView popupView =  findPopupView(popupType); // TODO make better use of Optional
144

    
145
	    if(AbstractPopupEditor.class.isAssignableFrom(popupView.getClass())){
146
	        AbstractEditorPresenter presenter = ((AbstractPopupEditor)popupView).presenter();
147
	        eventListenerManager.addEventListeners(presenter);
148
	    }
149

    
150
		Window window = new Window();
151
		window.setCaption(popupView.getWindowCaption());
152
		window.center();
153
		window.setResizable(popupView.isResizable());
154
		// due to issue #6673 (https://dev.e-taxonomy.eu/redmine/issues/6673) popup editors must be modal!
155
		//window.setModal(popupView.isModal());
156
		window.setModal(true);
157
		window.setCaptionAsHtml(popupView.isWindowCaptionAsHtml());
158
		window.setWidth(popupView.getWindowPixelWidth(), Unit.PIXELS);
159
		// setting 100% as default height. If the height
160
		// would be undefined the window, will fit the size of
161
		// the content and will sometimes exceed the height of the
162
		// main window and will not get a scroll bar in this situation.
163
		// see #6843
164
		window.setHeight("100%");
165
		window.setContent(popupView.asComponent());
166
		// window.addCloseListener(e -> popupView.cancel());
167
		UI.getCurrent().addWindow(window);
168
		popupView.viewEntered();
169
		popupView.focusFirst();
170

    
171
		popupMap.put(popupView, window);
172

    
173
		return (T) popupView;
174
	}
175

    
176
    @EventListener
177
	protected void onDoneWithTheEditor(DoneWithPopupEvent e) {
178

    
179
		PopupView popup = e.getPopup();
180
        Window window = popupMap.get(popup);
181
		if (window != null) {
182
			window.close();
183
			popupMap.remove(popup);
184
		}
185
		if(AbstractPopupEditor.class.isAssignableFrom(popup.getClass())){
186
		    AbstractEditorPresenter presenter = ((AbstractPopupEditor)popup).presenter();
187
		    eventListenerManager.removeEventListeners(presenter);
188
		}
189

    
190
	}
191

    
192
    /**
193
     * {@inheritDoc}
194
     */
195
    @Override
196
    public void reloadCurrentView() {
197
        if(logger.isTraceEnabled()){
198
            logger.trace("reloading " + getState());
199
        }
200
        navigateTo(getState(), false);
201
    }
202

    
203
    /**
204
     * This method requires that the {@SpringView} annotation is used to ser the name of the <code>View</code>.
205
     *
206
     * @return the current view name or <code>null</code>
207
     */
208
    @Override
209
    public String getCurrentViewName() {
210
        if(getCurrentView() != null){
211
            SpringView springViewAnnotation = getCurrentView().getClass().getAnnotation(SpringView.class);
212
            if(springViewAnnotation != null){
213
                return springViewAnnotation.name();
214
            }
215
        }
216
        return null;
217
    }
218

    
219
    @Override
220
    public List<String> getCurrentViewParameters(){
221
        String substate = getState();
222
        String currentViewName = getCurrentViewName();
223
        if(currentViewName != null){
224
            substate = substate.replaceAll("^" + currentViewName + "/?", "");
225

    
226
        }
227
        return Arrays.asList(substate.split("/"));
228
    }
229

    
230
    /**
231
     * {@inheritDoc}
232
     */
233
    @Override
234
    public List<AbstractEditorPresenter<?, ?>> getPopupEditorPresenters() {
235
        // TODO Auto-generated method stub
236
        return null;
237
    }
238

    
239

    
240
    /**
241
     * @return the defaultViewName
242
     */
243
    public String getDefaultViewName() {
244
        return defaultViewName;
245
    }
246

    
247

    
248
    /**
249
     * @param defaultViewName the defaultViewName to set
250
     */
251
    public void setDefaultViewName(String defaultViewName) {
252
        this.defaultViewName = defaultViewName;
253
    }
254
}
(4-4/7)