Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ApplicationConnection.java 56KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. * Copyright 2000-2014 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;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.logging.Logger;
  20. import com.google.gwt.aria.client.LiveValue;
  21. import com.google.gwt.aria.client.RelevantValue;
  22. import com.google.gwt.aria.client.Roles;
  23. import com.google.gwt.core.client.Duration;
  24. import com.google.gwt.core.client.GWT;
  25. import com.google.gwt.core.client.JavaScriptObject;
  26. import com.google.gwt.core.client.JsArrayString;
  27. import com.google.gwt.core.client.Scheduler;
  28. import com.google.gwt.dom.client.Element;
  29. import com.google.gwt.event.shared.EventBus;
  30. import com.google.gwt.event.shared.EventHandler;
  31. import com.google.gwt.event.shared.GwtEvent;
  32. import com.google.gwt.event.shared.HandlerRegistration;
  33. import com.google.gwt.event.shared.HasHandlers;
  34. import com.google.gwt.event.shared.SimpleEventBus;
  35. import com.google.gwt.http.client.URL;
  36. import com.google.gwt.user.client.Command;
  37. import com.google.gwt.user.client.DOM;
  38. import com.google.gwt.user.client.Timer;
  39. import com.google.gwt.user.client.ui.HasWidgets;
  40. import com.google.gwt.user.client.ui.Widget;
  41. import com.vaadin.client.ApplicationConfiguration.ErrorMessage;
  42. import com.vaadin.client.ApplicationConnection.ApplicationStoppedEvent;
  43. import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
  44. import com.vaadin.client.ResourceLoader.ResourceLoadListener;
  45. import com.vaadin.client.communication.ConnectionStateHandler;
  46. import com.vaadin.client.communication.Heartbeat;
  47. import com.vaadin.client.communication.MessageHandler;
  48. import com.vaadin.client.communication.MessageSender;
  49. import com.vaadin.client.communication.RpcManager;
  50. import com.vaadin.client.communication.ServerRpcQueue;
  51. import com.vaadin.client.componentlocator.ComponentLocator;
  52. import com.vaadin.client.metadata.ConnectorBundleLoader;
  53. import com.vaadin.client.ui.AbstractComponentConnector;
  54. import com.vaadin.client.ui.FontIcon;
  55. import com.vaadin.client.ui.Icon;
  56. import com.vaadin.client.ui.ImageIcon;
  57. import com.vaadin.client.ui.VContextMenu;
  58. import com.vaadin.client.ui.VNotification;
  59. import com.vaadin.client.ui.VOverlay;
  60. import com.vaadin.client.ui.ui.UIConnector;
  61. import com.vaadin.shared.VaadinUriResolver;
  62. import com.vaadin.shared.Version;
  63. import com.vaadin.shared.communication.LegacyChangeVariablesInvocation;
  64. import com.vaadin.shared.util.SharedUtil;
  65. /**
  66. * This is the client side communication "engine", managing client-server
  67. * communication with its server side counterpart
  68. * com.vaadin.server.VaadinService.
  69. *
  70. * Client-side connectors receive updates from the corresponding server-side
  71. * connector (typically component) as state updates or RPC calls. The connector
  72. * has the possibility to communicate back with its server side counter part
  73. * through RPC calls.
  74. *
  75. * TODO document better
  76. *
  77. * Entry point classes (widgetsets) define <code>onModuleLoad()</code>.
  78. */
  79. public class ApplicationConnection implements HasHandlers {
  80. @Deprecated
  81. public static final String MODIFIED_CLASSNAME = StyleConstants.MODIFIED;
  82. @Deprecated
  83. public static final String DISABLED_CLASSNAME = StyleConstants.DISABLED;
  84. @Deprecated
  85. public static final String REQUIRED_CLASSNAME = StyleConstants.REQUIRED;
  86. @Deprecated
  87. public static final String REQUIRED_CLASSNAME_EXT = StyleConstants.REQUIRED_EXT;
  88. @Deprecated
  89. public static final String ERROR_CLASSNAME_EXT = StyleConstants.ERROR_EXT;
  90. /**
  91. * A string that, if found in a non-JSON response to a UIDL request, will
  92. * cause the browser to refresh the page. If followed by a colon, optional
  93. * whitespace, and a URI, causes the browser to synchronously load the URI.
  94. *
  95. * <p>
  96. * This allows, for instance, a servlet filter to redirect the application
  97. * to a custom login page when the session expires. For example:
  98. * </p>
  99. *
  100. * <pre>
  101. * if (sessionExpired) {
  102. * response.setHeader(&quot;Content-Type&quot;, &quot;text/html&quot;);
  103. * response.getWriter().write(
  104. * myLoginPageHtml + &quot;&lt;!-- Vaadin-Refresh: &quot;
  105. * + request.getContextPath() + &quot; --&gt;&quot;);
  106. * }
  107. * </pre>
  108. */
  109. public static final String UIDL_REFRESH_TOKEN = "Vaadin-Refresh";
  110. private final HashMap<String, String> resourcesMap = new HashMap<String, String>();
  111. private WidgetSet widgetSet;
  112. private VContextMenu contextMenu = null;
  113. private final UIConnector uIConnector;
  114. protected boolean cssLoaded = false;
  115. /** Parameters for this application connection loaded from the web-page */
  116. private ApplicationConfiguration configuration;
  117. private final LayoutManager layoutManager;
  118. private final RpcManager rpcManager;
  119. /** Event bus for communication events */
  120. private EventBus eventBus = GWT.create(SimpleEventBus.class);
  121. public enum ApplicationState {
  122. INITIALIZING, RUNNING, TERMINATED;
  123. }
  124. private ApplicationState applicationState = ApplicationState.INITIALIZING;
  125. /**
  126. * The communication handler methods are called at certain points during
  127. * communication with the server. This allows for making add-ons that keep
  128. * track of different aspects of the communication.
  129. */
  130. public interface CommunicationHandler extends EventHandler {
  131. void onRequestStarting(RequestStartingEvent e);
  132. void onResponseHandlingStarted(ResponseHandlingStartedEvent e);
  133. void onResponseHandlingEnded(ResponseHandlingEndedEvent e);
  134. }
  135. public static class RequestStartingEvent extends ApplicationConnectionEvent {
  136. public static Type<CommunicationHandler> TYPE = new Type<CommunicationHandler>();
  137. public RequestStartingEvent(ApplicationConnection connection) {
  138. super(connection);
  139. }
  140. @Override
  141. public Type<CommunicationHandler> getAssociatedType() {
  142. return TYPE;
  143. }
  144. @Override
  145. protected void dispatch(CommunicationHandler handler) {
  146. handler.onRequestStarting(this);
  147. }
  148. }
  149. public static class ResponseHandlingEndedEvent extends
  150. ApplicationConnectionEvent {
  151. public static Type<CommunicationHandler> TYPE = new Type<CommunicationHandler>();
  152. public ResponseHandlingEndedEvent(ApplicationConnection connection) {
  153. super(connection);
  154. }
  155. @Override
  156. public Type<CommunicationHandler> getAssociatedType() {
  157. return TYPE;
  158. }
  159. @Override
  160. protected void dispatch(CommunicationHandler handler) {
  161. handler.onResponseHandlingEnded(this);
  162. }
  163. }
  164. public static abstract class ApplicationConnectionEvent extends
  165. GwtEvent<CommunicationHandler> {
  166. private ApplicationConnection connection;
  167. protected ApplicationConnectionEvent(ApplicationConnection connection) {
  168. this.connection = connection;
  169. }
  170. public ApplicationConnection getConnection() {
  171. return connection;
  172. }
  173. }
  174. public static class ResponseHandlingStartedEvent extends
  175. ApplicationConnectionEvent {
  176. public ResponseHandlingStartedEvent(ApplicationConnection connection) {
  177. super(connection);
  178. }
  179. public static Type<CommunicationHandler> TYPE = new Type<CommunicationHandler>();
  180. @Override
  181. public Type<CommunicationHandler> getAssociatedType() {
  182. return TYPE;
  183. }
  184. @Override
  185. protected void dispatch(CommunicationHandler handler) {
  186. handler.onResponseHandlingStarted(this);
  187. }
  188. }
  189. /**
  190. * Event triggered when a application is stopped by calling
  191. * {@link ApplicationConnection#setApplicationRunning(false)}.
  192. *
  193. * To listen for the event add a {@link ApplicationStoppedHandler} by
  194. * invoking
  195. * {@link ApplicationConnection#addHandler(ApplicationConnection.ApplicationStoppedEvent.Type, ApplicationStoppedHandler)}
  196. * to the {@link ApplicationConnection}
  197. *
  198. * @since 7.1.8
  199. * @author Vaadin Ltd
  200. */
  201. public static class ApplicationStoppedEvent extends
  202. GwtEvent<ApplicationStoppedHandler> {
  203. public static Type<ApplicationStoppedHandler> TYPE = new Type<ApplicationStoppedHandler>();
  204. @Override
  205. public Type<ApplicationStoppedHandler> getAssociatedType() {
  206. return TYPE;
  207. }
  208. @Override
  209. protected void dispatch(ApplicationStoppedHandler listener) {
  210. listener.onApplicationStopped(this);
  211. }
  212. }
  213. /**
  214. * Allows custom handling of communication errors.
  215. */
  216. public interface CommunicationErrorHandler {
  217. /**
  218. * Called when a communication error has occurred. Returning
  219. * <code>true</code> from this method suppresses error handling.
  220. *
  221. * @param details
  222. * A string describing the error.
  223. * @param statusCode
  224. * The HTTP status code (e.g. 404, etc).
  225. * @return true if the error reporting should be suppressed, false to
  226. * perform normal error reporting.
  227. */
  228. public boolean onError(String details, int statusCode);
  229. }
  230. /**
  231. * A listener for listening to application stopped events. The listener can
  232. * be added to a {@link ApplicationConnection} by invoking
  233. * {@link ApplicationConnection#addHandler(ApplicationStoppedEvent.Type, ApplicationStoppedHandler)}
  234. *
  235. * @since 7.1.8
  236. * @author Vaadin Ltd
  237. */
  238. public interface ApplicationStoppedHandler extends EventHandler {
  239. /**
  240. * Triggered when the {@link ApplicationConnection} marks a previously
  241. * running application as stopped by invoking
  242. * {@link ApplicationConnection#setApplicationRunning(false)}
  243. *
  244. * @param event
  245. * the event triggered by the {@link ApplicationConnection}
  246. */
  247. void onApplicationStopped(ApplicationStoppedEvent event);
  248. }
  249. private CommunicationErrorHandler communicationErrorDelegate = null;
  250. private VLoadingIndicator loadingIndicator;
  251. private Heartbeat heartbeat = GWT.create(Heartbeat.class);
  252. private boolean tooltipInitialized = false;
  253. private final VaadinUriResolver uriResolver = new VaadinUriResolver() {
  254. @Override
  255. protected String getVaadinDirUrl() {
  256. return getConfiguration().getVaadinDirUrl();
  257. }
  258. @Override
  259. protected String getServiceUrlParameterName() {
  260. return getConfiguration().getServiceUrlParameterName();
  261. }
  262. @Override
  263. protected String getServiceUrl() {
  264. return getConfiguration().getServiceUrl();
  265. }
  266. @Override
  267. protected String getThemeUri() {
  268. return ApplicationConnection.this.getThemeUri();
  269. }
  270. @Override
  271. protected String encodeQueryStringParameterValue(String queryString) {
  272. return URL.encodeQueryString(queryString);
  273. }
  274. };
  275. public static class MultiStepDuration extends Duration {
  276. private int previousStep = elapsedMillis();
  277. public void logDuration(String message) {
  278. logDuration(message, 0);
  279. }
  280. public void logDuration(String message, int minDuration) {
  281. int currentTime = elapsedMillis();
  282. int stepDuration = currentTime - previousStep;
  283. if (stepDuration >= minDuration) {
  284. getLogger().info(message + ": " + stepDuration + " ms");
  285. }
  286. previousStep = currentTime;
  287. }
  288. }
  289. public ApplicationConnection() {
  290. // Assuming UI data is eagerly loaded
  291. ConnectorBundleLoader.get().loadBundle(
  292. ConnectorBundleLoader.EAGER_BUNDLE_NAME, null);
  293. uIConnector = GWT.create(UIConnector.class);
  294. rpcManager = GWT.create(RpcManager.class);
  295. layoutManager = GWT.create(LayoutManager.class);
  296. tooltip = GWT.create(VTooltip.class);
  297. loadingIndicator = GWT.create(VLoadingIndicator.class);
  298. serverRpcQueue = GWT.create(ServerRpcQueue.class);
  299. connectionStateHandler = GWT.create(ConnectionStateHandler.class);
  300. messageHandler = GWT.create(MessageHandler.class);
  301. messageSender = GWT.create(MessageSender.class);
  302. }
  303. public void init(WidgetSet widgetSet, ApplicationConfiguration cnf) {
  304. getLogger().info("Starting application " + cnf.getRootPanelId());
  305. getLogger().info("Using theme: " + cnf.getThemeName());
  306. getLogger().info(
  307. "Vaadin application servlet version: "
  308. + cnf.getServletVersion());
  309. if (!cnf.getServletVersion().equals(Version.getFullVersion())) {
  310. getLogger()
  311. .severe("Warning: your widget set seems to be built with a different "
  312. + "version than the one used on server. Unexpected "
  313. + "behavior may occur.");
  314. }
  315. this.widgetSet = widgetSet;
  316. configuration = cnf;
  317. layoutManager.setConnection(this);
  318. loadingIndicator.setConnection(this);
  319. serverRpcQueue.setConnection(this);
  320. messageHandler.setConnection(this);
  321. messageSender.setConnection(this);
  322. ComponentLocator componentLocator = new ComponentLocator(this);
  323. String appRootPanelName = cnf.getRootPanelId();
  324. // remove the end (window name) of autogenerated rootpanel id
  325. appRootPanelName = appRootPanelName.replaceFirst("-\\d+$", "");
  326. initializeTestbenchHooks(componentLocator, appRootPanelName);
  327. initializeClientHooks();
  328. uIConnector.init(cnf.getRootPanelId(), this);
  329. // Connection state handler preloads the reconnect dialog, which uses
  330. // overlay container. This in turn depends on VUI being attached
  331. // (done in uiConnector.init)
  332. connectionStateHandler.setConnection(this);
  333. tooltip.setOwner(uIConnector.getWidget());
  334. getLoadingIndicator().show();
  335. heartbeat.init(this);
  336. // Ensure the overlay container is added to the dom and set as a live
  337. // area for assistive devices
  338. Element overlayContainer = VOverlay.getOverlayContainer(this);
  339. Roles.getAlertRole().setAriaLiveProperty(overlayContainer,
  340. LiveValue.ASSERTIVE);
  341. VOverlay.setOverlayContainerLabel(this,
  342. getUIConnector().getState().overlayContainerLabel);
  343. Roles.getAlertRole().setAriaRelevantProperty(overlayContainer,
  344. RelevantValue.ADDITIONS);
  345. }
  346. /**
  347. * Starts this application. Don't call this method directly - it's called by
  348. * {@link ApplicationConfiguration#startNextApplication()}, which should be
  349. * called once this application has started (first response received) or
  350. * failed to start. This ensures that the applications are started in order,
  351. * to avoid session-id problems.
  352. *
  353. */
  354. public void start() {
  355. String jsonText = configuration.getUIDL();
  356. if (jsonText == null) {
  357. // initial UIDL not in DOM, request from server
  358. getMessageSender().resynchronize();
  359. } else {
  360. // initial UIDL provided in DOM, continue as if returned by request
  361. // Hack to avoid logging an error in endRequest()
  362. getMessageSender().startRequest();
  363. getMessageHandler().handleMessage(
  364. MessageHandler.parseJson(jsonText));
  365. }
  366. // Tooltip can't be created earlier because the
  367. // necessary fields are not setup to add it in the
  368. // correct place in the DOM
  369. if (!tooltipInitialized) {
  370. tooltipInitialized = true;
  371. ApplicationConfiguration.runWhenDependenciesLoaded(new Command() {
  372. @Override
  373. public void execute() {
  374. getVTooltip().initializeAssistiveTooltips();
  375. }
  376. });
  377. }
  378. }
  379. /**
  380. * Checks if there is some work to be done on the client side
  381. *
  382. * @return true if the client has some work to be done, false otherwise
  383. */
  384. private boolean isActive() {
  385. return !getMessageHandler().isInitialUidlHandled() || isWorkPending()
  386. || getMessageSender().hasActiveRequest()
  387. || isExecutingDeferredCommands();
  388. }
  389. private native void initializeTestbenchHooks(
  390. ComponentLocator componentLocator, String TTAppId)
  391. /*-{
  392. var ap = this;
  393. var client = {};
  394. client.isActive = $entry(function() {
  395. return ap.@com.vaadin.client.ApplicationConnection::isActive()();
  396. });
  397. var vi = ap.@com.vaadin.client.ApplicationConnection::getVersionInfo()();
  398. if (vi) {
  399. client.getVersionInfo = function() {
  400. return vi;
  401. }
  402. }
  403. client.getProfilingData = $entry(function() {
  404. var smh = ap.@com.vaadin.client.ApplicationConnection::getMessageHandler();
  405. var pd = [
  406. smh.@com.vaadin.client.communication.MessageHandler::lastProcessingTime,
  407. smh.@com.vaadin.client.communication.MessageHandler::totalProcessingTime
  408. ];
  409. pd = pd.concat(smh.@com.vaadin.client.communication.MessageHandler::serverTimingInfo);
  410. pd[pd.length] = smh.@com.vaadin.client.communication.MessageHandler::bootstrapTime;
  411. return pd;
  412. });
  413. client.getElementByPath = $entry(function(id) {
  414. return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getElementByPath(Ljava/lang/String;)(id);
  415. });
  416. client.getElementByPathStartingAt = $entry(function(id, element) {
  417. return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getElementByPathStartingAt(Ljava/lang/String;Lcom/google/gwt/dom/client/Element;)(id, element);
  418. });
  419. client.getElementsByPath = $entry(function(id) {
  420. return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getElementsByPath(Ljava/lang/String;)(id);
  421. });
  422. client.getElementsByPathStartingAt = $entry(function(id, element) {
  423. return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getElementsByPathStartingAt(Ljava/lang/String;Lcom/google/gwt/dom/client/Element;)(id, element);
  424. });
  425. client.getPathForElement = $entry(function(element) {
  426. return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getPathForElement(Lcom/google/gwt/dom/client/Element;)(element);
  427. });
  428. client.initializing = false;
  429. $wnd.vaadin.clients[TTAppId] = client;
  430. }-*/;
  431. /**
  432. * Helper for tt initialization
  433. */
  434. private JavaScriptObject getVersionInfo() {
  435. return configuration.getVersionInfoJSObject();
  436. }
  437. /**
  438. * Publishes a JavaScript API for mash-up applications.
  439. * <ul>
  440. * <li><code>vaadin.forceSync()</code> sends pending variable changes, in
  441. * effect synchronizing the server and client state. This is done for all
  442. * applications on host page.</li>
  443. * <li><code>vaadin.postRequestHooks</code> is a map of functions which gets
  444. * called after each XHR made by vaadin application. Note, that it is
  445. * attaching js functions responsibility to create the variable like this:
  446. *
  447. * <code><pre>
  448. * if(!vaadin.postRequestHooks) {vaadin.postRequestHooks = new Object();}
  449. * postRequestHooks.myHook = function(appId) {
  450. * if(appId == "MyAppOfInterest") {
  451. * // do the staff you need on xhr activity
  452. * }
  453. * }
  454. * </pre></code> First parameter passed to these functions is the identifier
  455. * of Vaadin application that made the request.
  456. * </ul>
  457. *
  458. * TODO make this multi-app aware
  459. */
  460. private native void initializeClientHooks()
  461. /*-{
  462. var app = this;
  463. var oldSync;
  464. if ($wnd.vaadin.forceSync) {
  465. oldSync = $wnd.vaadin.forceSync;
  466. }
  467. $wnd.vaadin.forceSync = $entry(function() {
  468. if (oldSync) {
  469. oldSync();
  470. }
  471. app.@com.vaadin.client.ApplicationConnection::sendPendingVariableChanges()();
  472. });
  473. var oldForceLayout;
  474. if ($wnd.vaadin.forceLayout) {
  475. oldForceLayout = $wnd.vaadin.forceLayout;
  476. }
  477. $wnd.vaadin.forceLayout = $entry(function() {
  478. if (oldForceLayout) {
  479. oldForceLayout();
  480. }
  481. app.@com.vaadin.client.ApplicationConnection::forceLayout()();
  482. });
  483. }-*/;
  484. /**
  485. * Requests an analyze of layouts, to find inconsistencies. Exclusively used
  486. * for debugging during development.
  487. *
  488. * @deprecated as of 7.1. Replaced by {@link UIConnector#analyzeLayouts()}
  489. */
  490. @Deprecated
  491. public void analyzeLayouts() {
  492. getUIConnector().analyzeLayouts();
  493. }
  494. /**
  495. * Sends a request to the server to print details to console that will help
  496. * the developer to locate the corresponding server-side connector in the
  497. * source code.
  498. *
  499. * @param serverConnector
  500. * @deprecated as of 7.1. Replaced by
  501. * {@link UIConnector#showServerDebugInfo(ServerConnector)}
  502. */
  503. @Deprecated
  504. void highlightConnector(ServerConnector serverConnector) {
  505. getUIConnector().showServerDebugInfo(serverConnector);
  506. }
  507. int cssWaits = 0;
  508. protected ServerRpcQueue serverRpcQueue;
  509. protected ConnectionStateHandler connectionStateHandler;
  510. protected MessageHandler messageHandler;
  511. protected MessageSender messageSender;
  512. static final int MAX_CSS_WAITS = 100;
  513. public void executeWhenCSSLoaded(final Command c) {
  514. if (!isCSSLoaded() && cssWaits < MAX_CSS_WAITS) {
  515. (new Timer() {
  516. @Override
  517. public void run() {
  518. executeWhenCSSLoaded(c);
  519. }
  520. }).schedule(50);
  521. // Show this message just once
  522. if (cssWaits++ == 0) {
  523. getLogger().warning(
  524. "Assuming CSS loading is not complete, "
  525. + "postponing render phase. "
  526. + "(.v-loading-indicator height == 0)");
  527. }
  528. } else {
  529. cssLoaded = true;
  530. if (cssWaits >= MAX_CSS_WAITS) {
  531. getLogger().severe("CSS files may have not loaded properly.");
  532. }
  533. c.execute();
  534. }
  535. }
  536. /**
  537. * Checks whether or not the CSS is loaded. By default checks the size of
  538. * the loading indicator element.
  539. *
  540. * @return
  541. */
  542. protected boolean isCSSLoaded() {
  543. return cssLoaded
  544. || getLoadingIndicator().getElement().getOffsetHeight() != 0;
  545. }
  546. /**
  547. * Shows the communication error notification.
  548. *
  549. * @param details
  550. * Optional details.
  551. * @param statusCode
  552. * The status code returned for the request
  553. *
  554. */
  555. public void showCommunicationError(String details, int statusCode) {
  556. getLogger().severe("Communication error: " + details);
  557. showError(details, configuration.getCommunicationError());
  558. }
  559. /**
  560. * Shows the authentication error notification.
  561. *
  562. * @param details
  563. * Optional details.
  564. */
  565. public void showAuthenticationError(String details) {
  566. getLogger().severe("Authentication error: " + details);
  567. showError(details, configuration.getAuthorizationError());
  568. }
  569. /**
  570. * Shows the session expiration notification.
  571. *
  572. * @param details
  573. * Optional details.
  574. */
  575. public void showSessionExpiredError(String details) {
  576. getLogger().severe("Session expired: " + details);
  577. showError(details, configuration.getSessionExpiredError());
  578. }
  579. /**
  580. * Shows an error notification.
  581. *
  582. * @param details
  583. * Optional details.
  584. * @param message
  585. * An ErrorMessage describing the error.
  586. */
  587. protected void showError(String details, ErrorMessage message) {
  588. VNotification.showError(this, message.getCaption(),
  589. message.getMessage(), details, message.getUrl());
  590. }
  591. /**
  592. * Checks if the client has running or scheduled commands
  593. */
  594. private boolean isWorkPending() {
  595. ConnectorMap connectorMap = getConnectorMap();
  596. JsArrayObject<ServerConnector> connectors = connectorMap
  597. .getConnectorsAsJsArray();
  598. int size = connectors.size();
  599. for (int i = 0; i < size; i++) {
  600. ServerConnector conn = connectors.get(i);
  601. if (isWorkPending(conn)) {
  602. return true;
  603. }
  604. if (conn instanceof ComponentConnector) {
  605. ComponentConnector compConn = (ComponentConnector) conn;
  606. if (isWorkPending(compConn.getWidget())) {
  607. return true;
  608. }
  609. }
  610. }
  611. return false;
  612. }
  613. private static boolean isWorkPending(Object object) {
  614. return object instanceof DeferredWorker
  615. && ((DeferredWorker) object).isWorkPending();
  616. }
  617. /**
  618. * Checks if deferred commands are (potentially) still being executed as a
  619. * result of an update from the server. Returns true if a deferred command
  620. * might still be executing, false otherwise. This will not work correctly
  621. * if a deferred command is added in another deferred command.
  622. * <p>
  623. * Used by the native "client.isActive" function.
  624. * </p>
  625. *
  626. * @return true if deferred commands are (potentially) being executed, false
  627. * otherwise
  628. */
  629. private boolean isExecutingDeferredCommands() {
  630. Scheduler s = Scheduler.get();
  631. if (s instanceof VSchedulerImpl) {
  632. return ((VSchedulerImpl) s).hasWorkQueued();
  633. } else {
  634. return false;
  635. }
  636. }
  637. /**
  638. * Returns the loading indicator used by this ApplicationConnection
  639. *
  640. * @return The loading indicator for this ApplicationConnection
  641. */
  642. public VLoadingIndicator getLoadingIndicator() {
  643. return loadingIndicator;
  644. }
  645. /**
  646. * Determines whether or not the loading indicator is showing.
  647. *
  648. * @return true if the loading indicator is visible
  649. * @deprecated As of 7.1. Use {@link #getLoadingIndicator()} and
  650. * {@link VLoadingIndicator#isVisible()}.isVisible() instead.
  651. */
  652. @Deprecated
  653. public boolean isLoadingIndicatorVisible() {
  654. return getLoadingIndicator().isVisible();
  655. }
  656. public void loadStyleDependencies(JsArrayString dependencies) {
  657. // Assuming no reason to interpret in a defined order
  658. ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
  659. @Override
  660. public void onLoad(ResourceLoadEvent event) {
  661. ApplicationConfiguration.endDependencyLoading();
  662. }
  663. @Override
  664. public void onError(ResourceLoadEvent event) {
  665. getLogger()
  666. .severe(event.getResourceUrl()
  667. + " could not be loaded, or the load detection failed because the stylesheet is empty.");
  668. // The show must go on
  669. onLoad(event);
  670. }
  671. };
  672. ResourceLoader loader = ResourceLoader.get();
  673. for (int i = 0; i < dependencies.length(); i++) {
  674. String url = translateVaadinUri(dependencies.get(i));
  675. ApplicationConfiguration.startDependencyLoading();
  676. loader.loadStylesheet(url, resourceLoadListener);
  677. }
  678. }
  679. public void loadScriptDependencies(final JsArrayString dependencies) {
  680. if (dependencies.length() == 0) {
  681. return;
  682. }
  683. // Listener that loads the next when one is completed
  684. ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
  685. @Override
  686. public void onLoad(ResourceLoadEvent event) {
  687. if (dependencies.length() != 0) {
  688. String url = translateVaadinUri(dependencies.shift());
  689. ApplicationConfiguration.startDependencyLoading();
  690. // Load next in chain (hopefully already preloaded)
  691. event.getResourceLoader().loadScript(url, this);
  692. }
  693. // Call start for next before calling end for current
  694. ApplicationConfiguration.endDependencyLoading();
  695. }
  696. @Override
  697. public void onError(ResourceLoadEvent event) {
  698. getLogger().severe(
  699. event.getResourceUrl() + " could not be loaded.");
  700. // The show must go on
  701. onLoad(event);
  702. }
  703. };
  704. ResourceLoader loader = ResourceLoader.get();
  705. // Start chain by loading first
  706. String url = translateVaadinUri(dependencies.shift());
  707. ApplicationConfiguration.startDependencyLoading();
  708. loader.loadScript(url, resourceLoadListener);
  709. if (ResourceLoader.supportsInOrderScriptExecution()) {
  710. for (int i = 0; i < dependencies.length(); i++) {
  711. String preloadUrl = translateVaadinUri(dependencies.get(i));
  712. loader.loadScript(preloadUrl, null);
  713. }
  714. } else {
  715. // Preload all remaining
  716. for (int i = 0; i < dependencies.length(); i++) {
  717. String preloadUrl = translateVaadinUri(dependencies.get(i));
  718. loader.preloadResource(preloadUrl, null);
  719. }
  720. }
  721. }
  722. private void addVariableToQueue(String connectorId, String variableName,
  723. Object value, boolean immediate) {
  724. boolean lastOnly = !immediate;
  725. // note that type is now deduced from value
  726. serverRpcQueue.add(new LegacyChangeVariablesInvocation(connectorId,
  727. variableName, value), lastOnly);
  728. if (immediate) {
  729. serverRpcQueue.flush();
  730. }
  731. }
  732. /**
  733. * @deprecated as of 7.6, use {@link ServerRpcQueue#flush()}
  734. */
  735. @Deprecated
  736. public void sendPendingVariableChanges() {
  737. serverRpcQueue.flush();
  738. }
  739. /**
  740. * Sends a new value for the given paintables given variable to the server.
  741. * <p>
  742. * The update is actually queued to be sent at a suitable time. If immediate
  743. * is true, the update is sent as soon as possible. If immediate is false,
  744. * the update will be sent along with the next immediate update.
  745. * </p>
  746. *
  747. * @param paintableId
  748. * the id of the paintable that owns the variable
  749. * @param variableName
  750. * the name of the variable
  751. * @param newValue
  752. * the new value to be sent
  753. * @param immediate
  754. * true if the update is to be sent as soon as possible
  755. */
  756. public void updateVariable(String paintableId, String variableName,
  757. ServerConnector newValue, boolean immediate) {
  758. addVariableToQueue(paintableId, variableName, newValue, immediate);
  759. }
  760. /**
  761. * Sends a new value for the given paintables given variable to the server.
  762. * <p>
  763. * The update is actually queued to be sent at a suitable time. If immediate
  764. * is true, the update is sent as soon as possible. If immediate is false,
  765. * the update will be sent along with the next immediate update.
  766. * </p>
  767. *
  768. * @param paintableId
  769. * the id of the paintable that owns the variable
  770. * @param variableName
  771. * the name of the variable
  772. * @param newValue
  773. * the new value to be sent
  774. * @param immediate
  775. * true if the update is to be sent as soon as possible
  776. */
  777. public void updateVariable(String paintableId, String variableName,
  778. String newValue, boolean immediate) {
  779. addVariableToQueue(paintableId, variableName, newValue, immediate);
  780. }
  781. /**
  782. * Sends a new value for the given paintables given variable to the server.
  783. * <p>
  784. * The update is actually queued to be sent at a suitable time. If immediate
  785. * is true, the update is sent as soon as possible. If immediate is false,
  786. * the update will be sent along with the next immediate update.
  787. * </p>
  788. *
  789. * @param paintableId
  790. * the id of the paintable that owns the variable
  791. * @param variableName
  792. * the name of the variable
  793. * @param newValue
  794. * the new value to be sent
  795. * @param immediate
  796. * true if the update is to be sent as soon as possible
  797. */
  798. public void updateVariable(String paintableId, String variableName,
  799. int newValue, boolean immediate) {
  800. addVariableToQueue(paintableId, variableName, newValue, immediate);
  801. }
  802. /**
  803. * Sends a new value for the given paintables given variable to the server.
  804. * <p>
  805. * The update is actually queued to be sent at a suitable time. If immediate
  806. * is true, the update is sent as soon as possible. If immediate is false,
  807. * the update will be sent along with the next immediate update.
  808. * </p>
  809. *
  810. * @param paintableId
  811. * the id of the paintable that owns the variable
  812. * @param variableName
  813. * the name of the variable
  814. * @param newValue
  815. * the new value to be sent
  816. * @param immediate
  817. * true if the update is to be sent as soon as possible
  818. */
  819. public void updateVariable(String paintableId, String variableName,
  820. long newValue, boolean immediate) {
  821. addVariableToQueue(paintableId, variableName, newValue, immediate);
  822. }
  823. /**
  824. * Sends a new value for the given paintables given variable to the server.
  825. * <p>
  826. * The update is actually queued to be sent at a suitable time. If immediate
  827. * is true, the update is sent as soon as possible. If immediate is false,
  828. * the update will be sent along with the next immediate update.
  829. * </p>
  830. *
  831. * @param paintableId
  832. * the id of the paintable that owns the variable
  833. * @param variableName
  834. * the name of the variable
  835. * @param newValue
  836. * the new value to be sent
  837. * @param immediate
  838. * true if the update is to be sent as soon as possible
  839. */
  840. public void updateVariable(String paintableId, String variableName,
  841. float newValue, boolean immediate) {
  842. addVariableToQueue(paintableId, variableName, newValue, immediate);
  843. }
  844. /**
  845. * Sends a new value for the given paintables given variable to the server.
  846. * <p>
  847. * The update is actually queued to be sent at a suitable time. If immediate
  848. * is true, the update is sent as soon as possible. If immediate is false,
  849. * the update will be sent along with the next immediate update.
  850. * </p>
  851. *
  852. * @param paintableId
  853. * the id of the paintable that owns the variable
  854. * @param variableName
  855. * the name of the variable
  856. * @param newValue
  857. * the new value to be sent
  858. * @param immediate
  859. * true if the update is to be sent as soon as possible
  860. */
  861. public void updateVariable(String paintableId, String variableName,
  862. double newValue, boolean immediate) {
  863. addVariableToQueue(paintableId, variableName, newValue, immediate);
  864. }
  865. /**
  866. * Sends a new value for the given paintables given variable to the server.
  867. * <p>
  868. * The update is actually queued to be sent at a suitable time. If immediate
  869. * is true, the update is sent as soon as possible. If immediate is false,
  870. * the update will be sent along with the next immediate update.
  871. * </p>
  872. *
  873. * @param paintableId
  874. * the id of the paintable that owns the variable
  875. * @param variableName
  876. * the name of the variable
  877. * @param newValue
  878. * the new value to be sent
  879. * @param immediate
  880. * true if the update is to be sent as soon as possible
  881. */
  882. public void updateVariable(String paintableId, String variableName,
  883. boolean newValue, boolean immediate) {
  884. addVariableToQueue(paintableId, variableName, newValue, immediate);
  885. }
  886. /**
  887. * Sends a new value for the given paintables given variable to the server.
  888. * <p>
  889. * The update is actually queued to be sent at a suitable time. If immediate
  890. * is true, the update is sent as soon as possible. If immediate is false,
  891. * the update will be sent along with the next immediate update.
  892. * </p>
  893. *
  894. * @param paintableId
  895. * the id of the paintable that owns the variable
  896. * @param variableName
  897. * the name of the variable
  898. * @param map
  899. * the new values to be sent
  900. * @param immediate
  901. * true if the update is to be sent as soon as possible
  902. */
  903. public void updateVariable(String paintableId, String variableName,
  904. Map<String, Object> map, boolean immediate) {
  905. addVariableToQueue(paintableId, variableName, map, immediate);
  906. }
  907. /**
  908. * Sends a new value for the given paintables given variable to the server.
  909. * <p>
  910. * The update is actually queued to be sent at a suitable time. If immediate
  911. * is true, the update is sent as soon as possible. If immediate is false,
  912. * the update will be sent along with the next immediate update.
  913. * <p>
  914. * A null array is sent as an empty array.
  915. *
  916. * @param paintableId
  917. * the id of the paintable that owns the variable
  918. * @param variableName
  919. * the name of the variable
  920. * @param values
  921. * the new value to be sent
  922. * @param immediate
  923. * true if the update is to be sent as soon as possible
  924. */
  925. public void updateVariable(String paintableId, String variableName,
  926. String[] values, boolean immediate) {
  927. addVariableToQueue(paintableId, variableName, values, immediate);
  928. }
  929. /**
  930. * Sends a new value for the given paintables given variable to the server.
  931. * <p>
  932. * The update is actually queued to be sent at a suitable time. If immediate
  933. * is true, the update is sent as soon as possible. If immediate is false,
  934. * the update will be sent along with the next immediate update.
  935. * <p>
  936. * A null array is sent as an empty array.
  937. *
  938. * @param paintableId
  939. * the id of the paintable that owns the variable
  940. * @param variableName
  941. * the name of the variable
  942. * @param values
  943. * the new value to be sent
  944. * @param immediate
  945. * true if the update is to be sent as soon as possible
  946. */
  947. public void updateVariable(String paintableId, String variableName,
  948. Object[] values, boolean immediate) {
  949. addVariableToQueue(paintableId, variableName, values, immediate);
  950. }
  951. /**
  952. * Does absolutely nothing. Replaced by {@link LayoutManager}.
  953. *
  954. * @param container
  955. * @deprecated As of 7.0, serves no purpose
  956. */
  957. @Deprecated
  958. public void runDescendentsLayout(HasWidgets container) {
  959. }
  960. /**
  961. * This will cause re-layouting of all components. Mainly used for
  962. * development. Published to JavaScript.
  963. */
  964. public void forceLayout() {
  965. Duration duration = new Duration();
  966. layoutManager.forceLayout();
  967. getLogger().info("forceLayout in " + duration.elapsedMillis() + " ms");
  968. }
  969. /**
  970. * Returns false
  971. *
  972. * @param paintable
  973. * @return false, always
  974. * @deprecated As of 7.0, serves no purpose
  975. */
  976. @Deprecated
  977. private boolean handleComponentRelativeSize(ComponentConnector paintable) {
  978. return false;
  979. }
  980. /**
  981. * Returns false
  982. *
  983. * @param paintable
  984. * @return false, always
  985. * @deprecated As of 7.0, serves no purpose
  986. */
  987. @Deprecated
  988. public boolean handleComponentRelativeSize(Widget widget) {
  989. return handleComponentRelativeSize(connectorMap.getConnector(widget));
  990. }
  991. @Deprecated
  992. public ComponentConnector getPaintable(UIDL uidl) {
  993. // Non-component connectors shouldn't be painted from legacy connectors
  994. return (ComponentConnector) getConnector(uidl.getId(),
  995. Integer.parseInt(uidl.getTag()));
  996. }
  997. /**
  998. * Get either an existing ComponentConnector or create a new
  999. * ComponentConnector with the given type and id.
  1000. *
  1001. * If a ComponentConnector with the given id already exists, returns it.
  1002. * Otherwise creates and registers a new ComponentConnector of the given
  1003. * type.
  1004. *
  1005. * @param connectorId
  1006. * Id of the paintable
  1007. * @param connectorType
  1008. * Type of the connector, as passed from the server side
  1009. *
  1010. * @return Either an existing ComponentConnector or a new ComponentConnector
  1011. * of the given type
  1012. */
  1013. public ServerConnector getConnector(String connectorId, int connectorType) {
  1014. if (!connectorMap.hasConnector(connectorId)) {
  1015. return createAndRegisterConnector(connectorId, connectorType);
  1016. }
  1017. return connectorMap.getConnector(connectorId);
  1018. }
  1019. /**
  1020. * Creates a new ServerConnector with the given type and id.
  1021. *
  1022. * Creates and registers a new ServerConnector of the given type. Should
  1023. * never be called with the connector id of an existing connector.
  1024. *
  1025. * @param connectorId
  1026. * Id of the new connector
  1027. * @param connectorType
  1028. * Type of the connector, as passed from the server side
  1029. *
  1030. * @return A new ServerConnector of the given type
  1031. */
  1032. private ServerConnector createAndRegisterConnector(String connectorId,
  1033. int connectorType) {
  1034. Profiler.enter("ApplicationConnection.createAndRegisterConnector");
  1035. // Create and register a new connector with the given type
  1036. ServerConnector p = widgetSet.createConnector(connectorType,
  1037. configuration);
  1038. connectorMap.registerConnector(connectorId, p);
  1039. p.doInit(connectorId, this);
  1040. Profiler.leave("ApplicationConnection.createAndRegisterConnector");
  1041. return p;
  1042. }
  1043. /**
  1044. * Gets a resource that has been pre-loaded via UIDL, such as custom
  1045. * layouts.
  1046. *
  1047. * @param name
  1048. * identifier of the resource to get
  1049. * @return the resource
  1050. */
  1051. public String getResource(String name) {
  1052. return resourcesMap.get(name);
  1053. }
  1054. /**
  1055. * Sets a resource that has been pre-loaded via UIDL, such as custom
  1056. * layouts.
  1057. *
  1058. * @since 7.6
  1059. * @param name
  1060. * identifier of the resource to Set
  1061. * @param resource
  1062. * the resource
  1063. */
  1064. public void setResource(String name, String resource) {
  1065. resourcesMap.put(name, resource);
  1066. }
  1067. /**
  1068. * Singleton method to get instance of app's context menu.
  1069. *
  1070. * @return VContextMenu object
  1071. */
  1072. public VContextMenu getContextMenu() {
  1073. if (contextMenu == null) {
  1074. contextMenu = new VContextMenu();
  1075. contextMenu.setOwner(uIConnector.getWidget());
  1076. DOM.setElementProperty(contextMenu.getElement(), "id",
  1077. "PID_VAADIN_CM");
  1078. }
  1079. return contextMenu;
  1080. }
  1081. /**
  1082. * Gets an {@link Icon} instance corresponding to a URI.
  1083. *
  1084. * @since 7.2
  1085. * @param uri
  1086. * @return Icon object
  1087. */
  1088. public Icon getIcon(String uri) {
  1089. Icon icon;
  1090. if (uri == null) {
  1091. return null;
  1092. } else if (FontIcon.isFontIconUri(uri)) {
  1093. icon = GWT.create(FontIcon.class);
  1094. } else {
  1095. icon = GWT.create(ImageIcon.class);
  1096. }
  1097. icon.setUri(translateVaadinUri(uri));
  1098. return icon;
  1099. }
  1100. /**
  1101. * Translates custom protocols in UIDL URI's to be recognizable by browser.
  1102. * All uri's from UIDL should be routed via this method before giving them
  1103. * to browser due URI's in UIDL may contain custom protocols like theme://.
  1104. *
  1105. * @param uidlUri
  1106. * Vaadin URI from uidl
  1107. * @return translated URI ready for browser
  1108. */
  1109. public String translateVaadinUri(String uidlUri) {
  1110. return uriResolver.resolveVaadinUri(uidlUri);
  1111. }
  1112. /**
  1113. * Gets the URI for the current theme. Can be used to reference theme
  1114. * resources.
  1115. *
  1116. * @return URI to the current theme
  1117. */
  1118. public String getThemeUri() {
  1119. return configuration.getVaadinDirUrl() + "themes/"
  1120. + getUIConnector().getActiveTheme();
  1121. }
  1122. /* Extended title handling */
  1123. private final VTooltip tooltip;
  1124. private ConnectorMap connectorMap = GWT.create(ConnectorMap.class);
  1125. /**
  1126. * Use to notify that the given component's caption has changed; layouts may
  1127. * have to be recalculated.
  1128. *
  1129. * @param component
  1130. * the Paintable whose caption has changed
  1131. * @deprecated As of 7.0.2, has not had any effect for a long time
  1132. */
  1133. @Deprecated
  1134. public void captionSizeUpdated(Widget widget) {
  1135. // This doesn't do anything, it's just kept here for compatibility
  1136. }
  1137. /**
  1138. * Gets the main view
  1139. *
  1140. * @return the main view
  1141. */
  1142. public UIConnector getUIConnector() {
  1143. return uIConnector;
  1144. }
  1145. /**
  1146. * Gets the {@link ApplicationConfiguration} for the current application.
  1147. *
  1148. * @see ApplicationConfiguration
  1149. * @return the configuration for this application
  1150. */
  1151. public ApplicationConfiguration getConfiguration() {
  1152. return configuration;
  1153. }
  1154. /**
  1155. * Checks if there is a registered server side listener for the event. The
  1156. * list of events which has server side listeners is updated automatically
  1157. * before the component is updated so the value is correct if called from
  1158. * updatedFromUIDL.
  1159. *
  1160. * @param connector
  1161. * The connector to register event listeners for
  1162. * @param eventIdentifier
  1163. * The identifier for the event
  1164. * @return true if at least one listener has been registered on server side
  1165. * for the event identified by eventIdentifier.
  1166. * @deprecated As of 7.0. Use
  1167. * {@link AbstractConnector#hasEventListener(String)} instead
  1168. */
  1169. @Deprecated
  1170. public boolean hasEventListeners(ComponentConnector connector,
  1171. String eventIdentifier) {
  1172. return connector.hasEventListener(eventIdentifier);
  1173. }
  1174. /**
  1175. * Adds the get parameters to the uri and returns the new uri that contains
  1176. * the parameters.
  1177. *
  1178. * @param uri
  1179. * The uri to which the parameters should be added.
  1180. * @param extraParams
  1181. * One or more parameters in the format "a=b" or "c=d&e=f". An
  1182. * empty string is allowed but will not modify the url.
  1183. * @return The modified URI with the get parameters in extraParams added.
  1184. * @deprecated Use {@link SharedUtil#addGetParameters(String,String)}
  1185. * instead
  1186. */
  1187. @Deprecated
  1188. public static String addGetParameters(String uri, String extraParams) {
  1189. return SharedUtil.addGetParameters(uri, extraParams);
  1190. }
  1191. ConnectorMap getConnectorMap() {
  1192. return connectorMap;
  1193. }
  1194. /**
  1195. * @deprecated As of 7.0. No longer serves any purpose.
  1196. */
  1197. @Deprecated
  1198. public void unregisterPaintable(ServerConnector p) {
  1199. getLogger().info(
  1200. "unregisterPaintable (unnecessarily) called for "
  1201. + Util.getConnectorString(p));
  1202. }
  1203. /**
  1204. * Get VTooltip instance related to application connection
  1205. *
  1206. * @return VTooltip instance
  1207. */
  1208. public VTooltip getVTooltip() {
  1209. return tooltip;
  1210. }
  1211. /**
  1212. * Method provided for backwards compatibility. Duties previously done by
  1213. * this method is now handled by the state change event handler in
  1214. * AbstractComponentConnector. The only function this method has is to
  1215. * return true if the UIDL is a "cached" update.
  1216. *
  1217. * @param component
  1218. * @param uidl
  1219. * @param manageCaption
  1220. * @deprecated As of 7.0, no longer serves any purpose
  1221. * @return
  1222. */
  1223. @Deprecated
  1224. public boolean updateComponent(Widget component, UIDL uidl,
  1225. boolean manageCaption) {
  1226. ComponentConnector connector = getConnectorMap()
  1227. .getConnector(component);
  1228. if (!AbstractComponentConnector.isRealUpdate(uidl)) {
  1229. return true;
  1230. }
  1231. if (!manageCaption) {
  1232. getLogger()
  1233. .warning(
  1234. Util.getConnectorString(connector)
  1235. + " called updateComponent with manageCaption=false. The parameter was ignored - override delegateCaption() to return false instead. It is however not recommended to use caption this way at all.");
  1236. }
  1237. return false;
  1238. }
  1239. /**
  1240. * @deprecated As of 7.0. Use
  1241. * {@link AbstractComponentConnector#hasEventListener(String)}
  1242. * instead
  1243. */
  1244. @Deprecated
  1245. public boolean hasEventListeners(Widget widget, String eventIdentifier) {
  1246. ComponentConnector connector = getConnectorMap().getConnector(widget);
  1247. if (connector == null) {
  1248. /*
  1249. * No connector will exist in cases where Vaadin widgets have been
  1250. * re-used without implementing server<->client communication.
  1251. */
  1252. return false;
  1253. }
  1254. return hasEventListeners(getConnectorMap().getConnector(widget),
  1255. eventIdentifier);
  1256. }
  1257. LayoutManager getLayoutManager() {
  1258. return layoutManager;
  1259. }
  1260. /**
  1261. * Schedules a heartbeat request to occur after the configured heartbeat
  1262. * interval elapses if the interval is a positive number. Otherwise, does
  1263. * nothing.
  1264. *
  1265. * @deprecated as of 7.2, use {@link Heartbeat#schedule()} instead
  1266. */
  1267. @Deprecated
  1268. protected void scheduleHeartbeat() {
  1269. heartbeat.schedule();
  1270. }
  1271. /**
  1272. * Sends a heartbeat request to the server.
  1273. * <p>
  1274. * Heartbeat requests are used to inform the server that the client-side is
  1275. * still alive. If the client page is closed or the connection lost, the
  1276. * server will eventually close the inactive UI.
  1277. *
  1278. * @deprecated as of 7.2, use {@link Heartbeat#send()} instead
  1279. */
  1280. @Deprecated
  1281. protected void sendHeartbeat() {
  1282. heartbeat.send();
  1283. }
  1284. public void handleCommunicationError(String details, int statusCode) {
  1285. boolean handled = false;
  1286. if (communicationErrorDelegate != null) {
  1287. handled = communicationErrorDelegate.onError(details, statusCode);
  1288. }
  1289. if (!handled) {
  1290. showCommunicationError(details, statusCode);
  1291. }
  1292. }
  1293. /**
  1294. * Sets the delegate that is called whenever a communication error occurrs.
  1295. *
  1296. * @param delegate
  1297. * the delegate.
  1298. */
  1299. public void setCommunicationErrorDelegate(CommunicationErrorHandler delegate) {
  1300. communicationErrorDelegate = delegate;
  1301. }
  1302. public void setApplicationRunning(boolean applicationRunning) {
  1303. if (getApplicationState() == ApplicationState.TERMINATED) {
  1304. if (applicationRunning) {
  1305. getLogger()
  1306. .severe("Tried to restart a terminated application. This is not supported");
  1307. } else {
  1308. getLogger()
  1309. .warning(
  1310. "Tried to stop a terminated application. This should not be done");
  1311. }
  1312. return;
  1313. } else if (getApplicationState() == ApplicationState.INITIALIZING) {
  1314. if (applicationRunning) {
  1315. applicationState = ApplicationState.RUNNING;
  1316. } else {
  1317. getLogger()
  1318. .warning(
  1319. "Tried to stop the application before it has started. This should not be done");
  1320. }
  1321. } else if (getApplicationState() == ApplicationState.RUNNING) {
  1322. if (!applicationRunning) {
  1323. applicationState = ApplicationState.TERMINATED;
  1324. eventBus.fireEvent(new ApplicationStoppedEvent());
  1325. } else {
  1326. getLogger()
  1327. .warning(
  1328. "Tried to start an already running application. This should not be done");
  1329. }
  1330. }
  1331. }
  1332. /**
  1333. * Checks if the application is in the {@link ApplicationState#RUNNING}
  1334. * state.
  1335. *
  1336. * @since 7.6
  1337. * @return true if the application is in the running state, false otherwise
  1338. */
  1339. public boolean isApplicationRunning() {
  1340. return applicationState == ApplicationState.RUNNING;
  1341. }
  1342. public <H extends EventHandler> HandlerRegistration addHandler(
  1343. GwtEvent.Type<H> type, H handler) {
  1344. return eventBus.addHandler(type, handler);
  1345. }
  1346. @Override
  1347. public void fireEvent(GwtEvent<?> event) {
  1348. eventBus.fireEvent(event);
  1349. }
  1350. /**
  1351. * Calls {@link ComponentConnector#flush()} on the active connector. Does
  1352. * nothing if there is no active (focused) connector.
  1353. */
  1354. public void flushActiveConnector() {
  1355. ComponentConnector activeConnector = getActiveConnector();
  1356. if (activeConnector == null) {
  1357. return;
  1358. }
  1359. activeConnector.flush();
  1360. }
  1361. /**
  1362. * Gets the active connector for focused element in browser.
  1363. *
  1364. * @return Connector for focused element or null.
  1365. */
  1366. private ComponentConnector getActiveConnector() {
  1367. Element focusedElement = WidgetUtil.getFocusedElement();
  1368. if (focusedElement == null) {
  1369. return null;
  1370. }
  1371. return Util.getConnectorForElement(this, getUIConnector().getWidget(),
  1372. focusedElement);
  1373. }
  1374. private static Logger getLogger() {
  1375. return Logger.getLogger(ApplicationConnection.class.getName());
  1376. }
  1377. /**
  1378. * Returns the hearbeat instance.
  1379. */
  1380. public Heartbeat getHeartbeat() {
  1381. return heartbeat;
  1382. }
  1383. /**
  1384. * Returns the state of this application. An application state goes from
  1385. * "initializing" to "running" to "stopped". There is no way for an
  1386. * application to go back to a previous state, i.e. a stopped application
  1387. * can never be re-started
  1388. *
  1389. * @since 7.6
  1390. * @return the current state of this application
  1391. */
  1392. public ApplicationState getApplicationState() {
  1393. return applicationState;
  1394. }
  1395. /**
  1396. * Gets the server RPC queue for this application
  1397. *
  1398. * @since 7.6
  1399. * @return the server RPC queue
  1400. */
  1401. public ServerRpcQueue getServerRpcQueue() {
  1402. return serverRpcQueue;
  1403. }
  1404. /**
  1405. * Gets the communication error handler for this application
  1406. *
  1407. * @since 7.6
  1408. * @return the server RPC queue
  1409. */
  1410. public ConnectionStateHandler getConnectionStateHandler() {
  1411. return connectionStateHandler;
  1412. }
  1413. /**
  1414. * Gets the (server to client) message handler for this application
  1415. *
  1416. * @since 7.6
  1417. * @return the message handler
  1418. */
  1419. public MessageHandler getMessageHandler() {
  1420. return messageHandler;
  1421. }
  1422. /**
  1423. * Gets the server rpc manager for this application
  1424. *
  1425. * @since 7.6
  1426. * @return the server rpc manager
  1427. */
  1428. public RpcManager getRpcManager() {
  1429. return rpcManager;
  1430. }
  1431. /**
  1432. * Gets the (client to server) message sender for this application
  1433. *
  1434. * @since 7.6
  1435. * @return the message sender
  1436. */
  1437. public MessageSender getMessageSender() {
  1438. return messageSender;
  1439. }
  1440. /**
  1441. * @since 7.6
  1442. * @return the widget set
  1443. */
  1444. public WidgetSet getWidgetSet() {
  1445. return widgetSet;
  1446. }
  1447. public int getLastSeenServerSyncId() {
  1448. return getMessageHandler().getLastSeenServerSyncId();
  1449. }
  1450. }