Project

General

Profile

Download (7.38 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
	public NavigationManagerBean() {
78
		popupMap = new HashMap<>();
79
	}
80

    
81

    
82
    private <P extends PopupView> PopupView findPopupView(Class<P> popupViewClass){
83
        return popupEditorFactory.newPopupView(popupViewClass);
84
    }
85

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

    
98

    
99
//	public void setUriFragmentManager(UriFragmentManager uriFragmentManager) {
100
//	    this.uriFragmentManager = uriFragmentManager;
101
//	}
102

    
103
	@Autowired
104
	ApplicationEventPublisher eventBus;
105

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

    
112
	public void navigateTo(String navigationState, boolean fireNavigationEvent) {
113
		if (fireNavigationEvent) {
114
			navigateTo(navigationState);
115
		} else {
116
			super.navigateTo(navigationState);
117
		}
118
	}
119

    
120
	@Override
121
	public void navigateTo(String navigationState) {
122
		super.navigateTo(navigationState);
123
		//eventBus.publishEvent(new NavigationEvent(navigationState));
124
	}
125

    
126
	@EventListener
127
	protected void onNavigationEvent(NavigationEvent e) {
128
		navigateTo(e.getViewName(), false);
129
	}
130

    
131
	@Override
132
	public <T extends PopupView> T showInPopup(Class<T> popupType) {
133

    
134
	    PopupView popupView =  findPopupView(popupType); // TODO make better use of Optional
135

    
136
	    if(AbstractPopupEditor.class.isAssignableFrom(popupView.getClass())){
137
	        AbstractEditorPresenter presenter = ((AbstractPopupEditor)popupView).presenter();
138
	        eventListenerManager.addEventListeners(presenter);
139
	    }
140

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

    
162
		popupMap.put(popupView, window);
163

    
164
		return (T) popupView;
165
	}
166

    
167
    @EventListener
168
	protected void onDoneWithTheEditor(DoneWithPopupEvent e) {
169

    
170
		PopupView popup = e.getPopup();
171
        Window window = popupMap.get(popup);
172
		if (window != null) {
173
			window.close();
174
			popupMap.remove(popup);
175
		}
176
		if(AbstractPopupEditor.class.isAssignableFrom(popup.getClass())){
177
		    AbstractEditorPresenter presenter = ((AbstractPopupEditor)popup).presenter();
178
		    eventListenerManager.removeEventListeners(presenter);
179
		}
180

    
181
	}
182

    
183
    /**
184
     * {@inheritDoc}
185
     */
186
    @Override
187
    public void reloadCurrentView() {
188
        if(logger.isTraceEnabled()){
189
            logger.trace("reloading " + getState());
190
        }
191
        navigateTo(getState(), false);
192
    }
193

    
194
    /**
195
     * This method requires that the {@SpringView} annotation is used to ser the name of the <code>View</code>.
196
     *
197
     * @return the current view name or <code>null</code>
198
     */
199
    @Override
200
    public String getCurrentViewName() {
201
        SpringView springViewAnnotation = getCurrentView().getClass().getAnnotation(SpringView.class);
202
        if(springViewAnnotation != null){
203
            return springViewAnnotation.name();
204
        }
205
        return null;
206
    }
207

    
208
    @Override
209
    public List<String> getCurrentViewParameters(){
210
        String substate = getState();
211
        String currentViewName = getCurrentViewName();
212
        if(currentViewName != null){
213
            substate = substate.replaceAll("^" + currentViewName + "/?", "");
214

    
215
        }
216
        return Arrays.asList(substate.split("/"));
217
    }
218

    
219
    /**
220
     * {@inheritDoc}
221
     */
222
    @Override
223
    public List<AbstractEditorPresenter<?, ?>> getPopupEditorPresenters() {
224
        // TODO Auto-generated method stub
225
        return null;
226
    }
227
}
(4-4/7)