You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApplicationConnection.java 53KB

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