aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/ui/LegacyWindow.java
blob: 2705d56486c4ee01f8e2139c088a5a3f36005dec (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
 * Copyright 2000-2016 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.ui;

import java.net.MalformedURLException;
import java.net.URL;

import com.vaadin.server.LegacyApplication;
import com.vaadin.server.Page.BrowserWindowResizeEvent;
import com.vaadin.server.Page.BrowserWindowResizeListener;
import com.vaadin.server.Resource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.BorderStyle;

/**
 * Helper class to emulate the main window from Vaadin 6 using UIs. This class
 * should be used in the same way as Window used as a browser level window in
 * Vaadin 6 with {@link com.vaadin.server.LegacyApplication}
 */
@Deprecated
public class LegacyWindow extends UI {
    private String name;
    private LegacyApplication application;

    /**
     * Create a new legacy window
     */
    public LegacyWindow() {
        super(new VerticalLayout());
        ((VerticalLayout) getContent()).setMargin(true);
    }

    /**
     * Creates a new legacy window with the given caption
     *
     * @param caption
     *            the caption of the window
     */
    public LegacyWindow(String caption) {
        super(new VerticalLayout());
        ((VerticalLayout) getContent()).setMargin(true);
        setCaption(caption);
    }

    /**
     * Creates a legacy window with the given caption and content layout
     *
     * @param caption
     * @param content
     */
    public LegacyWindow(String caption, ComponentContainer content) {
        super(content);
        setCaption(caption);
    }

    @Override
    protected void init(VaadinRequest request) {
        // Just empty
    }

    public void setApplication(LegacyApplication application) {
        this.application = application;
    }

    public LegacyApplication getApplication() {
        return application;
    }

    /**
     * Gets the unique name of the window. The name of the window is used to
     * uniquely identify it.
     * <p>
     * The name also determines the URL that can be used for direct access to a
     * window. All windows can be accessed through
     * {@code http://host:port/app/win} where {@code http://host:port/app} is
     * the application URL (as returned by {@link LegacyApplication#getURL()}
     * and {@code win} is the window name.
     * </p>
     * <p>
     * Note! Portlets do not support direct window access through URLs.
     * </p>
     *
     * @return the Name of the Window.
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the unique name of the window. The name of the window is used to
     * uniquely identify it inside the application.
     * <p>
     * The name also determines the URL that can be used for direct access to a
     * window. All windows can be accessed through
     * {@code http://host:port/app/win} where {@code http://host:port/app} is
     * the application URL (as returned by {@link LegacyApplication#getURL()}
     * and {@code win} is the window name.
     * </p>
     * <p>
     * This method can only be called before the window is added to an
     * application.
     * <p>
     * Note! Portlets do not support direct window access through URLs.
     * </p>
     *
     * @param name
     *            the new name for the window or null if the application should
     *            automatically assign a name to it
     * @throws IllegalStateException
     *             if the window is attached to an application
     */
    public void setName(String name) {
        this.name = name;
        // The name can not be changed in application
        if (isAttached()) {
            throw new IllegalStateException(
                    "Window name can not be changed while "
                            + "the window is in application");
        }

    }

    /**
     * Gets the full URL of the window. The returned URL is window specific and
     * can be used to directly refer to the window.
     * <p>
     * Note! This method can not be used for portlets.
     * </p>
     *
     * @return the URL of the window or null if the window is not attached to an
     *         application
     */
    public URL getURL() {
        LegacyApplication application = getApplication();
        if (application == null) {
            return null;
        }

        try {
            return new URL(application.getURL(), getName() + "/");
        } catch (MalformedURLException e) {
            throw new RuntimeException(
                    "Internal problem getting window URL, please report");
        }
    }

    /**
     * Opens the given resource in this UI. The contents of this UI is replaced
     * by the {@code Resource}.
     *
     * @param resource
     *            the resource to show in this UI
     *
     * @deprecated As of 7.0, use getPage().setLocation instead
     */
    @Deprecated
    public void open(Resource resource) {
        open(resource, null, false);
    }

    /* ********************************************************************* */

    /**
     * Opens the given resource in a window with the given name.
     * <p>
     * The supplied {@code windowName} is used as the target name in a
     * window.open call in the client. This means that special values such as
     * "_blank", "_self", "_top", "_parent" have special meaning. An empty or
     * <code>null</code> window name is also a special case.
     * </p>
     * <p>
     * "", null and "_self" as {@code windowName} all causes the resource to be
     * opened in the current window, replacing any old contents. For
     * downloadable content you should avoid "_self" as "_self" causes the
     * client to skip rendering of any other changes as it considers them
     * irrelevant (the page will be replaced by the resource). This can speed up
     * the opening of a resource, but it might also put the client side into an
     * inconsistent state if the window content is not completely replaced e.g.,
     * if the resource is downloaded instead of displayed in the browser.
     * </p>
     * <p>
     * "_blank" as {@code windowName} causes the resource to always be opened in
     * a new window or tab (depends on the browser and browser settings).
     * </p>
     * <p>
     * "_top" and "_parent" as {@code windowName} works as specified by the HTML
     * standard.
     * </p>
     * <p>
     * Any other {@code windowName} will open the resource in a window with that
     * name, either by opening a new window/tab in the browser or by replacing
     * the contents of an existing window with that name.
     * </p>
     * <p>
     * As of Vaadin 7.0.0, the functionality for opening a Resource in a Page
     * has been replaced with similar methods based on a String URL. This is
     * because the usage of Resource is problematic with memory management and
     * with security features in some browsers. Is is recommended to instead use
     * {@link Link} for starting downloads.
     * </p>
     *
     * @param resource
     *            the resource.
     * @param windowName
     *            the name of the window.
     * @deprecated As of 7.0, use getPage().open instead
     */
    @Deprecated
    public void open(Resource resource, String windowName) {
        open(resource, windowName, true);
    }

    /**
     * Opens the given resource in a window with the given name and optionally
     * tries to force the resource to open in a new window instead of a new tab.
     * <p>
     * The supplied {@code windowName} is used as the target name in a
     * window.open call in the client. This means that special values such as
     * "_blank", "_self", "_top", "_parent" have special meaning. An empty or
     * <code>null</code> window name is also a special case.
     * </p>
     * <p>
     * "", null and "_self" as {@code windowName} all causes the resource to be
     * opened in the current window, replacing any old contents. For
     * downloadable content you should avoid "_self" as "_self" causes the
     * client to skip rendering of any other changes as it considers them
     * irrelevant (the page will be replaced by the resource). This can speed up
     * the opening of a resource, but it might also put the client side into an
     * inconsistent state if the window content is not completely replaced e.g.,
     * if the resource is downloaded instead of displayed in the browser.
     * </p>
     * <p>
     * "_blank" as {@code windowName} causes the resource to always be opened in
     * a new window or tab (depends on the browser and browser settings).
     * </p>
     * <p>
     * "_top" and "_parent" as {@code windowName} works as specified by the HTML
     * standard.
     * </p>
     * <p>
     * Any other {@code windowName} will open the resource in a window with that
     * name, either by opening a new window/tab in the browser or by replacing
     * the contents of an existing window with that name.
     * </p>
     * <p>
     * If {@code windowName} is set to open the resource in a new window or tab
     * and {@code tryToOpenAsPopup} is true, this method attempts to force the
     * browser to open a new window instead of a tab. NOTE: This is a
     * best-effort attempt and may not work reliably with all browsers and
     * different pop-up preferences. With most browsers using default settings,
     * {@code tryToOpenAsPopup} works properly.
     * </p>
     * <p>
     * As of Vaadin 7.0.0, the functionality for opening a Resource in a Page
     * has been replaced with similar methods based on a String URL. This is
     * because the usage of Resource is problematic with memory management and
     * with security features in some browsers. Is is recommended to instead use
     * {@link Link} for starting downloads.
     * </p>
     *
     * @param resource
     *            the resource.
     * @param windowName
     *            the name of the window.
     * @param tryToOpenAsPopup
     *            Whether to try to force the resource to be opened in a new
     *            window
     */
    public void open(Resource resource, String windowName,
            boolean tryToOpenAsPopup) {
        getPage().open(resource, windowName, tryToOpenAsPopup);
    }

    /**
     * Opens the given resource in a window with the given size, border and
     * name. For more information on the meaning of {@code windowName}, see
     * {@link #open(Resource, String)}.
     * <p>
     * As of Vaadin 7.0.0, the functionality for opening a Resource in a Page
     * has been replaced with similar methods based on a String URL. This is
     * because the usage of Resource is problematic with memory management and
     * with security features in some browsers. Is is recommended to instead use
     * {@link Link} for starting downloads.
     * </p>
     *
     * @param resource
     *            the resource.
     * @param windowName
     *            the name of the window.
     * @param width
     *            the width of the window in pixels
     * @param height
     *            the height of the window in pixels
     * @param border
     *            the border style of the window.
     * @deprecated As of 7.0, use getPage().open instead
     */
    @Deprecated
    public void open(Resource resource, String windowName, int width,
            int height, BorderStyle border) {
        getPage().open(resource, windowName, width, height, border);
    }

    /**
     * Adds a new {@link BrowserWindowResizeListener} to this UI. The listener
     * will be notified whenever the browser window within which this UI resides
     * is resized.
     *
     * @param resizeListener
     *            the listener to add
     *
     * @see BrowserWindowResizeListener#browserWindowResized(BrowserWindowResizeEvent)
     * @see #setResizeLazy(boolean)
     *
     * @deprecated As of 7.0, use the similarly named api in Page instead
     */
    @Deprecated
    public void addListener(BrowserWindowResizeListener resizeListener) {
        getPage().addBrowserWindowResizeListener(resizeListener);
    }

    /**
     * Removes a {@link BrowserWindowResizeListener} from this UI. The listener
     * will no longer be notified when the browser window is resized.
     *
     * @param resizeListener
     *            the listener to remove
     * @deprecated As of 7.0, use the similarly named api in Page instead
     */
    @Deprecated
    public void removeListener(BrowserWindowResizeListener resizeListener) {
        getPage().removeBrowserWindowResizeListener(resizeListener);
    }

    /**
     * Gets the last known height of the browser window in which this UI
     * resides.
     *
     * @return the browser window height in pixels
     * @deprecated As of 7.0, use the similarly named api in Page instead
     */
    @Deprecated
    public int getBrowserWindowHeight() {
        return getPage().getBrowserWindowHeight();
    }

    /**
     * Gets the last known width of the browser window in which this UI resides.
     *
     * @return the browser window width in pixels
     *
     * @deprecated As of 7.0, use the similarly named api in Page instead
     */
    @Deprecated
    public int getBrowserWindowWidth() {
        return getPage().getBrowserWindowWidth();
    }

    /**
     * Executes JavaScript in this window.
     *
     * <p>
     * This method allows one to inject javascript from the server to client. A
     * client implementation is not required to implement this functionality,
     * but currently all web-based clients do implement this.
     * </p>
     *
     * <p>
     * Executing javascript this way often leads to cross-browser compatibility
     * issues and regressions that are hard to resolve. Use of this method
     * should be avoided and instead it is recommended to create new widgets
     * with GWT. For more info on creating own, reusable client-side widgets in
     * Java, read the corresponding chapter in Book of Vaadin.
     * </p>
     *
     * @param script
     *            JavaScript snippet that will be executed.
     *
     * @deprecated As of 7.0, use JavaScript.getCurrent().execute(String)
     *             instead
     */
    @Deprecated
    public void executeJavaScript(String script) {
        getPage().getJavaScript().execute(script);
    }

    @Override
    public void setCaption(String caption) {
        // Override to provide backwards compatibility
        getState().caption = caption;
        getPage().setTitle(caption);
    }

    @Override
    public ComponentContainer getContent() {
        return (ComponentContainer) super.getContent();
    }

    /**
     * Set the content of the window. For a {@link LegacyWindow}, the content
     * must be a {@link ComponentContainer}.
     *
     * @param content
     */
    @Override
    public void setContent(Component content) {
        if (!(content instanceof ComponentContainer)) {
            throw new IllegalArgumentException(
                    "The content of a LegacyWindow must be a ComponentContainer");
        }
        super.setContent(content);
    }

    /**
     * This implementation replaces a component in the content container (
     * {@link #getContent()}) instead of in the actual UI.
     *
     * This method should only be called when the content is a
     * {@link ComponentContainer} (default {@link VerticalLayout} or explicitly
     * set).
     */
    public void replaceComponent(Component oldComponent,
            Component newComponent) {
        getContent().replaceComponent(oldComponent, newComponent);
    }

    /**
     * Adds a component to this UI. The component is not added directly to the
     * UI, but instead to the content container ({@link #getContent()}).
     *
     * This method should only be called when the content is a
     * {@link ComponentContainer} (default {@link VerticalLayout} or explicitly
     * set).
     *
     * @param component
     *            the component to add to this UI
     *
     * @see #getContent()
     */
    public void addComponent(Component component) {
        getContent().addComponent(component);
    }

    /**
     * This implementation removes the component from the content container (
     * {@link #getContent()}) instead of from the actual UI.
     *
     * This method should only be called when the content is a
     * {@link ComponentContainer} (default {@link VerticalLayout} or explicitly
     * set).
     */
    public void removeComponent(Component component) {
        getContent().removeComponent(component);
    }

    /**
     * This implementation removes the components from the content container (
     * {@link #getContent()}) instead of from the actual UI.
     *
     * This method should only be called when the content is a
     * {@link ComponentContainer} (default {@link VerticalLayout} or explicitly
     * set).
     */
    public void removeAllComponents() {
        getContent().removeAllComponents();
    }

}