You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WindowConnector.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright 2000-2014 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.dom.client.Element;
  19. import com.google.gwt.dom.client.NativeEvent;
  20. import com.google.gwt.dom.client.Node;
  21. import com.google.gwt.dom.client.NodeList;
  22. import com.google.gwt.dom.client.Style;
  23. import com.google.gwt.dom.client.Style.Position;
  24. import com.google.gwt.dom.client.Style.Unit;
  25. import com.google.gwt.event.dom.client.ClickEvent;
  26. import com.google.gwt.event.dom.client.ClickHandler;
  27. import com.google.gwt.event.dom.client.DoubleClickEvent;
  28. import com.google.gwt.event.dom.client.DoubleClickHandler;
  29. import com.google.gwt.user.client.DOM;
  30. import com.google.gwt.user.client.Event;
  31. import com.google.gwt.user.client.Window;
  32. import com.vaadin.client.ApplicationConnection;
  33. import com.vaadin.client.BrowserInfo;
  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.WidgetUtil;
  40. import com.vaadin.client.communication.RpcProxy;
  41. import com.vaadin.client.communication.StateChangeEvent;
  42. import com.vaadin.client.ui.AbstractSingleComponentContainerConnector;
  43. import com.vaadin.client.ui.ClickEventHandler;
  44. import com.vaadin.client.ui.PostLayoutListener;
  45. import com.vaadin.client.ui.ShortcutActionHandler;
  46. import com.vaadin.client.ui.ShortcutActionHandler.BeforeShortcutActionListener;
  47. import com.vaadin.client.ui.SimpleManagedLayout;
  48. import com.vaadin.client.ui.VWindow;
  49. import com.vaadin.client.ui.layout.MayScrollChildren;
  50. import com.vaadin.shared.MouseEventDetails;
  51. import com.vaadin.shared.ui.Connect;
  52. import com.vaadin.shared.ui.window.WindowMode;
  53. import com.vaadin.shared.ui.window.WindowServerRpc;
  54. import com.vaadin.shared.ui.window.WindowState;
  55. @Connect(value = com.vaadin.ui.Window.class)
  56. public class WindowConnector extends AbstractSingleComponentContainerConnector
  57. implements Paintable, BeforeShortcutActionListener,
  58. SimpleManagedLayout, PostLayoutListener, MayScrollChildren,
  59. WindowMoveHandler {
  60. private Node windowClone;
  61. private ClickEventHandler clickEventHandler = new ClickEventHandler(this) {
  62. @Override
  63. protected void fireClick(NativeEvent event,
  64. MouseEventDetails mouseDetails) {
  65. getRpcProxy(WindowServerRpc.class).click(mouseDetails);
  66. }
  67. };
  68. abstract class WindowEventHandler implements ClickHandler,
  69. DoubleClickHandler {
  70. }
  71. private WindowEventHandler maximizeRestoreClickHandler = new WindowEventHandler() {
  72. @Override
  73. public void onClick(ClickEvent event) {
  74. final Element target = event.getNativeEvent().getEventTarget()
  75. .cast();
  76. if (target == getWidget().maximizeRestoreBox) {
  77. // Click on maximize/restore box
  78. onMaximizeRestore();
  79. }
  80. }
  81. @Override
  82. public void onDoubleClick(DoubleClickEvent event) {
  83. final Element target = event.getNativeEvent().getEventTarget()
  84. .cast();
  85. if (getWidget().header.isOrHasChild(target)) {
  86. // Double click on header
  87. onMaximizeRestore();
  88. }
  89. }
  90. };
  91. @Override
  92. public boolean delegateCaptionHandling() {
  93. return false;
  94. }
  95. @Override
  96. protected void init() {
  97. super.init();
  98. VWindow window = getWidget();
  99. window.id = getConnectorId();
  100. window.client = getConnection();
  101. getLayoutManager().registerDependency(this,
  102. window.contentPanel.getElement());
  103. getLayoutManager().registerDependency(this, window.header);
  104. getLayoutManager().registerDependency(this, window.footer);
  105. window.addHandler(maximizeRestoreClickHandler, ClickEvent.getType());
  106. window.addHandler(maximizeRestoreClickHandler,
  107. DoubleClickEvent.getType());
  108. window.setOwner(getConnection().getUIConnector().getWidget());
  109. window.addMoveHandler(this);
  110. }
  111. @Override
  112. public void onUnregister() {
  113. LayoutManager lm = getLayoutManager();
  114. VWindow window = getWidget();
  115. lm.unregisterDependency(this, window.contentPanel.getElement());
  116. lm.unregisterDependency(this, window.header);
  117. lm.unregisterDependency(this, window.footer);
  118. }
  119. @Override
  120. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  121. VWindow window = getWidget();
  122. String connectorId = getConnectorId();
  123. // Workaround needed for Testing Tools (GWT generates window DOM
  124. // slightly different in different browsers).
  125. window.closeBox.setId(connectorId + "_window_close");
  126. window.maximizeRestoreBox
  127. .setId(connectorId + "_window_maximizerestore");
  128. window.visibilityChangesDisabled = true;
  129. if (!isRealUpdate(uidl)) {
  130. return;
  131. }
  132. window.visibilityChangesDisabled = false;
  133. // we may have actions
  134. for (int i = 0; i < uidl.getChildCount(); i++) {
  135. UIDL childUidl = uidl.getChildUIDL(i);
  136. if (childUidl.getTag().equals("actions")) {
  137. if (window.shortcutHandler == null) {
  138. window.shortcutHandler = new ShortcutActionHandler(
  139. connectorId, client);
  140. }
  141. window.shortcutHandler.updateActionMap(childUidl);
  142. }
  143. }
  144. if (uidl.hasAttribute("bringToFront")) {
  145. /*
  146. * Focus as a side-effect. Will be overridden by
  147. * ApplicationConnection if another component was focused by the
  148. * server side.
  149. */
  150. window.contentPanel.focus();
  151. window.bringToFrontSequence = uidl.getIntAttribute("bringToFront");
  152. VWindow.deferOrdering();
  153. }
  154. }
  155. @Override
  156. public void updateCaption(ComponentConnector component) {
  157. // NOP, window has own caption, layout caption not rendered
  158. }
  159. @Override
  160. public void onBeforeShortcutAction(Event e) {
  161. // NOP, nothing to update just avoid workaround ( causes excess
  162. // blur/focus )
  163. }
  164. @Override
  165. public VWindow getWidget() {
  166. return (VWindow) super.getWidget();
  167. }
  168. @Override
  169. public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
  170. // We always have 1 child, unless the child is hidden
  171. getWidget().contentPanel.setWidget(getContentWidget());
  172. if (getParent() == null && windowClone != null) {
  173. // If the window is removed from the UI, add the copy of the
  174. // contents to the window (in case of an 'out-animation')
  175. getWidget().getElement().removeAllChildren();
  176. getWidget().getElement().appendChild(windowClone);
  177. // Clean reference
  178. windowClone = null;
  179. }
  180. }
  181. @Override
  182. public void layout() {
  183. LayoutManager lm = getLayoutManager();
  184. VWindow window = getWidget();
  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()
  200. .setPropertyPx("minHeight", 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. // IE8 needs some hackery to measure its content correctly
  211. WidgetUtil.forceIE8Redraw(layoutElement);
  212. if (content.isRelativeHeight() && !BrowserInfo.get().isIE9()) {
  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()).warning(
  236. "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(getWidget().getElement()
  256. .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", 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. if (!window.isAttached()) {
  307. window.setVisible(false); // hide until possible centering
  308. window.show();
  309. }
  310. boolean resizeable = state.resizable
  311. && state.windowMode == WindowMode.NORMAL;
  312. window.setResizable(resizeable);
  313. window.resizeLazy = state.resizeLazy;
  314. window.setDraggable(state.draggable
  315. && state.windowMode == WindowMode.NORMAL);
  316. window.updateMaximizeRestoreClassName(state.resizable, state.windowMode);
  317. // Caption must be set before required header size is measured. If
  318. // the caption attribute is missing the caption should be cleared.
  319. String iconURL = null;
  320. if (getIconUri() != null) {
  321. iconURL = getIconUri();
  322. }
  323. window.setAssistivePrefix(state.assistivePrefix);
  324. window.setAssistivePostfix(state.assistivePostfix);
  325. window.setCaption(state.caption, iconURL);
  326. window.setWaiAriaRole(getState().role);
  327. window.setAssistiveDescription(state.contentDescription);
  328. window.setTabStopEnabled(getState().assistiveTabStop);
  329. window.setTabStopTopAssistiveText(getState().assistiveTabStopTopText);
  330. window.setTabStopBottomAssistiveText(getState().assistiveTabStopBottomText);
  331. clickEventHandler.handleEventHandlerRegistration();
  332. window.immediate = state.immediate;
  333. window.setClosable(!isReadOnly());
  334. // initialize position from state
  335. updateWindowPosition();
  336. // setting scrollposition must happen after children is rendered
  337. window.contentPanel.setScrollPosition(state.scrollTop);
  338. window.contentPanel.setHorizontalScrollPosition(state.scrollLeft);
  339. // Center this window on screen if requested
  340. // This had to be here because we might not know the content size before
  341. // everything is painted into the window
  342. // centered is this is unset on move/resize
  343. window.centered = state.centered;
  344. window.setVisible(true);
  345. // ensure window is not larger than browser window
  346. if (window.getOffsetWidth() > Window.getClientWidth()) {
  347. window.setWidth(Window.getClientWidth() + "px");
  348. }
  349. if (window.getOffsetHeight() > Window.getClientHeight()) {
  350. window.setHeight(Window.getClientHeight() + "px");
  351. }
  352. }
  353. // Need to override default because of window mode
  354. @Override
  355. protected void updateComponentSize() {
  356. if (getState().windowMode == WindowMode.NORMAL) {
  357. super.updateComponentSize();
  358. } else if (getState().windowMode == WindowMode.MAXIMIZED) {
  359. super.updateComponentSize("100%", "100%");
  360. }
  361. }
  362. protected void updateWindowPosition() {
  363. VWindow window = getWidget();
  364. WindowState state = getState();
  365. if (state.windowMode == WindowMode.NORMAL) {
  366. // if centered, position handled in postLayout()
  367. if (!state.centered
  368. && (state.positionX >= 0 || state.positionY >= 0)) {
  369. // If both positions are negative, then
  370. // setWindowOrderAndPosition has already taken care of
  371. // positioning the window so it stacks with other windows
  372. window.setPopupPosition(state.positionX, state.positionY);
  373. }
  374. } else if (state.windowMode == WindowMode.MAXIMIZED) {
  375. window.setPopupPositionNoUpdate(0, 0);
  376. window.bringToFront();
  377. }
  378. }
  379. protected void updateWindowMode() {
  380. VWindow window = getWidget();
  381. WindowState state = getState();
  382. // update draggable on widget
  383. window.setDraggable(state.draggable
  384. && state.windowMode == WindowMode.NORMAL);
  385. // update resizable on widget
  386. window.setResizable(state.resizable
  387. && state.windowMode == WindowMode.NORMAL);
  388. updateComponentSize();
  389. updateWindowPosition();
  390. window.updateMaximizeRestoreClassName(state.resizable, state.windowMode);
  391. window.updateContentsSize();
  392. }
  393. protected void onMaximizeRestore() {
  394. WindowState state = getState();
  395. if (state.resizable) {
  396. if (state.windowMode == WindowMode.MAXIMIZED) {
  397. state.windowMode = WindowMode.NORMAL;
  398. } else {
  399. state.windowMode = WindowMode.MAXIMIZED;
  400. }
  401. updateWindowMode();
  402. getRpcProxy(WindowServerRpc.class).windowModeChanged(
  403. state.windowMode);
  404. }
  405. }
  406. /**
  407. * Gives the WindowConnector an order number. As a side effect, moves the
  408. * window according to its order number so the windows are stacked. This
  409. * method should be called for each window in the order they should appear.
  410. */
  411. public void setWindowOrderAndPosition() {
  412. getWidget().setWindowOrderAndPosition();
  413. }
  414. @Override
  415. public boolean hasTooltip() {
  416. /*
  417. * Tooltip event handler always needed on the window widget to make sure
  418. * tooltips are properly hidden. (#11448)
  419. */
  420. return true;
  421. }
  422. @Override
  423. public void onWindowMove(WindowMoveEvent event) {
  424. RpcProxy.create(WindowServerRpc.class, this).windowMoved(
  425. event.getNewX(), event.getNewY());
  426. }
  427. }