|
1 |
/**
|
|
2 |
* Copyright (C) 2021 EDIT
|
|
3 |
* European Distributed Institute of Taxonomy
|
|
4 |
* http://www.e-taxonomy.eu
|
|
5 |
*
|
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms.
|
|
8 |
*/
|
|
9 |
package eu.etaxonomy.cdm.vaadin.ui;
|
|
10 |
|
|
11 |
|
|
12 |
import org.apache.log4j.Logger;
|
|
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
14 |
import org.vaadin.spring.events.EventBus.UIEventBus;
|
|
15 |
|
|
16 |
import com.flowingcode.vaadin.addons.errorwindow.WindowErrorHandler;
|
|
17 |
import com.vaadin.navigator.View;
|
|
18 |
import com.vaadin.navigator.ViewDisplay;
|
|
19 |
import com.vaadin.server.ExternalResource;
|
|
20 |
import com.vaadin.server.Page;
|
|
21 |
import com.vaadin.server.Resource;
|
|
22 |
import com.vaadin.server.Responsive;
|
|
23 |
import com.vaadin.server.VaadinRequest;
|
|
24 |
import com.vaadin.server.VaadinSession;
|
|
25 |
import com.vaadin.spring.navigator.SpringViewProvider;
|
|
26 |
import com.vaadin.ui.Component;
|
|
27 |
import com.vaadin.ui.UI;
|
|
28 |
import com.vaadin.ui.themes.ValoTheme;
|
|
29 |
|
|
30 |
import eu.etaxonomy.cdm.database.PermissionDeniedException;
|
|
31 |
import eu.etaxonomy.cdm.vaadin.event.error.DelegatingErrorHandler;
|
|
32 |
import eu.etaxonomy.cdm.vaadin.event.error.ErrorTypeErrorHandlerWrapper;
|
|
33 |
import eu.etaxonomy.cdm.vaadin.event.error.PermissionDeniedErrorHandler;
|
|
34 |
import eu.etaxonomy.cdm.vaadin.permission.ReleasableResourcesView;
|
|
35 |
import eu.etaxonomy.cdm.vaadin.view.RedirectToLoginView;
|
|
36 |
import eu.etaxonomy.vaadin.ui.UIInitializedEvent;
|
|
37 |
import eu.etaxonomy.vaadin.ui.navigation.NavigationManagerBean;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* @author a.kohlbecker
|
|
41 |
* @since Nov 11, 2021
|
|
42 |
*/
|
|
43 |
public abstract class AbstractUI extends UI {
|
|
44 |
|
|
45 |
private static final long serialVersionUID = 7430086500775997281L;
|
|
46 |
|
|
47 |
private static Logger logger = Logger.getLogger(AbstractUI.class);
|
|
48 |
|
|
49 |
abstract protected ViewDisplay getViewDisplay();
|
|
50 |
|
|
51 |
@Autowired
|
|
52 |
protected SpringViewProvider viewProvider;
|
|
53 |
|
|
54 |
@Autowired
|
|
55 |
final private void setNavigationManagerBean(NavigationManagerBean navigatorBean) {
|
|
56 |
// logger.debug("setNavigationManagerBean()" + navigatorBean.toString());
|
|
57 |
setNavigator(navigatorBean);
|
|
58 |
}
|
|
59 |
|
|
60 |
final private NavigationManagerBean getNavigationManagerBean() {
|
|
61 |
if(getNavigator() != null) {
|
|
62 |
return (NavigationManagerBean) getNavigator();
|
|
63 |
}
|
|
64 |
return null;
|
|
65 |
}
|
|
66 |
|
|
67 |
@Autowired
|
|
68 |
protected UIEventBus uiEventBus;
|
|
69 |
|
|
70 |
public AbstractUI() {
|
|
71 |
super();
|
|
72 |
}
|
|
73 |
|
|
74 |
public AbstractUI(Component content) {
|
|
75 |
super(content);
|
|
76 |
}
|
|
77 |
|
|
78 |
@Override
|
|
79 |
protected void init(VaadinRequest request) {
|
|
80 |
|
|
81 |
logger.debug("init()");
|
|
82 |
registerErrorHandlers();
|
|
83 |
|
|
84 |
configureAccessDeniedView();
|
|
85 |
|
|
86 |
setContent((Component) getViewDisplay());
|
|
87 |
initContent();
|
|
88 |
|
|
89 |
getNavigationManagerBean().setViewDisplay(getViewDisplay());
|
|
90 |
assert getInitialViewName() != null;
|
|
91 |
getNavigationManagerBean().setDefaultViewName(getInitialViewName());
|
|
92 |
|
|
93 |
Responsive.makeResponsive(this);
|
|
94 |
|
|
95 |
addDetachListener(e -> {
|
|
96 |
if(getNavigator() != null) {
|
|
97 |
// no point using viewProvider.getView() without the navigator
|
|
98 |
for(String viewName : viewProvider.getViewNamesForCurrentUI()){
|
|
99 |
View view = viewProvider.getView(viewName);
|
|
100 |
if(view != null && view instanceof ReleasableResourcesView) {
|
|
101 |
((ReleasableResourcesView)view).releaseResourcesOnAccessDenied();
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|
|
105 |
});
|
|
106 |
|
|
107 |
if(getBrandName() != null) {
|
|
108 |
//TODO create annotation:
|
|
109 |
// @Styles(files={""}, branding="brand")
|
|
110 |
//
|
|
111 |
// the branding can either be specified or can be read from the properties file in .cdmLibrary/remote-webapp/{instance-name}-app.properties
|
|
112 |
// See CdmUtils for appropriate methods to access this folder
|
|
113 |
// the 'vaadin://' protocol refers to the VAADIN folder
|
|
114 |
Resource registryCssFile = new ExternalResource("vaadin://branding/" + getBrandName() + "/css/branding.css");
|
|
115 |
Page.getCurrent().getStyles().add(registryCssFile);
|
|
116 |
}
|
|
117 |
|
|
118 |
|
|
119 |
uiEventBus.publish(this, new UIInitializedEvent());
|
|
120 |
|
|
121 |
}
|
|
122 |
|
|
123 |
/**
|
|
124 |
* @return The name of the initial view to
|
|
125 |
*/
|
|
126 |
abstract protected String getInitialViewName();
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Branding can either be specified or can be read from the properties file
|
|
130 |
* in <code>.cdmLibrary/remote-webapp/{instance-name}-app.properties</code>
|
|
131 |
* See CdmUtils for appropriate methods to access this folder the
|
|
132 |
* <code>'vaadin://'</code> protocol refers to the VAADIN folder.
|
|
133 |
* <p>
|
|
134 |
* Can be overridden by implementing classes to set a brand.
|
|
135 |
*
|
|
136 |
* @return <code>NULL</code> for no branding or
|
|
137 |
*/
|
|
138 |
protected String getBrandName() {
|
|
139 |
return null;
|
|
140 |
}
|
|
141 |
|
|
142 |
/**
|
|
143 |
* Implementing classes may add additional content to the UI. This
|
|
144 |
* will for example be interesting when using the {@link ValoTheme.UI_WITH_MENU}
|
|
145 |
* style.
|
|
146 |
*/
|
|
147 |
abstract protected void initContent();
|
|
148 |
|
|
149 |
protected void registerErrorHandlers() {
|
|
150 |
DelegatingErrorHandler delegatingErrorHander = new DelegatingErrorHandler();
|
|
151 |
WindowErrorHandler errorHandler = new WindowErrorHandler(this, RegistrationUIDefaults.ERROR_CONTACT_MESSAGE_LINE + "</br></br>"
|
|
152 |
+ "<i>To help analyzing the problem please describe your actions that lead to this error and provide the error details from below in your email. "
|
|
153 |
+ "You also might want to add a sreenshot of the browser page in error.</i>");
|
|
154 |
delegatingErrorHander.registerHandler(new ErrorTypeErrorHandlerWrapper<PermissionDeniedException>(PermissionDeniedException.class, new PermissionDeniedErrorHandler(this)));
|
|
155 |
delegatingErrorHander.registerHandler(new ErrorTypeErrorHandlerWrapper<Exception>(Exception.class, errorHandler));
|
|
156 |
setErrorHandler(delegatingErrorHander);
|
|
157 |
VaadinSession.getCurrent().setErrorHandler(delegatingErrorHander);
|
|
158 |
}
|
|
159 |
|
|
160 |
protected void configureAccessDeniedView() {
|
|
161 |
viewProvider.setAccessDeniedViewClass(RedirectToLoginView.class);
|
|
162 |
}
|
|
163 |
|
|
164 |
|
|
165 |
private String pageFragmentAsState() {
|
|
166 |
Page page = Page.getCurrent();
|
|
167 |
String fragment = page.getUriFragment();
|
|
168 |
String state = null;
|
|
169 |
if(fragment != null && fragment.startsWith("!")){
|
|
170 |
state = fragment.substring(1, fragment.length());
|
|
171 |
}
|
|
172 |
return state;
|
|
173 |
}
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
}
|
fix #8503 abstract base UI created