/
WindowConnector.java
555 lines (479 loc) · 20.3 KB
/
WindowConnector.java
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* Copyright 2000-2022 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.client.ui.window;
import java.util.logging.Logger;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.vaadin.client.ApplicationConnection;
import com.vaadin.client.ComponentConnector;
import com.vaadin.client.ConnectorHierarchyChangeEvent;
import com.vaadin.client.LayoutManager;
import com.vaadin.client.Paintable;
import com.vaadin.client.UIDL;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractSingleComponentContainerConnector;
import com.vaadin.client.ui.ClickEventHandler;
import com.vaadin.client.ui.PostLayoutListener;
import com.vaadin.client.ui.ShortcutActionHandler;
import com.vaadin.client.ui.SimpleManagedLayout;
import com.vaadin.client.ui.VWindow;
import com.vaadin.client.ui.layout.MayScrollChildren;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.window.WindowMode;
import com.vaadin.shared.ui.window.WindowServerRpc;
import com.vaadin.shared.ui.window.WindowState;
/**
* A connector class for the Window component.
*
* @author Vaadin Ltd
*/
@SuppressWarnings("deprecation")
@Connect(value = com.vaadin.ui.Window.class)
public class WindowConnector extends AbstractSingleComponentContainerConnector
implements Paintable, SimpleManagedLayout, PostLayoutListener,
MayScrollChildren, WindowMoveHandler {
private Node windowClone;
private ClickEventHandler clickEventHandler = new ClickEventHandler(this) {
@Override
protected void fireClick(NativeEvent event,
MouseEventDetails mouseDetails) {
getRpcProxy(WindowServerRpc.class).click(mouseDetails);
}
};
abstract class WindowEventHandler
implements ClickHandler, DoubleClickHandler, KeyUpHandler {
}
private WindowEventHandler maximizeRestoreClickHandler = new WindowEventHandler() {
@Override
public void onClick(ClickEvent event) {
final Element target = event.getNativeEvent().getEventTarget()
.cast();
if (target == getWidget().maximizeRestoreBox) {
// Click on maximize/restore box
onMaximizeRestore();
}
}
@Override
public void onDoubleClick(DoubleClickEvent event) {
final Element target = event.getNativeEvent().getEventTarget()
.cast();
if (getWidget().header.isOrHasChild(target)) {
// Double click on header
onMaximizeRestore();
}
}
@Override
public void onKeyUp(KeyUpEvent event) {
final int keyCode = event.getNativeKeyCode();
final Element target = event.getNativeEvent().getEventTarget()
.cast();
// key ENTER or SPACE on maximize/restore box
if (target == getWidget().maximizeRestoreBox
&& isKeyEnterOrSpace(keyCode)) {
onMaximizeRestore();
}
}
};
@Override
public boolean delegateCaptionHandling() {
return false;
}
@Override
protected void init() {
super.init();
VWindow window = getWidget();
window.id = getConnectorId();
window.client = getConnection();
window.connector = this;
getLayoutManager().registerDependency(this,
window.contentPanel.getElement());
getLayoutManager().registerDependency(this, window.header);
getLayoutManager().registerDependency(this, window.footer);
window.addHandler(maximizeRestoreClickHandler, ClickEvent.getType());
window.addHandler(maximizeRestoreClickHandler,
DoubleClickEvent.getType());
window.addHandler(maximizeRestoreClickHandler, KeyUpEvent.getType());
window.setOwner(getConnection().getUIConnector().getWidget());
window.addMoveHandler(this);
}
@Override
public void onUnregister() {
LayoutManager lm = getLayoutManager();
VWindow window = getWidget();
lm.unregisterDependency(this, window.contentPanel.getElement());
lm.unregisterDependency(this, window.header);
lm.unregisterDependency(this, window.footer);
}
@Override
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
VWindow window = getWidget();
String connectorId = getConnectorId();
// Workaround needed for Testing Tools (GWT generates window DOM
// slightly different in different browsers).
window.closeBox.setId(connectorId + "_window_close");
window.maximizeRestoreBox
.setId(connectorId + "_window_maximizerestore");
window.visibilityChangesDisabled = true;
if (!isRealUpdate(uidl)) {
return;
}
window.visibilityChangesDisabled = false;
// we may have actions
for (int i = 0; i < uidl.getChildCount(); i++) {
UIDL childUidl = uidl.getChildUIDL(i);
if (childUidl.getTag().equals("actions")) {
if (window.shortcutHandler == null) {
window.shortcutHandler = new ShortcutActionHandler(
connectorId, client);
}
window.shortcutHandler.updateActionMap(childUidl);
}
}
if (uidl.hasAttribute("bringToFront")) {
/*
* Focus as a side-effect. Will be overridden by
* ApplicationConnection if another component was focused by the
* server side.
*/
window.contentPanel.focus();
window.bringToFrontSequence = uidl.getIntAttribute("bringToFront");
VWindow.deferOrdering();
}
}
@Override
public void updateCaption(ComponentConnector component) {
// NOP, window has own caption, layout caption not rendered
}
@Override
public VWindow getWidget() {
return (VWindow) super.getWidget();
}
@Override
public void onConnectorHierarchyChange(
ConnectorHierarchyChangeEvent event) {
// We always have 1 child, unless the child is hidden
getWidget().contentPanel.setWidget(getContentWidget());
if (getParent() == null && windowClone != null) {
// If the window is removed from the UI, add the copy of the
// contents to the window (in case of an 'out-animation')
getWidget().getElement().removeAllChildren();
getWidget().getElement().appendChild(windowClone);
// Clean reference
windowClone = null;
}
}
@Override
public void layout() {
LayoutManager lm = getLayoutManager();
VWindow window = getWidget();
// ensure window is not larger than browser window
if (window.getOffsetWidth() > Window.getClientWidth()) {
window.setWidth(Window.getClientWidth() + "px");
lm.setNeedsMeasure(getContent());
}
if (window.getOffsetHeight() > Window.getClientHeight()) {
window.setHeight(Window.getClientHeight() + "px");
lm.setNeedsMeasure(getContent());
}
ComponentConnector content = getContent();
boolean hasContent = content != null;
Element contentElement = window.contentPanel.getElement();
Style contentStyle = window.contents.getStyle();
int headerHeight = lm.getOuterHeight(window.header);
contentStyle.setPaddingTop(headerHeight, Unit.PX);
contentStyle.setMarginTop(-headerHeight, Unit.PX);
int footerHeight = lm.getOuterHeight(window.footer);
contentStyle.setPaddingBottom(footerHeight, Unit.PX);
contentStyle.setMarginBottom(-footerHeight, Unit.PX);
int minWidth = lm.getOuterWidth(window.header)
- lm.getInnerWidth(window.header);
int minHeight = footerHeight + headerHeight;
getWidget().getElement().getStyle().setPropertyPx("minWidth", minWidth);
getWidget().getElement().getStyle().setPropertyPx("minHeight",
minHeight);
/*
* Must set absolute position if the child has relative height and
* there's a chance of horizontal scrolling as some browsers will
* otherwise not take the scrollbar into account when calculating the
* height.
*/
if (hasContent) {
Element layoutElement = content.getWidget().getElement();
Style childStyle = layoutElement.getStyle();
if (content.isRelativeHeight()) {
childStyle.setPosition(Position.ABSOLUTE);
Style wrapperStyle = contentElement.getStyle();
if (window.getElement().getStyle().getWidth().isEmpty()
&& !content.isRelativeWidth()) {
/*
* Need to lock width to make undefined width work even with
* absolute positioning
*/
int contentWidth = lm.getOuterWidth(layoutElement);
wrapperStyle.setWidth(contentWidth, Unit.PX);
} else {
wrapperStyle.clearWidth();
}
} else {
childStyle.clearPosition();
}
}
}
@Override
public void postLayout() {
VWindow window = getWidget();
if (!window.isAttached()) {
Logger.getLogger(WindowConnector.class.getName())
.warning("Called postLayout to detached Window.");
return;
}
if (window.centered && getState().windowMode != WindowMode.MAXIMIZED) {
window.center();
}
window.positionOrSizeUpdated();
if (getParent() != null) {
// Take a copy of the contents, since the server will detach all
// children of this window when it's closed, and the window will be
// emptied during the following hierarchy update (we need to keep
// the contents visible for the duration of a possible
// 'out-animation')
// Fix for #14645 and #14785 - as soon as we clone audio and video
// tags, they start fetching data, and playing immediately in
// background, in case autoplay attribute is present. Therefore we
// have to replace them with stubs in the clone. And we can't just
// erase them, because there are corresponding player widgets to
// animate
windowClone = cloneNodeFilteringMedia(
getWidget().getElement().getFirstChild());
}
}
private Node cloneNodeFilteringMedia(Node node) {
if (node instanceof Element) {
Element old = (Element) node;
if ("audio".equalsIgnoreCase(old.getTagName())
|| "video".equalsIgnoreCase(old.getTagName())) {
if (!old.hasAttribute("controls")
&& "audio".equalsIgnoreCase(old.getTagName())) {
// nothing to animate, so we won't add this to
// the clone
return null;
}
Element newEl = DOM.createElement(old.getTagName());
if (old.hasAttribute("controls")) {
newEl.setAttribute("controls",
old.getAttribute("controls"));
}
if (old.hasAttribute("style")) {
newEl.setAttribute("style", old.getAttribute("style"));
}
if (old.hasAttribute("class")) {
newEl.setAttribute("class", old.getAttribute("class"));
}
return newEl;
}
if ("iframe".equalsIgnoreCase(old.getTagName())
&& old.hasAttribute("src")
&& old.getAttribute("src").contains("APP/connector")) {
// an iframe reloads when reattached to the DOM, but
// unfortunately, when newEl is reattached, server-side
// resource (e.g. generated PDF) is already gone, so this will
// end up with 404, which might be undesirable.
// Instead of the resource that is not available anymore,
// let's just use empty iframe.
// See
// https://github.com/vaadin/framework/issues/11369
// for details of the issue this works around
Element newEl = old.cloneNode(false).cast();
newEl.setAttribute("src", "about:blank");
return newEl;
}
}
Node res = node.cloneNode(false);
if (node.hasChildNodes()) {
NodeList<Node> nl = node.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node clone = cloneNodeFilteringMedia(nl.getItem(i));
if (clone != null) {
res.appendChild(clone);
}
}
}
return res;
}
@Override
public WindowState getState() {
return (WindowState) super.getState();
}
@Override
public void onStateChanged(StateChangeEvent stateChangeEvent) {
super.onStateChanged(stateChangeEvent);
VWindow window = getWidget();
WindowState state = getState();
if (state.modal != window.vaadinModality) {
window.setVaadinModality(!window.vaadinModality);
}
boolean resizeable = state.resizable
&& state.windowMode == WindowMode.NORMAL;
window.setResizable(resizeable);
window.resizeLazy = state.resizeLazy;
window.setDraggable(
state.draggable && state.windowMode == WindowMode.NORMAL);
window.updateMaximizeRestoreClassName(state.resizable,
state.windowMode);
// Caption must be set before required header size is measured. If
// the caption attribute is missing the caption should be cleared.
String iconURL = null;
if (getIconUri() != null) {
iconURL = getIconUri();
}
window.setAssistivePrefix(state.assistivePrefix);
window.setAssistivePostfix(state.assistivePostfix);
window.setCaption(state.caption, iconURL, getState().captionAsHtml);
window.setWaiAriaRole(getState().role);
window.setAssistiveDescription(state.contentDescription);
window.setTabStopEnabled(getState().assistiveTabStop);
window.setTabStopTopAssistiveText(getState().assistiveTabStopTopText);
window.setTabStopBottomAssistiveText(
getState().assistiveTabStopBottomText);
clickEventHandler.handleEventHandlerRegistration();
window.setClosable(state.closable);
// initialize position from state
updateWindowPosition();
// setting scrollposition must happen after children is rendered
window.contentPanel.setScrollPosition(state.scrollTop);
window.contentPanel.setHorizontalScrollPosition(state.scrollLeft);
// Center this window on screen if requested
// This had to be here because we might not know the content size before
// everything is painted into the window
// centered if this is unset on move/resize
window.centered = state.centered;
// Ensure centering before setting visible (#16486)
if (window.centered && getState().windowMode != WindowMode.MAXIMIZED) {
Scheduler.get().scheduleFinally(() -> {
// the window may have got removed again before this is
// triggered, and centering would re-display it
if (getWidget().isShowing()) {
getWidget().center();
}
});
}
window.setVisible(true);
}
// Need to override default because of window mode
@Override
protected void updateComponentSize() {
if (getState().windowMode == WindowMode.NORMAL) {
super.updateComponentSize();
} else if (getState().windowMode == WindowMode.MAXIMIZED) {
super.updateComponentSize("100%", "100%");
}
}
/**
* Initializes or updates position from state.
*/
protected void updateWindowPosition() {
VWindow window = getWidget();
WindowState state = getState();
if (state.windowMode == WindowMode.NORMAL) {
// if centered, position handled in postLayout()
if (!state.centered
&& (state.positionX >= 0 || state.positionY >= 0)) {
// If both positions are negative, then
// setWindowOrderAndPosition has already taken care of
// positioning the window so it stacks with other windows
window.setPopupPosition(state.positionX, state.positionY);
}
} else if (state.windowMode == WindowMode.MAXIMIZED) {
window.setPopupPositionNoUpdate(0, 0);
}
}
/**
* Updates the window state to match the current mode.
*/
protected void updateWindowMode() {
VWindow window = getWidget();
WindowState state = getState();
// update draggable on widget
window.setDraggable(
state.draggable && state.windowMode == WindowMode.NORMAL);
// update resizable on widget
window.setResizable(
state.resizable && state.windowMode == WindowMode.NORMAL);
updateComponentSize();
updateWindowPosition();
window.updateMaximizeRestoreClassName(state.resizable,
state.windowMode);
window.updateContentsSize();
}
/**
* Maximizes or restores the window depending on the current mode.
*/
protected void onMaximizeRestore() {
WindowState state = getState();
if (state.resizable) {
if (state.windowMode == WindowMode.MAXIMIZED) {
state.windowMode = WindowMode.NORMAL;
} else {
state.windowMode = WindowMode.MAXIMIZED;
}
updateWindowMode();
VWindow window = getWidget();
window.bringToFront();
getRpcProxy(WindowServerRpc.class)
.windowModeChanged(state.windowMode);
}
}
/**
* Gives the WindowConnector an order number. As a side effect, moves the
* window according to its order number so the windows are stacked. This
* method should be called for each window in the order they should appear.
*/
public void setWindowOrderAndPosition() {
getWidget().setWindowOrderAndPosition();
}
@Override
public boolean hasTooltip() {
/*
* Tooltip event handler always needed on the window widget to make sure
* tooltips are properly hidden. (#11448)
*/
return true;
}
@Override
public void onWindowMove(WindowMoveEvent event) {
getRpcProxy(WindowServerRpc.class).windowMoved(event.getNewX(),
event.getNewY());
}
private boolean isKeyEnterOrSpace(int keyCode) {
return keyCode == KeyCodes.KEY_ENTER || keyCode == KeyCodes.KEY_SPACE;
}
}