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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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 ArrayList<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. ArrayList<VWindow> list = new ArrayList<>();
  244. list.add(this);
  245. fireOrderEvent(list);
  246. }
  247. private static void fireOrderEvent(ArrayList<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.size() > 0) {
  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.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 (int i = 0; i < array.length; i++) {
  478. VWindow w = array[i];
  479. if (w.bringToFrontSequence != -1 || w.vaadinModality) {
  480. w.bringToFront(false);
  481. w.bringToFrontSequence = -1;
  482. }
  483. }
  484. focusTopmostModalWindow();
  485. }
  486. private static void focusTopmostModalWindow() {
  487. VWindow topmost = getTopmostWindow();
  488. if (topmost != null && topmost.vaadinModality) {
  489. topmost.focus();
  490. }
  491. fireOrderEvent();
  492. }
  493. @Override
  494. public void setVisible(boolean visible) {
  495. /*
  496. * Visibility with VWindow works differently than with other Paintables
  497. * in Vaadin. Invisible VWindows are not attached to DOM at all. Flag is
  498. * used to avoid visibility call from
  499. * ApplicationConnection.updateComponent();
  500. */
  501. if (!visibilityChangesDisabled) {
  502. super.setVisible(visible);
  503. }
  504. if (visible && BrowserInfo.get()
  505. .requiresPositionAbsoluteOverflowAutoFix()) {
  506. /*
  507. * Shake up the DOM a bit to make the window shed unnecessary
  508. * scrollbars and resize correctly afterwards. The version fixing
  509. * ticket #11994 which was changing the size to 110% was replaced
  510. * with this due to ticket #12943
  511. */
  512. WidgetUtil
  513. .runWebkitOverflowAutoFix(contents.getFirstChildElement());
  514. }
  515. }
  516. /** For internal use only. May be removed or replaced in the future. */
  517. public void setDraggable(boolean draggable) {
  518. if (this.draggable == draggable) {
  519. return;
  520. }
  521. this.draggable = draggable;
  522. setCursorProperties();
  523. }
  524. private void setCursorProperties() {
  525. if (!draggable) {
  526. header.getStyle().setProperty("cursor", "default");
  527. footer.getStyle().setProperty("cursor", "default");
  528. } else {
  529. header.getStyle().setProperty("cursor", "");
  530. footer.getStyle().setProperty("cursor", "");
  531. }
  532. }
  533. /**
  534. * Sets the closable state of the window. Additionally hides/shows the close
  535. * button according to the new state.
  536. *
  537. * @param closable
  538. * true if the window can be closed by the user
  539. */
  540. public void setClosable(boolean closable) {
  541. if (this.closable == closable) {
  542. return;
  543. }
  544. this.closable = closable;
  545. if (closable) {
  546. DOM.setElementProperty(closeBox, "className",
  547. CLASSNAME + "-closebox");
  548. } else {
  549. DOM.setElementProperty(closeBox, "className", CLASSNAME
  550. + "-closebox " + CLASSNAME + "-closebox-disabled");
  551. }
  552. }
  553. /**
  554. * Returns the closable state of the sub window. If the sub window is
  555. * closable a decoration (typically an X) is shown to the user. By clicking
  556. * on the X the user can close the window.
  557. *
  558. * @return true if the sub window is closable
  559. */
  560. protected boolean isClosable() {
  561. return closable;
  562. }
  563. @Override
  564. public void show() {
  565. if (!windowOrder.contains(this)) {
  566. // This is needed if the window is hidden and then shown again.
  567. // Otherwise this VWindow is added to windowOrder in the
  568. // constructor.
  569. windowOrder.add(this);
  570. }
  571. if (vaadinModality) {
  572. showModalityCurtain();
  573. }
  574. super.show();
  575. }
  576. @Override
  577. public void hide() {
  578. if (vaadinModality) {
  579. hideModalityCurtain();
  580. }
  581. super.hide();
  582. int curIndex = getWindowOrder();
  583. // Remove window from windowOrder to avoid references being left
  584. // hanging.
  585. windowOrder.remove(curIndex);
  586. // Update the z-indices of any remaining windows
  587. ArrayList<VWindow> update = new ArrayList<>(
  588. windowOrder.size() - curIndex + 1);
  589. update.add(this);
  590. while (curIndex < windowOrder.size()) {
  591. VWindow window = windowOrder.get(curIndex);
  592. window.setWindowOrder(curIndex++);
  593. update.add(window);
  594. }
  595. focusTopmostModalWindow();
  596. fireOrderEvent(update);
  597. }
  598. /** For internal use only. May be removed or replaced in the future. */
  599. public void setVaadinModality(boolean modality) {
  600. vaadinModality = modality;
  601. if (vaadinModality) {
  602. if (isAttached()) {
  603. showModalityCurtain();
  604. }
  605. addTabBlockHandlers();
  606. deferOrdering();
  607. } else {
  608. if (modalityCurtain != null) {
  609. if (isAttached()) {
  610. hideModalityCurtain();
  611. }
  612. modalityCurtain = null;
  613. }
  614. if (!doTabStop) {
  615. removeTabBlockHandlers();
  616. }
  617. }
  618. }
  619. private void showModalityCurtain() {
  620. getModalityCurtain().getStyle().setZIndex(getWindowOrder() + Z_INDEX);
  621. if (isShowing()) {
  622. getOverlayContainer().insertBefore(getModalityCurtain(),
  623. getElement());
  624. } else {
  625. getOverlayContainer().appendChild(getModalityCurtain());
  626. }
  627. Document.get().getBody().addClassName(MODAL_WINDOW_OPEN_CLASSNAME);
  628. }
  629. private void hideModalityCurtain() {
  630. Document.get().getBody().removeClassName(MODAL_WINDOW_OPEN_CLASSNAME);
  631. modalityCurtain.removeFromParent();
  632. // IE leaks memory in certain cases unless we release the reference
  633. // (#9197)
  634. modalityCurtain = null;
  635. }
  636. /*
  637. * Shows an empty div on top of all other content; used when moving, so that
  638. * iframes (etc) do not steal event.
  639. */
  640. private void showDraggingCurtain() {
  641. getElement().getParentElement().insertBefore(getDraggingCurtain(),
  642. getElement());
  643. }
  644. private void hideDraggingCurtain() {
  645. if (draggingCurtain != null) {
  646. draggingCurtain.removeFromParent();
  647. }
  648. }
  649. /*
  650. * Shows an empty div on top of all other content; used when resizing, so
  651. * that iframes (etc) do not steal event.
  652. */
  653. private void showResizingCurtain() {
  654. getElement().getParentElement().insertBefore(getResizingCurtain(),
  655. getElement());
  656. }
  657. private void hideResizingCurtain() {
  658. if (resizingCurtain != null) {
  659. resizingCurtain.removeFromParent();
  660. }
  661. }
  662. private Element getDraggingCurtain() {
  663. if (draggingCurtain == null) {
  664. draggingCurtain = createCurtain();
  665. draggingCurtain.setClassName(CLASSNAME + "-draggingCurtain");
  666. }
  667. return draggingCurtain;
  668. }
  669. private Element getResizingCurtain() {
  670. if (resizingCurtain == null) {
  671. resizingCurtain = createCurtain();
  672. resizingCurtain.setClassName(CLASSNAME + "-resizingCurtain");
  673. }
  674. return resizingCurtain;
  675. }
  676. private Element createCurtain() {
  677. Element curtain = DOM.createDiv();
  678. curtain.getStyle().setPosition(Position.ABSOLUTE);
  679. curtain.getStyle().setTop(0, Unit.PX);
  680. curtain.getStyle().setLeft(0, Unit.PX);
  681. curtain.getStyle().setWidth(100, Unit.PCT);
  682. curtain.getStyle().setHeight(100, Unit.PCT);
  683. curtain.getStyle().setZIndex(VOverlay.Z_INDEX);
  684. return curtain;
  685. }
  686. /** For internal use only. May be removed or replaced in the future. */
  687. public void setResizable(boolean resizability) {
  688. resizable = resizability;
  689. if (resizability) {
  690. DOM.setElementProperty(footer, "className", CLASSNAME + "-footer");
  691. DOM.setElementProperty(resizeBox, "className",
  692. CLASSNAME + "-resizebox");
  693. } else {
  694. DOM.setElementProperty(footer, "className",
  695. CLASSNAME + "-footer " + CLASSNAME + "-footer-noresize");
  696. DOM.setElementProperty(resizeBox, "className", CLASSNAME
  697. + "-resizebox " + CLASSNAME + "-resizebox-disabled");
  698. }
  699. }
  700. public void updateMaximizeRestoreClassName(boolean visible,
  701. WindowMode windowMode) {
  702. String className;
  703. if (windowMode == WindowMode.MAXIMIZED) {
  704. className = CLASSNAME + "-restorebox";
  705. } else {
  706. className = CLASSNAME + "-maximizebox";
  707. }
  708. if (!visible) {
  709. className = className + " " + className + "-disabled";
  710. }
  711. maximizeRestoreBox.setClassName(className);
  712. }
  713. // TODO this will eventually be removed, currently used to avoid updating to
  714. // server side.
  715. public void setPopupPositionNoUpdate(int left, int top) {
  716. if (top < 0) {
  717. // ensure window is not moved out of browser window from top of the
  718. // screen
  719. top = 0;
  720. }
  721. super.setPopupPosition(left, top);
  722. }
  723. @Override
  724. public void setPopupPosition(int left, int top) {
  725. if (top < 0) {
  726. // ensure window is not moved out of browser window from top of the
  727. // screen
  728. top = 0;
  729. }
  730. super.setPopupPosition(left, top);
  731. if (left != uidlPositionX && client != null) {
  732. client.updateVariable(id, "positionx", left, false);
  733. uidlPositionX = left;
  734. }
  735. if (top != uidlPositionY && client != null) {
  736. client.updateVariable(id, "positiony", top, false);
  737. uidlPositionY = top;
  738. }
  739. }
  740. public void setCaption(String c) {
  741. setCaption(c, null);
  742. }
  743. public void setCaption(String c, String iconURL) {
  744. setCaption(c, iconURL, false);
  745. }
  746. public void setCaption(String c, String iconURL, boolean asHtml) {
  747. String html;
  748. if (asHtml) {
  749. html = c == null ? "" : c;
  750. } else {
  751. html = WidgetUtil.escapeHTML(c);
  752. }
  753. // Provide information to assistive device users that a sub window was
  754. // opened
  755. String prefix = "<span class='" + AriaHelper.ASSISTIVE_DEVICE_ONLY_STYLE
  756. + "'>" + assistivePrefix + "</span>";
  757. String postfix = "<span class='"
  758. + AriaHelper.ASSISTIVE_DEVICE_ONLY_STYLE + "'>"
  759. + assistivePostfix + "</span>";
  760. html = prefix + html + postfix;
  761. headerText.setInnerHTML(html);
  762. if (iconURL != null) {
  763. Icon icon = client.getIcon(iconURL);
  764. DOM.insertChild(headerText, icon.getElement(), 0);
  765. }
  766. }
  767. /**
  768. * Setter for the text for assistive devices the window caption is prefixed
  769. * with.
  770. *
  771. * @param assistivePrefix
  772. * the assistivePrefix to set
  773. */
  774. public void setAssistivePrefix(String assistivePrefix) {
  775. this.assistivePrefix = assistivePrefix;
  776. }
  777. /**
  778. * Getter for the text for assistive devices the window caption is prefixed
  779. * with.
  780. *
  781. * @return the assistivePrefix
  782. */
  783. public String getAssistivePrefix() {
  784. return assistivePrefix;
  785. }
  786. /**
  787. * Setter for the text for assistive devices the window caption is postfixed
  788. * with.
  789. *
  790. * @param assistivePostfix
  791. * the assistivePostfix to set
  792. */
  793. public void setAssistivePostfix(String assistivePostfix) {
  794. this.assistivePostfix = assistivePostfix;
  795. }
  796. /**
  797. * Getter for the text for assistive devices the window caption is postfixed
  798. * with.
  799. *
  800. * @return the assistivePostfix
  801. */
  802. public String getAssistivePostfix() {
  803. return assistivePostfix;
  804. }
  805. @Override
  806. protected com.google.gwt.user.client.Element getContainerElement() {
  807. // in GWT 1.5 this method is used in PopupPanel constructor
  808. if (contents == null) {
  809. return super.getContainerElement();
  810. }
  811. return DOM.asOld(contents);
  812. }
  813. private Event headerDragPending;
  814. @Override
  815. public void onBrowserEvent(final Event event) {
  816. boolean bubble = true;
  817. final int type = event.getTypeInt();
  818. final Element target = DOM.eventGetTarget(event);
  819. if (resizing || resizeBox == target) {
  820. onResizeEvent(event);
  821. bubble = false;
  822. } else if (isClosable() && target == closeBox) {
  823. if (type == Event.ONCLICK) {
  824. onCloseClick();
  825. }
  826. bubble = false;
  827. } else if (target == maximizeRestoreBox) {
  828. // handled in connector
  829. if (type != Event.ONCLICK) {
  830. bubble = false;
  831. }
  832. } else if (header.isOrHasChild(target) && !dragging) {
  833. // dblclick handled in connector
  834. if (type != Event.ONDBLCLICK && draggable) {
  835. if (type == Event.ONMOUSEDOWN || type == Event.ONTOUCHSTART) {
  836. /**
  837. * Prevents accidental selection of window caption or
  838. * content. (#12726)
  839. */
  840. event.preventDefault();
  841. headerDragPending = event;
  842. bubble = false;
  843. } else if (type == Event.ONMOUSEMOVE
  844. && headerDragPending != null) {
  845. // ie won't work unless this is set here
  846. dragging = true;
  847. onDragEvent(headerDragPending);
  848. onDragEvent(event);
  849. headerDragPending = null;
  850. bubble = false;
  851. } else if (type != Event.ONMOUSEMOVE) {
  852. // The event can propagate to the parent in case it is a
  853. // mouse move event. This is needed for tooltips to work in
  854. // header and footer, see Ticket #19073
  855. headerDragPending = null;
  856. bubble = false;
  857. } else {
  858. headerDragPending = null;
  859. }
  860. }
  861. if (type == Event.ONCLICK) {
  862. activateOnClick();
  863. }
  864. } else if (footer.isOrHasChild(target) && !dragging) {
  865. onDragEvent(event);
  866. if (type != Event.ONMOUSEMOVE) {
  867. // This is needed for tooltips to work in header and footer, see
  868. // Ticket #19073
  869. bubble = false;
  870. }
  871. } else if (dragging || !contents.isOrHasChild(target)) {
  872. onDragEvent(event);
  873. bubble = false;
  874. } else if (type == Event.ONCLICK) {
  875. activateOnClick();
  876. }
  877. /*
  878. * If clicking on other than the content, move focus to the window.
  879. * After that this windows e.g. gets all keyboard shortcuts.
  880. */
  881. if ((type == Event.ONMOUSEDOWN || type == Event.ONTOUCHSTART)
  882. && !contentPanel.getElement().isOrHasChild(target)
  883. && target != closeBox && target != maximizeRestoreBox) {
  884. contentPanel.focus();
  885. }
  886. if (!bubble) {
  887. event.stopPropagation();
  888. }
  889. // Super.onBrowserEvent takes care of Handlers added by the
  890. // ClickEventHandler
  891. super.onBrowserEvent(event);
  892. }
  893. private void activateOnClick() {
  894. // clicked inside window or inside header, ensure to be on top
  895. if (!isActive()) {
  896. bringToFront();
  897. }
  898. }
  899. private void onCloseClick() {
  900. // Send the close event to the server
  901. client.updateVariable(id, "close", true, true);
  902. }
  903. private void onResizeEvent(Event event) {
  904. if (resizable && WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  905. switch (event.getTypeInt()) {
  906. case Event.ONMOUSEDOWN:
  907. case Event.ONTOUCHSTART:
  908. if (!isActive()) {
  909. bringToFront();
  910. }
  911. showResizingCurtain();
  912. if (BrowserInfo.get().isIE()) {
  913. resizeBox.getStyle().setVisibility(Visibility.HIDDEN);
  914. }
  915. resizing = true;
  916. startX = WidgetUtil.getTouchOrMouseClientX(event);
  917. startY = WidgetUtil.getTouchOrMouseClientY(event);
  918. origW = getElement().getOffsetWidth();
  919. origH = getElement().getOffsetHeight();
  920. DOM.setCapture(getElement());
  921. event.preventDefault();
  922. break;
  923. case Event.ONMOUSEUP:
  924. case Event.ONTOUCHEND:
  925. setSize(event, true);
  926. case Event.ONTOUCHCANCEL:
  927. DOM.releaseCapture(getElement());
  928. case Event.ONLOSECAPTURE:
  929. hideResizingCurtain();
  930. if (BrowserInfo.get().isIE()) {
  931. resizeBox.getStyle().clearVisibility();
  932. }
  933. resizing = false;
  934. break;
  935. case Event.ONMOUSEMOVE:
  936. case Event.ONTOUCHMOVE:
  937. if (resizing) {
  938. centered = false;
  939. setSize(event, false);
  940. event.preventDefault();
  941. }
  942. break;
  943. default:
  944. event.preventDefault();
  945. break;
  946. }
  947. }
  948. }
  949. /**
  950. * TODO check if we need to support this with touch based devices.
  951. *
  952. * Checks if the cursor was inside the browser content area when the event
  953. * happened.
  954. *
  955. * @param event
  956. * The event to be checked
  957. * @return true, if the cursor is inside the browser content area
  958. *
  959. * false, otherwise
  960. */
  961. private boolean cursorInsideBrowserContentArea(Event event) {
  962. if (event.getClientX() < 0 || event.getClientY() < 0) {
  963. // Outside to the left or above
  964. return false;
  965. }
  966. if (event.getClientX() > Window.getClientWidth()
  967. || event.getClientY() > Window.getClientHeight()) {
  968. // Outside to the right or below
  969. return false;
  970. }
  971. return true;
  972. }
  973. private void setSize(Event event, boolean updateVariables) {
  974. if (!cursorInsideBrowserContentArea(event)) {
  975. // Only drag while cursor is inside the browser client area
  976. return;
  977. }
  978. int w = WidgetUtil.getTouchOrMouseClientX(event) - startX + origW;
  979. int h = WidgetUtil.getTouchOrMouseClientY(event) - startY + origH;
  980. w = Math.max(w, getMinWidth());
  981. h = Math.max(h, getMinHeight());
  982. setWidth(w + "px");
  983. setHeight(h + "px");
  984. if (updateVariables) {
  985. // sending width back always as pixels, no need for unit
  986. client.updateVariable(id, "width", w, false);
  987. client.updateVariable(id, "height", h, true);
  988. }
  989. if (updateVariables || !resizeLazy) {
  990. // Resize has finished or is not lazy
  991. updateContentsSize();
  992. } else {
  993. // Lazy resize - wait for a while before re-rendering contents
  994. delayedContentsSizeUpdater.trigger();
  995. }
  996. }
  997. private int getMinHeight() {
  998. return getPixelValue(getElement().getStyle().getProperty("minHeight"));
  999. }
  1000. private int getMinWidth() {
  1001. return getPixelValue(getElement().getStyle().getProperty("minWidth"));
  1002. }
  1003. private static int getPixelValue(String size) {
  1004. if (size == null || !size.endsWith("px")) {
  1005. return -1;
  1006. } else {
  1007. return Integer.parseInt(size.substring(0, size.length() - 2));
  1008. }
  1009. }
  1010. public void updateContentsSize() {
  1011. LayoutManager layoutManager = getLayoutManager();
  1012. layoutManager
  1013. .setNeedsMeasure(ConnectorMap.get(client).getConnector(this));
  1014. layoutManager.layoutNow();
  1015. }
  1016. private native void addTransitionEndLayoutListener(Element e)
  1017. /*-{
  1018. var self = this;
  1019. e.addEventListener("transitionend", function(e) {
  1020. if (e.propertyName == "width" || e.propertyName == 'height') {
  1021. $entry(function() {
  1022. self.@com.vaadin.client.ui.VWindow::updateContentsSize()();
  1023. })();
  1024. }
  1025. });
  1026. }-*/;
  1027. @Override
  1028. public void setWidth(String width) {
  1029. // Override PopupPanel which sets the width to the contents
  1030. getElement().getStyle().setProperty("width", width);
  1031. // Update v-has-width in case undefined window is resized
  1032. setStyleName("v-has-width", width != null && width.length() > 0);
  1033. }
  1034. @Override
  1035. public void setHeight(String height) {
  1036. // Override PopupPanel which sets the height to the contents
  1037. getElement().getStyle().setProperty("height", height);
  1038. // Update v-has-height in case undefined window is resized
  1039. setStyleName("v-has-height", height != null && height.length() > 0);
  1040. }
  1041. private void onDragEvent(Event event) {
  1042. if (!WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  1043. return;
  1044. }
  1045. switch (DOM.eventGetType(event)) {
  1046. case Event.ONTOUCHSTART:
  1047. if (event.getTouches().length() > 1) {
  1048. return;
  1049. }
  1050. case Event.ONMOUSEDOWN:
  1051. if (!isActive()) {
  1052. bringToFront();
  1053. }
  1054. beginMovingWindow(event);
  1055. break;
  1056. case Event.ONMOUSEUP:
  1057. case Event.ONTOUCHEND:
  1058. case Event.ONTOUCHCANCEL:
  1059. case Event.ONLOSECAPTURE:
  1060. stopMovingWindow();
  1061. break;
  1062. case Event.ONMOUSEMOVE:
  1063. case Event.ONTOUCHMOVE:
  1064. moveWindow(event);
  1065. break;
  1066. default:
  1067. break;
  1068. }
  1069. }
  1070. private void moveWindow(Event event) {
  1071. if (dragging) {
  1072. centered = false;
  1073. if (cursorInsideBrowserContentArea(event)) {
  1074. // Only drag while cursor is inside the browser client area
  1075. final int x = WidgetUtil.getTouchOrMouseClientX(event) - startX
  1076. + origX;
  1077. final int y = WidgetUtil.getTouchOrMouseClientY(event) - startY
  1078. + origY;
  1079. setPopupPosition(x, y);
  1080. }
  1081. DOM.eventPreventDefault(event);
  1082. }
  1083. }
  1084. private void beginMovingWindow(Event event) {
  1085. if (draggable) {
  1086. showDraggingCurtain();
  1087. dragging = true;
  1088. startX = WidgetUtil.getTouchOrMouseClientX(event);
  1089. startY = WidgetUtil.getTouchOrMouseClientY(event);
  1090. origX = DOM.getAbsoluteLeft(getElement());
  1091. origY = DOM.getAbsoluteTop(getElement());
  1092. DOM.setCapture(getElement());
  1093. DOM.eventPreventDefault(event);
  1094. }
  1095. }
  1096. private void stopMovingWindow() {
  1097. dragging = false;
  1098. hideDraggingCurtain();
  1099. DOM.releaseCapture(getElement());
  1100. // fire move event
  1101. fireEvent(new WindowMoveEvent(uidlPositionX, uidlPositionY));
  1102. }
  1103. @Override
  1104. public boolean onEventPreview(Event event) {
  1105. if (dragging) {
  1106. onDragEvent(event);
  1107. return false;
  1108. } else if (resizing) {
  1109. onResizeEvent(event);
  1110. return false;
  1111. }
  1112. // TODO This is probably completely unnecessary as the modality curtain
  1113. // prevents events from reaching other windows and any security check
  1114. // must be done on the server side and not here.
  1115. // The code here is also run many times as each VWindow has an event
  1116. // preview but we cannot check only the current VWindow here (e.g.
  1117. // if(isTopMost) {...}) because PopupPanel will cause all events that
  1118. // are not cancelled here and target this window to be consume():d
  1119. // meaning the event won't be sent to the rest of the preview handlers.
  1120. if (getTopmostWindow() != null && getTopmostWindow().vaadinModality) {
  1121. // Topmost window is modal. Cancel the event if it targets something
  1122. // outside that window (except debug console...)
  1123. if (DOM.getCaptureElement() != null) {
  1124. // Allow events when capture is set
  1125. return true;
  1126. }
  1127. final Element target = event.getEventTarget().cast();
  1128. if (!DOM.isOrHasChild(getTopmostWindow().getElement(), target)) {
  1129. // not within the modal window, but let's see if it's in the
  1130. // debug window
  1131. Widget w = WidgetUtil.findWidget(target, null);
  1132. while (w != null) {
  1133. if (w instanceof VDebugWindow) {
  1134. return true; // allow debug-window clicks
  1135. } else if (ConnectorMap.get(client).isConnector(w)) {
  1136. return false;
  1137. }
  1138. w = w.getParent();
  1139. }
  1140. return false;
  1141. }
  1142. }
  1143. return true;
  1144. }
  1145. @Override
  1146. public void addStyleDependentName(String styleSuffix) {
  1147. // VWindow's getStyleElement() does not return the same element as
  1148. // getElement(), so we need to override this.
  1149. setStyleName(getElement(), getStylePrimaryName() + "-" + styleSuffix,
  1150. true);
  1151. }
  1152. @Override
  1153. public ShortcutActionHandler getShortcutActionHandler() {
  1154. return shortcutHandler;
  1155. }
  1156. @Override
  1157. public void onScroll(ScrollEvent event) {
  1158. client.updateVariable(id, "scrollTop", contentPanel.getScrollPosition(),
  1159. false);
  1160. client.updateVariable(id, "scrollLeft",
  1161. contentPanel.getHorizontalScrollPosition(), false);
  1162. }
  1163. @Override
  1164. public void onKeyDown(KeyDownEvent event) {
  1165. if (vaadinModality && event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE
  1166. && !isFocusedElementEditable()) {
  1167. event.preventDefault();
  1168. }
  1169. if (shortcutHandler != null) {
  1170. shortcutHandler
  1171. .handleKeyboardEvent(Event.as(event.getNativeEvent()));
  1172. return;
  1173. }
  1174. }
  1175. @Override
  1176. public void onBlur(BlurEvent event) {
  1177. if (client.hasEventListeners(this, EventId.BLUR)) {
  1178. client.updateVariable(id, EventId.BLUR, "", true);
  1179. }
  1180. }
  1181. @Override
  1182. public void onFocus(FocusEvent event) {
  1183. if (client.hasEventListeners(this, EventId.FOCUS)) {
  1184. client.updateVariable(id, EventId.FOCUS, "", true);
  1185. }
  1186. }
  1187. @Override
  1188. public void focus() {
  1189. // We don't want to use contentPanel.focus() as that will use a timer in
  1190. // Chrome/Safari and ultimately run focus events in the wrong order when
  1191. // opening a modal window and focusing some other component at the same
  1192. // time
  1193. contentPanel.getElement().focus();
  1194. }
  1195. private int getDecorationHeight() {
  1196. LayoutManager lm = getLayoutManager();
  1197. int headerHeight = lm.getOuterHeight(header);
  1198. int footerHeight = lm.getOuterHeight(footer);
  1199. return headerHeight + footerHeight;
  1200. }
  1201. private LayoutManager getLayoutManager() {
  1202. return LayoutManager.get(client);
  1203. }
  1204. private int getDecorationWidth() {
  1205. LayoutManager layoutManager = getLayoutManager();
  1206. return layoutManager.getOuterWidth(getElement())
  1207. - contentPanel.getElement().getOffsetWidth();
  1208. }
  1209. /**
  1210. * Allows to specify which connectors contain the description for the
  1211. * window. Text contained in the widgets of the connectors will be read by
  1212. * assistive devices when it is opened.
  1213. * <p>
  1214. * When the provided array is empty, an existing description is removed.
  1215. *
  1216. * @param connectors
  1217. * with the connectors of the widgets to use as description
  1218. */
  1219. public void setAssistiveDescription(Connector[] connectors) {
  1220. if (connectors != null) {
  1221. assistiveConnectors = connectors;
  1222. if (connectors.length == 0) {
  1223. Roles.getDialogRole()
  1224. .removeAriaDescribedbyProperty(getElement());
  1225. } else {
  1226. Id[] ids = new Id[connectors.length];
  1227. for (int index = 0; index < connectors.length; index++) {
  1228. if (connectors[index] == null) {
  1229. throw new IllegalArgumentException(
  1230. "All values in parameter description need to be non-null");
  1231. }
  1232. Element element = ((ComponentConnector) connectors[index])
  1233. .getWidget().getElement();
  1234. AriaHelper.ensureHasId(element);
  1235. ids[index] = Id.of(element);
  1236. }
  1237. Roles.getDialogRole().setAriaDescribedbyProperty(getElement(),
  1238. ids);
  1239. }
  1240. } else {
  1241. throw new IllegalArgumentException(
  1242. "Parameter description must be non-null");
  1243. }
  1244. }
  1245. /**
  1246. * Gets the connectors that are used as assistive description. Text
  1247. * contained in these connectors will be read by assistive devices when the
  1248. * window is opened.
  1249. *
  1250. * @return list of previously set connectors
  1251. */
  1252. public List<Connector> getAssistiveDescription() {
  1253. return Collections.unmodifiableList(Arrays.asList(assistiveConnectors));
  1254. }
  1255. /**
  1256. * Sets the WAI-ARIA role the window.
  1257. *
  1258. * This role defines how an assistive device handles a window. Available
  1259. * roles are alertdialog and dialog (@see
  1260. * <a href="http://www.w3.org/TR/2011/CR-wai-aria-20110118/roles">Roles
  1261. * Model</a>).
  1262. *
  1263. * The default role is dialog.
  1264. *
  1265. * @param role
  1266. * WAI-ARIA role to set for the window
  1267. */
  1268. public void setWaiAriaRole(WindowRole role) {
  1269. if (role == WindowRole.ALERTDIALOG) {
  1270. Roles.getAlertdialogRole().set(getElement());
  1271. } else {
  1272. Roles.getDialogRole().set(getElement());
  1273. }
  1274. }
  1275. /**
  1276. * Registers the handlers that prevent to leave the window using the
  1277. * Tab-key.
  1278. * <p>
  1279. * The value of the parameter doTabStop is stored and used for non-modal
  1280. * windows. For modal windows, the handlers are always registered, while
  1281. * preserving the stored value.
  1282. *
  1283. * @param doTabStop
  1284. * true to prevent leaving the window, false to allow leaving the
  1285. * window for non modal windows
  1286. */
  1287. public void setTabStopEnabled(boolean doTabStop) {
  1288. this.doTabStop = doTabStop;
  1289. if (doTabStop || vaadinModality) {
  1290. addTabBlockHandlers();
  1291. } else {
  1292. removeTabBlockHandlers();
  1293. }
  1294. }
  1295. /**
  1296. * Adds a Handler for when user moves the window.
  1297. *
  1298. * @since 7.1.9
  1299. *
  1300. * @return {@link HandlerRegistration} used to remove the handler
  1301. */
  1302. public HandlerRegistration addMoveHandler(WindowMoveHandler handler) {
  1303. return addHandler(handler, WindowMoveEvent.getType());
  1304. }
  1305. /**
  1306. * Adds a Handler for window order change event.
  1307. *
  1308. * @since 8.0.0
  1309. *
  1310. * @return registration object to deregister the handler
  1311. */
  1312. public static HandlerRegistration addWindowOrderHandler(
  1313. WindowOrderHandler handler) {
  1314. return WINDOW_ORDER_HANDLER.addHandler(WindowOrderEvent.getType(),
  1315. handler);
  1316. }
  1317. /**
  1318. * Checks if a modal window is currently open.
  1319. *
  1320. * @return <code>true</code> if a modal window is open, <code>false</code>
  1321. * otherwise.
  1322. */
  1323. public static boolean isModalWindowOpen() {
  1324. return Document.get().getBody()
  1325. .hasClassName(MODAL_WINDOW_OPEN_CLASSNAME);
  1326. }
  1327. }