12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123 |
- /*
- * Copyright 2000-2014 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.Date;
- 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.Request;
- import com.google.gwt.http.client.RequestBuilder;
- import com.google.gwt.http.client.RequestCallback;
- import com.google.gwt.http.client.RequestException;
- import com.google.gwt.http.client.Response;
- 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.Window;
- import com.google.gwt.user.client.Window.ClosingEvent;
- import com.google.gwt.user.client.Window.ClosingHandler;
- 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.ApplicationConnection.ApplicationStoppedEvent;
- import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
- import com.vaadin.client.ResourceLoader.ResourceLoadListener;
- import com.vaadin.client.communication.CommunicationProblemEvent;
- import com.vaadin.client.communication.CommunicationProblemHandler;
- import com.vaadin.client.communication.Heartbeat;
- import com.vaadin.client.communication.PushConnection;
- import com.vaadin.client.communication.RpcManager;
- import com.vaadin.client.communication.ServerMessageHandler;
- 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.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.AbstractComponentState;
- import com.vaadin.shared.ApplicationConstants;
- import com.vaadin.shared.JsonConstants;
- import com.vaadin.shared.VaadinUriResolver;
- import com.vaadin.shared.Version;
- import com.vaadin.shared.communication.LegacyChangeVariablesInvocation;
- import com.vaadin.shared.ui.ui.UIConstants;
- import com.vaadin.shared.ui.ui.UIState.PushConfigurationState;
- import com.vaadin.shared.util.SharedUtil;
-
- import elemental.json.Json;
- import elemental.json.JsonArray;
- import elemental.json.JsonObject;
-
- /**
- * 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 String JSON_COMMUNICATION_PREFIX = "for(;;);[";
- private final String JSON_COMMUNICATION_SUFFIX = "]";
-
- private final HashMap<String, String> resourcesMap = new HashMap<String, String>();
-
- private WidgetSet widgetSet;
-
- private VContextMenu contextMenu = null;
-
- private final UIConnector uIConnector;
-
- private boolean hasActiveRequest = false;
-
- /**
- * Webkit will ignore outgoing requests while waiting for a response to a
- * navigation event (indicated by a beforeunload event). When this happens,
- * we should keep trying to send the request every now and then until there
- * is a response or until it throws an exception saying that it is already
- * being sent.
- */
- private boolean webkitMaybeIgnoringRequests = false;
-
- protected boolean cssLoaded = false;
-
- /** Parameters for this application connection loaded from the web-page */
- private ApplicationConfiguration configuration;
-
- private Date requestStartTime;
-
- private final LayoutManager layoutManager;
-
- private final RpcManager rpcManager;
-
- private PushConnection push;
-
- /** Event bus for communication events */
- private EventBus eventBus = GWT.create(SimpleEventBus.class);
-
- public enum State {
- INITIALIZING, RUNNING, TERMINATED;
- }
-
- private State state = State.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<CommunicationHandler>();
-
- 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<CommunicationHandler>();
-
- 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<CommunicationHandler>();
-
- @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<ApplicationStoppedHandler>();
-
- @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);
- layoutManager.setConnection(this);
- tooltip = GWT.create(VTooltip.class);
- loadingIndicator = GWT.create(VLoadingIndicator.class);
- loadingIndicator.setConnection(this);
- serverRpcQueue = GWT.create(ServerRpcQueue.class);
- serverRpcQueue.setConnection(this);
- communicationProblemHandler = GWT
- .create(CommunicationProblemHandler.class);
- communicationProblemHandler.setConnection(this);
- serverMessageHandler = GWT.create(ServerMessageHandler.class);
- serverMessageHandler.setConnection(this);
- }
-
- 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;
-
- 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);
-
- tooltip.setOwner(uIConnector.getWidget());
-
- getLoadingIndicator().show();
-
- heartbeat.init(this);
-
- Window.addWindowClosingHandler(new ClosingHandler() {
- @Override
- public void onWindowClosing(ClosingEvent event) {
- webkitMaybeIgnoringRequests = true;
- }
- });
-
- // 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) {
- // inital UIDL not in DOM, request later
- repaintAll();
- } else {
- // initial UIDL provided in DOM, continue as if returned by request
- // Hack to avoid logging an error in endRequest()
- startRequest();
- handleJSONText(jsonText, -1);
- }
-
- // 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 !getServerMessageHandler().isInitialUidlHandled()
- || isWorkPending() || 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::getServerMessageHandler();
- var pd = [
- smh.@com.vaadin.client.communication.ServerMessageHandler::lastProcessingTime,
- smh.@com.vaadin.client.communication.ServerMessageHandler::totalProcessingTime
- ];
- pd = pd.concat(smh.@com.vaadin.client.communication.ServerMessageHandler::serverTimingInfo);
- pd[pd.length] = smh.@com.vaadin.client.communication.ServerMessageHandler::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();
- }
- app.@com.vaadin.client.ApplicationConnection::sendPendingVariableChanges()();
- });
- var oldForceLayout;
- if ($wnd.vaadin.forceLayout) {
- oldForceLayout = $wnd.vaadin.forceLayout;
- }
- $wnd.vaadin.forceLayout = $entry(function() {
- if (oldForceLayout) {
- oldForceLayout();
- }
- app.@com.vaadin.client.ApplicationConnection::forceLayout()();
- });
- }-*/;
-
- /**
- * Runs possibly registered client side post request hooks. This is expected
- * to be run after each uidl request made by Vaadin application.
- *
- * @param appId
- */
- private static native void runPostRequestHooks(String appId)
- /*-{
- if ($wnd.vaadin.postRequestHooks) {
- for ( var hook in $wnd.vaadin.postRequestHooks) {
- if (typeof ($wnd.vaadin.postRequestHooks[hook]) == "function") {
- try {
- $wnd.vaadin.postRequestHooks[hook](appId);
- } catch (e) {
- }
- }
- }
- }
- }-*/;
-
- /**
- * If on Liferay and logged in, ask the client side session management
- * JavaScript to extend the session duration.
- *
- * Otherwise, Liferay client side JavaScript will explicitly expire the
- * session even though the server side considers the session to be active.
- * See ticket #8305 for more information.
- */
- protected native void extendLiferaySession()
- /*-{
- if ($wnd.Liferay && $wnd.Liferay.Session) {
- $wnd.Liferay.Session.extend();
- // if the extend banner is visible, hide it
- if ($wnd.Liferay.Session.banner) {
- $wnd.Liferay.Session.banner.remove();
- }
- }
- }-*/;
-
- /**
- * Indicates whether or not there are currently active UIDL requests. Used
- * internally to sequence requests properly, seldom needed in Widgets.
- *
- * @return true if there are active requests
- */
- public boolean hasActiveRequest() {
- return hasActiveRequest;
- }
-
- private String getRepaintAllParameters() {
- String parameters = ApplicationConstants.URL_PARAMETER_REPAINT_ALL
- + "=1";
- return parameters;
- }
-
- public void repaintAll() {
- makeUidlRequest(Json.createArray(), getRepaintAllParameters());
- }
-
- /**
- * 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);
- }
-
- /**
- * Makes an UIDL request to the server.
- *
- * @param reqInvocations
- * Data containing RPC invocations and all related information.
- * @param extraParams
- * Parameters that are added as GET parameters to the url.
- * Contains key=value pairs joined by & characters or is empty if
- * no parameters should be added. Should not start with any
- * special character.
- */
- protected void makeUidlRequest(final JsonArray reqInvocations,
- final String extraParams) {
- startRequest();
-
- JsonObject payload = Json.createObject();
- String csrfToken = getServerMessageHandler().getCsrfToken();
- if (!csrfToken.equals(ApplicationConstants.CSRF_TOKEN_DEFAULT_VALUE)) {
- payload.put(ApplicationConstants.CSRF_TOKEN, csrfToken);
- }
- payload.put(ApplicationConstants.RPC_INVOCATIONS, reqInvocations);
- payload.put(ApplicationConstants.SERVER_SYNC_ID,
- getServerMessageHandler().getLastSeenServerSyncId());
-
- getLogger()
- .info("Making UIDL Request with params: " + payload.toJson());
- String uri = translateVaadinUri(ApplicationConstants.APP_PROTOCOL_PREFIX
- + ApplicationConstants.UIDL_PATH + '/');
-
- if (extraParams != null && extraParams.length() > 0) {
- if (extraParams.equals(getRepaintAllParameters())) {
- payload.put(ApplicationConstants.RESYNCHRONIZE_ID, true);
- } else {
- uri = SharedUtil.addGetParameters(uri, extraParams);
- }
- }
- uri = SharedUtil.addGetParameters(uri, UIConstants.UI_ID_PARAMETER
- + "=" + configuration.getUIId());
-
- doUidlRequest(uri, payload, true);
-
- }
-
- /**
- * Sends an asynchronous or synchronous UIDL request to the server using the
- * given URI.
- *
- * @param uri
- * The URI to use for the request. May includes GET parameters
- * @param payload
- * The contents of the request to send
- * @param retry
- * true when a status code 0 should be retried
- * @since 7.3.7
- */
- public void doUidlRequest(final String uri, final JsonObject payload,
- final boolean retry) {
- RequestCallback requestCallback = new RequestCallback() {
-
- @Override
- public void onError(Request request, Throwable exception) {
- getCommunicationProblemHandler().xhrException(
- payload,
- new CommunicationProblemEvent(request, uri, payload,
- exception));
- }
-
- @Override
- public void onResponseReceived(Request request, Response response) {
- getLogger().info(
- "Server visit took "
- + String.valueOf((new Date()).getTime()
- - requestStartTime.getTime()) + "ms");
-
- int statusCode = response.getStatusCode();
-
- if (statusCode != 200) {
- // There was a problem
- CommunicationProblemEvent problemEvent = new CommunicationProblemEvent(
- request, uri, payload, response);
-
- getCommunicationProblemHandler().xhrInvalidStatusCode(
- problemEvent, retry);
- return;
- }
-
- String contentType = response.getHeader("Content-Type");
- if (contentType == null
- || !contentType.startsWith("application/json")) {
- getCommunicationProblemHandler().xhrInvalidContent(
- new CommunicationProblemEvent(request, uri,
- payload, response));
- return;
- }
-
- // for(;;);["+ realJson +"]"
- String responseText = response.getText();
-
- if (!responseText.startsWith(JSON_COMMUNICATION_PREFIX)) {
- getCommunicationProblemHandler().xhrInvalidContent(
- new CommunicationProblemEvent(request, uri,
- payload, response));
- return;
- }
-
- final String jsonText = responseText.substring(
- JSON_COMMUNICATION_PREFIX.length(),
- responseText.length()
- - JSON_COMMUNICATION_SUFFIX.length());
-
- handleJSONText(jsonText, statusCode);
- }
- };
- if (push != null) {
- push.push(payload);
- } else {
- try {
- doAjaxRequest(uri, payload, requestCallback);
- } catch (RequestException e) {
- getCommunicationProblemHandler().xhrException(payload,
- new CommunicationProblemEvent(null, uri, payload, e));
- }
- }
- }
-
- /**
- * Handles received UIDL JSON text, parsing it, and passing it on to the
- * appropriate handlers, while logging timing information.
- *
- * @param jsonText
- * @param statusCode
- */
- private void handleJSONText(String jsonText, int statusCode) {
- final Date start = new Date();
- final ValueMap json;
- try {
- json = parseJSONResponse(jsonText);
- } catch (final Exception e) {
- endRequest();
- showCommunicationError(e.getMessage() + " - Original JSON-text:"
- + jsonText, statusCode);
- return;
- }
-
- getLogger().info(
- "JSON parsing took " + (new Date().getTime() - start.getTime())
- + "ms");
- if (getState() == State.RUNNING) {
- getServerMessageHandler().handleUIDLMessage(start, jsonText, json);
- } else if (getState() == State.INITIALIZING) {
- // Application is starting up for the first time
- setApplicationRunning(true);
- handleWhenCSSLoaded(jsonText, json);
- } else {
- getLogger()
- .warning(
- "Ignored received message because application has already been stopped");
- return;
- }
- }
-
- /**
- * Sends an asynchronous UIDL request to the server using the given URI.
- *
- * @param uri
- * The URI to use for the request. May includes GET parameters
- * @param payload
- * The contents of the request to send
- * @param requestCallback
- * The handler for the response
- * @throws RequestException
- * if the request could not be sent
- */
- protected void doAjaxRequest(String uri, JsonObject payload,
- RequestCallback requestCallback) throws RequestException {
- RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri);
- // TODO enable timeout
- // rb.setTimeoutMillis(timeoutMillis);
- // TODO this should be configurable
- rb.setHeader("Content-Type", JsonConstants.JSON_CONTENT_TYPE);
- rb.setRequestData(payload.toJson());
- rb.setCallback(requestCallback);
-
- final Request request = rb.send();
- if (webkitMaybeIgnoringRequests && BrowserInfo.get().isWebkit()) {
- final int retryTimeout = 250;
- new Timer() {
- @Override
- public void run() {
- // Use native js to access private field in Request
- if (resendRequest(request) && webkitMaybeIgnoringRequests) {
- // Schedule retry if still needed
- schedule(retryTimeout);
- }
- }
- }.schedule(retryTimeout);
- }
- }
-
- private static native boolean resendRequest(Request request)
- /*-{
- var xhr = request.@com.google.gwt.http.client.Request::xmlHttpRequest
- if (xhr.readyState != 1) {
- // Progressed to some other readyState -> no longer blocked
- return false;
- }
- try {
- xhr.send();
- return true;
- } catch (e) {
- // send throws exception if it is running for real
- return false;
- }
- }-*/;
-
- int cssWaits = 0;
-
- protected ServerRpcQueue serverRpcQueue;
- protected CommunicationProblemHandler communicationProblemHandler;
- protected ServerMessageHandler serverMessageHandler;
-
- static final int MAX_CSS_WAITS = 100;
-
- protected void handleWhenCSSLoaded(final String jsonText,
- final ValueMap json) {
- if (!isCSSLoaded() && cssWaits < MAX_CSS_WAITS) {
- (new Timer() {
- @Override
- public void run() {
- handleWhenCSSLoaded(jsonText, json);
- }
- }).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.");
- }
-
- getServerMessageHandler().handleUIDLMessage(new Date(), jsonText,
- json);
- }
- }
-
- /**
- * 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
- *
- */
- protected 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());
- }
-
- protected void startRequest() {
- if (hasActiveRequest) {
- getLogger().severe(
- "Trying to start a new request while another is active");
- }
- hasActiveRequest = true;
- requestStartTime = new Date();
- eventBus.fireEvent(new RequestStartingEvent(this));
- }
-
- public void endRequest() {
- if (!hasActiveRequest) {
- getLogger().severe("No active request");
- }
- // After sendInvocationsToServer() there may be a new active
- // request, so we must set hasActiveRequest to false before, not after,
- // the call. Active requests used to be tracked with an integer counter,
- // so setting it after used to work but not with the #8505 changes.
- hasActiveRequest = false;
-
- webkitMaybeIgnoringRequests = false;
-
- if (isApplicationRunning()) {
- if (serverRpcQueue.isFlushPending()) {
- sendInvocationsToServer();
- }
- runPostRequestHooks(configuration.getRootPanelId());
- }
-
- // deferring to avoid flickering
- Scheduler.get().scheduleDeferred(new Command() {
- @Override
- public void execute() {
- if (!isApplicationRunning()
- || !(hasActiveRequest() || serverRpcQueue
- .isFlushPending())) {
- getLoadingIndicator().hide();
-
- // If on Liferay and session expiration management is in
- // use, extend session duration on each request.
- // Doing it here rather than before the request to improve
- // responsiveness.
- // Postponed until the end of the next request if other
- // requests still pending.
- extendLiferaySession();
- }
- }
- });
- eventBus.fireEvent(new ResponseHandlingEndedEvent(this));
- }
-
- /**
- * 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();
- }
-
- private static native ValueMap parseJSONResponse(String jsonText)
- /*-{
- try {
- return JSON.parse(jsonText);
- } catch (ignored) {
- return eval('(' + jsonText + ')');
- }
- }-*/;
-
- 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();
- }
-
- public void doSendPendingVariableChanges() {
- if (!isApplicationRunning()) {
- getLogger()
- .warning(
- "Trying to send RPC from not yet started or stopped application");
- return;
- }
-
- if (hasActiveRequest() || (push != null && !push.isActive())) {
- // There is an active request or push is enabled but not active
- // -> send when current request completes or push becomes active
- } else {
- sendInvocationsToServer();
- }
- }
-
- /**
- * Sends all pending method invocations (server RPC and legacy variable
- * changes) to the server.
- *
- */
- private void sendInvocationsToServer() {
- if (serverRpcQueue.isEmpty()) {
- return;
- }
-
- if (ApplicationConfiguration.isDebugMode()) {
- Util.logMethodInvocations(this, serverRpcQueue.getAll());
- }
-
- boolean showLoadingIndicator = serverRpcQueue.showLoadingIndicator();
- JsonArray reqJson = serverRpcQueue.toJson();
- serverRpcQueue.clear();
-
- if (reqJson.length() == 0) {
- // Nothing to send, all invocations were filtered out (for
- // non-existing connectors)
- getLogger()
- .warning(
- "All RPCs filtered out, not sending anything to the server");
- return;
- }
-
- String extraParams = "";
- if (!getConfiguration().isWidgetsetVersionSent()) {
- if (!extraParams.isEmpty()) {
- extraParams += "&";
- }
- String widgetsetVersion = Version.getFullVersion();
- extraParams += "v-wsver=" + widgetsetVersion;
-
- getConfiguration().setWidgetsetVersionSent();
- }
- if (showLoadingIndicator) {
- getLoadingIndicator().trigger();
- }
- makeUidlRequest(reqJson, extraParams);
- }
-
- /**
- * 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.
- *
- * @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 paintable
- * 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 AbstractComponentState#hasEventListener(String)}
- * instead
- */
- @Deprecated
- public boolean hasEventListeners(ComponentConnector paintable,
- String eventIdentifier) {
- return paintable.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 (getState() == State.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 (getState() == State.INITIALIZING) {
- if (applicationRunning) {
- state = State.RUNNING;
- } else {
- getLogger()
- .warning(
- "Tried to stop the application before it has started. This should not be done");
- }
- } else if (getState() == State.RUNNING) {
- if (!applicationRunning) {
- state = State.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 State#RUNNING} state.
- *
- * @since
- * @return true if the application is in the running state, false otherwise
- */
- public boolean isApplicationRunning() {
- return state == State.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);
- }
-
- /**
- * Sets the status for the push connection.
- *
- * @param enabled
- * <code>true</code> to enable the push connection;
- * <code>false</code> to disable the push connection.
- */
- public void setPushEnabled(boolean enabled) {
- final PushConfigurationState pushState = uIConnector.getState().pushConfiguration;
-
- if (enabled && push == null) {
- push = GWT.create(PushConnection.class);
- push.init(this, pushState, new CommunicationErrorHandler() {
- @Override
- public boolean onError(String details, int statusCode) {
- handleCommunicationError(details, statusCode);
- return true;
- }
- });
- } else if (!enabled && push != null && push.isActive()) {
- push.disconnect(new Command() {
- @Override
- public void execute() {
- push = null;
- /*
- * If push has been enabled again while we were waiting for
- * the old connection to disconnect, now is the right time
- * to open a new connection
- */
- if (pushState.mode.isEnabled()) {
- setPushEnabled(true);
- }
-
- /*
- * Send anything that was enqueued while we waited for the
- * connection to close
- */
- if (serverRpcQueue.isFlushPending()) {
- sendPendingVariableChanges();
- }
- }
- });
- }
- }
-
- public void handlePushMessage(String message) {
- handleJSONText(message, 200);
- }
-
- /**
- * Returns a human readable string representation of the method used to
- * communicate with the server.
- *
- * @since 7.1
- * @return A string representation of the current transport type
- */
- public String getCommunicationMethodName() {
- if (push != null) {
- return "Push (" + push.getTransportType() + ")";
- } else {
- return "XHR";
- }
- }
-
- 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
- * @return the current state of this application
- */
- public State getState() {
- return state;
- }
-
- /**
- * Gets the server RPC queue for this application
- *
- * @return the server RPC queue
- */
- public ServerRpcQueue getServerRpcQueue() {
- return serverRpcQueue;
- }
-
- /**
- * Gets the communication error handler for this application
- *
- * @return the server RPC queue
- */
- public CommunicationProblemHandler getCommunicationProblemHandler() {
- return communicationProblemHandler;
- }
-
- /**
- * Gets the server message handler for this application
- *
- * @return the server message handler
- */
- public ServerMessageHandler getServerMessageHandler() {
- return serverMessageHandler;
- }
-
- /**
- * Gets the server rpc manager for this application
- *
- * @return the server rpc manager
- */
- public RpcManager getRpcManager() {
- return rpcManager;
- }
-
- /**
- * @return the widget set
- */
- public WidgetSet getWidgetSet() {
- return widgetSet;
- }
-
- /**
- * @since
- * @return
- */
- public int getLastSeenServerSyncId() {
- return getServerMessageHandler().getLastSeenServerSyncId();
- }
- }
|