1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
* Copyright 2011 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.vaadin.server.AbstractUIProvider;
import com.vaadin.server.VaadinSession;
import com.vaadin.server.Terminal.ErrorEvent;
import com.vaadin.server.Terminal.ErrorListener;
import com.vaadin.server.WrappedRequest;
import com.vaadin.ui.UI;
/**
* A special application designed to help migrating applications from Vaadin 6
* to Vaadin 7. The legacy application supports setting a main window, adding
* additional browser level windows and defining the theme for the entire
* application.
*
* @deprecated This class is only intended to ease migration and should not be
* used for new projects.
*
* @since 7.0
*/
@Deprecated
public abstract class Application extends AbstractUIProvider implements
ErrorListener {
/**
* Ignore initial / and then get everything up to the next /
*/
private static final Pattern WINDOW_NAME_PATTERN = Pattern
.compile("^/?([^/]+).*");
private UI.LegacyWindow mainWindow;
private String theme;
private Map<String, UI.LegacyWindow> legacyUINames = new HashMap<String, UI.LegacyWindow>();
/**
* Sets the main window of this application. Setting window as a main window
* of this application also adds the window to this application.
*
* @param mainWindow
* the UI to set as the default window
*/
public void setMainWindow(UI.LegacyWindow mainWindow) {
if (this.mainWindow != null) {
throw new IllegalStateException("mainWindow has already been set");
}
if (mainWindow.getSession() == null) {
mainWindow.setSession(VaadinSession.getCurrent());
} else if (mainWindow.getSession() != VaadinSession.getCurrent()) {
throw new IllegalStateException(
"mainWindow is attached to another application");
}
if (UI.getCurrent() == null) {
// Assume setting a main window from Application.init if there's
// no current UI -> set the main window as the current UI
UI.setCurrent(mainWindow);
}
this.mainWindow = mainWindow;
}
public void doInit() {
VaadinSession.getCurrent().setErrorHandler(this);
init();
}
protected abstract void init();
@Override
public Class<? extends UI> getUIClass(VaadinSession application,
WrappedRequest request) {
UI uiInstance = getUIInstance(request);
if (uiInstance != null) {
return uiInstance.getClass();
}
return null;
}
@Override
public UI createInstance(VaadinSession application, Class<? extends UI> type,
WrappedRequest request) {
return getUIInstance(request);
}
@Override
public String getThemeForUI(WrappedRequest request,
Class<? extends UI> uiClass) {
return theme;
}
@Override
public String getPageTitleForUI(WrappedRequest request,
Class<? extends UI> uiClass) {
UI uiInstance = getUIInstance(request);
if (uiInstance != null) {
return uiInstance.getCaption();
} else {
return super.getPageTitleForUI(request, uiClass);
}
}
/**
* Gets the mainWindow of the application.
*
* <p>
* The main window is the window attached to the application URL (
* {@link #getURL()}) and thus which is show by default to the user.
* </p>
* <p>
* Note that each application must have at least one main window.
* </p>
*
* @return the UI used as the default window
*/
public UI.LegacyWindow getMainWindow() {
return mainWindow;
}
private UI getUIInstance(WrappedRequest request) {
String pathInfo = request.getRequestPathInfo();
String name = null;
if (pathInfo != null && pathInfo.length() > 0) {
Matcher matcher = WINDOW_NAME_PATTERN.matcher(pathInfo);
if (matcher.matches()) {
// Skip the initial slash
name = matcher.group(1);
}
}
UI.LegacyWindow window = getWindow(name);
if (window != null) {
return window;
}
return mainWindow;
}
/**
* This implementation simulates the way of finding a window for a request
* by extracting a window name from the requested path and passes that name
* to {@link #getWindow(String)}.
* <p>
* {@inheritDoc}
*/
@Override
public UI getExistingUI(WrappedRequest request) {
UI uiInstance = getUIInstance(request);
if (uiInstance.getUIId() == -1) {
// Not initialized -> Let go through createUIInstance to make it
// initialized
return null;
} else {
UI.setCurrent(uiInstance);
return uiInstance;
}
}
/**
* Sets the application's theme.
* <p>
* Note that this theme can be overridden for a specific UI with
* {@link VaadinSession#getThemeForUI(UI)}. Setting theme to be
* <code>null</code> selects the default theme. For the available theme
* names, see the contents of the VAADIN/themes directory.
* </p>
*
* @param theme
* the new theme for this application.
*/
public void setTheme(String theme) {
this.theme = theme;
}
/**
* Gets the application's theme. The application's theme is the default
* theme used by all the uIs for which a theme is not explicitly defined. If
* the application theme is not explicitly set, <code>null</code> is
* returned.
*
* @return the name of the application's theme.
*/
public String getTheme() {
return theme;
}
/**
* <p>
* Gets a UI by name. Returns <code>null</code> if the application is not
* running or it does not contain a window corresponding to the name.
* </p>
*
* @param name
* the name of the requested window
* @return a UI corresponding to the name, or <code>null</code> to use the
* default window
*/
public UI.LegacyWindow getWindow(String name) {
return legacyUINames.get(name);
}
/**
* Counter to get unique names for windows with no explicit name
*/
private int namelessUIIndex = 0;
/**
* Adds a new browser level window to this application. Please note that UI
* doesn't have a name that is used in the URL - to add a named window you
* should instead use {@link #addWindow(UI, String)}
*
* @param uI
* the UI window to add to the application
* @return returns the name that has been assigned to the window
*
* @see #addWindow(UI, String)
*/
public void addWindow(UI.LegacyWindow uI) {
if (uI.getName() == null) {
String name = Integer.toString(namelessUIIndex++);
uI.setName(name);
}
legacyUINames.put(uI.getName(), uI);
uI.setSession(VaadinSession.getCurrent());
}
/**
* Removes the specified window from the application. This also removes all
* name mappings for the window (see {@link #addWindow(UI, String) and
* #getWindowName(UI)}.
*
* <p>
* Note that removing window from the application does not close the browser
* window - the window is only removed from the server-side.
* </p>
*
* @param uI
* the UI to remove
*/
public void removeWindow(UI.LegacyWindow uI) {
for (Entry<String, UI.LegacyWindow> entry : legacyUINames.entrySet()) {
if (entry.getValue() == uI) {
legacyUINames.remove(entry.getKey());
}
}
}
/**
* Gets the set of windows contained by the application.
*
* <p>
* Note that the returned set of windows can not be modified.
* </p>
*
* @return the unmodifiable collection of windows.
*/
public Collection<UI.LegacyWindow> getWindows() {
return Collections.unmodifiableCollection(legacyUINames.values());
}
@Override
public void terminalError(ErrorEvent event) {
VaadinSession.getCurrent().terminalError(event);
}
public VaadinSession getContext() {
return VaadinSession.getCurrent();
}
protected void close() {
VaadinSession.getCurrent().close();
}
public boolean isRunning() {
return VaadinSession.getCurrent().isRunning();
}
public URL getURL() {
return VaadinSession.getCurrent().getURL();
}
}
|