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

ApplicationConnection.java 56KB

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