Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

UIConnector.java 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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.ui;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.Comparator;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.logging.Logger;
  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.HeadElement;
  30. import com.google.gwt.dom.client.LinkElement;
  31. import com.google.gwt.dom.client.NativeEvent;
  32. import com.google.gwt.dom.client.NodeList;
  33. import com.google.gwt.dom.client.Style;
  34. import com.google.gwt.dom.client.Style.Position;
  35. import com.google.gwt.dom.client.StyleInjector;
  36. import com.google.gwt.event.dom.client.KeyDownEvent;
  37. import com.google.gwt.event.dom.client.KeyDownHandler;
  38. import com.google.gwt.event.dom.client.ScrollEvent;
  39. import com.google.gwt.event.dom.client.ScrollHandler;
  40. import com.google.gwt.event.logical.shared.ResizeEvent;
  41. import com.google.gwt.event.logical.shared.ResizeHandler;
  42. import com.google.gwt.event.shared.HandlerRegistration;
  43. import com.google.gwt.user.client.Command;
  44. import com.google.gwt.user.client.DOM;
  45. import com.google.gwt.user.client.Event;
  46. import com.google.gwt.user.client.History;
  47. import com.google.gwt.user.client.Timer;
  48. import com.google.gwt.user.client.Window;
  49. import com.google.gwt.user.client.ui.RootPanel;
  50. import com.google.gwt.user.client.ui.Widget;
  51. import com.vaadin.client.ApplicationConnection;
  52. import com.vaadin.client.ApplicationConnection.ApplicationStoppedEvent;
  53. import com.vaadin.client.BrowserInfo;
  54. import com.vaadin.client.ComponentConnector;
  55. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  56. import com.vaadin.client.Focusable;
  57. import com.vaadin.client.Paintable;
  58. import com.vaadin.client.ResourceLoader;
  59. import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
  60. import com.vaadin.client.ResourceLoader.ResourceLoadListener;
  61. import com.vaadin.client.ServerConnector;
  62. import com.vaadin.client.UIDL;
  63. import com.vaadin.client.Util;
  64. import com.vaadin.client.VConsole;
  65. import com.vaadin.client.ValueMap;
  66. import com.vaadin.client.annotations.OnStateChange;
  67. import com.vaadin.client.communication.StateChangeEvent;
  68. import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
  69. import com.vaadin.client.extensions.DragSourceExtensionConnector;
  70. import com.vaadin.client.extensions.DropTargetExtensionConnector;
  71. import com.vaadin.client.ui.AbstractConnector;
  72. import com.vaadin.client.ui.AbstractSingleComponentContainerConnector;
  73. import com.vaadin.client.ui.ClickEventHandler;
  74. import com.vaadin.client.ui.ShortcutActionHandler;
  75. import com.vaadin.client.ui.VOverlay;
  76. import com.vaadin.client.ui.VUI;
  77. import com.vaadin.client.ui.VWindow;
  78. import com.vaadin.client.ui.layout.MayScrollChildren;
  79. import com.vaadin.client.ui.window.WindowConnector;
  80. import com.vaadin.client.ui.window.WindowOrderEvent;
  81. import com.vaadin.client.ui.window.WindowOrderHandler;
  82. import com.vaadin.server.Page.Styles;
  83. import com.vaadin.shared.ApplicationConstants;
  84. import com.vaadin.shared.Connector;
  85. import com.vaadin.shared.EventId;
  86. import com.vaadin.shared.MouseEventDetails;
  87. import com.vaadin.shared.Version;
  88. import com.vaadin.shared.communication.MethodInvocation;
  89. import com.vaadin.shared.ui.ComponentStateUtil;
  90. import com.vaadin.shared.ui.Connect;
  91. import com.vaadin.shared.ui.Connect.LoadStyle;
  92. import com.vaadin.shared.ui.WindowOrderRpc;
  93. import com.vaadin.shared.ui.ui.DebugWindowClientRpc;
  94. import com.vaadin.shared.ui.ui.DebugWindowServerRpc;
  95. import com.vaadin.shared.ui.ui.PageClientRpc;
  96. import com.vaadin.shared.ui.ui.PageState;
  97. import com.vaadin.shared.ui.ui.ScrollClientRpc;
  98. import com.vaadin.shared.ui.ui.UIClientRpc;
  99. import com.vaadin.shared.ui.ui.UIConstants;
  100. import com.vaadin.shared.ui.ui.UIServerRpc;
  101. import com.vaadin.shared.ui.ui.UIState;
  102. import com.vaadin.shared.util.SharedUtil;
  103. import com.vaadin.ui.UI;
  104. import elemental.client.Browser;
  105. @Connect(value = UI.class, loadStyle = LoadStyle.EAGER)
  106. public class UIConnector extends AbstractSingleComponentContainerConnector
  107. implements Paintable, MayScrollChildren {
  108. private HandlerRegistration childStateChangeHandlerRegistration;
  109. private String activeTheme = null;
  110. private HandlerRegistration windowOrderRegistration;
  111. /*
  112. * Used to workaround IE bug related to popstate events and certain fragment
  113. * only changes
  114. */
  115. private String currentLocation;
  116. private final StateChangeHandler childStateChangeHandler = new StateChangeHandler() {
  117. @Override
  118. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  119. // TODO Should use a more specific handler that only reacts to
  120. // size changes
  121. onChildSizeChange();
  122. }
  123. };
  124. private WindowOrderHandler windowOrderHandler = new WindowOrderHandler() {
  125. @Override
  126. public void onWindowOrderChange(WindowOrderEvent event) {
  127. VWindow[] windows = event.getWindows();
  128. Map<Integer, Connector> orders = new HashMap<>();
  129. boolean hasEventListener = hasEventListener(EventId.WINDOW_ORDER);
  130. for (VWindow window : windows) {
  131. Connector connector = Util.findConnectorFor(window);
  132. orders.put(window.getWindowOrder(), connector);
  133. if (connector instanceof AbstractConnector
  134. && ((AbstractConnector) connector)
  135. .hasEventListener(EventId.WINDOW_ORDER)) {
  136. hasEventListener = true;
  137. }
  138. }
  139. if (hasEventListener) {
  140. getRpcProxy(WindowOrderRpc.class).windowOrderChanged(orders);
  141. }
  142. }
  143. };
  144. @Override
  145. protected void init() {
  146. super.init();
  147. windowOrderRegistration = VWindow
  148. .addWindowOrderHandler(windowOrderHandler);
  149. registerRpc(PageClientRpc.class, new PageClientRpc() {
  150. @Override
  151. public void reload() {
  152. Window.Location.reload();
  153. }
  154. @Override
  155. public void initializeMobileHtml5DndPolyfill() {
  156. initializeMobileDndPolyfill();
  157. }
  158. });
  159. registerRpc(ScrollClientRpc.class, new ScrollClientRpc() {
  160. @Override
  161. public void setScrollTop(int scrollTop) {
  162. getWidget().getElement().setScrollTop(scrollTop);
  163. }
  164. @Override
  165. public void setScrollLeft(int scrollLeft) {
  166. getWidget().getElement().setScrollLeft(scrollLeft);
  167. }
  168. });
  169. registerRpc(UIClientRpc.class, new UIClientRpc() {
  170. @Override
  171. public void uiClosed(final boolean sessionExpired) {
  172. Scheduler.get().scheduleDeferred(() -> {
  173. // Only notify user if we're still running and not eg.
  174. // navigating away (#12298)
  175. if (getConnection().isApplicationRunning()) {
  176. if (sessionExpired) {
  177. getConnection().showSessionExpiredError(null);
  178. } else {
  179. getState().enabled = false;
  180. updateEnabledState(getState().enabled);
  181. }
  182. getConnection().setApplicationRunning(false);
  183. }
  184. });
  185. }
  186. });
  187. registerRpc(DebugWindowClientRpc.class, new DebugWindowClientRpc() {
  188. @Override
  189. public void reportLayoutProblems(String json) {
  190. VConsole.printLayoutProblems(getValueMap(json),
  191. getConnection());
  192. }
  193. private native ValueMap getValueMap(String json)
  194. /*-{
  195. return JSON.parse(json);
  196. }-*/;
  197. });
  198. getWidget().addResizeHandler(new ResizeHandler() {
  199. @Override
  200. public void onResize(ResizeEvent event) {
  201. getRpcProxy(UIServerRpc.class).resize(event.getWidth(),
  202. event.getHeight(), Window.getClientWidth(),
  203. Window.getClientHeight());
  204. getConnection().getServerRpcQueue().flush();
  205. }
  206. });
  207. getWidget().addScrollHandler(new ScrollHandler() {
  208. private int lastSentScrollTop = Integer.MAX_VALUE;
  209. private int lastSentScrollLeft = Integer.MAX_VALUE;
  210. @Override
  211. public void onScroll(ScrollEvent event) {
  212. Element element = getWidget().getElement();
  213. int newScrollTop = element.getScrollTop();
  214. int newScrollLeft = element.getScrollLeft();
  215. if (newScrollTop != lastSentScrollTop
  216. || newScrollLeft != lastSentScrollLeft) {
  217. lastSentScrollTop = newScrollTop;
  218. lastSentScrollLeft = newScrollLeft;
  219. getRpcProxy(UIServerRpc.class).scroll(newScrollTop,
  220. newScrollLeft);
  221. }
  222. }
  223. });
  224. Browser.getWindow().setOnpopstate(evt -> {
  225. final String newLocation = Browser.getWindow().getLocation()
  226. .toString();
  227. getRpcProxy(UIServerRpc.class).popstate(newLocation);
  228. currentLocation = newLocation;
  229. });
  230. // IE doesn't fire popstate correctly with certain hash changes.
  231. // Simulate the missing event with History handler.
  232. if (BrowserInfo.get().isIE()) {
  233. History.addValueChangeHandler(evt -> {
  234. final String newLocation = Browser.getWindow().getLocation()
  235. .toString();
  236. if (!newLocation.equals(currentLocation)) {
  237. currentLocation = newLocation;
  238. getRpcProxy(UIServerRpc.class).popstate(
  239. Browser.getWindow().getLocation().toString());
  240. }
  241. });
  242. currentLocation = Browser.getWindow().getLocation().toString();
  243. }
  244. }
  245. private native void open(String url, String name)
  246. /*-{
  247. $wnd.open(url, name);
  248. }-*/;
  249. @Override
  250. public void updateFromUIDL(final UIDL uidl, ApplicationConnection client) {
  251. getWidget().id = getConnectorId();
  252. boolean firstPaint = getWidget().connection == null;
  253. getWidget().connection = client;
  254. getWidget().resizeLazy = uidl.hasAttribute(UIConstants.RESIZE_LAZY);
  255. // this also implicitly removes old styles
  256. String styles = "";
  257. styles += getWidget().getStylePrimaryName() + " ";
  258. if (ComponentStateUtil.hasStyles(getState())) {
  259. for (String style : getState().styles) {
  260. styles += style + " ";
  261. }
  262. }
  263. if (!client.getConfiguration().isStandalone()) {
  264. styles += getWidget().getStylePrimaryName() + "-embedded";
  265. }
  266. getWidget().setStyleName(styles.trim());
  267. getWidget().makeScrollable();
  268. clickEventHandler.handleEventHandlerRegistration();
  269. // Process children
  270. int childIndex = 0;
  271. // Open URL:s
  272. boolean isClosed = false; // was this window closed?
  273. while (childIndex < uidl.getChildCount()
  274. && "open".equals(uidl.getChildUIDL(childIndex).getTag())) {
  275. final UIDL open = uidl.getChildUIDL(childIndex);
  276. final String url = client
  277. .translateVaadinUri(open.getStringAttribute("src"));
  278. final String target = open.getStringAttribute("name");
  279. if (target == null) {
  280. // source will be opened to this browser window, but we may have
  281. // to finish rendering this window in case this is a download
  282. // (and window stays open).
  283. Scheduler.get().scheduleDeferred(new Command() {
  284. @Override
  285. public void execute() {
  286. VUI.goTo(url);
  287. }
  288. });
  289. } else if ("_self".equals(target)) {
  290. // This window is closing (for sure). Only other opens are
  291. // relevant in this change. See #3558, #2144
  292. isClosed = true;
  293. VUI.goTo(url);
  294. } else {
  295. String options;
  296. boolean alwaysAsPopup = true;
  297. if (open.hasAttribute("popup")) {
  298. alwaysAsPopup = open.getBooleanAttribute("popup");
  299. }
  300. if (alwaysAsPopup) {
  301. if (open.hasAttribute("border")) {
  302. if (open.getStringAttribute("border")
  303. .equals("minimal")) {
  304. options = "menubar=yes,location=no,status=no";
  305. } else {
  306. options = "menubar=no,location=no,status=no";
  307. }
  308. } else {
  309. options = "resizable=yes,menubar=yes,toolbar=yes,directories=yes,location=yes,scrollbars=yes,status=yes";
  310. }
  311. if (open.hasAttribute("width")) {
  312. int w = open.getIntAttribute("width");
  313. options += ",width=" + w;
  314. }
  315. if (open.hasAttribute("height")) {
  316. int h = open.getIntAttribute("height");
  317. options += ",height=" + h;
  318. }
  319. Window.open(url, target, options);
  320. } else {
  321. open(url, target);
  322. }
  323. }
  324. childIndex++;
  325. }
  326. if (isClosed) {
  327. // We're navigating away, so stop the application.
  328. client.setApplicationRunning(false);
  329. return;
  330. }
  331. // Handle other UIDL children
  332. UIDL childUidl;
  333. while ((childUidl = uidl.getChildUIDL(childIndex++)) != null) {
  334. String tag = childUidl.getTag().intern();
  335. if (tag == "actions") {
  336. if (getWidget().actionHandler == null) {
  337. getWidget().actionHandler = new ShortcutActionHandler(
  338. getWidget().id, client);
  339. }
  340. getWidget().actionHandler.updateActionMap(childUidl);
  341. } else if (tag == "css-injections") {
  342. injectCSS(childUidl);
  343. }
  344. }
  345. if (uidl.hasAttribute("focused")) {
  346. // set focused component when render phase is finished
  347. Scheduler.get().scheduleDeferred(new Command() {
  348. @Override
  349. public void execute() {
  350. ComponentConnector connector = (ComponentConnector) uidl
  351. .getPaintableAttribute("focused", getConnection());
  352. if (connector == null) {
  353. // Do not try to focus invisible components which not
  354. // present in UIDL
  355. return;
  356. }
  357. final Widget toBeFocused = connector.getWidget();
  358. /*
  359. * Two types of Widgets can be focused, either implementing
  360. * GWT Focusable of a thinner Vaadin specific Focusable
  361. * interface.
  362. */
  363. if (toBeFocused instanceof com.google.gwt.user.client.ui.Focusable) {
  364. final com.google.gwt.user.client.ui.Focusable toBeFocusedWidget = (com.google.gwt.user.client.ui.Focusable) toBeFocused;
  365. toBeFocusedWidget.setFocus(true);
  366. } else if (toBeFocused instanceof Focusable) {
  367. ((Focusable) toBeFocused).focus();
  368. } else {
  369. getLogger().severe(
  370. "Server is trying to set focus to the widget of connector "
  371. + Util.getConnectorString(connector)
  372. + " but it is not focusable. The widget should implement either "
  373. + com.google.gwt.user.client.ui.Focusable.class
  374. .getName()
  375. + " or " + Focusable.class.getName());
  376. }
  377. }
  378. });
  379. }
  380. // Add window listeners on first paint, to prevent premature
  381. // variablechanges
  382. if (firstPaint) {
  383. Window.addWindowClosingHandler(getWidget());
  384. Window.addResizeHandler(getWidget());
  385. }
  386. if (uidl.hasAttribute("scrollTo")) {
  387. final ComponentConnector connector = (ComponentConnector) uidl
  388. .getPaintableAttribute("scrollTo", getConnection());
  389. scrollIntoView(connector);
  390. }
  391. if (uidl.hasAttribute(UIConstants.ATTRIBUTE_PUSH_STATE)) {
  392. Browser.getWindow().getHistory().pushState(null,
  393. getState().pageState.title,
  394. uidl.getStringAttribute(UIConstants.ATTRIBUTE_PUSH_STATE));
  395. }
  396. if (uidl.hasAttribute(UIConstants.ATTRIBUTE_REPLACE_STATE)) {
  397. Browser.getWindow().getHistory().replaceState(null,
  398. getState().pageState.title, uidl.getStringAttribute(
  399. UIConstants.ATTRIBUTE_REPLACE_STATE));
  400. }
  401. if (firstPaint) {
  402. // Queue the initial window size to be sent with the following
  403. // request.
  404. Scheduler.get()
  405. .scheduleDeferred(() -> getWidget().sendClientResized());
  406. }
  407. }
  408. /**
  409. * Reads CSS strings and resources injected by {@link Styles#inject} from
  410. * the UIDL stream.
  411. *
  412. * @param uidl
  413. * The uidl which contains "css-resource" and "css-string" tags
  414. */
  415. private void injectCSS(UIDL uidl) {
  416. /*
  417. * Search the UIDL stream for CSS resources and strings to be injected.
  418. */
  419. for (Object child : uidl) {
  420. UIDL cssInjectionsUidl = (UIDL) child;
  421. // Check if we have resources to inject
  422. if (cssInjectionsUidl.getTag().equals("css-resource")) {
  423. String url = getWidget().connection.translateVaadinUri(
  424. cssInjectionsUidl.getStringAttribute("url"));
  425. LinkElement link = LinkElement
  426. .as(DOM.createElement(LinkElement.TAG));
  427. link.setRel("stylesheet");
  428. link.setHref(url);
  429. link.setType("text/css");
  430. getHead().appendChild(link);
  431. // Check if we have CSS string to inject
  432. } else if (cssInjectionsUidl.getTag().equals("css-string")) {
  433. for (Object c : cssInjectionsUidl) {
  434. StyleInjector.injectAtEnd((String) c);
  435. StyleInjector.flush();
  436. }
  437. }
  438. }
  439. }
  440. /**
  441. * Internal helper to get the <head> tag of the page
  442. *
  443. * @since 7.3
  444. * @return the head element
  445. */
  446. private HeadElement getHead() {
  447. return HeadElement.as(Document.get()
  448. .getElementsByTagName(HeadElement.TAG).getItem(0));
  449. }
  450. /**
  451. * Internal helper for removing any stylesheet with the given URL
  452. *
  453. * @since 7.3
  454. * @param url
  455. * the url to match with existing stylesheets
  456. */
  457. private void removeStylesheet(String url) {
  458. NodeList<Element> linkTags = getHead()
  459. .getElementsByTagName(LinkElement.TAG);
  460. for (int i = 0; i < linkTags.getLength(); i++) {
  461. LinkElement link = LinkElement.as(linkTags.getItem(i));
  462. if (!"stylesheet".equals(link.getRel())) {
  463. continue;
  464. }
  465. if (!"text/css".equals(link.getType())) {
  466. continue;
  467. }
  468. if (url.equals(link.getHref())) {
  469. getHead().removeChild(link);
  470. }
  471. }
  472. }
  473. public void init(String rootPanelId,
  474. ApplicationConnection applicationConnection) {
  475. Widget shortcutContextWidget = getWidget();
  476. if (applicationConnection.getConfiguration().isStandalone()) {
  477. // Listen to body for standalone apps (#19392)
  478. shortcutContextWidget = RootPanel.get(); // document body
  479. }
  480. shortcutContextWidget.addDomHandler(new KeyDownHandler() {
  481. @Override
  482. public void onKeyDown(KeyDownEvent event) {
  483. if (VWindow.isModalWindowOpen()) {
  484. return;
  485. }
  486. if (getWidget().actionHandler != null) {
  487. Element target = Element
  488. .as(event.getNativeEvent().getEventTarget());
  489. if (target == Document.get().getBody()
  490. || getWidget().getElement().isOrHasChild(target)) {
  491. // Only react to body and elements inside the UI
  492. getWidget().actionHandler.handleKeyboardEvent(
  493. (Event) event.getNativeEvent().cast());
  494. }
  495. }
  496. }
  497. }, KeyDownEvent.getType());
  498. DOM.sinkEvents(getWidget().getElement(), Event.ONSCROLL);
  499. RootPanel root = RootPanel.get(rootPanelId);
  500. // Remove the v-app-loading or any splash screen added inside the div by
  501. // the user
  502. root.getElement().setInnerHTML("");
  503. // Activate the initial theme by only adding the class name. Not calling
  504. // activateTheme here as it will also cause a full layout and updates to
  505. // the overlay container which has not yet been created at this point
  506. activeTheme = applicationConnection.getConfiguration().getThemeName();
  507. root.addStyleName(activeTheme);
  508. root.add(getWidget());
  509. // Set default tab index before focus call. State change handler
  510. // will update this later if needed.
  511. getWidget().setTabIndex(1);
  512. if (applicationConnection.getConfiguration().isStandalone()) {
  513. // set focus to iview element by default to listen possible keyboard
  514. // shortcuts. For embedded applications this is unacceptable as we
  515. // don't want to steal focus from the main page nor we don't want
  516. // side-effects from focusing (scrollIntoView).
  517. getWidget().getElement().focus();
  518. }
  519. applicationConnection.addHandler(
  520. ApplicationConnection.ApplicationStoppedEvent.TYPE,
  521. new ApplicationConnection.ApplicationStoppedHandler() {
  522. @Override
  523. public void onApplicationStopped(
  524. ApplicationStoppedEvent event) {
  525. // Stop any polling
  526. if (pollTimer != null) {
  527. pollTimer.cancel();
  528. pollTimer = null;
  529. }
  530. }
  531. });
  532. }
  533. private ClickEventHandler clickEventHandler = new ClickEventHandler(this) {
  534. @Override
  535. protected void fireClick(NativeEvent event,
  536. MouseEventDetails mouseDetails) {
  537. getRpcProxy(UIServerRpc.class).click(mouseDetails);
  538. }
  539. };
  540. private Timer pollTimer = null;
  541. @Override
  542. public void updateCaption(ComponentConnector component) {
  543. // NOP The main view never draws caption for its layout
  544. }
  545. @Override
  546. public VUI getWidget() {
  547. return (VUI) super.getWidget();
  548. }
  549. @Override
  550. protected ComponentConnector getContent() {
  551. ComponentConnector connector = super.getContent();
  552. // VWindow (WindowConnector is its connector)is also a child component
  553. // but it's never a content widget
  554. if (connector instanceof WindowConnector) {
  555. return null;
  556. } else {
  557. return connector;
  558. }
  559. }
  560. protected void onChildSizeChange() {
  561. ComponentConnector child = getContent();
  562. if (child == null) {
  563. return;
  564. }
  565. Style childStyle = child.getWidget().getElement().getStyle();
  566. /*
  567. * Must set absolute position if the child has relative height and
  568. * there's a chance of horizontal scrolling as some browsers will
  569. * otherwise not take the scrollbar into account when calculating the
  570. * height. Assuming v-ui does not have an undefined width for now, see
  571. * #8460.
  572. */
  573. if (child.isRelativeHeight()) {
  574. childStyle.setPosition(Position.ABSOLUTE);
  575. } else {
  576. childStyle.clearPosition();
  577. }
  578. }
  579. /**
  580. * Checks if the given sub window is a child of this UI Connector.
  581. *
  582. * @deprecated Should be replaced by a more generic mechanism for getting
  583. * non-ComponentConnector children
  584. * @param wc
  585. * @return
  586. */
  587. @Deprecated
  588. public boolean hasSubWindow(WindowConnector wc) {
  589. return getChildComponents().contains(wc);
  590. }
  591. /**
  592. * Return an iterator for current subwindows. This method is meant for
  593. * testing purposes only.
  594. *
  595. * @return
  596. */
  597. public List<WindowConnector> getSubWindows() {
  598. List<WindowConnector> windows = new ArrayList<>();
  599. for (ComponentConnector child : getChildComponents()) {
  600. if (child instanceof WindowConnector) {
  601. windows.add((WindowConnector) child);
  602. }
  603. }
  604. return windows;
  605. }
  606. @Override
  607. public UIState getState() {
  608. return (UIState) super.getState();
  609. }
  610. /**
  611. * Returns the state of the Page associated with the UI.
  612. * <p>
  613. * Note that state is considered an internal part of the connector. You
  614. * should not rely on the state object outside of the connector who owns it.
  615. * If you depend on the state of other connectors you should use their
  616. * public API instead of their state object directly. The page state might
  617. * not be an independent state object but can be embedded in UI state.
  618. * </p>
  619. *
  620. * @since 7.1
  621. * @return state object of the page
  622. */
  623. public PageState getPageState() {
  624. return getState().pageState;
  625. }
  626. @Override
  627. public void onConnectorHierarchyChange(
  628. ConnectorHierarchyChangeEvent event) {
  629. ComponentConnector oldChild = null;
  630. ComponentConnector newChild = getContent();
  631. for (ComponentConnector c : event.getOldChildren()) {
  632. if (!(c instanceof WindowConnector)) {
  633. oldChild = c;
  634. break;
  635. }
  636. }
  637. if (oldChild != newChild) {
  638. if (childStateChangeHandlerRegistration != null) {
  639. childStateChangeHandlerRegistration.removeHandler();
  640. childStateChangeHandlerRegistration = null;
  641. }
  642. if (newChild != null) {
  643. getWidget().setWidget(newChild.getWidget());
  644. childStateChangeHandlerRegistration = newChild
  645. .addStateChangeHandler(childStateChangeHandler);
  646. // Must handle new child here as state change events are already
  647. // fired
  648. onChildSizeChange();
  649. } else {
  650. getWidget().setWidget(null);
  651. }
  652. }
  653. for (ComponentConnector c : getChildComponents()) {
  654. if (c instanceof WindowConnector) {
  655. WindowConnector wc = (WindowConnector) c;
  656. wc.setWindowOrderAndPosition();
  657. VWindow window = wc.getWidget();
  658. if (!window.isAttached()) {
  659. // Attach so that all widgets inside the Window are attached
  660. // when their onStateChange is run
  661. // Made invisible here for legacy reasons and made visible
  662. // at the end of stateChange. This dance could probably be
  663. // removed
  664. window.setVisible(false);
  665. window.show();
  666. }
  667. }
  668. }
  669. setWindowOrderAndPosition();
  670. // Close removed sub windows
  671. for (ComponentConnector c : event.getOldChildren()) {
  672. if (c.getParent() != this && c instanceof WindowConnector) {
  673. ((WindowConnector) c).getWidget().hide();
  674. }
  675. }
  676. }
  677. @Override
  678. public boolean hasTooltip() {
  679. /*
  680. * Always return true so there's always top level tooltip handler that
  681. * takes care of hiding tooltips whenever the mouse is moved somewhere
  682. * else.
  683. */
  684. return true;
  685. }
  686. /**
  687. * Tries to scroll the viewport so that the given connector is in view.
  688. *
  689. * @param componentConnector
  690. * The connector which should be visible
  691. *
  692. */
  693. public void scrollIntoView(final ComponentConnector componentConnector) {
  694. if (componentConnector == null) {
  695. return;
  696. }
  697. Scheduler.get().scheduleDeferred(new Command() {
  698. @Override
  699. public void execute() {
  700. componentConnector.getWidget().getElement().scrollIntoView();
  701. }
  702. });
  703. }
  704. @Override
  705. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  706. super.onStateChanged(stateChangeEvent);
  707. if (stateChangeEvent.hasPropertyChanged("tooltipConfiguration")) {
  708. getConnection().getVTooltip().setCloseTimeout(
  709. getState().tooltipConfiguration.closeTimeout);
  710. getConnection().getVTooltip()
  711. .setOpenDelay(getState().tooltipConfiguration.openDelay);
  712. getConnection().getVTooltip().setQuickOpenDelay(
  713. getState().tooltipConfiguration.quickOpenDelay);
  714. getConnection().getVTooltip().setQuickOpenTimeout(
  715. getState().tooltipConfiguration.quickOpenTimeout);
  716. getConnection().getVTooltip()
  717. .setMaxWidth(getState().tooltipConfiguration.maxWidth);
  718. }
  719. if (stateChangeEvent
  720. .hasPropertyChanged("loadingIndicatorConfiguration")) {
  721. getConnection().getLoadingIndicator().setFirstDelay(
  722. getState().loadingIndicatorConfiguration.firstDelay);
  723. getConnection().getLoadingIndicator().setSecondDelay(
  724. getState().loadingIndicatorConfiguration.secondDelay);
  725. getConnection().getLoadingIndicator().setThirdDelay(
  726. getState().loadingIndicatorConfiguration.thirdDelay);
  727. }
  728. if (stateChangeEvent.hasPropertyChanged("pollInterval")) {
  729. configurePolling();
  730. }
  731. if (stateChangeEvent.hasPropertyChanged("pageState.title")) {
  732. String title = getState().pageState.title;
  733. if (title != null) {
  734. com.google.gwt.user.client.Window.setTitle(title);
  735. }
  736. }
  737. if (stateChangeEvent.hasPropertyChanged("pushConfiguration")) {
  738. getConnection().getMessageSender().setPushEnabled(
  739. getState().pushConfiguration.mode.isEnabled());
  740. }
  741. if (stateChangeEvent
  742. .hasPropertyChanged("reconnectDialogConfiguration")) {
  743. getConnection().getConnectionStateHandler().configurationUpdated();
  744. }
  745. if (stateChangeEvent.hasPropertyChanged("overlayContainerLabel")) {
  746. VOverlay.setOverlayContainerLabel(getConnection(),
  747. getState().overlayContainerLabel);
  748. }
  749. }
  750. private void configurePolling() {
  751. if (pollTimer != null) {
  752. pollTimer.cancel();
  753. pollTimer = null;
  754. }
  755. if (getState().pollInterval >= 0) {
  756. pollTimer = new Timer() {
  757. @Override
  758. public void run() {
  759. if (getState().pollInterval < 0) {
  760. // Polling has been cancelled server side
  761. pollTimer.cancel();
  762. pollTimer = null;
  763. return;
  764. }
  765. getRpcProxy(UIServerRpc.class).poll();
  766. // Send changes even though poll is @Delayed
  767. getConnection().getServerRpcQueue().flush();
  768. }
  769. };
  770. pollTimer.scheduleRepeating(getState().pollInterval);
  771. } else {
  772. // Ensure no more polls are sent as polling has been disabled
  773. getConnection().getServerRpcQueue()
  774. .removeMatching(new MethodInvocation(getConnectorId(),
  775. UIServerRpc.class.getName(), "poll"));
  776. }
  777. }
  778. /**
  779. * Invokes the layout analyzer on the server.
  780. *
  781. * @since 7.1
  782. */
  783. public void analyzeLayouts() {
  784. getRpcProxy(DebugWindowServerRpc.class).analyzeLayouts();
  785. }
  786. /**
  787. * Sends a request to the server to print details to console that will help
  788. * the developer to locate the corresponding server-side connector in the
  789. * source code.
  790. *
  791. * @since 7.1
  792. * @param serverConnector
  793. * the connector to locate
  794. */
  795. public void showServerDebugInfo(ServerConnector serverConnector) {
  796. getRpcProxy(DebugWindowServerRpc.class)
  797. .showServerDebugInfo(serverConnector);
  798. }
  799. /**
  800. * Sends a request to the server to print a design to the console for the
  801. * given component.
  802. *
  803. * @since 7.5
  804. * @param connector
  805. * the component connector to output a declarative design for
  806. */
  807. public void showServerDesign(ServerConnector connector) {
  808. getRpcProxy(DebugWindowServerRpc.class).showServerDesign(connector);
  809. }
  810. @OnStateChange("theme")
  811. void onThemeChange() {
  812. final String oldTheme = activeTheme;
  813. final String newTheme = getState().theme;
  814. final String oldThemeUrl = getThemeUrl(oldTheme);
  815. final String newThemeUrl = getThemeUrl(newTheme);
  816. if (SharedUtil.equals(oldTheme, newTheme)) {
  817. // This should only happen on the initial load when activeTheme has
  818. // been updated in init.
  819. if (newTheme == null) {
  820. return;
  821. }
  822. // For the embedded case we cannot be 100% sure that the theme has
  823. // been loaded and that the style names have been set.
  824. if (findStylesheetTag(oldThemeUrl) == null) {
  825. // If there is no style tag, load it the normal way (the class
  826. // name will be added when theme has been loaded)
  827. replaceTheme(null, newTheme, null, newThemeUrl);
  828. } else if (!getWidget().getParent().getElement()
  829. .hasClassName(newTheme)) {
  830. // If only the class name is missing, add that
  831. activateTheme(newTheme);
  832. }
  833. return;
  834. }
  835. getLogger().info("Changing theme from " + oldTheme + " to " + newTheme);
  836. replaceTheme(oldTheme, newTheme, oldThemeUrl, newThemeUrl);
  837. }
  838. /**
  839. * Loads the new theme and removes references to the old theme.
  840. *
  841. * @since 7.4.3
  842. * @param oldTheme
  843. * The name of the old theme
  844. * @param newTheme
  845. * The name of the new theme
  846. * @param oldThemeUrl
  847. * The url of the old theme
  848. * @param newThemeUrl
  849. * The url of the new theme
  850. */
  851. protected void replaceTheme(final String oldTheme, final String newTheme,
  852. String oldThemeUrl, final String newThemeUrl) {
  853. LinkElement tagToReplace = null;
  854. if (oldTheme != null) {
  855. tagToReplace = findStylesheetTag(oldThemeUrl);
  856. if (tagToReplace == null) {
  857. getLogger()
  858. .warning("Did not find the link tag for the old theme ("
  859. + oldThemeUrl
  860. + "), adding a new stylesheet for the new theme ("
  861. + newThemeUrl + ")");
  862. }
  863. }
  864. if (newTheme != null) {
  865. loadTheme(newTheme, newThemeUrl, tagToReplace);
  866. } else {
  867. if (tagToReplace != null) {
  868. tagToReplace.getParentElement().removeChild(tagToReplace);
  869. }
  870. activateTheme(null);
  871. }
  872. }
  873. private void updateVaadinFavicon(String newTheme) {
  874. NodeList<Element> iconElements = querySelectorAll(
  875. "link[rel~=\"icon\"]");
  876. for (int i = 0; i < iconElements.getLength(); i++) {
  877. Element iconElement = iconElements.getItem(i);
  878. String href = iconElement.getAttribute("href");
  879. if (href != null && href.contains("VAADIN/themes")
  880. && href.endsWith("/favicon.ico")) {
  881. href = href.replaceFirst("VAADIN/themes/.+?/favicon.ico",
  882. "VAADIN/themes/" + newTheme + "/favicon.ico");
  883. iconElement.setAttribute("href", href);
  884. }
  885. }
  886. }
  887. private static native NodeList<Element> querySelectorAll(String selector)
  888. /*-{
  889. return $doc.querySelectorAll(selector);
  890. }-*/;
  891. /**
  892. * Finds a link tag for a style sheet with the given URL
  893. *
  894. * @since 7.3
  895. * @param url
  896. * the URL of the style sheet
  897. * @return the link tag or null if no matching link tag was found
  898. */
  899. private LinkElement findStylesheetTag(String url) {
  900. NodeList<Element> linkTags = getHead()
  901. .getElementsByTagName(LinkElement.TAG);
  902. for (int i = 0; i < linkTags.getLength(); i++) {
  903. final LinkElement link = LinkElement.as(linkTags.getItem(i));
  904. if ("stylesheet".equals(link.getRel())
  905. && "text/css".equals(link.getType())
  906. && url.equals(link.getHref())) {
  907. return link;
  908. }
  909. }
  910. return null;
  911. }
  912. /**
  913. * Loads the given theme and replaces the given link element with the new
  914. * theme link element.
  915. *
  916. * @param newTheme
  917. * The name of the new theme
  918. * @param newThemeUrl
  919. * The url of the new theme
  920. * @param tagToReplace
  921. * The link element to replace. If null, then the new link
  922. * element is added at the end.
  923. */
  924. private void loadTheme(final String newTheme, final String newThemeUrl,
  925. final LinkElement tagToReplace) {
  926. LinkElement newThemeLinkElement = Document.get().createLinkElement();
  927. newThemeLinkElement.setRel("stylesheet");
  928. newThemeLinkElement.setType("text/css");
  929. newThemeLinkElement.setHref(newThemeUrl);
  930. ResourceLoader.addOnloadHandler(newThemeLinkElement,
  931. new ResourceLoadListener() {
  932. @Override
  933. public void onLoad(ResourceLoadEvent event) {
  934. getLogger().info("Loading of " + newTheme + " from "
  935. + newThemeUrl + " completed");
  936. if (tagToReplace != null) {
  937. tagToReplace.getParentElement()
  938. .removeChild(tagToReplace);
  939. }
  940. activateTheme(newTheme);
  941. }
  942. @Override
  943. public void onError(ResourceLoadEvent event) {
  944. getLogger().warning("Could not load theme from "
  945. + getThemeUrl(newTheme));
  946. }
  947. }, null);
  948. if (tagToReplace != null) {
  949. getHead().insertBefore(newThemeLinkElement, tagToReplace);
  950. } else {
  951. getHead().appendChild(newThemeLinkElement);
  952. }
  953. }
  954. /**
  955. * Activates the new theme. Assumes the theme has been loaded and taken into
  956. * use in the browser.
  957. *
  958. * @since 7.4.3
  959. * @param newTheme
  960. * The name of the new theme
  961. */
  962. protected void activateTheme(String newTheme) {
  963. if (activeTheme != null) {
  964. getWidget().getParent().removeStyleName(activeTheme);
  965. VOverlay.getOverlayContainer(getConnection())
  966. .removeClassName(activeTheme);
  967. }
  968. String oldThemeBase = getConnection().translateVaadinUri("theme://");
  969. activeTheme = newTheme;
  970. if (newTheme != null) {
  971. getWidget().getParent().addStyleName(newTheme);
  972. VOverlay.getOverlayContainer(getConnection())
  973. .addClassName(activeTheme);
  974. updateVaadinFavicon(newTheme);
  975. }
  976. // Request a full resynchronization from the server to deal with legacy
  977. // components
  978. getConnection().getMessageSender().resynchronize();
  979. // Immediately update state and do layout while waiting for the resync
  980. forceStateChangeRecursively(UIConnector.this);
  981. getLayoutManager().forceLayout();
  982. }
  983. /**
  984. * Force a full recursive recheck of every connector's state variables.
  985. *
  986. * @see #forceStateChange()
  987. *
  988. * @since 7.3
  989. */
  990. protected static void forceStateChangeRecursively(
  991. AbstractConnector connector) {
  992. connector.forceStateChange();
  993. for (ServerConnector child : connector.getChildren()) {
  994. if (child instanceof AbstractConnector) {
  995. forceStateChangeRecursively((AbstractConnector) child);
  996. } else {
  997. getLogger().warning(
  998. "Could not force state change for unknown connector type: "
  999. + child.getClass().getName());
  1000. }
  1001. }
  1002. }
  1003. /**
  1004. * Internal helper to get the theme URL for a given theme
  1005. *
  1006. * @since 7.3
  1007. * @param theme
  1008. * the name of the theme
  1009. * @return The URL the theme can be loaded from
  1010. */
  1011. private String getThemeUrl(String theme) {
  1012. String themeUrl = getConnection()
  1013. .translateVaadinUri(ApplicationConstants.VAADIN_PROTOCOL_PREFIX
  1014. + "themes/" + theme + "/styles" + ".css");
  1015. // Parameter appended to bypass caches after version upgrade.
  1016. themeUrl += "?v=" + Version.getFullVersion();
  1017. return themeUrl;
  1018. }
  1019. /**
  1020. * Returns the name of the theme currently in used by the UI.
  1021. *
  1022. * @since 7.3
  1023. * @return the theme name used by this UI
  1024. */
  1025. public String getActiveTheme() {
  1026. return activeTheme;
  1027. }
  1028. /**
  1029. * Returns whether HTML5 DnD extensions {@link DragSourceExtensionConnector}
  1030. * and {@link DropTargetExtensionConnector} and alike should be enabled for
  1031. * mobile devices.
  1032. * <p>
  1033. * By default, it is disabled.
  1034. *
  1035. * @return {@code true} if enabled, {@code false} if not
  1036. * @since 8.1
  1037. */
  1038. public boolean isMobileHTML5DndEnabled() {
  1039. return getState().enableMobileHTML5DnD;
  1040. }
  1041. private static Logger getLogger() {
  1042. return Logger.getLogger(UIConnector.class.getName());
  1043. }
  1044. private void setWindowOrderAndPosition() {
  1045. if (windowOrderRegistration != null) {
  1046. windowOrderRegistration.removeHandler();
  1047. }
  1048. WindowOrderCollector collector = new WindowOrderCollector();
  1049. HandlerRegistration registration = VWindow
  1050. .addWindowOrderHandler(collector);
  1051. for (ComponentConnector c : getChildComponents()) {
  1052. if (c instanceof WindowConnector) {
  1053. WindowConnector wc = (WindowConnector) c;
  1054. wc.setWindowOrderAndPosition();
  1055. }
  1056. }
  1057. windowOrderHandler.onWindowOrderChange(
  1058. new WindowOrderEvent(collector.getWindows()));
  1059. registration.removeHandler();
  1060. windowOrderRegistration = VWindow
  1061. .addWindowOrderHandler(windowOrderHandler);
  1062. }
  1063. private static class WindowOrderCollector
  1064. implements WindowOrderHandler, Comparator<VWindow> {
  1065. private HashSet<VWindow> windows = new HashSet<>();
  1066. @Override
  1067. public void onWindowOrderChange(WindowOrderEvent event) {
  1068. windows.addAll(Arrays.asList(event.getWindows()));
  1069. }
  1070. @Override
  1071. public int compare(VWindow window1, VWindow window2) {
  1072. if (window1.getWindowOrder() == window2.getWindowOrder()) {
  1073. return 0;
  1074. }
  1075. return window1.getWindowOrder() > window2.getWindowOrder() ? 1 : -1;
  1076. }
  1077. List<VWindow> getWindows() {
  1078. List<VWindow> result = new ArrayList<>();
  1079. result.addAll(windows);
  1080. Collections.sort(result, this);
  1081. return result;
  1082. }
  1083. }
  1084. private static native void initializeMobileDndPolyfill()
  1085. /*-{
  1086. var conf = new Object();
  1087. // this is needed or the drag image will offset to weird place in for grid rows
  1088. conf['dragImageCenterOnTouch'] = true;
  1089. $wnd.DragDropPolyfill.Initialize(conf);
  1090. }-*/;
  1091. }