1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639 |
- /*
- * Copyright 2000-2016 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
- package com.vaadin.client;
-
- import java.util.HashMap;
- import java.util.Map;
- import java.util.logging.Logger;
-
- import com.google.gwt.aria.client.LiveValue;
- import com.google.gwt.aria.client.RelevantValue;
- import com.google.gwt.aria.client.Roles;
- import com.google.gwt.core.client.Duration;
- import com.google.gwt.core.client.GWT;
- import com.google.gwt.core.client.JavaScriptObject;
- import com.google.gwt.core.client.JsArrayString;
- import com.google.gwt.core.client.Scheduler;
- import com.google.gwt.dom.client.Element;
- import com.google.gwt.event.shared.EventBus;
- import com.google.gwt.event.shared.EventHandler;
- import com.google.gwt.event.shared.GwtEvent;
- import com.google.gwt.event.shared.HandlerRegistration;
- import com.google.gwt.event.shared.HasHandlers;
- import com.google.gwt.event.shared.SimpleEventBus;
- import com.google.gwt.http.client.URL;
- import com.google.gwt.user.client.Command;
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.Timer;
- import com.google.gwt.user.client.ui.HasWidgets;
- import com.google.gwt.user.client.ui.Widget;
- import com.vaadin.client.ApplicationConfiguration.ErrorMessage;
- import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
- import com.vaadin.client.ResourceLoader.ResourceLoadListener;
- import com.vaadin.client.communication.ConnectionStateHandler;
- import com.vaadin.client.communication.Heartbeat;
- import com.vaadin.client.communication.MessageHandler;
- import com.vaadin.client.communication.MessageSender;
- import com.vaadin.client.communication.RpcManager;
- import com.vaadin.client.communication.ServerRpcQueue;
- import com.vaadin.client.componentlocator.ComponentLocator;
- import com.vaadin.client.metadata.ConnectorBundleLoader;
- import com.vaadin.client.ui.AbstractComponentConnector;
- import com.vaadin.client.ui.AbstractConnector;
- import com.vaadin.client.ui.FontIcon;
- import com.vaadin.client.ui.Icon;
- import com.vaadin.client.ui.ImageIcon;
- import com.vaadin.client.ui.VContextMenu;
- import com.vaadin.client.ui.VNotification;
- import com.vaadin.client.ui.VOverlay;
- import com.vaadin.client.ui.ui.UIConnector;
- import com.vaadin.shared.VaadinUriResolver;
- import com.vaadin.shared.Version;
- import com.vaadin.shared.communication.LegacyChangeVariablesInvocation;
- import com.vaadin.shared.util.SharedUtil;
-
- /**
- * This is the client side communication "engine", managing client-server
- * communication with its server side counterpart
- * com.vaadin.server.VaadinService.
- *
- * Client-side connectors receive updates from the corresponding server-side
- * connector (typically component) as state updates or RPC calls. The connector
- * has the possibility to communicate back with its server side counter part
- * through RPC calls.
- *
- * TODO document better
- *
- * Entry point classes (widgetsets) define <code>onModuleLoad()</code>.
- */
- public class ApplicationConnection implements HasHandlers {
-
- @Deprecated
- public static final String MODIFIED_CLASSNAME = StyleConstants.MODIFIED;
-
- @Deprecated
- public static final String DISABLED_CLASSNAME = StyleConstants.DISABLED;
-
- @Deprecated
- public static final String REQUIRED_CLASSNAME = StyleConstants.REQUIRED;
-
- @Deprecated
- public static final String REQUIRED_CLASSNAME_EXT = StyleConstants.REQUIRED_EXT;
-
- @Deprecated
- public static final String ERROR_CLASSNAME_EXT = StyleConstants.ERROR_EXT;
-
- /**
- * A string that, if found in a non-JSON response to a UIDL request, will
- * cause the browser to refresh the page. If followed by a colon, optional
- * whitespace, and a URI, causes the browser to synchronously load the URI.
- *
- * <p>
- * This allows, for instance, a servlet filter to redirect the application
- * to a custom login page when the session expires. For example:
- * </p>
- *
- * <pre>
- * if (sessionExpired) {
- * response.setHeader("Content-Type", "text/html");
- * response.getWriter().write(myLoginPageHtml + "<!-- Vaadin-Refresh: "
- * + request.getContextPath() + " -->");
- * }
- * </pre>
- */
- public static final String UIDL_REFRESH_TOKEN = "Vaadin-Refresh";
-
- private final HashMap<String, String> resourcesMap = new HashMap<>();
-
- private WidgetSet widgetSet;
-
- private VContextMenu contextMenu = null;
-
- private final UIConnector uIConnector;
-
- protected boolean cssLoaded = false;
-
- /** Parameters for this application connection loaded from the web-page */
- private ApplicationConfiguration configuration;
-
- private final LayoutManager layoutManager;
-
- private final RpcManager rpcManager;
-
- /** Event bus for communication events */
- private EventBus eventBus = GWT.create(SimpleEventBus.class);
-
- public enum ApplicationState {
- INITIALIZING, RUNNING, TERMINATED;
- }
-
- private ApplicationState applicationState = ApplicationState.INITIALIZING;
-
- /**
- * The communication handler methods are called at certain points during
- * communication with the server. This allows for making add-ons that keep
- * track of different aspects of the communication.
- */
- public interface CommunicationHandler extends EventHandler {
- void onRequestStarting(RequestStartingEvent e);
-
- void onResponseHandlingStarted(ResponseHandlingStartedEvent e);
-
- void onResponseHandlingEnded(ResponseHandlingEndedEvent e);
- }
-
- public static class RequestStartingEvent
- extends ApplicationConnectionEvent {
-
- public static Type<CommunicationHandler> TYPE = new Type<>();
-
- public RequestStartingEvent(ApplicationConnection connection) {
- super(connection);
- }
-
- @Override
- public Type<CommunicationHandler> getAssociatedType() {
- return TYPE;
- }
-
- @Override
- protected void dispatch(CommunicationHandler handler) {
- handler.onRequestStarting(this);
- }
- }
-
- public static class ResponseHandlingEndedEvent
- extends ApplicationConnectionEvent {
-
- public static Type<CommunicationHandler> TYPE = new Type<>();
-
- public ResponseHandlingEndedEvent(ApplicationConnection connection) {
- super(connection);
- }
-
- @Override
- public Type<CommunicationHandler> getAssociatedType() {
- return TYPE;
- }
-
- @Override
- protected void dispatch(CommunicationHandler handler) {
- handler.onResponseHandlingEnded(this);
- }
- }
-
- public static abstract class ApplicationConnectionEvent
- extends GwtEvent<CommunicationHandler> {
-
- private ApplicationConnection connection;
-
- protected ApplicationConnectionEvent(ApplicationConnection connection) {
- this.connection = connection;
- }
-
- public ApplicationConnection getConnection() {
- return connection;
- }
-
- }
-
- public static class ResponseHandlingStartedEvent
- extends ApplicationConnectionEvent {
-
- public ResponseHandlingStartedEvent(ApplicationConnection connection) {
- super(connection);
- }
-
- public static Type<CommunicationHandler> TYPE = new Type<>();
-
- @Override
- public Type<CommunicationHandler> getAssociatedType() {
- return TYPE;
- }
-
- @Override
- protected void dispatch(CommunicationHandler handler) {
- handler.onResponseHandlingStarted(this);
- }
- }
-
- /**
- * Event triggered when a application is stopped by calling
- * {@link ApplicationConnection#setApplicationRunning(false)}.
- *
- * To listen for the event add a {@link ApplicationStoppedHandler} by
- * invoking
- * {@link ApplicationConnection#addHandler(ApplicationConnection.ApplicationStoppedEvent.Type, ApplicationStoppedHandler)}
- * to the {@link ApplicationConnection}
- *
- * @since 7.1.8
- * @author Vaadin Ltd
- */
- public static class ApplicationStoppedEvent
- extends GwtEvent<ApplicationStoppedHandler> {
-
- public static Type<ApplicationStoppedHandler> TYPE = new Type<>();
-
- @Override
- public Type<ApplicationStoppedHandler> getAssociatedType() {
- return TYPE;
- }
-
- @Override
- protected void dispatch(ApplicationStoppedHandler listener) {
- listener.onApplicationStopped(this);
- }
- }
-
- /**
- * Allows custom handling of communication errors.
- */
- public interface CommunicationErrorHandler {
- /**
- * Called when a communication error has occurred. Returning
- * <code>true</code> from this method suppresses error handling.
- *
- * @param details
- * A string describing the error.
- * @param statusCode
- * The HTTP status code (e.g. 404, etc).
- * @return true if the error reporting should be suppressed, false to
- * perform normal error reporting.
- */
- public boolean onError(String details, int statusCode);
- }
-
- /**
- * A listener for listening to application stopped events. The listener can
- * be added to a {@link ApplicationConnection} by invoking
- * {@link ApplicationConnection#addHandler(ApplicationStoppedEvent.Type, ApplicationStoppedHandler)}
- *
- * @since 7.1.8
- * @author Vaadin Ltd
- */
- public interface ApplicationStoppedHandler extends EventHandler {
-
- /**
- * Triggered when the {@link ApplicationConnection} marks a previously
- * running application as stopped by invoking
- * {@link ApplicationConnection#setApplicationRunning(false)}
- *
- * @param event
- * the event triggered by the {@link ApplicationConnection}
- */
- void onApplicationStopped(ApplicationStoppedEvent event);
- }
-
- private CommunicationErrorHandler communicationErrorDelegate = null;
-
- private VLoadingIndicator loadingIndicator;
-
- private Heartbeat heartbeat = GWT.create(Heartbeat.class);
-
- private boolean tooltipInitialized = false;
-
- private final VaadinUriResolver uriResolver = new VaadinUriResolver() {
- @Override
- protected String getVaadinDirUrl() {
- return getConfiguration().getVaadinDirUrl();
- }
-
- @Override
- protected String getServiceUrlParameterName() {
- return getConfiguration().getServiceUrlParameterName();
- }
-
- @Override
- protected String getServiceUrl() {
- return getConfiguration().getServiceUrl();
- }
-
- @Override
- protected String getThemeUri() {
- return ApplicationConnection.this.getThemeUri();
- }
-
- @Override
- protected String encodeQueryStringParameterValue(String queryString) {
- return URL.encodeQueryString(queryString);
- }
- };
-
- public static class MultiStepDuration extends Duration {
- private int previousStep = elapsedMillis();
-
- public void logDuration(String message) {
- logDuration(message, 0);
- }
-
- public void logDuration(String message, int minDuration) {
- int currentTime = elapsedMillis();
- int stepDuration = currentTime - previousStep;
- if (stepDuration >= minDuration) {
- getLogger().info(message + ": " + stepDuration + " ms");
- }
- previousStep = currentTime;
- }
- }
-
- public ApplicationConnection() {
- // Assuming UI data is eagerly loaded
- ConnectorBundleLoader.get()
- .loadBundle(ConnectorBundleLoader.EAGER_BUNDLE_NAME, null);
- uIConnector = GWT.create(UIConnector.class);
- rpcManager = GWT.create(RpcManager.class);
- layoutManager = GWT.create(LayoutManager.class);
- tooltip = GWT.create(VTooltip.class);
- loadingIndicator = GWT.create(VLoadingIndicator.class);
- serverRpcQueue = GWT.create(ServerRpcQueue.class);
- connectionStateHandler = GWT.create(ConnectionStateHandler.class);
- messageHandler = GWT.create(MessageHandler.class);
- messageSender = GWT.create(MessageSender.class);
- }
-
- public void init(WidgetSet widgetSet, ApplicationConfiguration cnf) {
- getLogger().info("Starting application " + cnf.getRootPanelId());
- getLogger().info("Using theme: " + cnf.getThemeName());
-
- getLogger().info("Vaadin application servlet version: "
- + cnf.getServletVersion());
-
- if (!cnf.getServletVersion().equals(Version.getFullVersion())) {
- getLogger()
- .severe("Warning: your widget set seems to be built with a different "
- + "version than the one used on server. Unexpected "
- + "behavior may occur.");
- }
-
- this.widgetSet = widgetSet;
- configuration = cnf;
-
- layoutManager.setConnection(this);
- loadingIndicator.setConnection(this);
- serverRpcQueue.setConnection(this);
- messageHandler.setConnection(this);
- messageSender.setConnection(this);
-
- ComponentLocator componentLocator = new ComponentLocator(this);
-
- String appRootPanelName = cnf.getRootPanelId();
- // remove the end (window name) of autogenerated rootpanel id
- appRootPanelName = appRootPanelName.replaceFirst("-\\d+$", "");
-
- initializeTestbenchHooks(componentLocator, appRootPanelName);
-
- initializeClientHooks();
-
- uIConnector.init(cnf.getRootPanelId(), this);
-
- // Connection state handler preloads the reconnect dialog, which uses
- // overlay container. This in turn depends on VUI being attached
- // (done in uiConnector.init)
- connectionStateHandler.setConnection(this);
-
- tooltip.setOwner(uIConnector.getWidget());
-
- getLoadingIndicator().show();
-
- heartbeat.init(this);
-
- // Ensure the overlay container is added to the dom and set as a live
- // area for assistive devices
- Element overlayContainer = VOverlay.getOverlayContainer(this);
- Roles.getAlertRole().setAriaLiveProperty(overlayContainer,
- LiveValue.ASSERTIVE);
- VOverlay.setOverlayContainerLabel(this,
- getUIConnector().getState().overlayContainerLabel);
- Roles.getAlertRole().setAriaRelevantProperty(overlayContainer,
- RelevantValue.ADDITIONS);
- }
-
- /**
- * Starts this application. Don't call this method directly - it's called by
- * {@link ApplicationConfiguration#startNextApplication()}, which should be
- * called once this application has started (first response received) or
- * failed to start. This ensures that the applications are started in order,
- * to avoid session-id problems.
- *
- */
- public void start() {
- String jsonText = configuration.getUIDL();
- if (jsonText == null) {
- // initial UIDL not in DOM, request from server
- getMessageSender().resynchronize();
- } else {
- // initial UIDL provided in DOM, continue as if returned by request
-
- // Hack to avoid logging an error in endRequest()
- getMessageSender().startRequest();
- getMessageHandler()
- .handleMessage(MessageHandler.parseJson(jsonText));
- }
-
- // Tooltip can't be created earlier because the
- // necessary fields are not setup to add it in the
- // correct place in the DOM
- if (!tooltipInitialized) {
- tooltipInitialized = true;
- ApplicationConfiguration.runWhenDependenciesLoaded(new Command() {
- @Override
- public void execute() {
- getVTooltip().initializeAssistiveTooltips();
- }
- });
- }
- }
-
- /**
- * Checks if there is some work to be done on the client side
- *
- * @return true if the client has some work to be done, false otherwise
- */
- private boolean isActive() {
- return !getMessageHandler().isInitialUidlHandled() || isWorkPending()
- || getMessageSender().hasActiveRequest()
- || isExecutingDeferredCommands();
- }
-
- private native void initializeTestbenchHooks(
- ComponentLocator componentLocator, String TTAppId)
- /*-{
- var ap = this;
- var client = {};
- client.isActive = $entry(function() {
- return ap.@com.vaadin.client.ApplicationConnection::isActive()();
- });
- var vi = ap.@com.vaadin.client.ApplicationConnection::getVersionInfo()();
- if (vi) {
- client.getVersionInfo = function() {
- return vi;
- }
- }
-
- client.getProfilingData = $entry(function() {
- var smh = ap.@com.vaadin.client.ApplicationConnection::getMessageHandler()();
- var pd = [
- smh.@com.vaadin.client.communication.MessageHandler::lastProcessingTime,
- smh.@com.vaadin.client.communication.MessageHandler::totalProcessingTime
- ];
- if (null != smh.@com.vaadin.client.communication.MessageHandler::serverTimingInfo) {
- pd = pd.concat(smh.@com.vaadin.client.communication.MessageHandler::serverTimingInfo);
- } else {
- pd = pd.concat(-1, -1);
- }
- pd[pd.length] = smh.@com.vaadin.client.communication.MessageHandler::bootstrapTime;
- return pd;
- });
-
- client.getElementByPath = $entry(function(id) {
- return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getElementByPath(Ljava/lang/String;)(id);
- });
- client.getElementByPathStartingAt = $entry(function(id, element) {
- return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getElementByPathStartingAt(Ljava/lang/String;Lcom/google/gwt/dom/client/Element;)(id, element);
- });
- client.getElementsByPath = $entry(function(id) {
- return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getElementsByPath(Ljava/lang/String;)(id);
- });
- client.getElementsByPathStartingAt = $entry(function(id, element) {
- return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getElementsByPathStartingAt(Ljava/lang/String;Lcom/google/gwt/dom/client/Element;)(id, element);
- });
- client.getPathForElement = $entry(function(element) {
- return componentLocator.@com.vaadin.client.componentlocator.ComponentLocator::getPathForElement(Lcom/google/gwt/dom/client/Element;)(element);
- });
- client.initializing = false;
-
- $wnd.vaadin.clients[TTAppId] = client;
- }-*/;
-
- /**
- * Helper for tt initialization
- */
- private JavaScriptObject getVersionInfo() {
- return configuration.getVersionInfoJSObject();
- }
-
- /**
- * Publishes a JavaScript API for mash-up applications.
- * <ul>
- * <li><code>vaadin.forceSync()</code> sends pending variable changes, in
- * effect synchronizing the server and client state. This is done for all
- * applications on host page.</li>
- * <li><code>vaadin.postRequestHooks</code> is a map of functions which gets
- * called after each XHR made by vaadin application. Note, that it is
- * attaching js functions responsibility to create the variable like this:
- *
- * <code><pre>
- * if(!vaadin.postRequestHooks) {vaadin.postRequestHooks = new Object();}
- * postRequestHooks.myHook = function(appId) {
- * if(appId == "MyAppOfInterest") {
- * // do the staff you need on xhr activity
- * }
- * }
- * </pre></code> First parameter passed to these functions is the identifier
- * of Vaadin application that made the request.
- * </ul>
- *
- * TODO make this multi-app aware
- */
- private native void initializeClientHooks()
- /*-{
- var app = this;
- var oldSync;
- if ($wnd.vaadin.forceSync) {
- oldSync = $wnd.vaadin.forceSync;
- }
- $wnd.vaadin.forceSync = $entry(function() {
- if (oldSync) {
- oldSync();
- }
- var sender = app.@com.vaadin.client.ApplicationConnection::messageSender;
- sender.@com.vaadin.client.communication.MessageSender::resynchronize()();
- });
- var oldForceLayout;
- if ($wnd.vaadin.forceLayout) {
- oldForceLayout = $wnd.vaadin.forceLayout;
- }
- $wnd.vaadin.forceLayout = $entry(function() {
- if (oldForceLayout) {
- oldForceLayout();
- }
- app.@com.vaadin.client.ApplicationConnection::forceLayout()();
- });
- }-*/;
-
- /**
- * Requests an analyze of layouts, to find inconsistencies. Exclusively used
- * for debugging during development.
- *
- * @deprecated as of 7.1. Replaced by {@link UIConnector#analyzeLayouts()}
- */
- @Deprecated
- public void analyzeLayouts() {
- getUIConnector().analyzeLayouts();
- }
-
- /**
- * Sends a request to the server to print details to console that will help
- * the developer to locate the corresponding server-side connector in the
- * source code.
- *
- * @param serverConnector
- * @deprecated as of 7.1. Replaced by
- * {@link UIConnector#showServerDebugInfo(ServerConnector)}
- */
- @Deprecated
- void highlightConnector(ServerConnector serverConnector) {
- getUIConnector().showServerDebugInfo(serverConnector);
- }
-
- int cssWaits = 0;
-
- protected ServerRpcQueue serverRpcQueue;
- protected ConnectionStateHandler connectionStateHandler;
- protected MessageHandler messageHandler;
- protected MessageSender messageSender;
-
- static final int MAX_CSS_WAITS = 100;
-
- public void executeWhenCSSLoaded(final Command c) {
- if (!isCSSLoaded() && cssWaits < MAX_CSS_WAITS) {
- (new Timer() {
- @Override
- public void run() {
- executeWhenCSSLoaded(c);
- }
- }).schedule(50);
-
- // Show this message just once
- if (cssWaits++ == 0) {
- getLogger().warning("Assuming CSS loading is not complete, "
- + "postponing render phase. "
- + "(.v-loading-indicator height == 0)");
- }
- } else {
- cssLoaded = true;
- if (cssWaits >= MAX_CSS_WAITS) {
- getLogger().severe("CSS files may have not loaded properly.");
- }
-
- c.execute();
- }
- }
-
- /**
- * Checks whether or not the CSS is loaded. By default checks the size of
- * the loading indicator element.
- *
- * @return
- */
- protected boolean isCSSLoaded() {
- return cssLoaded
- || getLoadingIndicator().getElement().getOffsetHeight() != 0;
- }
-
- /**
- * Shows the communication error notification.
- *
- * @param details
- * Optional details.
- * @param statusCode
- * The status code returned for the request
- *
- */
- public void showCommunicationError(String details, int statusCode) {
- getLogger().severe("Communication error: " + details);
- showError(details, configuration.getCommunicationError());
- }
-
- /**
- * Shows the authentication error notification.
- *
- * @param details
- * Optional details.
- */
- public void showAuthenticationError(String details) {
- getLogger().severe("Authentication error: " + details);
- showError(details, configuration.getAuthorizationError());
- }
-
- /**
- * Shows the session expiration notification.
- *
- * @param details
- * Optional details.
- */
- public void showSessionExpiredError(String details) {
- getLogger().severe("Session expired: " + details);
- showError(details, configuration.getSessionExpiredError());
- }
-
- /**
- * Shows an error notification.
- *
- * @param details
- * Optional details.
- * @param message
- * An ErrorMessage describing the error.
- */
- protected void showError(String details, ErrorMessage message) {
- VNotification.showError(this, message.getCaption(),
- message.getMessage(), details, message.getUrl());
- }
-
- /**
- * Checks if the client has running or scheduled commands
- */
- private boolean isWorkPending() {
- ConnectorMap connectorMap = getConnectorMap();
- JsArrayObject<ServerConnector> connectors = connectorMap
- .getConnectorsAsJsArray();
- int size = connectors.size();
- for (int i = 0; i < size; i++) {
- ServerConnector conn = connectors.get(i);
- if (isWorkPending(conn)) {
- return true;
- }
-
- if (conn instanceof ComponentConnector) {
- ComponentConnector compConn = (ComponentConnector) conn;
- if (isWorkPending(compConn.getWidget())) {
- return true;
- }
- }
- }
- return false;
- }
-
- private static boolean isWorkPending(Object object) {
- return object instanceof DeferredWorker
- && ((DeferredWorker) object).isWorkPending();
- }
-
- /**
- * Checks if deferred commands are (potentially) still being executed as a
- * result of an update from the server. Returns true if a deferred command
- * might still be executing, false otherwise. This will not work correctly
- * if a deferred command is added in another deferred command.
- * <p>
- * Used by the native "client.isActive" function.
- * </p>
- *
- * @return true if deferred commands are (potentially) being executed, false
- * otherwise
- */
- private boolean isExecutingDeferredCommands() {
- Scheduler s = Scheduler.get();
- if (s instanceof VSchedulerImpl) {
- return ((VSchedulerImpl) s).hasWorkQueued();
- } else {
- return false;
- }
- }
-
- /**
- * Returns the loading indicator used by this ApplicationConnection
- *
- * @return The loading indicator for this ApplicationConnection
- */
- public VLoadingIndicator getLoadingIndicator() {
- return loadingIndicator;
- }
-
- /**
- * Determines whether or not the loading indicator is showing.
- *
- * @return true if the loading indicator is visible
- * @deprecated As of 7.1. Use {@link #getLoadingIndicator()} and
- * {@link VLoadingIndicator#isVisible()}.isVisible() instead.
- */
- @Deprecated
- public boolean isLoadingIndicatorVisible() {
- return getLoadingIndicator().isVisible();
- }
-
- public void loadStyleDependencies(JsArrayString dependencies) {
- // Assuming no reason to interpret in a defined order
- ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
- @Override
- public void onLoad(ResourceLoadEvent event) {
- ApplicationConfiguration.endDependencyLoading();
- }
-
- @Override
- public void onError(ResourceLoadEvent event) {
- getLogger().severe(event.getResourceUrl()
- + " could not be loaded, or the load detection failed because the stylesheet is empty.");
- // The show must go on
- onLoad(event);
- }
- };
- ResourceLoader loader = ResourceLoader.get();
- for (int i = 0; i < dependencies.length(); i++) {
- String url = translateVaadinUri(dependencies.get(i));
- ApplicationConfiguration.startDependencyLoading();
- loader.loadStylesheet(url, resourceLoadListener);
- }
- }
-
- public void loadScriptDependencies(final JsArrayString dependencies) {
- if (dependencies.length() == 0) {
- return;
- }
-
- // Listener that loads the next when one is completed
- ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
- @Override
- public void onLoad(ResourceLoadEvent event) {
- if (dependencies.length() != 0) {
- String url = translateVaadinUri(dependencies.shift());
- ApplicationConfiguration.startDependencyLoading();
- // Load next in chain (hopefully already preloaded)
- event.getResourceLoader().loadScript(url, this);
- }
- // Call start for next before calling end for current
- ApplicationConfiguration.endDependencyLoading();
- }
-
- @Override
- public void onError(ResourceLoadEvent event) {
- getLogger().severe(
- event.getResourceUrl() + " could not be loaded.");
- // The show must go on
- onLoad(event);
- }
- };
-
- ResourceLoader loader = ResourceLoader.get();
-
- // Start chain by loading first
- String url = translateVaadinUri(dependencies.shift());
- ApplicationConfiguration.startDependencyLoading();
- loader.loadScript(url, resourceLoadListener);
-
- if (ResourceLoader.supportsInOrderScriptExecution()) {
- for (int i = 0; i < dependencies.length(); i++) {
- String preloadUrl = translateVaadinUri(dependencies.get(i));
- loader.loadScript(preloadUrl, null);
- }
- } else {
- // Preload all remaining
- for (int i = 0; i < dependencies.length(); i++) {
- String preloadUrl = translateVaadinUri(dependencies.get(i));
- loader.preloadResource(preloadUrl, null);
- }
- }
- }
-
- private void addVariableToQueue(String connectorId, String variableName,
- Object value, boolean immediate) {
- boolean lastOnly = !immediate;
- // note that type is now deduced from value
- serverRpcQueue.add(new LegacyChangeVariablesInvocation(connectorId,
- variableName, value), lastOnly);
- if (immediate) {
- serverRpcQueue.flush();
- }
- }
-
- /**
- * @deprecated as of 7.6, use {@link ServerRpcQueue#flush()}
- */
- @Deprecated
- public void sendPendingVariableChanges() {
- serverRpcQueue.flush();
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * </p>
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param newValue
- * the new value to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
- public void updateVariable(String paintableId, String variableName,
- ServerConnector newValue, boolean immediate) {
- addVariableToQueue(paintableId, variableName, newValue, immediate);
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * </p>
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param newValue
- * the new value to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
-
- public void updateVariable(String paintableId, String variableName,
- String newValue, boolean immediate) {
- addVariableToQueue(paintableId, variableName, newValue, immediate);
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * </p>
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param newValue
- * the new value to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
-
- public void updateVariable(String paintableId, String variableName,
- int newValue, boolean immediate) {
- addVariableToQueue(paintableId, variableName, newValue, immediate);
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * </p>
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param newValue
- * the new value to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
-
- public void updateVariable(String paintableId, String variableName,
- long newValue, boolean immediate) {
- addVariableToQueue(paintableId, variableName, newValue, immediate);
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * </p>
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param newValue
- * the new value to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
-
- public void updateVariable(String paintableId, String variableName,
- float newValue, boolean immediate) {
- addVariableToQueue(paintableId, variableName, newValue, immediate);
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * </p>
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param newValue
- * the new value to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
-
- public void updateVariable(String paintableId, String variableName,
- double newValue, boolean immediate) {
- addVariableToQueue(paintableId, variableName, newValue, immediate);
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * </p>
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param newValue
- * the new value to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
-
- public void updateVariable(String paintableId, String variableName,
- boolean newValue, boolean immediate) {
- addVariableToQueue(paintableId, variableName, newValue, immediate);
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * </p>
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param map
- * the new values to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
- public void updateVariable(String paintableId, String variableName,
- Map<String, Object> map, boolean immediate) {
- addVariableToQueue(paintableId, variableName, map, immediate);
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * <p>
- * A null array is sent as an empty array.
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param values
- * the new value to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
- public void updateVariable(String paintableId, String variableName,
- String[] values, boolean immediate) {
- addVariableToQueue(paintableId, variableName, values, immediate);
- }
-
- /**
- * Sends a new value for the given paintables given variable to the server.
- * <p>
- * The update is actually queued to be sent at a suitable time. If immediate
- * is true, the update is sent as soon as possible. If immediate is false,
- * the update will be sent along with the next immediate update.
- * <p>
- * A null array is sent as an empty array.
- *
- * @param paintableId
- * the id of the paintable that owns the variable
- * @param variableName
- * the name of the variable
- * @param values
- * the new value to be sent
- * @param immediate
- * true if the update is to be sent as soon as possible
- */
- public void updateVariable(String paintableId, String variableName,
- Object[] values, boolean immediate) {
- addVariableToQueue(paintableId, variableName, values, immediate);
- }
-
- /**
- * Does absolutely nothing. Replaced by {@link LayoutManager}.
- *
- * @param container
- * @deprecated As of 7.0, serves no purpose
- */
- @Deprecated
- public void runDescendentsLayout(HasWidgets container) {
- }
-
- /**
- * This will cause re-layouting of all components. Mainly used for
- * development. Published to JavaScript.
- */
- public void forceLayout() {
- Duration duration = new Duration();
-
- layoutManager.forceLayout();
-
- getLogger().info("forceLayout in " + duration.elapsedMillis() + " ms");
- }
-
- /**
- * Returns false
- *
- * @param paintable
- * @return false, always
- * @deprecated As of 7.0, serves no purpose
- */
- @Deprecated
- private boolean handleComponentRelativeSize(ComponentConnector paintable) {
- return false;
- }
-
- /**
- * Returns false
- *
- * @param paintable
- * @return false, always
- * @deprecated As of 7.0, serves no purpose
- */
- @Deprecated
- public boolean handleComponentRelativeSize(Widget widget) {
- return handleComponentRelativeSize(connectorMap.getConnector(widget));
-
- }
-
- @Deprecated
- public ComponentConnector getPaintable(UIDL uidl) {
- // Non-component connectors shouldn't be painted from legacy connectors
- return (ComponentConnector) getConnector(uidl.getId(),
- Integer.parseInt(uidl.getTag()));
- }
-
- /**
- * Get either an existing ComponentConnector or create a new
- * ComponentConnector with the given type and id.
- *
- * If a ComponentConnector with the given id already exists, returns it.
- * Otherwise creates and registers a new ComponentConnector of the given
- * type.
- *
- * @param connectorId
- * Id of the paintable
- * @param connectorType
- * Type of the connector, as passed from the server side
- *
- * @return Either an existing ComponentConnector or a new ComponentConnector
- * of the given type
- */
- public ServerConnector getConnector(String connectorId, int connectorType) {
- if (!connectorMap.hasConnector(connectorId)) {
- return createAndRegisterConnector(connectorId, connectorType);
- }
- return connectorMap.getConnector(connectorId);
- }
-
- /**
- * Creates a new ServerConnector with the given type and id.
- *
- * Creates and registers a new ServerConnector of the given type. Should
- * never be called with the connector id of an existing connector.
- *
- * @param connectorId
- * Id of the new connector
- * @param connectorType
- * Type of the connector, as passed from the server side
- *
- * @return A new ServerConnector of the given type
- */
- private ServerConnector createAndRegisterConnector(String connectorId,
- int connectorType) {
- Profiler.enter("ApplicationConnection.createAndRegisterConnector");
-
- // Create and register a new connector with the given type
- ServerConnector p = widgetSet.createConnector(connectorType,
- configuration);
- connectorMap.registerConnector(connectorId, p);
- p.doInit(connectorId, this);
-
- Profiler.leave("ApplicationConnection.createAndRegisterConnector");
- return p;
- }
-
- /**
- * Gets a resource that has been pre-loaded via UIDL, such as custom
- * layouts.
- *
- * @param name
- * identifier of the resource to get
- * @return the resource
- */
- public String getResource(String name) {
- return resourcesMap.get(name);
- }
-
- /**
- * Sets a resource that has been pre-loaded via UIDL, such as custom
- * layouts.
- *
- * @since 7.6
- * @param name
- * identifier of the resource to Set
- * @param resource
- * the resource
- */
- public void setResource(String name, String resource) {
- resourcesMap.put(name, resource);
- }
-
- /**
- * Singleton method to get instance of app's context menu.
- *
- * @return VContextMenu object
- */
- public VContextMenu getContextMenu() {
- if (contextMenu == null) {
- contextMenu = new VContextMenu();
- contextMenu.setOwner(uIConnector.getWidget());
- DOM.setElementProperty(contextMenu.getElement(), "id",
- "PID_VAADIN_CM");
- }
- return contextMenu;
- }
-
- /**
- * Gets an {@link Icon} instance corresponding to a URI.
- *
- * @since 7.2
- * @param uri
- * @return Icon object
- */
- public Icon getIcon(String uri) {
- Icon icon;
- if (uri == null) {
- return null;
- } else if (FontIcon.isFontIconUri(uri)) {
- icon = GWT.create(FontIcon.class);
- } else {
- icon = GWT.create(ImageIcon.class);
- }
- icon.setUri(translateVaadinUri(uri));
- return icon;
- }
-
- /**
- * Translates custom protocols in UIDL URI's to be recognizable by browser.
- * All uri's from UIDL should be routed via this method before giving them
- * to browser due URI's in UIDL may contain custom protocols like theme://.
- *
- * @param uidlUri
- * Vaadin URI from uidl
- * @return translated URI ready for browser
- */
- public String translateVaadinUri(String uidlUri) {
- return uriResolver.resolveVaadinUri(uidlUri);
- }
-
- /**
- * Gets the URI for the current theme. Can be used to reference theme
- * resources.
- *
- * @return URI to the current theme
- */
- public String getThemeUri() {
- return configuration.getVaadinDirUrl() + "themes/"
- + getUIConnector().getActiveTheme();
- }
-
- /* Extended title handling */
-
- private final VTooltip tooltip;
-
- private ConnectorMap connectorMap = GWT.create(ConnectorMap.class);
-
- /**
- * Use to notify that the given component's caption has changed; layouts may
- * have to be recalculated.
- *
- * @param component
- * the Paintable whose caption has changed
- * @deprecated As of 7.0.2, has not had any effect for a long time
- */
- @Deprecated
- public void captionSizeUpdated(Widget widget) {
- // This doesn't do anything, it's just kept here for compatibility
- }
-
- /**
- * Gets the main view
- *
- * @return the main view
- */
- public UIConnector getUIConnector() {
- return uIConnector;
- }
-
- /**
- * Gets the {@link ApplicationConfiguration} for the current application.
- *
- * @see ApplicationConfiguration
- * @return the configuration for this application
- */
- public ApplicationConfiguration getConfiguration() {
- return configuration;
- }
-
- /**
- * Checks if there is a registered server side listener for the event. The
- * list of events which has server side listeners is updated automatically
- * before the component is updated so the value is correct if called from
- * updatedFromUIDL.
- *
- * @param connector
- * The connector to register event listeners for
- * @param eventIdentifier
- * The identifier for the event
- * @return true if at least one listener has been registered on server side
- * for the event identified by eventIdentifier.
- * @deprecated As of 7.0. Use
- * {@link AbstractConnector#hasEventListener(String)} instead
- */
- @Deprecated
- public boolean hasEventListeners(ComponentConnector connector,
- String eventIdentifier) {
- return connector.hasEventListener(eventIdentifier);
- }
-
- /**
- * Adds the get parameters to the uri and returns the new uri that contains
- * the parameters.
- *
- * @param uri
- * The uri to which the parameters should be added.
- * @param extraParams
- * One or more parameters in the format "a=b" or "c=d&e=f". An
- * empty string is allowed but will not modify the url.
- * @return The modified URI with the get parameters in extraParams added.
- * @deprecated Use {@link SharedUtil#addGetParameters(String,String)}
- * instead
- */
- @Deprecated
- public static String addGetParameters(String uri, String extraParams) {
- return SharedUtil.addGetParameters(uri, extraParams);
- }
-
- ConnectorMap getConnectorMap() {
- return connectorMap;
- }
-
- /**
- * @deprecated As of 7.0. No longer serves any purpose.
- */
- @Deprecated
- public void unregisterPaintable(ServerConnector p) {
- getLogger().info("unregisterPaintable (unnecessarily) called for "
- + Util.getConnectorString(p));
- }
-
- /**
- * Get VTooltip instance related to application connection
- *
- * @return VTooltip instance
- */
- public VTooltip getVTooltip() {
- return tooltip;
- }
-
- /**
- * Method provided for backwards compatibility. Duties previously done by
- * this method is now handled by the state change event handler in
- * AbstractComponentConnector. The only function this method has is to
- * return true if the UIDL is a "cached" update.
- *
- * @param component
- * @param uidl
- * @param manageCaption
- * @deprecated As of 7.0, no longer serves any purpose
- * @return
- */
- @Deprecated
- public boolean updateComponent(Widget component, UIDL uidl,
- boolean manageCaption) {
- ComponentConnector connector = getConnectorMap()
- .getConnector(component);
- if (!AbstractComponentConnector.isRealUpdate(uidl)) {
- return true;
- }
-
- if (!manageCaption) {
- getLogger().warning(Util.getConnectorString(connector)
- + " 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.");
- }
- return false;
- }
-
- /**
- * @deprecated As of 7.0. Use
- * {@link AbstractComponentConnector#hasEventListener(String)}
- * instead
- */
- @Deprecated
- public boolean hasEventListeners(Widget widget, String eventIdentifier) {
- ComponentConnector connector = getConnectorMap().getConnector(widget);
- if (connector == null) {
- /*
- * No connector will exist in cases where Vaadin widgets have been
- * re-used without implementing server<->client communication.
- */
- return false;
- }
-
- return hasEventListeners(getConnectorMap().getConnector(widget),
- eventIdentifier);
- }
-
- LayoutManager getLayoutManager() {
- return layoutManager;
- }
-
- /**
- * Schedules a heartbeat request to occur after the configured heartbeat
- * interval elapses if the interval is a positive number. Otherwise, does
- * nothing.
- *
- * @deprecated as of 7.2, use {@link Heartbeat#schedule()} instead
- */
- @Deprecated
- protected void scheduleHeartbeat() {
- heartbeat.schedule();
- }
-
- /**
- * Sends a heartbeat request to the server.
- * <p>
- * Heartbeat requests are used to inform the server that the client-side is
- * still alive. If the client page is closed or the connection lost, the
- * server will eventually close the inactive UI.
- *
- * @deprecated as of 7.2, use {@link Heartbeat#send()} instead
- */
- @Deprecated
- protected void sendHeartbeat() {
- heartbeat.send();
- }
-
- public void handleCommunicationError(String details, int statusCode) {
- boolean handled = false;
- if (communicationErrorDelegate != null) {
- handled = communicationErrorDelegate.onError(details, statusCode);
-
- }
-
- if (!handled) {
- showCommunicationError(details, statusCode);
- }
-
- }
-
- /**
- * Sets the delegate that is called whenever a communication error occurrs.
- *
- * @param delegate
- * the delegate.
- */
- public void setCommunicationErrorDelegate(
- CommunicationErrorHandler delegate) {
- communicationErrorDelegate = delegate;
- }
-
- public void setApplicationRunning(boolean applicationRunning) {
- if (getApplicationState() == ApplicationState.TERMINATED) {
- if (applicationRunning) {
- getLogger().severe(
- "Tried to restart a terminated application. This is not supported");
- } else {
- getLogger().warning(
- "Tried to stop a terminated application. This should not be done");
- }
- return;
- } else if (getApplicationState() == ApplicationState.INITIALIZING) {
- if (applicationRunning) {
- applicationState = ApplicationState.RUNNING;
- } else {
- getLogger().warning(
- "Tried to stop the application before it has started. This should not be done");
- }
- } else if (getApplicationState() == ApplicationState.RUNNING) {
- if (!applicationRunning) {
- applicationState = ApplicationState.TERMINATED;
- eventBus.fireEvent(new ApplicationStoppedEvent());
- } else {
- getLogger().warning(
- "Tried to start an already running application. This should not be done");
- }
- }
- }
-
- /**
- * Checks if the application is in the {@link ApplicationState#RUNNING}
- * state.
- *
- * @since 7.6
- * @return true if the application is in the running state, false otherwise
- */
- public boolean isApplicationRunning() {
- return applicationState == ApplicationState.RUNNING;
- }
-
- public <H extends EventHandler> HandlerRegistration addHandler(
- GwtEvent.Type<H> type, H handler) {
- return eventBus.addHandler(type, handler);
- }
-
- @Override
- public void fireEvent(GwtEvent<?> event) {
- eventBus.fireEvent(event);
- }
-
- /**
- * Calls {@link ComponentConnector#flush()} on the active connector. Does
- * nothing if there is no active (focused) connector.
- */
- public void flushActiveConnector() {
- ComponentConnector activeConnector = getActiveConnector();
- if (activeConnector == null) {
- return;
- }
- activeConnector.flush();
- }
-
- /**
- * Gets the active connector for focused element in browser.
- *
- * @return Connector for focused element or null.
- */
- private ComponentConnector getActiveConnector() {
- Element focusedElement = WidgetUtil.getFocusedElement();
- if (focusedElement == null) {
- return null;
- }
- return Util.getConnectorForElement(this, getUIConnector().getWidget(),
- focusedElement);
- }
-
- private static Logger getLogger() {
- return Logger.getLogger(ApplicationConnection.class.getName());
- }
-
- /**
- * Returns the hearbeat instance.
- */
- public Heartbeat getHeartbeat() {
- return heartbeat;
- }
-
- /**
- * Returns the state of this application. An application state goes from
- * "initializing" to "running" to "stopped". There is no way for an
- * application to go back to a previous state, i.e. a stopped application
- * can never be re-started
- *
- * @since 7.6
- * @return the current state of this application
- */
- public ApplicationState getApplicationState() {
- return applicationState;
- }
-
- /**
- * Gets the server RPC queue for this application
- *
- * @since 7.6
- * @return the server RPC queue
- */
- public ServerRpcQueue getServerRpcQueue() {
- return serverRpcQueue;
- }
-
- /**
- * Gets the communication error handler for this application
- *
- * @since 7.6
- * @return the server RPC queue
- */
- public ConnectionStateHandler getConnectionStateHandler() {
- return connectionStateHandler;
- }
-
- /**
- * Gets the (server to client) message handler for this application
- *
- * @since 7.6
- * @return the message handler
- */
- public MessageHandler getMessageHandler() {
- return messageHandler;
- }
-
- /**
- * Gets the server rpc manager for this application
- *
- * @since 7.6
- * @return the server rpc manager
- */
- public RpcManager getRpcManager() {
- return rpcManager;
- }
-
- /**
- * Gets the (client to server) message sender for this application
- *
- * @since 7.6
- * @return the message sender
- */
- public MessageSender getMessageSender() {
- return messageSender;
- }
-
- /**
- * @since 7.6
- * @return the widget set
- */
- public WidgetSet getWidgetSet() {
- return widgetSet;
- }
-
- public int getLastSeenServerSyncId() {
- return getMessageHandler().getLastSeenServerSyncId();
- }
-
- }
|