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

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