Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

WindowConnector.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui.window;
  17. import java.util.logging.Logger;
  18. import com.google.gwt.core.client.Scheduler;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.dom.client.NativeEvent;
  21. import com.google.gwt.dom.client.Node;
  22. import com.google.gwt.dom.client.NodeList;
  23. import com.google.gwt.dom.client.Style;
  24. import com.google.gwt.dom.client.Style.Position;
  25. import com.google.gwt.dom.client.Style.Unit;
  26. import com.google.gwt.event.dom.client.ClickEvent;
  27. import com.google.gwt.event.dom.client.ClickHandler;
  28. import com.google.gwt.event.dom.client.DoubleClickEvent;
  29. import com.google.gwt.event.dom.client.DoubleClickHandler;
  30. import com.google.gwt.user.client.DOM;
  31. import com.google.gwt.user.client.Window;
  32. import com.vaadin.client.ApplicationConnection;
  33. import com.vaadin.client.ComponentConnector;
  34. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  35. import com.vaadin.client.LayoutManager;
  36. import com.vaadin.client.Paintable;
  37. import com.vaadin.client.UIDL;
  38. import com.vaadin.client.communication.StateChangeEvent;
  39. import com.vaadin.client.ui.AbstractSingleComponentContainerConnector;
  40. import com.vaadin.client.ui.ClickEventHandler;
  41. import com.vaadin.client.ui.PostLayoutListener;
  42. import com.vaadin.client.ui.ShortcutActionHandler;
  43. import com.vaadin.client.ui.SimpleManagedLayout;
  44. import com.vaadin.client.ui.VWindow;
  45. import com.vaadin.client.ui.layout.MayScrollChildren;
  46. import com.vaadin.shared.MouseEventDetails;
  47. import com.vaadin.shared.ui.Connect;
  48. import com.vaadin.shared.ui.window.WindowMode;
  49. import com.vaadin.shared.ui.window.WindowServerRpc;
  50. import com.vaadin.shared.ui.window.WindowState;
  51. @Connect(value = com.vaadin.ui.Window.class)
  52. public class WindowConnector extends AbstractSingleComponentContainerConnector
  53. implements Paintable, SimpleManagedLayout, PostLayoutListener,
  54. MayScrollChildren, WindowMoveHandler {
  55. private Node windowClone;
  56. private ClickEventHandler clickEventHandler = new ClickEventHandler(this) {
  57. @Override
  58. protected void fireClick(NativeEvent event,
  59. MouseEventDetails mouseDetails) {
  60. getRpcProxy(WindowServerRpc.class).click(mouseDetails);
  61. }
  62. };
  63. abstract class WindowEventHandler
  64. implements ClickHandler, DoubleClickHandler {
  65. }
  66. private WindowEventHandler maximizeRestoreClickHandler = new WindowEventHandler() {
  67. @Override
  68. public void onClick(ClickEvent event) {
  69. final Element target = event.getNativeEvent().getEventTarget()
  70. .cast();
  71. if (target == getWidget().maximizeRestoreBox) {
  72. // Click on maximize/restore box
  73. onMaximizeRestore();
  74. }
  75. }
  76. @Override
  77. public void onDoubleClick(DoubleClickEvent event) {
  78. final Element target = event.getNativeEvent().getEventTarget()
  79. .cast();
  80. if (getWidget().header.isOrHasChild(target)) {
  81. // Double click on header
  82. onMaximizeRestore();
  83. }
  84. }
  85. };
  86. @Override
  87. public boolean delegateCaptionHandling() {
  88. return false;
  89. }
  90. @Override
  91. protected void init() {
  92. super.init();
  93. VWindow window = getWidget();
  94. window.id = getConnectorId();
  95. window.client = getConnection();
  96. getLayoutManager().registerDependency(this,
  97. window.contentPanel.getElement());
  98. getLayoutManager().registerDependency(this, window.header);
  99. getLayoutManager().registerDependency(this, window.footer);
  100. window.addHandler(maximizeRestoreClickHandler, ClickEvent.getType());
  101. window.addHandler(maximizeRestoreClickHandler,
  102. DoubleClickEvent.getType());
  103. window.setOwner(getConnection().getUIConnector().getWidget());
  104. window.addMoveHandler(this);
  105. }
  106. @Override
  107. public void onUnregister() {
  108. LayoutManager lm = getLayoutManager();
  109. VWindow window = getWidget();
  110. lm.unregisterDependency(this, window.contentPanel.getElement());
  111. lm.unregisterDependency(this, window.header);
  112. lm.unregisterDependency(this, window.footer);
  113. }
  114. @Override
  115. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  116. VWindow window = getWidget();
  117. String connectorId = getConnectorId();
  118. // Workaround needed for Testing Tools (GWT generates window DOM
  119. // slightly different in different browsers).
  120. window.closeBox.setId(connectorId + "_window_close");
  121. window.maximizeRestoreBox
  122. .setId(connectorId + "_window_maximizerestore");
  123. window.visibilityChangesDisabled = true;
  124. if (!isRealUpdate(uidl)) {
  125. return;
  126. }
  127. window.visibilityChangesDisabled = false;
  128. // we may have actions
  129. for (int i = 0; i < uidl.getChildCount(); i++) {
  130. UIDL childUidl = uidl.getChildUIDL(i);
  131. if (childUidl.getTag().equals("actions")) {
  132. if (window.shortcutHandler == null) {
  133. window.shortcutHandler = new ShortcutActionHandler(
  134. connectorId, client);
  135. }
  136. window.shortcutHandler.updateActionMap(childUidl);
  137. }
  138. }
  139. if (uidl.hasAttribute("bringToFront")) {
  140. /*
  141. * Focus as a side-effect. Will be overridden by
  142. * ApplicationConnection if another component was focused by the
  143. * server side.
  144. */
  145. window.contentPanel.focus();
  146. window.bringToFrontSequence = uidl.getIntAttribute("bringToFront");
  147. VWindow.deferOrdering();
  148. }
  149. }
  150. @Override
  151. public void updateCaption(ComponentConnector component) {
  152. // NOP, window has own caption, layout caption not rendered
  153. }
  154. @Override
  155. public VWindow getWidget() {
  156. return (VWindow) super.getWidget();
  157. }
  158. @Override
  159. public void onConnectorHierarchyChange(
  160. ConnectorHierarchyChangeEvent event) {
  161. // We always have 1 child, unless the child is hidden
  162. getWidget().contentPanel.setWidget(getContentWidget());
  163. if (getParent() == null && windowClone != null) {
  164. // If the window is removed from the UI, add the copy of the
  165. // contents to the window (in case of an 'out-animation')
  166. getWidget().getElement().removeAllChildren();
  167. getWidget().getElement().appendChild(windowClone);
  168. // Clean reference
  169. windowClone = null;
  170. }
  171. }
  172. @Override
  173. public void layout() {
  174. LayoutManager lm = getLayoutManager();
  175. VWindow window = getWidget();
  176. // ensure window is not larger than browser window
  177. if (window.getOffsetWidth() > Window.getClientWidth()) {
  178. window.setWidth(Window.getClientWidth() + "px");
  179. lm.setNeedsMeasure(getContent());
  180. }
  181. if (window.getOffsetHeight() > Window.getClientHeight()) {
  182. window.setHeight(Window.getClientHeight() + "px");
  183. lm.setNeedsMeasure(getContent());
  184. }
  185. ComponentConnector content = getContent();
  186. boolean hasContent = content != null;
  187. Element contentElement = window.contentPanel.getElement();
  188. Style contentStyle = window.contents.getStyle();
  189. int headerHeight = lm.getOuterHeight(window.header);
  190. contentStyle.setPaddingTop(headerHeight, Unit.PX);
  191. contentStyle.setMarginTop(-headerHeight, Unit.PX);
  192. int footerHeight = lm.getOuterHeight(window.footer);
  193. contentStyle.setPaddingBottom(footerHeight, Unit.PX);
  194. contentStyle.setMarginBottom(-footerHeight, Unit.PX);
  195. int minWidth = lm.getOuterWidth(window.header)
  196. - lm.getInnerWidth(window.header);
  197. int minHeight = footerHeight + headerHeight;
  198. getWidget().getElement().getStyle().setPropertyPx("minWidth", minWidth);
  199. getWidget().getElement().getStyle().setPropertyPx("minHeight",
  200. minHeight);
  201. /*
  202. * Must set absolute position if the child has relative height and
  203. * there's a chance of horizontal scrolling as some browsers will
  204. * otherwise not take the scrollbar into account when calculating the
  205. * height.
  206. */
  207. if (hasContent) {
  208. Element layoutElement = content.getWidget().getElement();
  209. Style childStyle = layoutElement.getStyle();
  210. if (content.isRelativeHeight()) {
  211. childStyle.setPosition(Position.ABSOLUTE);
  212. Style wrapperStyle = contentElement.getStyle();
  213. if (window.getElement().getStyle().getWidth().isEmpty()
  214. && !content.isRelativeWidth()) {
  215. /*
  216. * Need to lock width to make undefined width work even with
  217. * absolute positioning
  218. */
  219. int contentWidth = lm.getOuterWidth(layoutElement);
  220. wrapperStyle.setWidth(contentWidth, Unit.PX);
  221. } else {
  222. wrapperStyle.clearWidth();
  223. }
  224. } else {
  225. childStyle.clearPosition();
  226. }
  227. }
  228. }
  229. @Override
  230. public void postLayout() {
  231. VWindow window = getWidget();
  232. if (!window.isAttached()) {
  233. Logger.getLogger(WindowConnector.class.getName())
  234. .warning("Called postLayout to detached Window.");
  235. return;
  236. }
  237. if (window.centered && getState().windowMode != WindowMode.MAXIMIZED) {
  238. window.center();
  239. }
  240. window.positionOrSizeUpdated();
  241. if (getParent() != null) {
  242. // Take a copy of the contents, since the server will detach all
  243. // children of this window when it's closed, and the window will be
  244. // emptied during the following hierarchy update (we need to keep
  245. // the contents visible for the duration of a possible
  246. // 'out-animation')
  247. // Fix for #14645 and #14785 - as soon as we clone audio and video
  248. // tags, they start fetching data, and playing immediately in
  249. // background, in case autoplay attribute is present. Therefore we
  250. // have to replace them with stubs in the clone. And we can't just
  251. // erase them, because there are corresponding player widgets to
  252. // animate
  253. windowClone = cloneNodeFilteringMedia(
  254. getWidget().getElement().getFirstChild());
  255. }
  256. }
  257. private Node cloneNodeFilteringMedia(Node node) {
  258. if (node instanceof Element) {
  259. Element old = (Element) node;
  260. if ("audio".equalsIgnoreCase(old.getTagName())
  261. || "video".equalsIgnoreCase(old.getTagName())) {
  262. if (!old.hasAttribute("controls")
  263. && "audio".equalsIgnoreCase(old.getTagName())) {
  264. // nothing to animate, so we won't add this to
  265. // the clone
  266. return null;
  267. }
  268. Element newEl = DOM.createElement(old.getTagName());
  269. if (old.hasAttribute("controls")) {
  270. newEl.setAttribute("controls",
  271. old.getAttribute("controls"));
  272. }
  273. if (old.hasAttribute("style")) {
  274. newEl.setAttribute("style", old.getAttribute("style"));
  275. }
  276. if (old.hasAttribute("class")) {
  277. newEl.setAttribute("class", old.getAttribute("class"));
  278. }
  279. return newEl;
  280. }
  281. }
  282. Node res = node.cloneNode(false);
  283. if (node.hasChildNodes()) {
  284. NodeList<Node> nl = node.getChildNodes();
  285. for (int i = 0; i < nl.getLength(); i++) {
  286. Node clone = cloneNodeFilteringMedia(nl.getItem(i));
  287. if (clone != null) {
  288. res.appendChild(clone);
  289. }
  290. }
  291. }
  292. return res;
  293. }
  294. @Override
  295. public WindowState getState() {
  296. return (WindowState) super.getState();
  297. }
  298. @Override
  299. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  300. super.onStateChanged(stateChangeEvent);
  301. VWindow window = getWidget();
  302. WindowState state = getState();
  303. if (state.modal != window.vaadinModality) {
  304. window.setVaadinModality(!window.vaadinModality);
  305. }
  306. boolean resizeable = state.resizable
  307. && state.windowMode == WindowMode.NORMAL;
  308. window.setResizable(resizeable);
  309. window.resizeLazy = state.resizeLazy;
  310. window.setDraggable(
  311. state.draggable && state.windowMode == WindowMode.NORMAL);
  312. window.updateMaximizeRestoreClassName(state.resizable,
  313. state.windowMode);
  314. // Caption must be set before required header size is measured. If
  315. // the caption attribute is missing the caption should be cleared.
  316. String iconURL = null;
  317. if (getIconUri() != null) {
  318. iconURL = getIconUri();
  319. }
  320. window.setAssistivePrefix(state.assistivePrefix);
  321. window.setAssistivePostfix(state.assistivePostfix);
  322. window.setCaption(state.caption, iconURL, getState().captionAsHtml);
  323. window.setWaiAriaRole(getState().role);
  324. window.setAssistiveDescription(state.contentDescription);
  325. window.setTabStopEnabled(getState().assistiveTabStop);
  326. window.setTabStopTopAssistiveText(getState().assistiveTabStopTopText);
  327. window.setTabStopBottomAssistiveText(
  328. getState().assistiveTabStopBottomText);
  329. clickEventHandler.handleEventHandlerRegistration();
  330. window.setClosable(state.closable);
  331. // initialize position from state
  332. updateWindowPosition();
  333. // setting scrollposition must happen after children is rendered
  334. window.contentPanel.setScrollPosition(state.scrollTop);
  335. window.contentPanel.setHorizontalScrollPosition(state.scrollLeft);
  336. // Center this window on screen if requested
  337. // This had to be here because we might not know the content size before
  338. // everything is painted into the window
  339. // centered is this is unset on move/resize
  340. window.centered = state.centered;
  341. // Ensure centering before setting visible (#16486)
  342. if (window.centered && getState().windowMode != WindowMode.MAXIMIZED) {
  343. Scheduler.get().scheduleFinally(() -> getWidget().center());
  344. }
  345. window.setVisible(true);
  346. }
  347. // Need to override default because of window mode
  348. @Override
  349. protected void updateComponentSize() {
  350. if (getState().windowMode == WindowMode.NORMAL) {
  351. super.updateComponentSize();
  352. } else if (getState().windowMode == WindowMode.MAXIMIZED) {
  353. super.updateComponentSize("100%", "100%");
  354. }
  355. }
  356. protected void updateWindowPosition() {
  357. VWindow window = getWidget();
  358. WindowState state = getState();
  359. if (state.windowMode == WindowMode.NORMAL) {
  360. // if centered, position handled in postLayout()
  361. if (!state.centered
  362. && (state.positionX >= 0 || state.positionY >= 0)) {
  363. // If both positions are negative, then
  364. // setWindowOrderAndPosition has already taken care of
  365. // positioning the window so it stacks with other windows
  366. window.setPopupPosition(state.positionX, state.positionY);
  367. }
  368. } else if (state.windowMode == WindowMode.MAXIMIZED) {
  369. window.setPopupPositionNoUpdate(0, 0);
  370. }
  371. }
  372. protected void updateWindowMode() {
  373. VWindow window = getWidget();
  374. WindowState state = getState();
  375. // update draggable on widget
  376. window.setDraggable(
  377. state.draggable && state.windowMode == WindowMode.NORMAL);
  378. // update resizable on widget
  379. window.setResizable(
  380. state.resizable && state.windowMode == WindowMode.NORMAL);
  381. updateComponentSize();
  382. updateWindowPosition();
  383. window.updateMaximizeRestoreClassName(state.resizable,
  384. state.windowMode);
  385. window.updateContentsSize();
  386. }
  387. protected void onMaximizeRestore() {
  388. WindowState state = getState();
  389. if (state.resizable) {
  390. if (state.windowMode == WindowMode.MAXIMIZED) {
  391. state.windowMode = WindowMode.NORMAL;
  392. } else {
  393. state.windowMode = WindowMode.MAXIMIZED;
  394. }
  395. updateWindowMode();
  396. VWindow window = getWidget();
  397. window.bringToFront();
  398. getRpcProxy(WindowServerRpc.class)
  399. .windowModeChanged(state.windowMode);
  400. }
  401. }
  402. /**
  403. * Gives the WindowConnector an order number. As a side effect, moves the
  404. * window according to its order number so the windows are stacked. This
  405. * method should be called for each window in the order they should appear.
  406. */
  407. public void setWindowOrderAndPosition() {
  408. getWidget().setWindowOrderAndPosition();
  409. }
  410. @Override
  411. public boolean hasTooltip() {
  412. /*
  413. * Tooltip event handler always needed on the window widget to make sure
  414. * tooltips are properly hidden. (#11448)
  415. */
  416. return true;
  417. }
  418. @Override
  419. public void onWindowMove(WindowMoveEvent event) {
  420. getRpcProxy(WindowServerRpc.class).windowMoved(event.getNewX(),
  421. event.getNewY());
  422. }
  423. }