Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VWindow.java 49KB

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