Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ApplicationConnection.java 56KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  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.DefaultConnectionStateHandler;
  47. import com.vaadin.client.communication.Heartbeat;
  48. import com.vaadin.client.communication.MessageHandler;
  49. import com.vaadin.client.communication.MessageSender;
  50. import com.vaadin.client.communication.RpcManager;
  51. import com.vaadin.client.communication.ServerRpcQueue;
  52. import com.vaadin.client.componentlocator.ComponentLocator;
  53. import com.vaadin.client.metadata.ConnectorBundleLoader;
  54. import com.vaadin.client.ui.AbstractComponentConnector;
  55. import com.vaadin.client.ui.FontIcon;
  56. import com.vaadin.client.ui.Icon;
  57. import com.vaadin.client.ui.ImageIcon;
  58. import com.vaadin.client.ui.VContextMenu;
  59. import com.vaadin.client.ui.VNotification;
  60. import com.vaadin.client.ui.VOverlay;
  61. import com.vaadin.client.ui.ui.UIConnector;
  62. import com.vaadin.shared.AbstractComponentState;
  63. import com.vaadin.shared.VaadinUriResolver;
  64. import com.vaadin.shared.Version;
  65. import com.vaadin.shared.communication.LegacyChangeVariablesInvocation;
  66. import com.vaadin.shared.util.SharedUtil;
  67. /**
  68. * This is the client side communication "engine", managing client-server
  69. * communication with its server side counterpart
  70. * com.vaadin.server.VaadinService.
  71. *
  72. * Client-side connectors receive updates from the corresponding server-side
  73. * connector (typically component) as state updates or RPC calls. The connector
  74. * has the possibility to communicate back with its server side counter part
  75. * through RPC calls.
  76. *
  77. * TODO document better
  78. *
  79. * Entry point classes (widgetsets) define <code>onModuleLoad()</code>.
  80. */
  81. public class ApplicationConnection implements HasHandlers {
  82. @Deprecated
  83. public static final String MODIFIED_CLASSNAME = StyleConstants.MODIFIED;
  84. @Deprecated
  85. public static final String DISABLED_CLASSNAME = StyleConstants.DISABLED;
  86. @Deprecated
  87. public static final String REQUIRED_CLASSNAME = StyleConstants.REQUIRED;
  88. @Deprecated
  89. public static final String REQUIRED_CLASSNAME_EXT = StyleConstants.REQUIRED_EXT;
  90. @Deprecated
  91. public static final String ERROR_CLASSNAME_EXT = StyleConstants.ERROR_EXT;
  92. /**
  93. * A string that, if found in a non-JSON response to a UIDL request, will
  94. * cause the browser to refresh the page. If followed by a colon, optional
  95. * whitespace, and a URI, causes the browser to synchronously load the URI.
  96. *
  97. * <p>
  98. * This allows, for instance, a servlet filter to redirect the application
  99. * to a custom login page when the session expires. For example:
  100. * </p>
  101. *
  102. * <pre>
  103. * if (sessionExpired) {
  104. * response.setHeader(&quot;Content-Type&quot;, &quot;text/html&quot;);
  105. * response.getWriter().write(
  106. * myLoginPageHtml + &quot;&lt;!-- Vaadin-Refresh: &quot;
  107. * + request.getContextPath() + &quot; --&gt;&quot;);
  108. * }
  109. * </pre>
  110. */
  111. public static final String UIDL_REFRESH_TOKEN = "Vaadin-Refresh";
  112. private final HashMap<String, String> resourcesMap = new HashMap<String, String>();
  113. private WidgetSet widgetSet;
  114. private VContextMenu contextMenu = null;
  115. private final UIConnector uIConnector;
  116. protected boolean cssLoaded = false;
  117. /** Parameters for this application connection loaded from the web-page */
  118. private ApplicationConfiguration configuration;
  119. private final LayoutManager layoutManager;
  120. private final RpcManager rpcManager;
  121. /** Event bus for communication events */
  122. private EventBus eventBus = GWT.create(SimpleEventBus.class);
  123. public enum ApplicationState {
  124. INITIALIZING, RUNNING, TERMINATED;
  125. }
  126. private ApplicationState applicationState = ApplicationState.INITIALIZING;
  127. /**
  128. * The communication handler methods are called at certain points during
  129. * communication with the server. This allows for making add-ons that keep
  130. * track of different aspects of the communication.
  131. */
  132. public interface CommunicationHandler extends EventHandler {
  133. void onRequestStarting(RequestStartingEvent e);
  134. void onResponseHandlingStarted(ResponseHandlingStartedEvent e);
  135. void onResponseHandlingEnded(ResponseHandlingEndedEvent e);
  136. }
  137. public static class RequestStartingEvent extends ApplicationConnectionEvent {
  138. public static Type<CommunicationHandler> TYPE = new Type<CommunicationHandler>();
  139. public RequestStartingEvent(ApplicationConnection connection) {
  140. super(connection);
  141. }
  142. @Override
  143. public Type<CommunicationHandler> getAssociatedType() {
  144. return TYPE;
  145. }
  146. @Override
  147. protected void dispatch(CommunicationHandler handler) {
  148. handler.onRequestStarting(this);
  149. }
  150. }
  151. public static class ResponseHandlingEndedEvent extends
  152. ApplicationConnectionEvent {
  153. public static Type<CommunicationHandler> TYPE = new Type<CommunicationHandler>();
  154. public ResponseHandlingEndedEvent(ApplicationConnection connection) {
  155. super(connection);
  156. }
  157. @Override
  158. public Type<CommunicationHandler> getAssociatedType() {
  159. return TYPE;
  160. }
  161. @Override
  162. protected void dispatch(CommunicationHandler handler) {
  163. handler.onResponseHandlingEnded(this);
  164. }
  165. }
  166. public static abstract class ApplicationConnectionEvent extends
  167. GwtEvent<CommunicationHandler> {
  168. private ApplicationConnection connection;
  169. protected ApplicationConnectionEvent(ApplicationConnection connection) {
  170. this.connection = connection;
  171. }
  172. public ApplicationConnection getConnection() {
  173. return connection;
  174. }
  175. }
  176. public static class ResponseHandlingStartedEvent extends
  177. ApplicationConnectionEvent {
  178. public ResponseHandlingStartedEvent(ApplicationConnection connection) {
  179. super(connection);
  180. }
  181. public static Type<CommunicationHandler> TYPE = new Type<CommunicationHandler>();
  182. @Override
  183. public Type<CommunicationHandler> getAssociatedType() {
  184. return TYPE;
  185. }
  186. @Override
  187. protected void dispatch(CommunicationHandler handler) {
  188. handler.onResponseHandlingStarted(this);
  189. }
  190. }
  191. /**
  192. * Event triggered when a application is stopped by calling
  193. * {@link ApplicationConnection#setApplicationRunning(false)}.
  194. *
  195. * To listen for the event add a {@link ApplicationStoppedHandler} by
  196. * invoking
  197. * {@link ApplicationConnection#addHandler(ApplicationConnection.ApplicationStoppedEvent.Type, ApplicationStoppedHandler)}
  198. * to the {@link ApplicationConnection}
  199. *
  200. * @since 7.1.8
  201. * @author Vaadin Ltd
  202. */
  203. public static class ApplicationStoppedEvent extends
  204. GwtEvent<ApplicationStoppedHandler> {
  205. public static Type<ApplicationStoppedHandler> TYPE = new Type<ApplicationStoppedHandler>();
  206. @Override
  207. public Type<ApplicationStoppedHandler> getAssociatedType() {
  208. return TYPE;
  209. }
  210. @Override
  211. protected void dispatch(ApplicationStoppedHandler listener) {
  212. listener.onApplicationStopped(this);
  213. }
  214. }
  215. /**
  216. * Allows custom handling of communication errors.
  217. */
  218. public interface CommunicationErrorHandler {
  219. /**
  220. * Called when a communication error has occurred. Returning
  221. * <code>true</code> from this method suppresses error handling.
  222. *
  223. * @param details
  224. * A string describing the error.
  225. * @param statusCode
  226. * The HTTP status code (e.g. 404, etc).
  227. * @return true if the error reporting should be suppressed, false to
  228. * perform normal error reporting.
  229. */
  230. public boolean onError(String details, int statusCode);
  231. }
  232. /**
  233. * A listener for listening to application stopped events. The listener can
  234. * be added to a {@link ApplicationConnection} by invoking
  235. * {@link ApplicationConnection#addHandler(ApplicationStoppedEvent.Type, ApplicationStoppedHandler)}
  236. *
  237. * @since 7.1.8
  238. * @author Vaadin Ltd
  239. */
  240. public interface ApplicationStoppedHandler extends EventHandler {
  241. /**
  242. * Triggered when the {@link ApplicationConnection} marks a previously
  243. * running application as stopped by invoking
  244. * {@link ApplicationConnection#setApplicationRunning(false)}
  245. *
  246. * @param event
  247. * the event triggered by the {@link ApplicationConnection}
  248. */
  249. void onApplicationStopped(ApplicationStoppedEvent event);
  250. }
  251. private CommunicationErrorHandler communicationErrorDelegate = null;
  252. private VLoadingIndicator loadingIndicator;
  253. private Heartbeat heartbeat = GWT.create(Heartbeat.class);
  254. private boolean tooltipInitialized = false;
  255. private final VaadinUriResolver uriResolver = new VaadinUriResolver() {
  256. @Override
  257. protected String getVaadinDirUrl() {
  258. return getConfiguration().getVaadinDirUrl();
  259. }
  260. @Override
  261. protected String getServiceUrlParameterName() {
  262. return getConfiguration().getServiceUrlParameterName();
  263. }
  264. @Override
  265. protected String getServiceUrl() {
  266. return getConfiguration().getServiceUrl();
  267. }
  268. @Override
  269. protected String getThemeUri() {
  270. return ApplicationConnection.this.getThemeUri();
  271. }
  272. @Override
  273. protected String encodeQueryStringParameterValue(String queryString) {
  274. return URL.encodeQueryString(queryString);
  275. }
  276. };
  277. public static class MultiStepDuration extends Duration {
  278. private int previousStep = elapsedMillis();
  279. public void logDuration(String message) {
  280. logDuration(message, 0);
  281. }
  282. public void logDuration(String message, int minDuration) {
  283. int currentTime = elapsedMillis();
  284. int stepDuration = currentTime - previousStep;
  285. if (stepDuration >= minDuration) {
  286. getLogger().info(message + ": " + stepDuration + " ms");
  287. }
  288. previousStep = currentTime;
  289. }
  290. }
  291. public ApplicationConnection() {
  292. // Assuming UI data is eagerly loaded
  293. ConnectorBundleLoader.get().loadBundle(
  294. ConnectorBundleLoader.EAGER_BUNDLE_NAME, null);
  295. uIConnector = GWT.create(UIConnector.class);
  296. rpcManager = GWT.create(RpcManager.class);
  297. layoutManager = GWT.create(LayoutManager.class);
  298. layoutManager.setConnection(this);
  299. tooltip = GWT.create(VTooltip.class);
  300. loadingIndicator = GWT.create(VLoadingIndicator.class);
  301. loadingIndicator.setConnection(this);
  302. serverRpcQueue = GWT.create(ServerRpcQueue.class);
  303. serverRpcQueue.setConnection(this);
  304. connectionStateHandler = GWT
  305. .create(DefaultConnectionStateHandler.class);
  306. connectionStateHandler.setConnection(this);
  307. messageHandler = GWT.create(MessageHandler.class);
  308. messageHandler.setConnection(this);
  309. messageSender = GWT.create(MessageSender.class);
  310. messageSender.setConnection(this);
  311. }
  312. public void init(WidgetSet widgetSet, ApplicationConfiguration cnf) {
  313. getLogger().info("Starting application " + cnf.getRootPanelId());
  314. getLogger().info("Using theme: " + cnf.getThemeName());
  315. getLogger().info(
  316. "Vaadin application servlet version: "
  317. + cnf.getServletVersion());
  318. if (!cnf.getServletVersion().equals(Version.getFullVersion())) {
  319. getLogger()
  320. .severe("Warning: your widget set seems to be built with a different "
  321. + "version than the one used on server. Unexpected "
  322. + "behavior may occur.");
  323. }
  324. this.widgetSet = widgetSet;
  325. configuration = cnf;
  326. ComponentLocator componentLocator = new ComponentLocator(this);
  327. String appRootPanelName = cnf.getRootPanelId();
  328. // remove the end (window name) of autogenerated rootpanel id
  329. appRootPanelName = appRootPanelName.replaceFirst("-\\d+$", "");
  330. initializeTestbenchHooks(componentLocator, appRootPanelName);
  331. initializeClientHooks();
  332. uIConnector.init(cnf.getRootPanelId(), 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. * @param name
  1059. * identifier of the resource to Set
  1060. * @param resource
  1061. * the resource
  1062. */
  1063. public void setResource(String name, String resource) {
  1064. resourcesMap.put(name, resource);
  1065. }
  1066. /**
  1067. * Singleton method to get instance of app's context menu.
  1068. *
  1069. * @return VContextMenu object
  1070. */
  1071. public VContextMenu getContextMenu() {
  1072. if (contextMenu == null) {
  1073. contextMenu = new VContextMenu();
  1074. contextMenu.setOwner(uIConnector.getWidget());
  1075. DOM.setElementProperty(contextMenu.getElement(), "id",
  1076. "PID_VAADIN_CM");
  1077. }
  1078. return contextMenu;
  1079. }
  1080. /**
  1081. * Gets an {@link Icon} instance corresponding to a URI.
  1082. *
  1083. * @since 7.2
  1084. * @param uri
  1085. * @return Icon object
  1086. */
  1087. public Icon getIcon(String uri) {
  1088. Icon icon;
  1089. if (uri == null) {
  1090. return null;
  1091. } else if (FontIcon.isFontIconUri(uri)) {
  1092. icon = GWT.create(FontIcon.class);
  1093. } else {
  1094. icon = GWT.create(ImageIcon.class);
  1095. }
  1096. icon.setUri(translateVaadinUri(uri));
  1097. return icon;
  1098. }
  1099. /**
  1100. * Translates custom protocols in UIDL URI's to be recognizable by browser.
  1101. * All uri's from UIDL should be routed via this method before giving them
  1102. * to browser due URI's in UIDL may contain custom protocols like theme://.
  1103. *
  1104. * @param uidlUri
  1105. * Vaadin URI from uidl
  1106. * @return translated URI ready for browser
  1107. */
  1108. public String translateVaadinUri(String uidlUri) {
  1109. return uriResolver.resolveVaadinUri(uidlUri);
  1110. }
  1111. /**
  1112. * Gets the URI for the current theme. Can be used to reference theme
  1113. * resources.
  1114. *
  1115. * @return URI to the current theme
  1116. */
  1117. public String getThemeUri() {
  1118. return configuration.getVaadinDirUrl() + "themes/"
  1119. + getUIConnector().getActiveTheme();
  1120. }
  1121. /* Extended title handling */
  1122. private final VTooltip tooltip;
  1123. private ConnectorMap connectorMap = GWT.create(ConnectorMap.class);
  1124. /**
  1125. * Use to notify that the given component's caption has changed; layouts may
  1126. * have to be recalculated.
  1127. *
  1128. * @param component
  1129. * the Paintable whose caption has changed
  1130. * @deprecated As of 7.0.2, has not had any effect for a long time
  1131. */
  1132. @Deprecated
  1133. public void captionSizeUpdated(Widget widget) {
  1134. // This doesn't do anything, it's just kept here for compatibility
  1135. }
  1136. /**
  1137. * Gets the main view
  1138. *
  1139. * @return the main view
  1140. */
  1141. public UIConnector getUIConnector() {
  1142. return uIConnector;
  1143. }
  1144. /**
  1145. * Gets the {@link ApplicationConfiguration} for the current application.
  1146. *
  1147. * @see ApplicationConfiguration
  1148. * @return the configuration for this application
  1149. */
  1150. public ApplicationConfiguration getConfiguration() {
  1151. return configuration;
  1152. }
  1153. /**
  1154. * Checks if there is a registered server side listener for the event. The
  1155. * list of events which has server side listeners is updated automatically
  1156. * before the component is updated so the value is correct if called from
  1157. * updatedFromUIDL.
  1158. *
  1159. * @param paintable
  1160. * The connector to register event listeners for
  1161. * @param eventIdentifier
  1162. * The identifier for the event
  1163. * @return true if at least one listener has been registered on server side
  1164. * for the event identified by eventIdentifier.
  1165. * @deprecated As of 7.0. Use
  1166. * {@link AbstractComponentState#hasEventListener(String)}
  1167. * instead
  1168. */
  1169. @Deprecated
  1170. public boolean hasEventListeners(ComponentConnector paintable,
  1171. String eventIdentifier) {
  1172. return paintable.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
  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. * @return the server RPC queue
  1399. */
  1400. public ServerRpcQueue getServerRpcQueue() {
  1401. return serverRpcQueue;
  1402. }
  1403. /**
  1404. * Gets the communication error handler for this application
  1405. *
  1406. * @return the server RPC queue
  1407. */
  1408. public ConnectionStateHandler getConnectionStateHandler() {
  1409. return connectionStateHandler;
  1410. }
  1411. /**
  1412. * Gets the (server to client) message handler for this application
  1413. *
  1414. * @return the message handler
  1415. */
  1416. public MessageHandler getMessageHandler() {
  1417. return messageHandler;
  1418. }
  1419. /**
  1420. * Gets the server rpc manager for this application
  1421. *
  1422. * @return the server rpc manager
  1423. */
  1424. public RpcManager getRpcManager() {
  1425. return rpcManager;
  1426. }
  1427. /**
  1428. * Gets the (client to server) message sender for this application
  1429. *
  1430. * @return the message sender
  1431. */
  1432. public MessageSender getMessageSender() {
  1433. return messageSender;
  1434. }
  1435. /**
  1436. * @return the widget set
  1437. */
  1438. public WidgetSet getWidgetSet() {
  1439. return widgetSet;
  1440. }
  1441. public int getLastSeenServerSyncId() {
  1442. return getMessageHandler().getLastSeenServerSyncId();
  1443. }
  1444. }