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.

VWindow.java 51KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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;
  17. import static com.vaadin.client.WidgetUtil.isFocusedElementEditable;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.Comparator;
  22. import java.util.List;
  23. import com.google.gwt.aria.client.Id;
  24. import com.google.gwt.aria.client.RelevantValue;
  25. import com.google.gwt.aria.client.Roles;
  26. import com.google.gwt.core.client.Scheduler;
  27. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  28. import com.google.gwt.dom.client.Document;
  29. import com.google.gwt.dom.client.Element;
  30. import com.google.gwt.dom.client.NativeEvent;
  31. import com.google.gwt.dom.client.Style.Position;
  32. import com.google.gwt.dom.client.Style.Unit;
  33. import com.google.gwt.dom.client.Style.Visibility;
  34. import com.google.gwt.event.dom.client.BlurEvent;
  35. import com.google.gwt.event.dom.client.BlurHandler;
  36. import com.google.gwt.event.dom.client.FocusEvent;
  37. import com.google.gwt.event.dom.client.FocusHandler;
  38. import com.google.gwt.event.dom.client.KeyCodes;
  39. import com.google.gwt.event.dom.client.KeyDownEvent;
  40. import com.google.gwt.event.dom.client.KeyDownHandler;
  41. import com.google.gwt.event.dom.client.ScrollEvent;
  42. import com.google.gwt.event.dom.client.ScrollHandler;
  43. import com.google.gwt.event.shared.HandlerManager;
  44. import com.google.gwt.event.shared.HandlerRegistration;
  45. import com.google.gwt.user.client.Command;
  46. import com.google.gwt.user.client.DOM;
  47. import com.google.gwt.user.client.Event;
  48. import com.google.gwt.user.client.Event.NativePreviewEvent;
  49. import com.google.gwt.user.client.Event.NativePreviewHandler;
  50. import com.google.gwt.user.client.Window;
  51. import com.google.gwt.user.client.ui.Widget;
  52. import com.vaadin.client.ApplicationConnection;
  53. import com.vaadin.client.BrowserInfo;
  54. import com.vaadin.client.ComponentConnector;
  55. import com.vaadin.client.ConnectorMap;
  56. import com.vaadin.client.Focusable;
  57. import com.vaadin.client.LayoutManager;
  58. import com.vaadin.client.WidgetUtil;
  59. import com.vaadin.client.debug.internal.VDebugWindow;
  60. import com.vaadin.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
  61. import com.vaadin.client.ui.aria.AriaHelper;
  62. import com.vaadin.client.ui.window.WindowMoveEvent;
  63. import com.vaadin.client.ui.window.WindowMoveHandler;
  64. import com.vaadin.client.ui.window.WindowOrderEvent;
  65. import com.vaadin.client.ui.window.WindowOrderHandler;
  66. import com.vaadin.shared.Connector;
  67. import com.vaadin.shared.EventId;
  68. import com.vaadin.shared.ui.window.WindowMode;
  69. import com.vaadin.shared.ui.window.WindowRole;
  70. /**
  71. * "Sub window" component.
  72. *
  73. * @author Vaadin Ltd
  74. */
  75. public class VWindow extends VOverlay implements ShortcutActionHandlerOwner,
  76. ScrollHandler, KeyDownHandler, FocusHandler, BlurHandler, Focusable {
  77. private static List<VWindow> windowOrder = new ArrayList<>();
  78. private static HandlerManager WINDOW_ORDER_HANDLER = new HandlerManager(
  79. VWindow.class);
  80. private static boolean orderingDefered;
  81. public static final String CLASSNAME = "v-window";
  82. private static final String MODAL_WINDOW_OPEN_CLASSNAME = "v-modal-window-open";
  83. private static final int STACKING_OFFSET_PIXELS = 15;
  84. public static final int Z_INDEX = 10000;
  85. /** For internal use only. May be removed or replaced in the future. */
  86. public Element contents;
  87. /** For internal use only. May be removed or replaced in the future. */
  88. public Element header;
  89. /** For internal use only. May be removed or replaced in the future. */
  90. public Element footer;
  91. private Element resizeBox;
  92. /** For internal use only. May be removed or replaced in the future. */
  93. public final FocusableScrollPanel contentPanel = new FocusableScrollPanel();
  94. private boolean dragging;
  95. private int startX;
  96. private int startY;
  97. private int origX;
  98. private int origY;
  99. private boolean resizing;
  100. private int origW;
  101. private int origH;
  102. /** For internal use only. May be removed or replaced in the future. */
  103. public Element closeBox;
  104. /** For internal use only. May be removed or replaced in the future. */
  105. public Element maximizeRestoreBox;
  106. /** For internal use only. May be removed or replaced in the future. */
  107. public ApplicationConnection client;
  108. /** For internal use only. May be removed or replaced in the future. */
  109. public String id;
  110. /** For internal use only. May be removed or replaced in the future. */
  111. public ShortcutActionHandler shortcutHandler;
  112. /**
  113. * Last known positionx read from UIDL or updated to application connection
  114. */
  115. private int uidlPositionX = -1;
  116. /**
  117. * Last known positiony read from UIDL or updated to application connection
  118. */
  119. private int uidlPositionY = -1;
  120. /** For internal use only. May be removed or replaced in the future. */
  121. public boolean vaadinModality = false;
  122. /** For internal use only. May be removed or replaced in the future. */
  123. public boolean resizable = true;
  124. private boolean draggable = true;
  125. /** For internal use only. May be removed or replaced in the future. */
  126. public boolean resizeLazy = false;
  127. private Element modalityCurtain;
  128. private Element draggingCurtain;
  129. private Element resizingCurtain;
  130. private Element headerText;
  131. private boolean closable = true;
  132. private Connector[] assistiveConnectors = new Connector[0];
  133. private String assistivePrefix;
  134. private String assistivePostfix;
  135. private Element topTabStop;
  136. private Element bottomTabStop;
  137. private NativePreviewHandler topEventBlocker;
  138. private NativePreviewHandler bottomEventBlocker;
  139. private HandlerRegistration topBlockerRegistration;
  140. private HandlerRegistration bottomBlockerRegistration;
  141. // Prevents leaving the window with the Tab key when true
  142. private boolean doTabStop;
  143. /**
  144. * If centered (via UIDL), the window should stay in the centered -mode
  145. * until a position is received from the server, or the user moves or
  146. * resizes the window.
  147. * <p>
  148. * For internal use only. May be removed or replaced in the future.
  149. */
  150. public boolean centered = false;
  151. private Element wrapper;
  152. /** For internal use only. May be removed or replaced in the future. */
  153. public boolean visibilityChangesDisabled;
  154. /** For internal use only. May be removed or replaced in the future. */
  155. public int bringToFrontSequence = -1;
  156. private VLazyExecutor delayedContentsSizeUpdater = new VLazyExecutor(200,
  157. new ScheduledCommand() {
  158. @Override
  159. public void execute() {
  160. updateContentsSize();
  161. }
  162. });
  163. public VWindow() {
  164. super(false, false); // no autohide, not modal
  165. Roles.getDialogRole().set(getElement());
  166. Roles.getDialogRole().setAriaRelevantProperty(getElement(),
  167. RelevantValue.ADDITIONS);
  168. constructDOM();
  169. contentPanel.addScrollHandler(this);
  170. contentPanel.addKeyDownHandler(this);
  171. contentPanel.addFocusHandler(this);
  172. contentPanel.addBlurHandler(this);
  173. addTransitionEndLayoutListener(getElement());
  174. }
  175. @Override
  176. protected void onAttach() {
  177. super.onAttach();
  178. /*
  179. * Stores the element that has focus in the application UI when the
  180. * window is opened, so it can be restored when the window closes.
  181. *
  182. * This is currently implemented for the case when one non-modal window
  183. * can be open at the same time, and the focus is not changed while the
  184. * window is open.
  185. */
  186. getApplicationConnection().getUIConnector().getWidget().storeFocus();
  187. /*
  188. * When this window gets reattached, set the tabstop to the previous
  189. * state.
  190. */
  191. setTabStopEnabled(doTabStop);
  192. }
  193. @Override
  194. protected void onDetach() {
  195. super.onDetach();
  196. /*
  197. * Restores the previously stored focused element.
  198. *
  199. * When the focus was changed outside the window while the window was
  200. * open, the originally stored element is restored.
  201. */
  202. getApplicationConnection().getUIConnector().getWidget()
  203. .focusStoredElement();
  204. removeTabBlockHandlers();
  205. }
  206. private void addTabBlockHandlers() {
  207. if (topBlockerRegistration == null) {
  208. topBlockerRegistration = Event
  209. .addNativePreviewHandler(topEventBlocker);
  210. bottomBlockerRegistration = Event
  211. .addNativePreviewHandler(bottomEventBlocker);
  212. }
  213. }
  214. private void removeTabBlockHandlers() {
  215. if (topBlockerRegistration != null) {
  216. topBlockerRegistration.removeHandler();
  217. topBlockerRegistration = null;
  218. bottomBlockerRegistration.removeHandler();
  219. bottomBlockerRegistration = null;
  220. }
  221. }
  222. public void bringToFront() {
  223. bringToFront(true);
  224. }
  225. private void bringToFront(boolean notifyListeners) {
  226. int curIndex = getWindowOrder();
  227. if (curIndex + 1 < windowOrder.size()) {
  228. windowOrder.remove(this);
  229. windowOrder.add(this);
  230. for (; curIndex < windowOrder.size(); curIndex++) {
  231. VWindow window = windowOrder.get(curIndex);
  232. window.setWindowOrder(curIndex);
  233. }
  234. }
  235. if (notifyListeners) {
  236. fireOrderEvent();
  237. }
  238. }
  239. static void fireOrderEvent() {
  240. fireOrderEvent(windowOrder);
  241. }
  242. private void doFireOrderEvent() {
  243. List<VWindow> list = new ArrayList<>();
  244. list.add(this);
  245. fireOrderEvent(list);
  246. }
  247. private static void fireOrderEvent(List<VWindow> windows) {
  248. WINDOW_ORDER_HANDLER
  249. .fireEvent(new WindowOrderEvent(new ArrayList<>(windows)));
  250. }
  251. /**
  252. * Returns true if this window is the topmost VWindow
  253. *
  254. * @return
  255. */
  256. private boolean isActive() {
  257. return equals(getTopmostWindow());
  258. }
  259. private static VWindow getTopmostWindow() {
  260. if (!windowOrder.isEmpty()) {
  261. return windowOrder.get(windowOrder.size() - 1);
  262. }
  263. return null;
  264. }
  265. /** For internal use only. May be removed or replaced in the future. */
  266. public void setWindowOrderAndPosition() {
  267. // This cannot be done in the constructor as the widgets are created in
  268. // a different order than on they should appear on screen
  269. if (windowOrder.contains(this)) {
  270. // Already set
  271. return;
  272. }
  273. final int order = windowOrder.size();
  274. setWindowOrder(order);
  275. windowOrder.add(this);
  276. setPopupPosition(order * STACKING_OFFSET_PIXELS,
  277. order * STACKING_OFFSET_PIXELS);
  278. doFireOrderEvent();
  279. }
  280. private void setWindowOrder(int order) {
  281. setZIndex(order + Z_INDEX);
  282. }
  283. /**
  284. * Returns window position in list of opened and shown windows.
  285. *
  286. * @since 8.0
  287. */
  288. public final int getWindowOrder() {
  289. return windowOrder.indexOf(this);
  290. }
  291. @Override
  292. protected void setZIndex(int zIndex) {
  293. super.setZIndex(zIndex);
  294. if (vaadinModality) {
  295. getModalityCurtain().getStyle().setZIndex(zIndex);
  296. }
  297. }
  298. protected com.google.gwt.user.client.Element getModalityCurtain() {
  299. if (modalityCurtain == null) {
  300. modalityCurtain = DOM.createDiv();
  301. modalityCurtain.setClassName(CLASSNAME + "-modalitycurtain");
  302. }
  303. return DOM.asOld(modalityCurtain);
  304. }
  305. protected void constructDOM() {
  306. setStyleName(CLASSNAME);
  307. topTabStop = DOM.createDiv();
  308. DOM.setElementAttribute(topTabStop, "tabindex", "0");
  309. header = DOM.createDiv();
  310. DOM.setElementProperty(header, "className", CLASSNAME + "-outerheader");
  311. headerText = DOM.createDiv();
  312. DOM.setElementProperty(headerText, "className", CLASSNAME + "-header");
  313. contents = DOM.createDiv();
  314. DOM.setElementProperty(contents, "className", CLASSNAME + "-contents");
  315. footer = DOM.createDiv();
  316. DOM.setElementProperty(footer, "className", CLASSNAME + "-footer");
  317. resizeBox = DOM.createDiv();
  318. DOM.setElementProperty(resizeBox, "className",
  319. CLASSNAME + "-resizebox");
  320. closeBox = DOM.createDiv();
  321. maximizeRestoreBox = DOM.createDiv();
  322. DOM.setElementProperty(maximizeRestoreBox, "className",
  323. CLASSNAME + "-maximizebox");
  324. DOM.setElementAttribute(maximizeRestoreBox, "tabindex", "0");
  325. DOM.setElementProperty(closeBox, "className", CLASSNAME + "-closebox");
  326. DOM.setElementAttribute(closeBox, "tabindex", "0");
  327. DOM.appendChild(footer, resizeBox);
  328. bottomTabStop = DOM.createDiv();
  329. DOM.setElementAttribute(bottomTabStop, "tabindex", "0");
  330. wrapper = DOM.createDiv();
  331. DOM.setElementProperty(wrapper, "className", CLASSNAME + "-wrap");
  332. DOM.appendChild(wrapper, topTabStop);
  333. DOM.appendChild(wrapper, header);
  334. DOM.appendChild(header, maximizeRestoreBox);
  335. DOM.appendChild(header, closeBox);
  336. DOM.appendChild(header, headerText);
  337. DOM.appendChild(wrapper, contents);
  338. DOM.appendChild(wrapper, footer);
  339. DOM.appendChild(wrapper, bottomTabStop);
  340. DOM.appendChild(super.getContainerElement(), wrapper);
  341. sinkEvents(Event.ONDBLCLICK | Event.MOUSEEVENTS | Event.TOUCHEVENTS
  342. | Event.ONCLICK | Event.ONLOSECAPTURE);
  343. setWidget(contentPanel);
  344. // Make the closebox accessible for assistive devices
  345. Roles.getButtonRole().set(closeBox);
  346. Roles.getButtonRole().setAriaLabelProperty(closeBox, "close button");
  347. // Make the maximizebox accessible for assistive devices
  348. Roles.getButtonRole().set(maximizeRestoreBox);
  349. Roles.getButtonRole().setAriaLabelProperty(maximizeRestoreBox,
  350. "maximize button");
  351. // Provide the title to assistive devices
  352. AriaHelper.ensureHasId(headerText);
  353. Roles.getDialogRole().setAriaLabelledbyProperty(getElement(),
  354. Id.of(headerText));
  355. // Handlers to Prevent tab to leave the window
  356. // and backspace to cause browser navigation
  357. topEventBlocker = new NativePreviewHandler() {
  358. @Override
  359. public void onPreviewNativeEvent(NativePreviewEvent event) {
  360. NativeEvent nativeEvent = event.getNativeEvent();
  361. if (nativeEvent.getEventTarget().cast() == topTabStop
  362. && nativeEvent.getKeyCode() == KeyCodes.KEY_TAB
  363. && nativeEvent.getShiftKey()) {
  364. nativeEvent.preventDefault();
  365. }
  366. if (nativeEvent.getEventTarget().cast() == topTabStop
  367. && nativeEvent.getKeyCode() == KeyCodes.KEY_BACKSPACE) {
  368. nativeEvent.preventDefault();
  369. }
  370. }
  371. };
  372. bottomEventBlocker = new NativePreviewHandler() {
  373. @Override
  374. public void onPreviewNativeEvent(NativePreviewEvent event) {
  375. NativeEvent nativeEvent = event.getNativeEvent();
  376. if (nativeEvent.getEventTarget().cast() == bottomTabStop
  377. && nativeEvent.getKeyCode() == KeyCodes.KEY_TAB
  378. && !nativeEvent.getShiftKey()) {
  379. nativeEvent.preventDefault();
  380. }
  381. if (nativeEvent.getEventTarget().cast() == bottomTabStop
  382. && nativeEvent.getKeyCode() == KeyCodes.KEY_BACKSPACE) {
  383. nativeEvent.preventDefault();
  384. }
  385. }
  386. };
  387. }
  388. /**
  389. * Sets the message that is provided to users of assistive devices when the
  390. * user reaches the top of the window when leaving a window with the tab key
  391. * is prevented.
  392. * <p>
  393. * This message is not visible on the screen.
  394. *
  395. * @param topMessage
  396. * String provided when the user navigates with Shift-Tab keys to
  397. * the top of the window
  398. */
  399. public void setTabStopTopAssistiveText(String topMessage) {
  400. Roles.getNoteRole().setAriaLabelProperty(topTabStop, topMessage);
  401. }
  402. /**
  403. * Sets the message that is provided to users of assistive devices when the
  404. * user reaches the bottom of the window when leaving a window with the tab
  405. * key is prevented.
  406. * <p>
  407. * This message is not visible on the screen.
  408. *
  409. * @param bottomMessage
  410. * String provided when the user navigates with the Tab key to
  411. * the bottom of the window
  412. */
  413. public void setTabStopBottomAssistiveText(String bottomMessage) {
  414. Roles.getNoteRole().setAriaLabelProperty(bottomTabStop, bottomMessage);
  415. }
  416. /**
  417. * Gets the message that is provided to users of assistive devices when the
  418. * user reaches the top of the window when leaving a window with the tab key
  419. * is prevented.
  420. *
  421. * @return the top message
  422. */
  423. public String getTabStopTopAssistiveText() {
  424. return Roles.getNoteRole().getAriaLabelProperty(topTabStop);
  425. }
  426. /**
  427. * Gets the message that is provided to users of assistive devices when the
  428. * user reaches the bottom of the window when leaving a window with the tab
  429. * key is prevented.
  430. *
  431. * @return the bottom message
  432. */
  433. public String getTabStopBottomAssistiveText() {
  434. return Roles.getNoteRole().getAriaLabelProperty(bottomTabStop);
  435. }
  436. /**
  437. * Calling this method will defer ordering algorithm, to order windows based
  438. * on servers bringToFront and modality instructions. Non changed windows
  439. * will be left intact.
  440. * <p>
  441. * For internal use only. May be removed or replaced in the future.
  442. */
  443. public static void deferOrdering() {
  444. if (!orderingDefered) {
  445. orderingDefered = true;
  446. Scheduler.get().scheduleFinally(new Command() {
  447. @Override
  448. public void execute() {
  449. doServerSideOrdering();
  450. VNotification.bringNotificationsToFront();
  451. }
  452. });
  453. }
  454. }
  455. private static void doServerSideOrdering() {
  456. orderingDefered = false;
  457. VWindow[] array = windowOrder.toArray(new VWindow[windowOrder.size()]);
  458. Arrays.sort(array, new Comparator<VWindow>() {
  459. @Override
  460. public int compare(VWindow o1, VWindow o2) {
  461. /*
  462. * Order by modality, then by bringtofront sequence.
  463. */
  464. if (o1.vaadinModality && !o2.vaadinModality) {
  465. return 1;
  466. } else if (!o1.vaadinModality && o2.vaadinModality) {
  467. return -1;
  468. } else if (o1.bringToFrontSequence > o2.bringToFrontSequence) {
  469. return 1;
  470. } else if (o1.bringToFrontSequence < o2.bringToFrontSequence) {
  471. return -1;
  472. } else {
  473. return 0;
  474. }
  475. }
  476. });
  477. for (VWindow w : array) {
  478. if (w.bringToFrontSequence != -1 || w.vaadinModality) {
  479. w.bringToFront(false);
  480. w.bringToFrontSequence = -1;
  481. }
  482. }
  483. focusTopmostModalWindow();
  484. }
  485. private static void focusTopmostModalWindow() {
  486. VWindow topmost = getTopmostWindow();
  487. if (topmost != null && topmost.vaadinModality) {
  488. topmost.focus();
  489. }
  490. fireOrderEvent();
  491. }
  492. @Override
  493. public void setVisible(boolean visible) {
  494. /*
  495. * Visibility with VWindow works differently than with other Paintables
  496. * in Vaadin. Invisible VWindows are not attached to DOM at all. Flag is
  497. * used to avoid visibility call from
  498. * ApplicationConnection.updateComponent();
  499. */
  500. if (!visibilityChangesDisabled) {
  501. super.setVisible(visible);
  502. }
  503. if (visible && BrowserInfo.get()
  504. .requiresPositionAbsoluteOverflowAutoFix()) {
  505. /*
  506. * Shake up the DOM a bit to make the window shed unnecessary
  507. * scrollbars and resize correctly afterwards. The version fixing
  508. * ticket #11994 which was changing the size to 110% was replaced
  509. * with this due to ticket #12943
  510. */
  511. WidgetUtil
  512. .runWebkitOverflowAutoFix(contents.getFirstChildElement());
  513. }
  514. }
  515. /** For internal use only. May be removed or replaced in the future. */
  516. public void setDraggable(boolean draggable) {
  517. if (this.draggable == draggable) {
  518. return;
  519. }
  520. this.draggable = draggable;
  521. setCursorProperties();
  522. }
  523. private void setCursorProperties() {
  524. if (!draggable) {
  525. header.getStyle().setProperty("cursor", "default");
  526. footer.getStyle().setProperty("cursor", "default");
  527. } else {
  528. header.getStyle().setProperty("cursor", "");
  529. footer.getStyle().setProperty("cursor", "");
  530. }
  531. }
  532. /**
  533. * Sets the closable state of the window. Additionally hides/shows the close
  534. * button according to the new state.
  535. *
  536. * @param closable
  537. * true if the window can be closed by the user
  538. */
  539. public void setClosable(boolean closable) {
  540. if (this.closable == closable) {
  541. return;
  542. }
  543. this.closable = closable;
  544. if (closable) {
  545. DOM.setElementProperty(closeBox, "className",
  546. CLASSNAME + "-closebox");
  547. } else {
  548. DOM.setElementProperty(closeBox, "className", CLASSNAME
  549. + "-closebox " + CLASSNAME + "-closebox-disabled");
  550. }
  551. }
  552. /**
  553. * Returns the closable state of the sub window. If the sub window is
  554. * closable a decoration (typically an X) is shown to the user. By clicking
  555. * on the X the user can close the window.
  556. *
  557. * @return true if the sub window is closable
  558. */
  559. protected boolean isClosable() {
  560. return closable;
  561. }
  562. @Override
  563. public void show() {
  564. if (!windowOrder.contains(this)) {
  565. // This is needed if the window is hidden and then shown again.
  566. // Otherwise this VWindow is added to windowOrder in the
  567. // constructor.
  568. windowOrder.add(this);
  569. }
  570. if (vaadinModality) {
  571. showModalityCurtain();
  572. }
  573. super.show();
  574. }
  575. @Override
  576. public void hide() {
  577. if (vaadinModality) {
  578. hideModalityCurtain();
  579. hideDraggingCurtain();
  580. hideResizingCurtain();
  581. }
  582. super.hide();
  583. int curIndex = getWindowOrder();
  584. // Remove window from windowOrder to avoid references being left
  585. // hanging.
  586. windowOrder.remove(curIndex);
  587. // Update the z-indices of any remaining windows
  588. List<VWindow> update = new ArrayList<>(
  589. windowOrder.size() - curIndex + 1);
  590. update.add(this);
  591. while (curIndex < windowOrder.size()) {
  592. VWindow window = windowOrder.get(curIndex);
  593. window.setWindowOrder(curIndex++);
  594. update.add(window);
  595. }
  596. focusTopmostModalWindow();
  597. fireOrderEvent(update);
  598. }
  599. /** For internal use only. May be removed or replaced in the future. */
  600. public void setVaadinModality(boolean modality) {
  601. vaadinModality = modality;
  602. if (vaadinModality) {
  603. if (isAttached()) {
  604. showModalityCurtain();
  605. }
  606. addTabBlockHandlers();
  607. deferOrdering();
  608. } else {
  609. if (modalityCurtain != null) {
  610. if (isAttached()) {
  611. hideModalityCurtain();
  612. }
  613. modalityCurtain = null;
  614. }
  615. if (!doTabStop) {
  616. removeTabBlockHandlers();
  617. }
  618. }
  619. }
  620. private void showModalityCurtain() {
  621. getModalityCurtain().getStyle().setZIndex(getWindowOrder() + Z_INDEX);
  622. if (isShowing()) {
  623. getOverlayContainer().insertBefore(getModalityCurtain(),
  624. getElement());
  625. } else {
  626. getOverlayContainer().appendChild(getModalityCurtain());
  627. }
  628. Document.get().getBody().addClassName(MODAL_WINDOW_OPEN_CLASSNAME);
  629. }
  630. private void hideModalityCurtain() {
  631. Document.get().getBody().removeClassName(MODAL_WINDOW_OPEN_CLASSNAME);
  632. modalityCurtain.removeFromParent();
  633. // IE leaks memory in certain cases unless we release the reference
  634. // (#9197)
  635. modalityCurtain = null;
  636. }
  637. /*
  638. * Shows an empty div on top of all other content; used when moving, so that
  639. * iframes (etc) do not steal event.
  640. */
  641. private void showDraggingCurtain() {
  642. getElement().getParentElement().insertBefore(getDraggingCurtain(),
  643. getElement());
  644. }
  645. private void hideDraggingCurtain() {
  646. if (draggingCurtain != null) {
  647. draggingCurtain.removeFromParent();
  648. }
  649. }
  650. /*
  651. * Shows an empty div on top of all other content; used when resizing, so
  652. * that iframes (etc) do not steal event.
  653. */
  654. private void showResizingCurtain() {
  655. getElement().getParentElement().insertBefore(getResizingCurtain(),
  656. getElement());
  657. }
  658. private void hideResizingCurtain() {
  659. if (resizingCurtain != null) {
  660. resizingCurtain.removeFromParent();
  661. }
  662. }
  663. private Element getDraggingCurtain() {
  664. if (draggingCurtain == null) {
  665. draggingCurtain = createCurtain();
  666. draggingCurtain.setClassName(CLASSNAME + "-draggingCurtain");
  667. }
  668. return draggingCurtain;
  669. }
  670. private Element getResizingCurtain() {
  671. if (resizingCurtain == null) {
  672. resizingCurtain = createCurtain();
  673. resizingCurtain.setClassName(CLASSNAME + "-resizingCurtain");
  674. }
  675. return resizingCurtain;
  676. }
  677. private Element createCurtain() {
  678. Element curtain = DOM.createDiv();
  679. curtain.getStyle().setPosition(Position.ABSOLUTE);
  680. curtain.getStyle().setTop(0, Unit.PX);
  681. curtain.getStyle().setLeft(0, Unit.PX);
  682. curtain.getStyle().setWidth(100, Unit.PCT);
  683. curtain.getStyle().setHeight(100, Unit.PCT);
  684. curtain.getStyle().setZIndex(VOverlay.Z_INDEX);
  685. return curtain;
  686. }
  687. /** For internal use only. May be removed or replaced in the future. */
  688. public void setResizable(boolean resizability) {
  689. resizable = resizability;
  690. if (resizability) {
  691. DOM.setElementProperty(footer, "className", CLASSNAME + "-footer");
  692. DOM.setElementProperty(resizeBox, "className",
  693. CLASSNAME + "-resizebox");
  694. } else {
  695. DOM.setElementProperty(footer, "className",
  696. CLASSNAME + "-footer " + CLASSNAME + "-footer-noresize");
  697. DOM.setElementProperty(resizeBox, "className", CLASSNAME
  698. + "-resizebox " + CLASSNAME + "-resizebox-disabled");
  699. }
  700. }
  701. public void updateMaximizeRestoreClassName(boolean visible,
  702. WindowMode windowMode) {
  703. String className;
  704. if (windowMode == WindowMode.MAXIMIZED) {
  705. className = CLASSNAME + "-restorebox";
  706. } else {
  707. className = CLASSNAME + "-maximizebox";
  708. }
  709. if (!visible) {
  710. className = className + " " + className + "-disabled";
  711. }
  712. maximizeRestoreBox.setClassName(className);
  713. }
  714. // TODO this will eventually be removed, currently used to avoid updating to
  715. // server side.
  716. public void setPopupPositionNoUpdate(int left, int top) {
  717. if (top < 0) {
  718. // ensure window is not moved out of browser window from top of the
  719. // screen
  720. top = 0;
  721. }
  722. super.setPopupPosition(left, top);
  723. }
  724. @Override
  725. public void setPopupPosition(int left, int top) {
  726. if (top < 0) {
  727. // ensure window is not moved out of browser window from top of the
  728. // screen
  729. top = 0;
  730. }
  731. super.setPopupPosition(left, top);
  732. if (left != uidlPositionX && client != null) {
  733. client.updateVariable(id, "positionx", left, false);
  734. uidlPositionX = left;
  735. }
  736. if (top != uidlPositionY && client != null) {
  737. client.updateVariable(id, "positiony", top, false);
  738. uidlPositionY = top;
  739. }
  740. }
  741. public void setCaption(String c) {
  742. setCaption(c, null);
  743. }
  744. public void setCaption(String c, String iconURL) {
  745. setCaption(c, iconURL, false);
  746. }
  747. public void setCaption(String c, String iconURL, boolean asHtml) {
  748. String html;
  749. if (asHtml) {
  750. html = c == null ? "" : c;
  751. } else {
  752. html = WidgetUtil.escapeHTML(c);
  753. }
  754. // Provide information to assistive device users that a sub window was
  755. // opened
  756. String prefix = "<span class='" + AriaHelper.ASSISTIVE_DEVICE_ONLY_STYLE
  757. + "'>" + assistivePrefix + "</span>";
  758. String postfix = "<span class='"
  759. + AriaHelper.ASSISTIVE_DEVICE_ONLY_STYLE + "'>"
  760. + assistivePostfix + "</span>";
  761. html = prefix + html + postfix;
  762. headerText.setInnerHTML(html);
  763. if (iconURL != null) {
  764. Icon icon = client.getIcon(iconURL);
  765. DOM.insertChild(headerText, icon.getElement(), 0);
  766. }
  767. }
  768. /**
  769. * Setter for the text for assistive devices the window caption is prefixed
  770. * with.
  771. *
  772. * @param assistivePrefix
  773. * the assistivePrefix to set
  774. */
  775. public void setAssistivePrefix(String assistivePrefix) {
  776. this.assistivePrefix = assistivePrefix;
  777. }
  778. /**
  779. * Getter for the text for assistive devices the window caption is prefixed
  780. * with.
  781. *
  782. * @return the assistivePrefix
  783. */
  784. public String getAssistivePrefix() {
  785. return assistivePrefix;
  786. }
  787. /**
  788. * Setter for the text for assistive devices the window caption is postfixed
  789. * with.
  790. *
  791. * @param assistivePostfix
  792. * the assistivePostfix to set
  793. */
  794. public void setAssistivePostfix(String assistivePostfix) {
  795. this.assistivePostfix = assistivePostfix;
  796. }
  797. /**
  798. * Getter for the text for assistive devices the window caption is postfixed
  799. * with.
  800. *
  801. * @return the assistivePostfix
  802. */
  803. public String getAssistivePostfix() {
  804. return assistivePostfix;
  805. }
  806. @Override
  807. protected com.google.gwt.user.client.Element getContainerElement() {
  808. // in GWT 1.5 this method is used in PopupPanel constructor
  809. if (contents == null) {
  810. return super.getContainerElement();
  811. }
  812. return DOM.asOld(contents);
  813. }
  814. private Event headerDragPending;
  815. @Override
  816. public void onBrowserEvent(final Event event) {
  817. boolean bubble = true;
  818. final int type = event.getTypeInt();
  819. final Element target = DOM.eventGetTarget(event);
  820. if (resizing || resizeBox == target) {
  821. onResizeEvent(event);
  822. bubble = false;
  823. } else if (isClosable() && target == closeBox) {
  824. if (type == Event.ONCLICK) {
  825. onCloseClick();
  826. }
  827. bubble = false;
  828. } else if (target == maximizeRestoreBox) {
  829. // handled in connector
  830. if (type != Event.ONCLICK) {
  831. bubble = false;
  832. }
  833. } else if (header.isOrHasChild(target) && !dragging) {
  834. // dblclick handled in connector
  835. if (type != Event.ONDBLCLICK && draggable) {
  836. if (type == Event.ONMOUSEDOWN || type == Event.ONTOUCHSTART) {
  837. /**
  838. * Prevents accidental selection of window caption or
  839. * content. (#12726)
  840. */
  841. event.preventDefault();
  842. headerDragPending = event;
  843. bubble = false;
  844. } else if (type == Event.ONMOUSEMOVE
  845. && headerDragPending != null) {
  846. // ie won't work unless this is set here
  847. dragging = true;
  848. onDragEvent(headerDragPending);
  849. onDragEvent(event);
  850. headerDragPending = null;
  851. bubble = false;
  852. } else if (type != Event.ONMOUSEMOVE) {
  853. // The event can propagate to the parent in case it is a
  854. // mouse move event. This is needed for tooltips to work in
  855. // header and footer, see Ticket #19073
  856. headerDragPending = null;
  857. bubble = false;
  858. } else {
  859. headerDragPending = null;
  860. }
  861. }
  862. if (type == Event.ONCLICK) {
  863. activateOnClick();
  864. }
  865. } else if (footer.isOrHasChild(target) && !dragging) {
  866. onDragEvent(event);
  867. if (type != Event.ONMOUSEMOVE) {
  868. // This is needed for tooltips to work in header and footer, see
  869. // Ticket #19073
  870. bubble = false;
  871. }
  872. } else if (dragging || !contents.isOrHasChild(target)) {
  873. onDragEvent(event);
  874. bubble = false;
  875. } else if (type == Event.ONCLICK) {
  876. activateOnClick();
  877. }
  878. /*
  879. * If clicking on other than the content, move focus to the window.
  880. * After that this windows e.g. gets all keyboard shortcuts.
  881. */
  882. if ((type == Event.ONMOUSEDOWN || type == Event.ONTOUCHSTART)
  883. && !contentPanel.getElement().isOrHasChild(target)
  884. && target != closeBox && target != maximizeRestoreBox) {
  885. contentPanel.focus();
  886. }
  887. if (!bubble) {
  888. event.stopPropagation();
  889. }
  890. // Super.onBrowserEvent takes care of Handlers added by the
  891. // ClickEventHandler
  892. super.onBrowserEvent(event);
  893. }
  894. private void activateOnClick() {
  895. // clicked inside window or inside header, ensure to be on top
  896. if (!isActive()) {
  897. bringToFront();
  898. }
  899. }
  900. private void onCloseClick() {
  901. // Send the close event to the server
  902. client.updateVariable(id, "close", true, true);
  903. }
  904. private void onResizeEvent(Event event) {
  905. if (resizable && WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  906. switch (event.getTypeInt()) {
  907. case Event.ONMOUSEDOWN:
  908. case Event.ONTOUCHSTART:
  909. if (!isActive()) {
  910. bringToFront();
  911. }
  912. showResizingCurtain();
  913. if (BrowserInfo.get().isIE()) {
  914. resizeBox.getStyle().setVisibility(Visibility.HIDDEN);
  915. }
  916. resizing = true;
  917. startX = WidgetUtil.getTouchOrMouseClientX(event);
  918. startY = WidgetUtil.getTouchOrMouseClientY(event);
  919. origW = getElement().getOffsetWidth();
  920. origH = getElement().getOffsetHeight();
  921. DOM.setCapture(getElement());
  922. event.preventDefault();
  923. break;
  924. case Event.ONMOUSEUP:
  925. case Event.ONTOUCHEND:
  926. setSize(event, true);
  927. case Event.ONTOUCHCANCEL:
  928. DOM.releaseCapture(getElement());
  929. case Event.ONLOSECAPTURE:
  930. hideResizingCurtain();
  931. if (BrowserInfo.get().isIE()) {
  932. resizeBox.getStyle().clearVisibility();
  933. }
  934. resizing = false;
  935. break;
  936. case Event.ONMOUSEMOVE:
  937. case Event.ONTOUCHMOVE:
  938. if (resizing) {
  939. centered = false;
  940. setSize(event, false);
  941. event.preventDefault();
  942. }
  943. break;
  944. default:
  945. event.preventDefault();
  946. break;
  947. }
  948. }
  949. }
  950. /**
  951. * TODO check if we need to support this with touch based devices.
  952. *
  953. * Checks if the cursor was inside the browser content area when the event
  954. * happened.
  955. *
  956. * @param event
  957. * The event to be checked
  958. * @return true, if the cursor is inside the browser content area
  959. *
  960. * false, otherwise
  961. */
  962. private boolean cursorInsideBrowserContentArea(Event event) {
  963. if (event.getClientX() < 0 || event.getClientY() < 0) {
  964. // Outside to the left or above
  965. return false;
  966. }
  967. if (event.getClientX() > Window.getClientWidth()
  968. || event.getClientY() > Window.getClientHeight()) {
  969. // Outside to the right or below
  970. return false;
  971. }
  972. return true;
  973. }
  974. private void setSize(Event event, boolean updateVariables) {
  975. if (!cursorInsideBrowserContentArea(event)) {
  976. // Only drag while cursor is inside the browser client area
  977. return;
  978. }
  979. int w = WidgetUtil.getTouchOrMouseClientX(event) - startX + origW;
  980. int h = WidgetUtil.getTouchOrMouseClientY(event) - startY + origH;
  981. w = Math.max(w, getMinWidth());
  982. h = Math.max(h, getMinHeight());
  983. setWidth(w + "px");
  984. setHeight(h + "px");
  985. if (updateVariables) {
  986. // sending width back always as pixels, no need for unit
  987. client.updateVariable(id, "width", w, false);
  988. client.updateVariable(id, "height", h, true);
  989. }
  990. if (updateVariables || !resizeLazy) {
  991. // Resize has finished or is not lazy
  992. updateContentsSize();
  993. } else {
  994. // Lazy resize - wait for a while before re-rendering contents
  995. delayedContentsSizeUpdater.trigger();
  996. }
  997. }
  998. private int getMinHeight() {
  999. return getPixelValue(getElement().getStyle().getProperty("minHeight"));
  1000. }
  1001. private int getMinWidth() {
  1002. return getPixelValue(getElement().getStyle().getProperty("minWidth"));
  1003. }
  1004. private static int getPixelValue(String size) {
  1005. if (size == null || !size.endsWith("px")) {
  1006. return -1;
  1007. } else {
  1008. return Integer.parseInt(size.substring(0, size.length() - 2));
  1009. }
  1010. }
  1011. public void updateContentsSize() {
  1012. LayoutManager layoutManager = getLayoutManager();
  1013. layoutManager
  1014. .setNeedsMeasure(ConnectorMap.get(client).getConnector(this));
  1015. layoutManager.layoutNow();
  1016. }
  1017. private native void addTransitionEndLayoutListener(Element e)
  1018. /*-{
  1019. var self = this;
  1020. e.addEventListener("transitionend", function(e) {
  1021. if (e.propertyName == "width" || e.propertyName == 'height') {
  1022. $entry(function() {
  1023. self.@com.vaadin.client.ui.VWindow::updateContentsSize()();
  1024. })();
  1025. }
  1026. });
  1027. }-*/;
  1028. @Override
  1029. public void setWidth(String width) {
  1030. // Override PopupPanel which sets the width to the contents
  1031. getElement().getStyle().setProperty("width", width);
  1032. // Update v-has-width in case undefined window is resized
  1033. setStyleName("v-has-width", width != null && !width.isEmpty());
  1034. }
  1035. @Override
  1036. public void setHeight(String height) {
  1037. // Override PopupPanel which sets the height to the contents
  1038. getElement().getStyle().setProperty("height", height);
  1039. // Update v-has-height in case undefined window is resized
  1040. setStyleName("v-has-height", height != null && !height.isEmpty());
  1041. }
  1042. private void onDragEvent(Event event) {
  1043. if (!WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  1044. return;
  1045. }
  1046. switch (DOM.eventGetType(event)) {
  1047. case Event.ONTOUCHSTART:
  1048. if (event.getTouches().length() > 1) {
  1049. return;
  1050. }
  1051. case Event.ONMOUSEDOWN:
  1052. if (!isActive()) {
  1053. bringToFront();
  1054. }
  1055. beginMovingWindow(event);
  1056. break;
  1057. case Event.ONMOUSEUP:
  1058. case Event.ONTOUCHEND:
  1059. case Event.ONTOUCHCANCEL:
  1060. case Event.ONLOSECAPTURE:
  1061. stopMovingWindow();
  1062. break;
  1063. case Event.ONMOUSEMOVE:
  1064. case Event.ONTOUCHMOVE:
  1065. moveWindow(event);
  1066. break;
  1067. default:
  1068. break;
  1069. }
  1070. }
  1071. private void moveWindow(Event event) {
  1072. if (dragging) {
  1073. centered = false;
  1074. if (cursorInsideBrowserContentArea(event)) {
  1075. // Only drag while cursor is inside the browser client area
  1076. final int x = WidgetUtil.getTouchOrMouseClientX(event) - startX
  1077. + origX;
  1078. final int y = WidgetUtil.getTouchOrMouseClientY(event) - startY
  1079. + origY;
  1080. setPopupPosition(x, y);
  1081. }
  1082. DOM.eventPreventDefault(event);
  1083. }
  1084. }
  1085. private void beginMovingWindow(Event event) {
  1086. if (draggable) {
  1087. showDraggingCurtain();
  1088. dragging = true;
  1089. startX = WidgetUtil.getTouchOrMouseClientX(event);
  1090. startY = WidgetUtil.getTouchOrMouseClientY(event);
  1091. origX = DOM.getAbsoluteLeft(getElement());
  1092. origY = DOM.getAbsoluteTop(getElement());
  1093. DOM.setCapture(getElement());
  1094. DOM.eventPreventDefault(event);
  1095. }
  1096. }
  1097. private void stopMovingWindow() {
  1098. dragging = false;
  1099. hideDraggingCurtain();
  1100. DOM.releaseCapture(getElement());
  1101. // fire move event
  1102. fireEvent(new WindowMoveEvent(uidlPositionX, uidlPositionY));
  1103. }
  1104. @Override
  1105. public boolean onEventPreview(Event event) {
  1106. if (dragging) {
  1107. onDragEvent(event);
  1108. return false;
  1109. } else if (resizing) {
  1110. onResizeEvent(event);
  1111. return false;
  1112. }
  1113. // TODO This is probably completely unnecessary as the modality curtain
  1114. // prevents events from reaching other windows and any security check
  1115. // must be done on the server side and not here.
  1116. // The code here is also run many times as each VWindow has an event
  1117. // preview but we cannot check only the current VWindow here (e.g.
  1118. // if(isTopMost) {...}) because PopupPanel will cause all events that
  1119. // are not cancelled here and target this window to be consume():d
  1120. // meaning the event won't be sent to the rest of the preview handlers.
  1121. if (getTopmostWindow() != null && getTopmostWindow().vaadinModality) {
  1122. // Topmost window is modal. Cancel the event if it targets something
  1123. // outside that window (except debug console...)
  1124. if (DOM.getCaptureElement() != null) {
  1125. // Allow events when capture is set
  1126. return true;
  1127. }
  1128. final Element target = event.getEventTarget().cast();
  1129. if (!DOM.isOrHasChild(getTopmostWindow().getElement(), target)) {
  1130. // not within the modal window, but let's see if it's in the
  1131. // debug window
  1132. Widget w = WidgetUtil.findWidget(target);
  1133. while (w != null) {
  1134. if (w instanceof VDebugWindow) {
  1135. return true; // allow debug-window clicks
  1136. } else if (ConnectorMap.get(client).isConnector(w)) {
  1137. return false;
  1138. }
  1139. w = w.getParent();
  1140. }
  1141. return false;
  1142. }
  1143. }
  1144. return true;
  1145. }
  1146. @Override
  1147. public void addStyleDependentName(String styleSuffix) {
  1148. // VWindow's getStyleElement() does not return the same element as
  1149. // getElement(), so we need to override this.
  1150. setStyleName(getElement(), getStylePrimaryName() + "-" + styleSuffix,
  1151. true);
  1152. }
  1153. @Override
  1154. public ShortcutActionHandler getShortcutActionHandler() {
  1155. return shortcutHandler;
  1156. }
  1157. @Override
  1158. public void onScroll(ScrollEvent event) {
  1159. client.updateVariable(id, "scrollTop", contentPanel.getScrollPosition(),
  1160. false);
  1161. client.updateVariable(id, "scrollLeft",
  1162. contentPanel.getHorizontalScrollPosition(), false);
  1163. }
  1164. @Override
  1165. public void onKeyDown(KeyDownEvent event) {
  1166. if (vaadinModality && event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE
  1167. && !isFocusedElementEditable()) {
  1168. event.preventDefault();
  1169. }
  1170. if (shortcutHandler != null) {
  1171. shortcutHandler
  1172. .handleKeyboardEvent(Event.as(event.getNativeEvent()));
  1173. return;
  1174. }
  1175. }
  1176. @Override
  1177. public void onBlur(BlurEvent event) {
  1178. if (client.hasEventListeners(this, EventId.BLUR)) {
  1179. client.updateVariable(id, EventId.BLUR, "", true);
  1180. }
  1181. }
  1182. @Override
  1183. public void onFocus(FocusEvent event) {
  1184. if (client.hasEventListeners(this, EventId.FOCUS)) {
  1185. client.updateVariable(id, EventId.FOCUS, "", true);
  1186. }
  1187. }
  1188. @Override
  1189. public void focus() {
  1190. // We don't want to use contentPanel.focus() as that will use a timer in
  1191. // Chrome/Safari and ultimately run focus events in the wrong order when
  1192. // opening a modal window and focusing some other component at the same
  1193. // time
  1194. contentPanel.getElement().focus();
  1195. }
  1196. private int getDecorationHeight() {
  1197. LayoutManager lm = getLayoutManager();
  1198. int headerHeight = lm.getOuterHeight(header);
  1199. int footerHeight = lm.getOuterHeight(footer);
  1200. return headerHeight + footerHeight;
  1201. }
  1202. private LayoutManager getLayoutManager() {
  1203. return LayoutManager.get(client);
  1204. }
  1205. private int getDecorationWidth() {
  1206. LayoutManager layoutManager = getLayoutManager();
  1207. return layoutManager.getOuterWidth(getElement())
  1208. - contentPanel.getElement().getOffsetWidth();
  1209. }
  1210. /**
  1211. * Allows to specify which connectors contain the description for the
  1212. * window. Text contained in the widgets of the connectors will be read by
  1213. * assistive devices when it is opened.
  1214. * <p>
  1215. * When the provided array is empty, an existing description is removed.
  1216. *
  1217. * @param connectors
  1218. * with the connectors of the widgets to use as description
  1219. */
  1220. public void setAssistiveDescription(Connector[] connectors) {
  1221. if (connectors != null) {
  1222. assistiveConnectors = connectors;
  1223. if (connectors.length == 0) {
  1224. Roles.getDialogRole()
  1225. .removeAriaDescribedbyProperty(getElement());
  1226. } else {
  1227. Id[] ids = new Id[connectors.length];
  1228. for (int index = 0; index < connectors.length; index++) {
  1229. if (connectors[index] == null) {
  1230. throw new IllegalArgumentException(
  1231. "All values in parameter description need to be non-null");
  1232. }
  1233. Element element = ((ComponentConnector) connectors[index])
  1234. .getWidget().getElement();
  1235. AriaHelper.ensureHasId(element);
  1236. ids[index] = Id.of(element);
  1237. }
  1238. Roles.getDialogRole().setAriaDescribedbyProperty(getElement(),
  1239. ids);
  1240. }
  1241. } else {
  1242. throw new IllegalArgumentException(
  1243. "Parameter description must be non-null");
  1244. }
  1245. }
  1246. /**
  1247. * Gets the connectors that are used as assistive description. Text
  1248. * contained in these connectors will be read by assistive devices when the
  1249. * window is opened.
  1250. *
  1251. * @return list of previously set connectors
  1252. */
  1253. public List<Connector> getAssistiveDescription() {
  1254. return Collections.unmodifiableList(Arrays.asList(assistiveConnectors));
  1255. }
  1256. /**
  1257. * Sets the WAI-ARIA role the window.
  1258. *
  1259. * This role defines how an assistive device handles a window. Available
  1260. * roles are alertdialog and dialog (@see
  1261. * <a href="http://www.w3.org/TR/2011/CR-wai-aria-20110118/roles">Roles
  1262. * Model</a>).
  1263. *
  1264. * The default role is dialog.
  1265. *
  1266. * @param role
  1267. * WAI-ARIA role to set for the window
  1268. */
  1269. public void setWaiAriaRole(WindowRole role) {
  1270. if (role == WindowRole.ALERTDIALOG) {
  1271. Roles.getAlertdialogRole().set(getElement());
  1272. } else {
  1273. Roles.getDialogRole().set(getElement());
  1274. }
  1275. }
  1276. /**
  1277. * Registers the handlers that prevent to leave the window using the
  1278. * Tab-key.
  1279. * <p>
  1280. * The value of the parameter doTabStop is stored and used for non-modal
  1281. * windows. For modal windows, the handlers are always registered, while
  1282. * preserving the stored value.
  1283. *
  1284. * @param doTabStop
  1285. * true to prevent leaving the window, false to allow leaving the
  1286. * window for non modal windows
  1287. */
  1288. public void setTabStopEnabled(boolean doTabStop) {
  1289. this.doTabStop = doTabStop;
  1290. if (doTabStop || vaadinModality) {
  1291. addTabBlockHandlers();
  1292. } else {
  1293. removeTabBlockHandlers();
  1294. }
  1295. }
  1296. /**
  1297. * Adds a Handler for when user moves the window.
  1298. *
  1299. * @since 7.1.9
  1300. *
  1301. * @return {@link HandlerRegistration} used to remove the handler
  1302. */
  1303. public HandlerRegistration addMoveHandler(WindowMoveHandler handler) {
  1304. return addHandler(handler, WindowMoveEvent.getType());
  1305. }
  1306. /**
  1307. * Adds a Handler for window order change event.
  1308. *
  1309. * @since 8.0
  1310. *
  1311. * @return registration object to deregister the handler
  1312. */
  1313. public static HandlerRegistration addWindowOrderHandler(
  1314. WindowOrderHandler handler) {
  1315. return WINDOW_ORDER_HANDLER.addHandler(WindowOrderEvent.getType(),
  1316. handler);
  1317. }
  1318. /**
  1319. * Checks if a modal window is currently open.
  1320. *
  1321. * @return <code>true</code> if a modal window is open, <code>false</code>
  1322. * otherwise.
  1323. */
  1324. public static boolean isModalWindowOpen() {
  1325. return Document.get().getBody()
  1326. .hasClassName(MODAL_WINDOW_OPEN_CLASSNAME);
  1327. }
  1328. }