您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VWindow.java 50KB

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