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

ApplicationConnection.java 79KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import com.google.gwt.core.client.Duration;
  13. import com.google.gwt.core.client.GWT;
  14. import com.google.gwt.core.client.JavaScriptObject;
  15. import com.google.gwt.core.client.JsArray;
  16. import com.google.gwt.core.client.JsArrayString;
  17. import com.google.gwt.core.client.Scheduler;
  18. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  19. import com.google.gwt.dom.client.Style;
  20. import com.google.gwt.http.client.Request;
  21. import com.google.gwt.http.client.RequestBuilder;
  22. import com.google.gwt.http.client.RequestCallback;
  23. import com.google.gwt.http.client.RequestException;
  24. import com.google.gwt.http.client.Response;
  25. import com.google.gwt.regexp.shared.MatchResult;
  26. import com.google.gwt.regexp.shared.RegExp;
  27. import com.google.gwt.user.client.Command;
  28. import com.google.gwt.user.client.DOM;
  29. import com.google.gwt.user.client.Element;
  30. import com.google.gwt.user.client.Event;
  31. import com.google.gwt.user.client.Timer;
  32. import com.google.gwt.user.client.ui.FocusWidget;
  33. import com.google.gwt.user.client.ui.Focusable;
  34. import com.google.gwt.user.client.ui.HasWidgets;
  35. import com.google.gwt.user.client.ui.Widget;
  36. import com.vaadin.terminal.gwt.client.ApplicationConfiguration.ErrorMessage;
  37. import com.vaadin.terminal.gwt.client.ui.Field;
  38. import com.vaadin.terminal.gwt.client.ui.VContextMenu;
  39. import com.vaadin.terminal.gwt.client.ui.VNotification;
  40. import com.vaadin.terminal.gwt.client.ui.VNotification.HideEvent;
  41. import com.vaadin.terminal.gwt.client.ui.VViewPaintable;
  42. import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager;
  43. import com.vaadin.terminal.gwt.server.AbstractCommunicationManager;
  44. /**
  45. * This is the client side communication "engine", managing client-server
  46. * communication with its server side counterpart
  47. * {@link AbstractCommunicationManager}.
  48. *
  49. * Client-side widgets receive updates from the corresponding server-side
  50. * components as calls to
  51. * {@link VPaintableWidget#updateFromUIDL(UIDL, ApplicationConnection)} (not to
  52. * be confused with the server side interface
  53. * {@link com.vaadin.terminal.Paintable} ). Any client-side changes (typically
  54. * resulting from user actions) are sent back to the server as variable changes
  55. * (see {@link #updateVariable()}).
  56. *
  57. * TODO document better
  58. *
  59. * Entry point classes (widgetsets) define <code>onModuleLoad()</code>.
  60. */
  61. public class ApplicationConnection {
  62. // This indicates the whole page is generated by us (not embedded)
  63. public static final String GENERATED_BODY_CLASSNAME = "v-generated-body";
  64. private static final String MODIFIED_CLASSNAME = "v-modified";
  65. public static final String DISABLED_CLASSNAME = "v-disabled";
  66. private static final String REQUIRED_CLASSNAME_EXT = "-required";
  67. private static final String ERROR_CLASSNAME_EXT = "-error";
  68. public static final char VAR_RECORD_SEPARATOR = '\u001e';
  69. public static final char VAR_FIELD_SEPARATOR = '\u001f';
  70. public static final char VAR_BURST_SEPARATOR = '\u001d';
  71. public static final char VAR_ARRAYITEM_SEPARATOR = '\u001c';
  72. public static final char VAR_ESCAPE_CHARACTER = '\u001b';
  73. public static final String UIDL_SECURITY_TOKEN_ID = "Vaadin-Security-Key";
  74. /**
  75. * Name of the parameter used to transmit root ids back and forth
  76. */
  77. public static final String ROOT_ID_PARAMETER = "rootId";
  78. /**
  79. * @deprecated use UIDL_SECURITY_TOKEN_ID instead
  80. */
  81. @Deprecated
  82. public static final String UIDL_SECURITY_HEADER = UIDL_SECURITY_TOKEN_ID;
  83. public static final String PARAM_UNLOADBURST = "onunloadburst";
  84. public static final String ATTRIBUTE_DESCRIPTION = "description";
  85. public static final String ATTRIBUTE_ERROR = "error";
  86. /**
  87. * A string that, if found in a non-JSON response to a UIDL request, will
  88. * cause the browser to refresh the page. If followed by a colon, optional
  89. * whitespace, and a URI, causes the browser to synchronously load the URI.
  90. *
  91. * <p>
  92. * This allows, for instance, a servlet filter to redirect the application
  93. * to a custom login page when the session expires. For example:
  94. * </p>
  95. *
  96. * <pre>
  97. * if (sessionExpired) {
  98. * response.setHeader(&quot;Content-Type&quot;, &quot;text/html&quot;);
  99. * response.getWriter().write(
  100. * myLoginPageHtml + &quot;&lt;!-- Vaadin-Refresh: &quot;
  101. * + request.getContextPath() + &quot; --&gt;&quot;);
  102. * }
  103. * </pre>
  104. */
  105. public static final String UIDL_REFRESH_TOKEN = "Vaadin-Refresh";
  106. // will hold the UIDL security key (for XSS protection) once received
  107. private String uidlSecurityKey = "init";
  108. private final HashMap<String, String> resourcesMap = new HashMap<String, String>();
  109. private final ArrayList<String> pendingVariables = new ArrayList<String>();
  110. private WidgetSet widgetSet;
  111. private VContextMenu contextMenu = null;
  112. private Timer loadTimer;
  113. private Timer loadTimer2;
  114. private Timer loadTimer3;
  115. private Element loadElement;
  116. private final VViewPaintable view;
  117. protected boolean applicationRunning = false;
  118. private int activeRequests = 0;
  119. protected boolean cssLoaded = false;
  120. /** Parameters for this application connection loaded from the web-page */
  121. private ApplicationConfiguration configuration;
  122. /** List of pending variable change bursts that must be submitted in order */
  123. private final ArrayList<ArrayList<String>> pendingVariableBursts = new ArrayList<ArrayList<String>>();
  124. /** Timer for automatic refirect to SessionExpiredURL */
  125. private Timer redirectTimer;
  126. /** redirectTimer scheduling interval in seconds */
  127. private int sessionExpirationInterval;
  128. private ArrayList<Widget> componentCaptionSizeChanges = new ArrayList<Widget>();
  129. private Date requestStartTime;
  130. private boolean validatingLayouts = false;
  131. private Set<VPaintableWidget> zeroWidthComponents = null;
  132. private Set<VPaintableWidget> zeroHeightComponents = null;
  133. private final MeasureManager measureManager = new MeasureManager();
  134. public ApplicationConnection() {
  135. view = GWT.create(VViewPaintable.class);
  136. view.setConnection(this);
  137. }
  138. public void init(WidgetSet widgetSet, ApplicationConfiguration cnf) {
  139. VConsole.log("Starting application " + cnf.getRootPanelId());
  140. VConsole.log("Vaadin application servlet version: "
  141. + cnf.getServletVersion());
  142. VConsole.log("Application version: " + cnf.getApplicationVersion());
  143. if (!cnf.getServletVersion().equals(ApplicationConfiguration.VERSION)) {
  144. VConsole.error("Warning: your widget set seems to be built with a different "
  145. + "version than the one used on server. Unexpected "
  146. + "behavior may occur.");
  147. }
  148. this.widgetSet = widgetSet;
  149. configuration = cnf;
  150. ComponentLocator componentLocator = new ComponentLocator(this);
  151. String appRootPanelName = cnf.getRootPanelId();
  152. // remove the end (window name) of autogenerated rootpanel id
  153. appRootPanelName = appRootPanelName.replaceFirst("-\\d+$", "");
  154. initializeTestbenchHooks(componentLocator, appRootPanelName);
  155. initializeClientHooks();
  156. view.init(cnf.getRootPanelId(), this);
  157. showLoadingIndicator();
  158. }
  159. /**
  160. * Starts this application. Don't call this method directly - it's called by
  161. * {@link ApplicationConfiguration#startNextApplication()}, which should be
  162. * called once this application has started (first response received) or
  163. * failed to start. This ensures that the applications are started in order,
  164. * to avoid session-id problems.
  165. *
  166. */
  167. public void start() {
  168. String jsonText = configuration.getUIDL();
  169. if (jsonText == null) {
  170. // inital UIDL not in DOM, request later
  171. repaintAll();
  172. } else {
  173. // Update counter so TestBench knows something is still going on
  174. incrementActiveRequests();
  175. // initial UIDL provided in DOM, continue as if returned by request
  176. handleJSONText(jsonText);
  177. }
  178. }
  179. private native void initializeTestbenchHooks(
  180. ComponentLocator componentLocator, String TTAppId)
  181. /*-{
  182. var ap = this;
  183. var client = {};
  184. client.isActive = function() {
  185. return ap.@com.vaadin.terminal.gwt.client.ApplicationConnection::hasActiveRequest()()
  186. || ap.@com.vaadin.terminal.gwt.client.ApplicationConnection::isExecutingDeferredCommands()();
  187. }
  188. var vi = ap.@com.vaadin.terminal.gwt.client.ApplicationConnection::getVersionInfo()();
  189. if (vi) {
  190. client.getVersionInfo = function() {
  191. return vi;
  192. }
  193. }
  194. client.getElementByPath = function(id) {
  195. return componentLocator.@com.vaadin.terminal.gwt.client.ComponentLocator::getElementByPath(Ljava/lang/String;)(id);
  196. }
  197. client.getPathForElement = function(element) {
  198. return componentLocator.@com.vaadin.terminal.gwt.client.ComponentLocator::getPathForElement(Lcom/google/gwt/user/client/Element;)(element);
  199. }
  200. $wnd.vaadin.clients[TTAppId] = client;
  201. }-*/;
  202. /**
  203. * Helper for tt initialization
  204. */
  205. private JavaScriptObject getVersionInfo() {
  206. return configuration.getVersionInfoJSObject();
  207. }
  208. /**
  209. * Publishes a JavaScript API for mash-up applications.
  210. * <ul>
  211. * <li><code>vaadin.forceSync()</code> sends pending variable changes, in
  212. * effect synchronizing the server and client state. This is done for all
  213. * applications on host page.</li>
  214. * <li><code>vaadin.postRequestHooks</code> is a map of functions which gets
  215. * called after each XHR made by vaadin application. Note, that it is
  216. * attaching js functions responsibility to create the variable like this:
  217. *
  218. * <code><pre>
  219. * if(!vaadin.postRequestHooks) {vaadin.postRequestHooks = new Object();}
  220. * postRequestHooks.myHook = function(appId) {
  221. * if(appId == "MyAppOfInterest") {
  222. * // do the staff you need on xhr activity
  223. * }
  224. * }
  225. * </pre></code> First parameter passed to these functions is the identifier
  226. * of Vaadin application that made the request.
  227. * </ul>
  228. *
  229. * TODO make this multi-app aware
  230. */
  231. private native void initializeClientHooks()
  232. /*-{
  233. var app = this;
  234. var oldSync;
  235. if ($wnd.vaadin.forceSync) {
  236. oldSync = $wnd.vaadin.forceSync;
  237. }
  238. $wnd.vaadin.forceSync = function() {
  239. if (oldSync) {
  240. oldSync();
  241. }
  242. app.@com.vaadin.terminal.gwt.client.ApplicationConnection::sendPendingVariableChanges()();
  243. }
  244. var oldForceLayout;
  245. if ($wnd.vaadin.forceLayout) {
  246. oldForceLayout = $wnd.vaadin.forceLayout;
  247. }
  248. $wnd.vaadin.forceLayout = function() {
  249. if (oldForceLayout) {
  250. oldForceLayout();
  251. }
  252. app.@com.vaadin.terminal.gwt.client.ApplicationConnection::forceLayout()();
  253. }
  254. }-*/;
  255. /**
  256. * Runs possibly registered client side post request hooks. This is expected
  257. * to be run after each uidl request made by Vaadin application.
  258. *
  259. * @param appId
  260. */
  261. private static native void runPostRequestHooks(String appId)
  262. /*-{
  263. if ($wnd.vaadin.postRequestHooks) {
  264. for ( var hook in $wnd.vaadin.postRequestHooks) {
  265. if (typeof ($wnd.vaadin.postRequestHooks[hook]) == "function") {
  266. try {
  267. $wnd.vaadin.postRequestHooks[hook](appId);
  268. } catch (e) {
  269. }
  270. }
  271. }
  272. }
  273. }-*/;
  274. /**
  275. * Get the active Console for writing debug messages. May return an actual
  276. * logging console, or the NullConsole if debugging is not turned on.
  277. *
  278. * @deprecated Developers should use {@link VConsole} since 6.4.5
  279. *
  280. * @return the active Console
  281. */
  282. @Deprecated
  283. public static Console getConsole() {
  284. return VConsole.getImplementation();
  285. }
  286. /**
  287. * Checks if client side is in debug mode. Practically this is invoked by
  288. * adding ?debug parameter to URI.
  289. *
  290. * @deprecated use ApplicationConfiguration isDebugMode instead.
  291. *
  292. * @return true if client side is currently been debugged
  293. */
  294. @Deprecated
  295. public static boolean isDebugMode() {
  296. return ApplicationConfiguration.isDebugMode();
  297. }
  298. /**
  299. * Gets the application base URI. Using this other than as the download
  300. * action URI can cause problems in Portlet 2.0 deployments.
  301. *
  302. * @return application base URI
  303. */
  304. public String getAppUri() {
  305. return configuration.getApplicationUri();
  306. };
  307. /**
  308. * Indicates whether or not there are currently active UIDL requests. Used
  309. * internally to squence requests properly, seldom needed in Widgets.
  310. *
  311. * @return true if there are active requests
  312. */
  313. public boolean hasActiveRequest() {
  314. return (activeRequests > 0);
  315. }
  316. public void incrementActiveRequests() {
  317. if (activeRequests < 0) {
  318. activeRequests = 1;
  319. } else {
  320. activeRequests++;
  321. }
  322. }
  323. public void decrementActiveRequests() {
  324. if (activeRequests > 0) {
  325. activeRequests--;
  326. }
  327. }
  328. private String getRepaintAllParameters() {
  329. // collect some client side data that will be sent to server on
  330. // initial uidl request
  331. String nativeBootstrapParameters = getNativeBrowserDetailsParameters(getConfiguration()
  332. .getRootPanelId());
  333. String widgetsetVersion = ApplicationConfiguration.VERSION;
  334. // TODO figure out how client and view size could be used better on
  335. // server. screen size can be accessed via Browser object, but other
  336. // values currently only via transaction listener.
  337. String parameters = "repaintAll=1&" + nativeBootstrapParameters
  338. + "&wsver=" + widgetsetVersion;
  339. return parameters;
  340. }
  341. /**
  342. * Gets the browser detail parameters that are sent by the bootstrap
  343. * javascript for two-request initialization.
  344. *
  345. * @param parentElementId
  346. * @return
  347. */
  348. private static native String getNativeBrowserDetailsParameters(
  349. String parentElementId)
  350. /*-{
  351. return $wnd.vaadin.getBrowserDetailsParameters(parentElementId);
  352. }-*/;
  353. protected void repaintAll() {
  354. String repainAllParameters = getRepaintAllParameters();
  355. makeUidlRequest("", repainAllParameters, false);
  356. }
  357. /**
  358. * Requests an analyze of layouts, to find inconsistencies. Exclusively used
  359. * for debugging during development.
  360. */
  361. public void analyzeLayouts() {
  362. String params = getRepaintAllParameters() + "&analyzeLayouts=1";
  363. makeUidlRequest("", params, false);
  364. }
  365. /**
  366. * Sends a request to the server to print details to console that will help
  367. * developer to locate component in the source code.
  368. *
  369. * @param paintable
  370. */
  371. void highlightComponent(VPaintableWidget paintable) {
  372. String params = getRepaintAllParameters() + "&highlightComponent="
  373. + paintableMap.getPid(paintable);
  374. makeUidlRequest("", params, false);
  375. }
  376. /**
  377. * Makes an UIDL request to the server.
  378. *
  379. * @param requestData
  380. * Data that is passed to the server.
  381. * @param extraParams
  382. * Parameters that are added as GET parameters to the url.
  383. * Contains key=value pairs joined by & characters or is empty if
  384. * no parameters should be added. Should not start with any
  385. * special character.
  386. * @param forceSync
  387. * true if the request should be synchronous, false otherwise
  388. */
  389. protected void makeUidlRequest(final String requestData,
  390. final String extraParams, final boolean forceSync) {
  391. startRequest();
  392. // Security: double cookie submission pattern
  393. final String payload = uidlSecurityKey + VAR_BURST_SEPARATOR
  394. + requestData;
  395. VConsole.log("Making UIDL Request with params: " + payload);
  396. String uri;
  397. if (configuration.usePortletURLs()) {
  398. uri = configuration.getPortletUidlURLBase();
  399. } else {
  400. uri = getAppUri() + "UIDL";
  401. }
  402. if (extraParams != null && extraParams.length() > 0) {
  403. uri = addGetParameters(uri, extraParams);
  404. }
  405. uri = addGetParameters(uri,
  406. ROOT_ID_PARAMETER + "=" + configuration.getRootId());
  407. doUidlRequest(uri, payload, forceSync);
  408. }
  409. /**
  410. * Sends an asynchronous or synchronous UIDL request to the server using the
  411. * given URI.
  412. *
  413. * @param uri
  414. * The URI to use for the request. May includes GET parameters
  415. * @param payload
  416. * The contents of the request to send
  417. * @param synchronous
  418. * true if the request should be synchronous, false otherwise
  419. */
  420. protected void doUidlRequest(final String uri, final String payload,
  421. final boolean synchronous) {
  422. if (!synchronous) {
  423. RequestCallback requestCallback = new RequestCallback() {
  424. public void onError(Request request, Throwable exception) {
  425. showCommunicationError(exception.getMessage());
  426. endRequest();
  427. }
  428. public void onResponseReceived(Request request,
  429. Response response) {
  430. VConsole.log("Server visit took "
  431. + String.valueOf((new Date()).getTime()
  432. - requestStartTime.getTime()) + "ms");
  433. int statusCode = response.getStatusCode();
  434. switch (statusCode) {
  435. case 0:
  436. showCommunicationError("Invalid status code 0 (server down?)");
  437. endRequest();
  438. return;
  439. case 401:
  440. /*
  441. * Authorization has failed. Could be that the session
  442. * has timed out and the container is redirecting to a
  443. * login page.
  444. */
  445. showAuthenticationError("");
  446. endRequest();
  447. return;
  448. case 503:
  449. // We'll assume msec instead of the usual seconds
  450. int delay = Integer.parseInt(response
  451. .getHeader("Retry-After"));
  452. VConsole.log("503, retrying in " + delay + "msec");
  453. (new Timer() {
  454. @Override
  455. public void run() {
  456. decrementActiveRequests();
  457. doUidlRequest(uri, payload, synchronous);
  458. }
  459. }).schedule(delay);
  460. return;
  461. }
  462. if ((statusCode / 100) == 4) {
  463. // Handle all 4xx errors the same way as (they are
  464. // all permanent errors)
  465. showCommunicationError("UIDL could not be read from server. Check servlets mappings. Error code: "
  466. + statusCode);
  467. endRequest();
  468. return;
  469. }
  470. String contentType = response.getHeader("Content-Type");
  471. if (contentType == null
  472. || !contentType.startsWith("application/json")) {
  473. /*
  474. * A servlet filter or equivalent may have intercepted
  475. * the request and served non-UIDL content (for
  476. * instance, a login page if the session has expired.)
  477. * If the response contains a magic substring, do a
  478. * synchronous refresh. See #8241.
  479. */
  480. MatchResult refreshToken = RegExp.compile(
  481. UIDL_REFRESH_TOKEN + "(:\\s*(.*?))?(\\s|$)")
  482. .exec(response.getText());
  483. if (refreshToken != null) {
  484. redirect(refreshToken.getGroup(2));
  485. return;
  486. }
  487. }
  488. // for(;;);[realjson]
  489. final String jsonText = response.getText().substring(9,
  490. response.getText().length() - 1);
  491. handleJSONText(jsonText);
  492. }
  493. };
  494. try {
  495. doAsyncUIDLRequest(uri, payload, requestCallback);
  496. } catch (RequestException e) {
  497. VConsole.error(e);
  498. endRequest();
  499. }
  500. } else {
  501. // Synchronized call, discarded response (leaving the page)
  502. SynchronousXHR syncXHR = (SynchronousXHR) SynchronousXHR.create();
  503. syncXHR.synchronousPost(uri + "&" + PARAM_UNLOADBURST + "=1",
  504. payload);
  505. /*
  506. * Although we are in theory leaving the page, the page may still
  507. * stay open. End request properly here too. See #3289
  508. */
  509. endRequest();
  510. }
  511. }
  512. /**
  513. * Handles received UIDL JSON text, parsing it, and passing it on to the
  514. * appropriate handlers, while logging timiing information.
  515. *
  516. * @param jsonText
  517. */
  518. private void handleJSONText(String jsonText) {
  519. final Date start = new Date();
  520. final ValueMap json;
  521. try {
  522. json = parseJSONResponse(jsonText);
  523. } catch (final Exception e) {
  524. endRequest();
  525. showCommunicationError(e.getMessage() + " - Original JSON-text:"
  526. + jsonText);
  527. return;
  528. }
  529. VConsole.log("JSON parsing took "
  530. + (new Date().getTime() - start.getTime()) + "ms");
  531. if (applicationRunning) {
  532. handleReceivedJSONMessage(start, jsonText, json);
  533. } else {
  534. applicationRunning = true;
  535. handleWhenCSSLoaded(jsonText, json);
  536. }
  537. }
  538. /**
  539. * Sends an asynchronous UIDL request to the server using the given URI.
  540. *
  541. * @param uri
  542. * The URI to use for the request. May includes GET parameters
  543. * @param payload
  544. * The contents of the request to send
  545. * @param requestCallback
  546. * The handler for the response
  547. * @throws RequestException
  548. * if the request could not be sent
  549. */
  550. protected void doAsyncUIDLRequest(String uri, String payload,
  551. RequestCallback requestCallback) throws RequestException {
  552. RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri);
  553. // TODO enable timeout
  554. // rb.setTimeoutMillis(timeoutMillis);
  555. rb.setHeader("Content-Type", "text/plain;charset=utf-8");
  556. rb.setRequestData(payload);
  557. rb.setCallback(requestCallback);
  558. rb.send();
  559. }
  560. int cssWaits = 0;
  561. static final int MAX_CSS_WAITS = 100;
  562. protected void handleWhenCSSLoaded(final String jsonText,
  563. final ValueMap json) {
  564. if (!isCSSLoaded() && cssWaits < MAX_CSS_WAITS) {
  565. (new Timer() {
  566. @Override
  567. public void run() {
  568. handleWhenCSSLoaded(jsonText, json);
  569. }
  570. }).schedule(50);
  571. VConsole.log("Assuming CSS loading is not complete, "
  572. + "postponing render phase. "
  573. + "(.v-loading-indicator height == 0)");
  574. cssWaits++;
  575. } else {
  576. cssLoaded = true;
  577. handleReceivedJSONMessage(new Date(), jsonText, json);
  578. if (cssWaits >= MAX_CSS_WAITS) {
  579. VConsole.error("CSS files may have not loaded properly.");
  580. }
  581. }
  582. }
  583. /**
  584. * Checks whether or not the CSS is loaded. By default checks the size of
  585. * the loading indicator element.
  586. *
  587. * @return
  588. */
  589. protected boolean isCSSLoaded() {
  590. return cssLoaded
  591. || DOM.getElementPropertyInt(loadElement, "offsetHeight") != 0;
  592. }
  593. /**
  594. * Shows the communication error notification.
  595. *
  596. * @param details
  597. * Optional details for debugging.
  598. */
  599. protected void showCommunicationError(String details) {
  600. VConsole.error("Communication error: " + details);
  601. ErrorMessage communicationError = configuration.getCommunicationError();
  602. showError(details, communicationError.getCaption(),
  603. communicationError.getMessage(), communicationError.getUrl());
  604. }
  605. /**
  606. * Shows the authentication error notification.
  607. *
  608. * @param details
  609. * Optional details for debugging.
  610. */
  611. protected void showAuthenticationError(String details) {
  612. VConsole.error("Authentication error: " + details);
  613. ErrorMessage authorizationError = configuration.getAuthorizationError();
  614. showError(details, authorizationError.getCaption(),
  615. authorizationError.getMessage(), authorizationError.getUrl());
  616. }
  617. /**
  618. * Shows the error notification.
  619. *
  620. * @param details
  621. * Optional details for debugging.
  622. */
  623. private void showError(String details, String caption, String message,
  624. String url) {
  625. StringBuilder html = new StringBuilder();
  626. if (caption != null) {
  627. html.append("<h1>");
  628. html.append(caption);
  629. html.append("</h1>");
  630. }
  631. if (message != null) {
  632. html.append("<p>");
  633. html.append(message);
  634. html.append("</p>");
  635. }
  636. if (html.length() > 0) {
  637. // Add error description
  638. html.append("<br/><p><I style=\"font-size:0.7em\">");
  639. html.append(details);
  640. html.append("</I></p>");
  641. VNotification n = VNotification.createNotification(1000 * 60 * 45);
  642. n.addEventListener(new NotificationRedirect(url));
  643. n.show(html.toString(), VNotification.CENTERED_TOP,
  644. VNotification.STYLE_SYSTEM);
  645. } else {
  646. redirect(url);
  647. }
  648. }
  649. protected void startRequest() {
  650. incrementActiveRequests();
  651. requestStartTime = new Date();
  652. // show initial throbber
  653. if (loadTimer == null) {
  654. loadTimer = new Timer() {
  655. @Override
  656. public void run() {
  657. /*
  658. * IE7 does not properly cancel the event with
  659. * loadTimer.cancel() so we have to check that we really
  660. * should make it visible
  661. */
  662. if (loadTimer != null) {
  663. showLoadingIndicator();
  664. }
  665. }
  666. };
  667. // First one kicks in at 300ms
  668. }
  669. loadTimer.schedule(300);
  670. }
  671. protected void endRequest() {
  672. if (applicationRunning) {
  673. checkForPendingVariableBursts();
  674. runPostRequestHooks(configuration.getRootPanelId());
  675. }
  676. decrementActiveRequests();
  677. // deferring to avoid flickering
  678. Scheduler.get().scheduleDeferred(new Command() {
  679. public void execute() {
  680. if (!hasActiveRequest()) {
  681. hideLoadingIndicator();
  682. }
  683. }
  684. });
  685. }
  686. /**
  687. * This method is called after applying uidl change set to application.
  688. *
  689. * It will clean current and queued variable change sets. And send next
  690. * change set if it exists.
  691. */
  692. private void checkForPendingVariableBursts() {
  693. cleanVariableBurst(pendingVariables);
  694. if (pendingVariableBursts.size() > 0) {
  695. for (Iterator<ArrayList<String>> iterator = pendingVariableBursts
  696. .iterator(); iterator.hasNext();) {
  697. cleanVariableBurst(iterator.next());
  698. }
  699. ArrayList<String> nextBurst = pendingVariableBursts.get(0);
  700. pendingVariableBursts.remove(0);
  701. buildAndSendVariableBurst(nextBurst, false);
  702. }
  703. }
  704. /**
  705. * Cleans given queue of variable changes of such changes that came from
  706. * components that do not exist anymore.
  707. *
  708. * @param variableBurst
  709. */
  710. private void cleanVariableBurst(ArrayList<String> variableBurst) {
  711. for (int i = 1; i < variableBurst.size(); i += 2) {
  712. String id = variableBurst.get(i);
  713. id = id.substring(0, id.indexOf(VAR_FIELD_SEPARATOR));
  714. if (!getPaintableMap().hasPaintable(id)
  715. && !getPaintableMap().isDragAndDropPaintable(id)) {
  716. // variable owner does not exist anymore
  717. variableBurst.remove(i - 1);
  718. variableBurst.remove(i - 1);
  719. i -= 2;
  720. VConsole.log("Removed variable from removed component: " + id);
  721. }
  722. }
  723. }
  724. private void showLoadingIndicator() {
  725. // show initial throbber
  726. if (loadElement == null) {
  727. loadElement = DOM.createDiv();
  728. DOM.setStyleAttribute(loadElement, "position", "absolute");
  729. DOM.appendChild(view.getWidgetForPaintable().getElement(),
  730. loadElement);
  731. VConsole.log("inserting load indicator");
  732. }
  733. DOM.setElementProperty(loadElement, "className", "v-loading-indicator");
  734. DOM.setStyleAttribute(loadElement, "display", "block");
  735. // Initialize other timers
  736. loadTimer2 = new Timer() {
  737. @Override
  738. public void run() {
  739. DOM.setElementProperty(loadElement, "className",
  740. "v-loading-indicator-delay");
  741. }
  742. };
  743. // Second one kicks in at 1500ms from request start
  744. loadTimer2.schedule(1200);
  745. loadTimer3 = new Timer() {
  746. @Override
  747. public void run() {
  748. DOM.setElementProperty(loadElement, "className",
  749. "v-loading-indicator-wait");
  750. }
  751. };
  752. // Third one kicks in at 5000ms from request start
  753. loadTimer3.schedule(4700);
  754. }
  755. private void hideLoadingIndicator() {
  756. if (loadTimer != null) {
  757. loadTimer.cancel();
  758. loadTimer = null;
  759. }
  760. if (loadTimer2 != null) {
  761. loadTimer2.cancel();
  762. loadTimer3.cancel();
  763. loadTimer2 = null;
  764. loadTimer3 = null;
  765. }
  766. if (loadElement != null) {
  767. DOM.setStyleAttribute(loadElement, "display", "none");
  768. }
  769. }
  770. /**
  771. * Checks if deferred commands are (potentially) still being executed as a
  772. * result of an update from the server. Returns true if a deferred command
  773. * might still be executing, false otherwise. This will not work correctly
  774. * if a deferred command is added in another deferred command.
  775. * <p>
  776. * Used by the native "client.isActive" function.
  777. * </p>
  778. *
  779. * @return true if deferred commands are (potentially) being executed, false
  780. * otherwise
  781. */
  782. private boolean isExecutingDeferredCommands() {
  783. Scheduler s = Scheduler.get();
  784. if (s instanceof VSchedulerImpl) {
  785. return ((VSchedulerImpl) s).hasWorkQueued();
  786. } else {
  787. return false;
  788. }
  789. }
  790. /**
  791. * Determines whether or not the loading indicator is showing.
  792. *
  793. * @return true if the loading indicator is visible
  794. */
  795. public boolean isLoadingIndicatorVisible() {
  796. if (loadElement == null) {
  797. return false;
  798. }
  799. if (loadElement.getStyle().getProperty("display").equals("none")) {
  800. return false;
  801. }
  802. return true;
  803. }
  804. private static native ValueMap parseJSONResponse(String jsonText)
  805. /*-{
  806. try {
  807. return JSON.parse(jsonText);
  808. } catch (ignored) {
  809. return eval('(' + jsonText + ')');
  810. }
  811. }-*/;
  812. private void handleReceivedJSONMessage(Date start, String jsonText,
  813. ValueMap json) {
  814. handleUIDLMessage(start, jsonText, json);
  815. }
  816. protected void handleUIDLMessage(final Date start, final String jsonText,
  817. final ValueMap json) {
  818. // Handle redirect
  819. if (json.containsKey("redirect")) {
  820. String url = json.getValueMap("redirect").getString("url");
  821. VConsole.log("redirecting to " + url);
  822. redirect(url);
  823. return;
  824. }
  825. // Get security key
  826. if (json.containsKey(UIDL_SECURITY_TOKEN_ID)) {
  827. uidlSecurityKey = json.getString(UIDL_SECURITY_TOKEN_ID);
  828. }
  829. if (json.containsKey("resources")) {
  830. ValueMap resources = json.getValueMap("resources");
  831. JsArrayString keyArray = resources.getKeyArray();
  832. int l = keyArray.length();
  833. for (int i = 0; i < l; i++) {
  834. String key = keyArray.get(i);
  835. resourcesMap.put(key, resources.getAsString(key));
  836. }
  837. }
  838. if (json.containsKey("typeMappings")) {
  839. configuration.addComponentMappings(
  840. json.getValueMap("typeMappings"), widgetSet);
  841. }
  842. Command c = new Command() {
  843. public void execute() {
  844. VConsole.dirUIDL(json, configuration);
  845. if (json.containsKey("locales")) {
  846. // Store locale data
  847. JsArray<ValueMap> valueMapArray = json
  848. .getJSValueMapArray("locales");
  849. LocaleService.addLocales(valueMapArray);
  850. }
  851. boolean repaintAll = false;
  852. ValueMap meta = null;
  853. if (json.containsKey("meta")) {
  854. meta = json.getValueMap("meta");
  855. if (meta.containsKey("repaintAll")) {
  856. repaintAll = true;
  857. view.getWidgetForPaintable().clear();
  858. getPaintableMap().clear();
  859. if (meta.containsKey("invalidLayouts")) {
  860. validatingLayouts = true;
  861. zeroWidthComponents = new HashSet<VPaintableWidget>();
  862. zeroHeightComponents = new HashSet<VPaintableWidget>();
  863. }
  864. }
  865. if (meta.containsKey("timedRedirect")) {
  866. final ValueMap timedRedirect = meta
  867. .getValueMap("timedRedirect");
  868. redirectTimer = new Timer() {
  869. @Override
  870. public void run() {
  871. redirect(timedRedirect.getString("url"));
  872. }
  873. };
  874. sessionExpirationInterval = timedRedirect
  875. .getInt("interval");
  876. }
  877. }
  878. if (redirectTimer != null) {
  879. redirectTimer.schedule(1000 * sessionExpirationInterval);
  880. }
  881. // Process changes
  882. JsArray<ValueMap> changes = json.getJSValueMapArray("changes");
  883. ArrayList<VPaintableWidget> updatedVPaintableWidgets = new ArrayList<VPaintableWidget>();
  884. componentCaptionSizeChanges.clear();
  885. Duration updateDuration = new Duration();
  886. int length = changes.length();
  887. for (int i = 0; i < length; i++) {
  888. try {
  889. final UIDL change = changes.get(i).cast();
  890. final UIDL uidl = change.getChildUIDL(0);
  891. // TODO optimize
  892. final VPaintableWidget paintable = (VPaintableWidget) paintableMap
  893. .getPaintable(uidl.getId());
  894. if (paintable != null) {
  895. paintable.updateFromUIDL(uidl,
  896. ApplicationConnection.this);
  897. updatedVPaintableWidgets.add(paintable);
  898. } else {
  899. if (!uidl.getTag().equals(
  900. configuration.getEncodedWindowTag())) {
  901. VConsole.error("Received update for "
  902. + uidl.getTag()
  903. + ", but there is no such paintable ("
  904. + uidl.getId() + ") rendered.");
  905. } else {
  906. String pid = uidl.getId();
  907. if (!paintableMap.hasPaintable(pid)) {
  908. paintableMap.registerPaintable(pid, view);
  909. }
  910. // VView does not call updateComponent so we
  911. // register any event listeners here
  912. paintableMap.registerEventListenersFromUIDL(
  913. pid, uidl);
  914. // Finally allow VView to update itself
  915. view.updateFromUIDL(uidl,
  916. ApplicationConnection.this);
  917. }
  918. }
  919. } catch (final Throwable e) {
  920. VConsole.error(e);
  921. }
  922. }
  923. if (json.containsKey("dd")) {
  924. // response contains data for drag and drop service
  925. VDragAndDropManager.get().handleServerResponse(
  926. json.getValueMap("dd"));
  927. }
  928. VConsole.log("updateFromUidl: "
  929. + updateDuration.elapsedMillis() + " ms");
  930. doLayout(false);
  931. if (meta != null) {
  932. if (meta.containsKey("appError")) {
  933. ValueMap error = meta.getValueMap("appError");
  934. String html = "";
  935. if (error.containsKey("caption")
  936. && error.getString("caption") != null) {
  937. html += "<h1>" + error.getAsString("caption")
  938. + "</h1>";
  939. }
  940. if (error.containsKey("message")
  941. && error.getString("message") != null) {
  942. html += "<p>" + error.getAsString("message")
  943. + "</p>";
  944. }
  945. String url = null;
  946. if (error.containsKey("url")) {
  947. url = error.getString("url");
  948. }
  949. if (html.length() != 0) {
  950. /* 45 min */
  951. VNotification n = VNotification
  952. .createNotification(1000 * 60 * 45);
  953. n.addEventListener(new NotificationRedirect(url));
  954. n.show(html, VNotification.CENTERED_TOP,
  955. VNotification.STYLE_SYSTEM);
  956. } else {
  957. redirect(url);
  958. }
  959. applicationRunning = false;
  960. }
  961. if (validatingLayouts) {
  962. VConsole.printLayoutProblems(meta,
  963. ApplicationConnection.this,
  964. zeroHeightComponents, zeroWidthComponents);
  965. zeroHeightComponents = null;
  966. zeroWidthComponents = null;
  967. validatingLayouts = false;
  968. }
  969. }
  970. if (repaintAll) {
  971. /*
  972. * idToPaintableDetail is already cleanded at the start of
  973. * the changeset handling, bypass cleanup.
  974. */
  975. paintableMap.purgeUnregistryBag(false);
  976. } else {
  977. paintableMap.purgeUnregistryBag(true);
  978. }
  979. // TODO build profiling for widget impl loading time
  980. final long prosessingTime = (new Date().getTime())
  981. - start.getTime();
  982. VConsole.log(" Processing time was "
  983. + String.valueOf(prosessingTime) + "ms for "
  984. + jsonText.length() + " characters of JSON");
  985. VConsole.log("Referenced paintables: " + paintableMap.size());
  986. endRequest();
  987. }
  988. };
  989. ApplicationConfiguration.runWhenWidgetsLoaded(c);
  990. }
  991. // Redirect browser, null reloads current page
  992. private static native void redirect(String url)
  993. /*-{
  994. if (url) {
  995. $wnd.location = url;
  996. } else {
  997. $wnd.location.reload(false);
  998. }
  999. }-*/;
  1000. private void addVariableToQueue(String paintableId, String variableName,
  1001. String encodedValue, boolean immediate, char type) {
  1002. final String id = paintableId + VAR_FIELD_SEPARATOR + variableName
  1003. + VAR_FIELD_SEPARATOR + type;
  1004. for (int i = 1; i < pendingVariables.size(); i += 2) {
  1005. if ((pendingVariables.get(i)).equals(id)) {
  1006. pendingVariables.remove(i - 1);
  1007. pendingVariables.remove(i - 1);
  1008. break;
  1009. }
  1010. }
  1011. pendingVariables.add(encodedValue);
  1012. pendingVariables.add(id);
  1013. if (immediate) {
  1014. sendPendingVariableChanges();
  1015. }
  1016. }
  1017. /**
  1018. * This method sends currently queued variable changes to server. It is
  1019. * called when immediate variable update must happen.
  1020. *
  1021. * To ensure correct order for variable changes (due servers multithreading
  1022. * or network), we always wait for active request to be handler before
  1023. * sending a new one. If there is an active request, we will put varible
  1024. * "burst" to queue that will be purged after current request is handled.
  1025. *
  1026. */
  1027. @SuppressWarnings("unchecked")
  1028. public void sendPendingVariableChanges() {
  1029. if (applicationRunning) {
  1030. if (hasActiveRequest()) {
  1031. // skip empty queues if there are pending bursts to be sent
  1032. if (pendingVariables.size() > 0
  1033. || pendingVariableBursts.size() == 0) {
  1034. ArrayList<String> burst = (ArrayList<String>) pendingVariables
  1035. .clone();
  1036. pendingVariableBursts.add(burst);
  1037. pendingVariables.clear();
  1038. }
  1039. } else {
  1040. buildAndSendVariableBurst(pendingVariables, false);
  1041. }
  1042. }
  1043. }
  1044. /**
  1045. * Build the variable burst and send it to server.
  1046. *
  1047. * When sync is forced, we also force sending of all pending variable-bursts
  1048. * at the same time. This is ok as we can assume that DOM will never be
  1049. * updated after this.
  1050. *
  1051. * @param pendingVariables
  1052. * Vector of variable changes to send
  1053. * @param forceSync
  1054. * Should we use synchronous request?
  1055. */
  1056. private void buildAndSendVariableBurst(ArrayList<String> pendingVariables,
  1057. boolean forceSync) {
  1058. final StringBuffer req = new StringBuffer();
  1059. while (!pendingVariables.isEmpty()) {
  1060. if (ApplicationConfiguration.isDebugMode()) {
  1061. Util.logVariableBurst(this, pendingVariables);
  1062. }
  1063. for (int i = 0; i < pendingVariables.size(); i++) {
  1064. if (i > 0) {
  1065. if (i % 2 == 0) {
  1066. req.append(VAR_RECORD_SEPARATOR);
  1067. } else {
  1068. req.append(VAR_FIELD_SEPARATOR);
  1069. }
  1070. }
  1071. req.append(pendingVariables.get(i));
  1072. }
  1073. pendingVariables.clear();
  1074. // Append all the busts to this synchronous request
  1075. if (forceSync && !pendingVariableBursts.isEmpty()) {
  1076. pendingVariables = pendingVariableBursts.get(0);
  1077. pendingVariableBursts.remove(0);
  1078. req.append(VAR_BURST_SEPARATOR);
  1079. }
  1080. }
  1081. // Include the browser detail parameters if they aren't already sent
  1082. String extraParams;
  1083. if (!getConfiguration().isBrowserDetailsSent()) {
  1084. extraParams = getNativeBrowserDetailsParameters(getConfiguration()
  1085. .getRootPanelId());
  1086. getConfiguration().setBrowserDetailsSent();
  1087. } else {
  1088. extraParams = "";
  1089. }
  1090. makeUidlRequest(req.toString(), extraParams, forceSync);
  1091. }
  1092. private void makeUidlRequest(String string) {
  1093. makeUidlRequest(string, "", false);
  1094. }
  1095. /**
  1096. * Sends a new value for the given paintables given variable to the server.
  1097. * <p>
  1098. * The update is actually queued to be sent at a suitable time. If immediate
  1099. * is true, the update is sent as soon as possible. If immediate is false,
  1100. * the update will be sent along with the next immediate update.
  1101. * </p>
  1102. *
  1103. * @param paintableId
  1104. * the id of the paintable that owns the variable
  1105. * @param variableName
  1106. * the name of the variable
  1107. * @param newValue
  1108. * the new value to be sent
  1109. * @param immediate
  1110. * true if the update is to be sent as soon as possible
  1111. */
  1112. public void updateVariable(String paintableId, String variableName,
  1113. VPaintable newValue, boolean immediate) {
  1114. String pid = paintableMap.getPid(newValue);
  1115. addVariableToQueue(paintableId, variableName, pid, immediate, 'p');
  1116. }
  1117. /**
  1118. * Sends a new value for the given paintables given variable to the server.
  1119. * <p>
  1120. * The update is actually queued to be sent at a suitable time. If immediate
  1121. * is true, the update is sent as soon as possible. If immediate is false,
  1122. * the update will be sent along with the next immediate update.
  1123. * </p>
  1124. *
  1125. * @param paintableId
  1126. * the id of the paintable that owns the variable
  1127. * @param variableName
  1128. * the name of the variable
  1129. * @param newValue
  1130. * the new value to be sent
  1131. * @param immediate
  1132. * true if the update is to be sent as soon as possible
  1133. */
  1134. public void updateVariable(String paintableId, String variableName,
  1135. String newValue, boolean immediate) {
  1136. addVariableToQueue(paintableId, variableName,
  1137. escapeVariableValue(newValue), immediate, 's');
  1138. }
  1139. /**
  1140. * Sends a new value for the given paintables given variable to the server.
  1141. * <p>
  1142. * The update is actually queued to be sent at a suitable time. If immediate
  1143. * is true, the update is sent as soon as possible. If immediate is false,
  1144. * the update will be sent along with the next immediate update.
  1145. * </p>
  1146. *
  1147. * @param paintableId
  1148. * the id of the paintable that owns the variable
  1149. * @param variableName
  1150. * the name of the variable
  1151. * @param newValue
  1152. * the new value to be sent
  1153. * @param immediate
  1154. * true if the update is to be sent as soon as possible
  1155. */
  1156. public void updateVariable(String paintableId, String variableName,
  1157. int newValue, boolean immediate) {
  1158. addVariableToQueue(paintableId, variableName, "" + newValue, immediate,
  1159. 'i');
  1160. }
  1161. /**
  1162. * Sends a new value for the given paintables given variable to the server.
  1163. * <p>
  1164. * The update is actually queued to be sent at a suitable time. If immediate
  1165. * is true, the update is sent as soon as possible. If immediate is false,
  1166. * the update will be sent along with the next immediate update.
  1167. * </p>
  1168. *
  1169. * @param paintableId
  1170. * the id of the paintable that owns the variable
  1171. * @param variableName
  1172. * the name of the variable
  1173. * @param newValue
  1174. * the new value to be sent
  1175. * @param immediate
  1176. * true if the update is to be sent as soon as possible
  1177. */
  1178. public void updateVariable(String paintableId, String variableName,
  1179. long newValue, boolean immediate) {
  1180. addVariableToQueue(paintableId, variableName, "" + newValue, immediate,
  1181. 'l');
  1182. }
  1183. /**
  1184. * Sends a new value for the given paintables given variable to the server.
  1185. * <p>
  1186. * The update is actually queued to be sent at a suitable time. If immediate
  1187. * is true, the update is sent as soon as possible. If immediate is false,
  1188. * the update will be sent along with the next immediate update.
  1189. * </p>
  1190. *
  1191. * @param paintableId
  1192. * the id of the paintable that owns the variable
  1193. * @param variableName
  1194. * the name of the variable
  1195. * @param newValue
  1196. * the new value to be sent
  1197. * @param immediate
  1198. * true if the update is to be sent as soon as possible
  1199. */
  1200. public void updateVariable(String paintableId, String variableName,
  1201. float newValue, boolean immediate) {
  1202. addVariableToQueue(paintableId, variableName, "" + newValue, immediate,
  1203. 'f');
  1204. }
  1205. /**
  1206. * Sends a new value for the given paintables given variable to the server.
  1207. * <p>
  1208. * The update is actually queued to be sent at a suitable time. If immediate
  1209. * is true, the update is sent as soon as possible. If immediate is false,
  1210. * the update will be sent along with the next immediate update.
  1211. * </p>
  1212. *
  1213. * @param paintableId
  1214. * the id of the paintable that owns the variable
  1215. * @param variableName
  1216. * the name of the variable
  1217. * @param newValue
  1218. * the new value to be sent
  1219. * @param immediate
  1220. * true if the update is to be sent as soon as possible
  1221. */
  1222. public void updateVariable(String paintableId, String variableName,
  1223. double newValue, boolean immediate) {
  1224. addVariableToQueue(paintableId, variableName, "" + newValue, immediate,
  1225. 'd');
  1226. }
  1227. /**
  1228. * Sends a new value for the given paintables given variable to the server.
  1229. * <p>
  1230. * The update is actually queued to be sent at a suitable time. If immediate
  1231. * is true, the update is sent as soon as possible. If immediate is false,
  1232. * the update will be sent along with the next immediate update.
  1233. * </p>
  1234. *
  1235. * @param paintableId
  1236. * the id of the paintable that owns the variable
  1237. * @param variableName
  1238. * the name of the variable
  1239. * @param newValue
  1240. * the new value to be sent
  1241. * @param immediate
  1242. * true if the update is to be sent as soon as possible
  1243. */
  1244. public void updateVariable(String paintableId, String variableName,
  1245. boolean newValue, boolean immediate) {
  1246. addVariableToQueue(paintableId, variableName, newValue ? "true"
  1247. : "false", immediate, 'b');
  1248. }
  1249. /**
  1250. * Sends a new value for the given paintables given variable to the server.
  1251. * <p>
  1252. * The update is actually queued to be sent at a suitable time. If immediate
  1253. * is true, the update is sent as soon as possible. If immediate is false,
  1254. * the update will be sent along with the next immediate update.
  1255. * </p>
  1256. *
  1257. * @param paintableId
  1258. * the id of the paintable that owns the variable
  1259. * @param variableName
  1260. * the name of the variable
  1261. * @param map
  1262. * the new value to be sent
  1263. * @param immediate
  1264. * true if the update is to be sent as soon as possible
  1265. */
  1266. public void updateVariable(String paintableId, String variableName,
  1267. Map<String, Object> map, boolean immediate) {
  1268. final StringBuffer buf = new StringBuffer();
  1269. Iterator<String> iterator = map.keySet().iterator();
  1270. while (iterator.hasNext()) {
  1271. String key = iterator.next();
  1272. Object value = map.get(key);
  1273. char transportType = getTransportType(value);
  1274. buf.append(transportType);
  1275. buf.append(escapeVariableValue(key));
  1276. buf.append(VAR_ARRAYITEM_SEPARATOR);
  1277. if (transportType == 'p') {
  1278. buf.append(paintableMap.getPid((VPaintable) value));
  1279. } else {
  1280. buf.append(escapeVariableValue(String.valueOf(value)));
  1281. }
  1282. if (iterator.hasNext()) {
  1283. buf.append(VAR_ARRAYITEM_SEPARATOR);
  1284. }
  1285. }
  1286. addVariableToQueue(paintableId, variableName, buf.toString(),
  1287. immediate, 'm');
  1288. }
  1289. private char getTransportType(Object value) {
  1290. if (value instanceof String) {
  1291. return 's';
  1292. } else if (value instanceof VPaintableWidget) {
  1293. return 'p';
  1294. } else if (value instanceof Boolean) {
  1295. return 'b';
  1296. } else if (value instanceof Integer) {
  1297. return 'i';
  1298. } else if (value instanceof Float) {
  1299. return 'f';
  1300. } else if (value instanceof Double) {
  1301. return 'd';
  1302. } else if (value instanceof Long) {
  1303. return 'l';
  1304. } else if (value instanceof Enum) {
  1305. return 's'; // transported as string representation
  1306. }
  1307. return 'u';
  1308. }
  1309. /**
  1310. * Sends a new value for the given paintables given variable to the server.
  1311. *
  1312. * The update is actually queued to be sent at a suitable time. If immediate
  1313. * is true, the update is sent as soon as possible. If immediate is false,
  1314. * the update will be sent along with the next immediate update.
  1315. *
  1316. * A null array is sent as an empty array.
  1317. *
  1318. * @param paintableId
  1319. * the id of the paintable that owns the variable
  1320. * @param variableName
  1321. * the name of the variable
  1322. * @param newValue
  1323. * the new value to be sent
  1324. * @param immediate
  1325. * true if the update is to be sent as soon as possible
  1326. */
  1327. public void updateVariable(String paintableId, String variableName,
  1328. String[] values, boolean immediate) {
  1329. final StringBuffer buf = new StringBuffer();
  1330. if (values != null) {
  1331. for (int i = 0; i < values.length; i++) {
  1332. buf.append(escapeVariableValue(values[i]));
  1333. // there will be an extra separator at the end to differentiate
  1334. // between an empty array and one containing an empty string
  1335. // only
  1336. buf.append(VAR_ARRAYITEM_SEPARATOR);
  1337. }
  1338. }
  1339. addVariableToQueue(paintableId, variableName, buf.toString(),
  1340. immediate, 'c');
  1341. }
  1342. /**
  1343. * Sends a new value for the given paintables given variable to the server.
  1344. *
  1345. * The update is actually queued to be sent at a suitable time. If immediate
  1346. * is true, the update is sent as soon as possible. If immediate is false,
  1347. * the update will be sent along with the next immediate update. </p>
  1348. *
  1349. * A null array is sent as an empty array.
  1350. *
  1351. *
  1352. * @param paintableId
  1353. * the id of the paintable that owns the variable
  1354. * @param variableName
  1355. * the name of the variable
  1356. * @param newValue
  1357. * the new value to be sent
  1358. * @param immediate
  1359. * true if the update is to be sent as soon as possible
  1360. */
  1361. public void updateVariable(String paintableId, String variableName,
  1362. Object[] values, boolean immediate) {
  1363. final StringBuffer buf = new StringBuffer();
  1364. if (values != null) {
  1365. for (int i = 0; i < values.length; i++) {
  1366. if (i > 0) {
  1367. buf.append(VAR_ARRAYITEM_SEPARATOR);
  1368. }
  1369. Object value = values[i];
  1370. char transportType = getTransportType(value);
  1371. // first char tells the type in array
  1372. buf.append(transportType);
  1373. if (transportType == 'p') {
  1374. buf.append(paintableMap.getPid((VPaintable) value));
  1375. } else {
  1376. buf.append(escapeVariableValue(String.valueOf(value)));
  1377. }
  1378. }
  1379. }
  1380. addVariableToQueue(paintableId, variableName, buf.toString(),
  1381. immediate, 'a');
  1382. }
  1383. /**
  1384. * Encode burst, record, field and array item separator characters in a
  1385. * String for transport over the network. This protects from separator
  1386. * injection attacks.
  1387. *
  1388. * @param value
  1389. * to encode
  1390. * @return encoded value
  1391. */
  1392. protected String escapeVariableValue(String value) {
  1393. final StringBuilder result = new StringBuilder();
  1394. for (int i = 0; i < value.length(); ++i) {
  1395. char character = value.charAt(i);
  1396. switch (character) {
  1397. case VAR_ESCAPE_CHARACTER:
  1398. // fall-through - escape character is duplicated
  1399. case VAR_BURST_SEPARATOR:
  1400. case VAR_RECORD_SEPARATOR:
  1401. case VAR_FIELD_SEPARATOR:
  1402. case VAR_ARRAYITEM_SEPARATOR:
  1403. result.append(VAR_ESCAPE_CHARACTER);
  1404. // encode as letters for easier reading
  1405. result.append(((char) (character + 0x30)));
  1406. break;
  1407. default:
  1408. // the char is not a special one - add it to the result as is
  1409. result.append(character);
  1410. break;
  1411. }
  1412. }
  1413. return result.toString();
  1414. }
  1415. /**
  1416. * Update generic component features.
  1417. *
  1418. * <h2>Selecting correct implementation</h2>
  1419. *
  1420. * <p>
  1421. * The implementation of a component depends on many properties, including
  1422. * styles, component features, etc. Sometimes the user changes those
  1423. * properties after the component has been created. Calling this method in
  1424. * the beginning of your updateFromUIDL -method automatically replaces your
  1425. * component with more appropriate if the requested implementation changes.
  1426. * </p>
  1427. *
  1428. * <h2>Caption, icon, error messages and description</h2>
  1429. *
  1430. * <p>
  1431. * Component can delegate management of caption, icon, error messages and
  1432. * description to parent layout. This is optional an should be decided by
  1433. * component author
  1434. * </p>
  1435. *
  1436. * <h2>Component visibility and disabling</h2>
  1437. *
  1438. * This method will manage component visibility automatically and if
  1439. * component is an instanceof FocusWidget, also handle component disabling
  1440. * when needed.
  1441. *
  1442. * @param component
  1443. * Widget to be updated, expected to implement an instance of
  1444. * Paintable
  1445. * @param uidl
  1446. * UIDL to be painted
  1447. * @param manageCaption
  1448. * True if you want to delegate caption, icon, description and
  1449. * error message management to parent.
  1450. *
  1451. * @return Returns true iff no further painting is needed by caller
  1452. */
  1453. @Deprecated
  1454. public boolean updateComponent(VPaintableWidget paintable, UIDL uidl,
  1455. boolean manageCaption) {
  1456. Widget component = paintable.getWidgetForPaintable();
  1457. String pid = paintableMap.getPid(paintable);
  1458. if (pid == null) {
  1459. VConsole.error("Trying to update an unregistered component: "
  1460. + Util.getSimpleName(component));
  1461. return true;
  1462. }
  1463. // If the server request that a cached instance should be used, do
  1464. // nothing
  1465. if (uidl.getBooleanAttribute("cached")) {
  1466. return true;
  1467. }
  1468. // register the listened events by the server-side to the event-handler
  1469. // of the component
  1470. paintableMap.registerEventListenersFromUIDL(pid, uidl);
  1471. // Visibility
  1472. boolean visible = !uidl.getBooleanAttribute("invisible");
  1473. boolean wasVisible = component.isVisible();
  1474. component.setVisible(visible);
  1475. if (wasVisible != visible) {
  1476. // Changed invisibile <-> visible
  1477. if (wasVisible && manageCaption) {
  1478. // Must hide caption when component is hidden
  1479. paintable.getParent().updateCaption(paintable, uidl);
  1480. }
  1481. }
  1482. if (configuration.useDebugIdInDOM() && uidl.getId().startsWith("PID_S")) {
  1483. DOM.setElementProperty(component.getElement(), "id", uidl.getId()
  1484. .substring(5));
  1485. }
  1486. if (!visible) {
  1487. // component is invisible, delete old size to notify parent, if
  1488. // later make visible
  1489. paintableMap.setOffsetSize(paintable, null);
  1490. return true;
  1491. }
  1492. boolean enabled = !uidl.getBooleanAttribute("disabled");
  1493. if (uidl.hasAttribute("tabindex") && component instanceof Focusable) {
  1494. ((Focusable) component).setTabIndex(uidl
  1495. .getIntAttribute("tabindex"));
  1496. }
  1497. /*
  1498. * Disabled state may affect (override) tabindex so the order must be
  1499. * first setting tabindex, then enabled state.
  1500. */
  1501. if (component instanceof FocusWidget) {
  1502. FocusWidget fw = (FocusWidget) component;
  1503. fw.setEnabled(enabled);
  1504. }
  1505. // Style names
  1506. component.setStyleName(getStyleName(component.getStylePrimaryName(),
  1507. uidl, component instanceof Field));
  1508. TooltipInfo tooltipInfo = paintableMap.getTooltipInfo(paintable, null);
  1509. // Update tooltip
  1510. if (uidl.hasAttribute(ATTRIBUTE_DESCRIPTION)) {
  1511. tooltipInfo
  1512. .setTitle(uidl.getStringAttribute(ATTRIBUTE_DESCRIPTION));
  1513. } else {
  1514. tooltipInfo.setTitle(null);
  1515. }
  1516. // Set captions
  1517. if (manageCaption) {
  1518. paintable.getParent().updateCaption(paintable, uidl);
  1519. }
  1520. // add error classname to components w/ error
  1521. if (uidl.hasAttribute(ATTRIBUTE_ERROR)) {
  1522. tooltipInfo.setErrorUidl(uidl.getErrors());
  1523. } else {
  1524. tooltipInfo.setErrorUidl(null);
  1525. }
  1526. /*
  1527. * updateComponentSize need to be after caption update so caption can be
  1528. * taken into account
  1529. */
  1530. updateComponentSize(paintable, uidl);
  1531. return false;
  1532. }
  1533. /**
  1534. * Generates the style name for the widget based on the given primary style
  1535. * name (typically returned by Widget.getPrimaryStyleName()) and the UIDL.
  1536. * An additional "modified" style name can be added if the field parameter
  1537. * is set to true.
  1538. *
  1539. * @param primaryStyleName
  1540. * @param uidl
  1541. * @param isField
  1542. * @return
  1543. */
  1544. public static String getStyleName(String primaryStyleName, UIDL uidl,
  1545. boolean field) {
  1546. boolean enabled = !uidl.getBooleanAttribute("disabled");
  1547. StringBuffer styleBuf = new StringBuffer();
  1548. styleBuf.append(primaryStyleName);
  1549. // first disabling and read-only status
  1550. if (!enabled) {
  1551. styleBuf.append(" ");
  1552. styleBuf.append(DISABLED_CLASSNAME);
  1553. }
  1554. if (uidl.getBooleanAttribute("readonly")) {
  1555. styleBuf.append(" ");
  1556. styleBuf.append("v-readonly");
  1557. }
  1558. // add additional styles as css classes, prefixed with component default
  1559. // stylename
  1560. if (uidl.hasAttribute("style")) {
  1561. final String[] styles = uidl.getStringAttribute("style").split(" ");
  1562. for (int i = 0; i < styles.length; i++) {
  1563. styleBuf.append(" ");
  1564. styleBuf.append(primaryStyleName);
  1565. styleBuf.append("-");
  1566. styleBuf.append(styles[i]);
  1567. styleBuf.append(" ");
  1568. styleBuf.append(styles[i]);
  1569. }
  1570. }
  1571. // add modified classname to Fields
  1572. if (field && uidl.hasAttribute("modified")) {
  1573. styleBuf.append(" ");
  1574. styleBuf.append(MODIFIED_CLASSNAME);
  1575. }
  1576. // add error classname to components w/ error
  1577. if (uidl.hasAttribute(ATTRIBUTE_ERROR)) {
  1578. styleBuf.append(" ");
  1579. styleBuf.append(primaryStyleName);
  1580. styleBuf.append(ERROR_CLASSNAME_EXT);
  1581. }
  1582. // add required style to required components
  1583. if (uidl.hasAttribute("required")) {
  1584. styleBuf.append(" ");
  1585. styleBuf.append(primaryStyleName);
  1586. styleBuf.append(REQUIRED_CLASSNAME_EXT);
  1587. }
  1588. return styleBuf.toString();
  1589. }
  1590. private void updateComponentSize(VPaintableWidget paintable, UIDL uidl) {
  1591. String w = uidl.hasAttribute("width") ? uidl
  1592. .getStringAttribute("width") : "";
  1593. String h = uidl.hasAttribute("height") ? uidl
  1594. .getStringAttribute("height") : "";
  1595. Widget component = paintableMap.getWidget(paintable);
  1596. Style style = component.getElement().getStyle();
  1597. // Dirty if either dimension changed between relative and non-relative
  1598. if (w.endsWith("%") != style.getWidth().endsWith("%")
  1599. || h.endsWith("%") != style.getHeight().endsWith("%")) {
  1600. MeasureManager.MeasuredSize measuredSize = getMeasuredSize(paintable);
  1601. if (measuredSize != null) {
  1602. measuredSize.setDirty(true);
  1603. }
  1604. }
  1605. // Set defined sizes
  1606. component.setHeight(h);
  1607. component.setWidth(w);
  1608. }
  1609. /**
  1610. * Traverses recursively child widgets until ContainerResizedListener child
  1611. * widget is found. They will delegate it further if needed.
  1612. *
  1613. * @param container
  1614. */
  1615. private boolean runningLayout = false;
  1616. /**
  1617. * Causes a re-calculation/re-layout of all paintables in a container.
  1618. *
  1619. * @param container
  1620. */
  1621. public void runDescendentsLayout(HasWidgets container) {
  1622. if (runningLayout) {
  1623. return;
  1624. }
  1625. runningLayout = true;
  1626. internalRunDescendentsLayout(container);
  1627. runningLayout = false;
  1628. }
  1629. /**
  1630. * This will cause re-layouting of all components. Mainly used for
  1631. * development. Published to JavaScript.
  1632. */
  1633. public void forceLayout() {
  1634. Duration duration = new Duration();
  1635. doLayout(false);
  1636. VConsole.log("forceLayout in " + duration.elapsedMillis() + " ms");
  1637. }
  1638. private void internalRunDescendentsLayout(HasWidgets container) {
  1639. // getConsole().log(
  1640. // "runDescendentsLayout(" + Util.getSimpleName(container) + ")");
  1641. final Iterator<Widget> childWidgets = container.iterator();
  1642. while (childWidgets.hasNext()) {
  1643. final Widget child = childWidgets.next();
  1644. if (getPaintableMap().isPaintable(child)) {
  1645. if (handleComponentRelativeSize(child)) {
  1646. /*
  1647. * Only need to propagate event if "child" has a relative
  1648. * size
  1649. */
  1650. if (child instanceof ContainerResizedListener) {
  1651. ((ContainerResizedListener) child).iLayout();
  1652. }
  1653. if (child instanceof HasWidgets) {
  1654. final HasWidgets childContainer = (HasWidgets) child;
  1655. internalRunDescendentsLayout(childContainer);
  1656. }
  1657. }
  1658. } else if (child instanceof HasWidgets) {
  1659. // propagate over non Paintable HasWidgets
  1660. internalRunDescendentsLayout((HasWidgets) child);
  1661. }
  1662. }
  1663. }
  1664. /**
  1665. * Converts relative sizes into pixel sizes.
  1666. *
  1667. * @param child
  1668. * @return true if the child has a relative size
  1669. */
  1670. private boolean handleComponentRelativeSize(VPaintableWidget paintable) {
  1671. return false;
  1672. }
  1673. /**
  1674. * Converts relative sizes into pixel sizes.
  1675. *
  1676. * @param child
  1677. * @return true if the child has a relative size
  1678. */
  1679. public boolean handleComponentRelativeSize(Widget widget) {
  1680. return handleComponentRelativeSize(paintableMap.getPaintable(widget));
  1681. }
  1682. /**
  1683. * Get either existing or new Paintable for given UIDL.
  1684. *
  1685. * If corresponding Paintable has been previously painted, return it.
  1686. * Otherwise create and register a new Paintable from UIDL. Caller must
  1687. * update the returned Paintable from UIDL after it has been connected to
  1688. * parent.
  1689. *
  1690. * @param uidl
  1691. * UIDL to create Paintable from.
  1692. * @return Either existing or new Paintable corresponding to UIDL.
  1693. */
  1694. public VPaintableWidget getPaintable(UIDL uidl) {
  1695. final String pid = uidl.getId();
  1696. if (!paintableMap.hasPaintable(pid)) {
  1697. // Create and register a new paintable if no old was found
  1698. VPaintableWidget p = widgetSet.createWidget(uidl, configuration);
  1699. p.setConnection(this);
  1700. p.setId(pid);
  1701. p.init();
  1702. paintableMap.registerPaintable(pid, p);
  1703. }
  1704. return (VPaintableWidget) paintableMap.getPaintable(pid);
  1705. }
  1706. /**
  1707. * Gets a recource that has been pre-loaded via UIDL, such as custom
  1708. * layouts.
  1709. *
  1710. * @param name
  1711. * identifier of the resource to get
  1712. * @return the resource
  1713. */
  1714. public String getResource(String name) {
  1715. return resourcesMap.get(name);
  1716. }
  1717. /**
  1718. * Singleton method to get instance of app's context menu.
  1719. *
  1720. * @return VContextMenu object
  1721. */
  1722. public VContextMenu getContextMenu() {
  1723. if (contextMenu == null) {
  1724. contextMenu = new VContextMenu();
  1725. DOM.setElementProperty(contextMenu.getElement(), "id",
  1726. "PID_VAADIN_CM");
  1727. }
  1728. return contextMenu;
  1729. }
  1730. /**
  1731. * Translates custom protocols in UIDL URI's to be recognizable by browser.
  1732. * All uri's from UIDL should be routed via this method before giving them
  1733. * to browser due URI's in UIDL may contain custom protocols like theme://.
  1734. *
  1735. * @param uidlUri
  1736. * Vaadin URI from uidl
  1737. * @return translated URI ready for browser
  1738. */
  1739. public String translateVaadinUri(String uidlUri) {
  1740. if (uidlUri == null) {
  1741. return null;
  1742. }
  1743. if (uidlUri.startsWith("theme://")) {
  1744. final String themeUri = configuration.getThemeUri();
  1745. if (themeUri == null) {
  1746. VConsole.error("Theme not set: ThemeResource will not be found. ("
  1747. + uidlUri + ")");
  1748. }
  1749. uidlUri = themeUri + uidlUri.substring(7);
  1750. }
  1751. if (uidlUri.startsWith("app://")) {
  1752. uidlUri = getAppUri() + uidlUri.substring(6);
  1753. }
  1754. return uidlUri;
  1755. }
  1756. /**
  1757. * Gets the URI for the current theme. Can be used to reference theme
  1758. * resources.
  1759. *
  1760. * @return URI to the current theme
  1761. */
  1762. public String getThemeUri() {
  1763. return configuration.getThemeUri();
  1764. }
  1765. /**
  1766. * Listens for Notification hide event, and redirects. Used for system
  1767. * messages, such as session expired.
  1768. *
  1769. */
  1770. private class NotificationRedirect implements VNotification.EventListener {
  1771. String url;
  1772. NotificationRedirect(String url) {
  1773. this.url = url;
  1774. }
  1775. public void notificationHidden(HideEvent event) {
  1776. redirect(url);
  1777. }
  1778. }
  1779. /* Extended title handling */
  1780. /**
  1781. * Data showed in tooltips are stored centrilized as it may be needed in
  1782. * varios place: caption, layouts, and in owner components themselves.
  1783. *
  1784. * Updating TooltipInfo is done in updateComponent method.
  1785. *
  1786. */
  1787. public TooltipInfo getTooltipTitleInfo(VPaintableWidget titleOwner,
  1788. Object key) {
  1789. if (null == titleOwner) {
  1790. return null;
  1791. }
  1792. return paintableMap.getTooltipInfo(titleOwner, key);
  1793. }
  1794. private final VTooltip tooltip = new VTooltip(this);
  1795. /**
  1796. * Component may want to delegate Tooltip handling to client. Layouts add
  1797. * Tooltip (description, errors) to caption, but some components may want
  1798. * them to appear one other elements too.
  1799. *
  1800. * Events wanted by this handler are same as in Tooltip.TOOLTIP_EVENTS
  1801. *
  1802. * @param event
  1803. * @param owner
  1804. */
  1805. public void handleTooltipEvent(Event event, VPaintableWidget owner) {
  1806. tooltip.handleTooltipEvent(event, owner, null);
  1807. }
  1808. /**
  1809. * Component may want to delegate Tooltip handling to client. Layouts add
  1810. * Tooltip (description, errors) to caption, but some components may want
  1811. * them to appear one other elements too.
  1812. *
  1813. * Events wanted by this handler are same as in Tooltip.TOOLTIP_EVENTS
  1814. *
  1815. * @param event
  1816. * @param owner
  1817. * @param key
  1818. * the key for tooltip if this is "additional" tooltip, null for
  1819. * components "main tooltip"
  1820. */
  1821. public void handleTooltipEvent(Event event, VPaintableWidget owner,
  1822. Object key) {
  1823. tooltip.handleTooltipEvent(event, owner, key);
  1824. }
  1825. /*
  1826. * Helper to run layout functions triggered by child components with a
  1827. * decent interval.
  1828. */
  1829. private final Timer layoutTimer = new Timer() {
  1830. private boolean isPending = false;
  1831. @Override
  1832. public void schedule(int delayMillis) {
  1833. if (!isPending) {
  1834. super.schedule(delayMillis);
  1835. isPending = true;
  1836. }
  1837. }
  1838. @Override
  1839. public void run() {
  1840. VConsole.log("Running re-layout of " + view.getClass().getName());
  1841. runDescendentsLayout(view.getWidgetForPaintable());
  1842. isPending = false;
  1843. }
  1844. };
  1845. private VPaintableMap paintableMap = GWT.create(VPaintableMap.class);
  1846. /**
  1847. * Components can call this function to run all layout functions. This is
  1848. * usually done, when component knows that its size has changed.
  1849. */
  1850. public void requestLayoutPhase() {
  1851. layoutTimer.schedule(500);
  1852. }
  1853. protected String getUidlSecurityKey() {
  1854. return uidlSecurityKey;
  1855. }
  1856. /**
  1857. * Use to notify that the given component's caption has changed; layouts may
  1858. * have to be recalculated.
  1859. *
  1860. * @param component
  1861. * the Paintable whose caption has changed
  1862. */
  1863. public void captionSizeUpdated(Widget widget) {
  1864. componentCaptionSizeChanges.add(widget);
  1865. }
  1866. /**
  1867. * Gets the main view, a.k.a top-level window.
  1868. *
  1869. * @return the main view
  1870. */
  1871. public VViewPaintable getView() {
  1872. return view;
  1873. }
  1874. /**
  1875. * If component has several tooltips in addition to the one provided by
  1876. * {@link com.vaadin.ui.AbstractComponent}, component can register them with
  1877. * this method.
  1878. * <p>
  1879. * Component must also pipe events to
  1880. * {@link #handleTooltipEvent(Event, VPaintableWidget, Object)} method.
  1881. * <p>
  1882. * This method can also be used to deregister tooltips by using null as
  1883. * tooltip
  1884. *
  1885. * @param paintable
  1886. * Paintable "owning" this tooltip
  1887. * @param key
  1888. * key assosiated with given tooltip. Can be any object. For
  1889. * example a related dom element. Same key must be given for
  1890. * {@link #handleTooltipEvent(Event, VPaintableWidget, Object)}
  1891. * method.
  1892. *
  1893. * @param tooltip
  1894. * the TooltipInfo object containing details shown in tooltip,
  1895. * null if deregistering tooltip
  1896. */
  1897. public void registerTooltip(VPaintableWidget paintable, Object key,
  1898. TooltipInfo tooltip) {
  1899. paintableMap.registerTooltip(paintable, key, tooltip);
  1900. }
  1901. /**
  1902. * Gets the {@link ApplicationConfiguration} for the current application.
  1903. *
  1904. * @see ApplicationConfiguration
  1905. * @return the configuration for this application
  1906. */
  1907. public ApplicationConfiguration getConfiguration() {
  1908. return configuration;
  1909. }
  1910. /**
  1911. * Checks if there is a registered server side listener for the event. The
  1912. * list of events which has server side listeners is updated automatically
  1913. * before the component is updated so the value is correct if called from
  1914. * updatedFromUIDL.
  1915. *
  1916. * @param eventIdentifier
  1917. * The identifier for the event
  1918. * @return true if at least one listener has been registered on server side
  1919. * for the event identified by eventIdentifier.
  1920. */
  1921. public boolean hasEventListeners(VPaintableWidget paintable,
  1922. String eventIdentifier) {
  1923. return paintableMap.hasEventListeners(paintable, eventIdentifier);
  1924. }
  1925. /**
  1926. * Adds the get parameters to the uri and returns the new uri that contains
  1927. * the parameters.
  1928. *
  1929. * @param uri
  1930. * The uri to which the parameters should be added.
  1931. * @param extraParams
  1932. * One or more parameters in the format "a=b" or "c=d&e=f". An
  1933. * empty string is allowed but will not modify the url.
  1934. * @return The modified URI with the get parameters in extraParams added.
  1935. */
  1936. public static String addGetParameters(String uri, String extraParams) {
  1937. if (extraParams == null || extraParams.length() == 0) {
  1938. return uri;
  1939. }
  1940. // RFC 3986: The query component is indicated by the first question
  1941. // mark ("?") character and terminated by a number sign ("#") character
  1942. // or by the end of the URI.
  1943. String fragment = null;
  1944. int hashPosition = uri.indexOf('#');
  1945. if (hashPosition != -1) {
  1946. // Fragment including "#"
  1947. fragment = uri.substring(hashPosition);
  1948. // The full uri before the fragment
  1949. uri = uri.substring(0, hashPosition);
  1950. }
  1951. if (uri.contains("?")) {
  1952. uri += "&";
  1953. } else {
  1954. uri += "?";
  1955. }
  1956. uri += extraParams;
  1957. if (fragment != null) {
  1958. uri += fragment;
  1959. }
  1960. return uri;
  1961. }
  1962. VPaintableMap getPaintableMap() {
  1963. return paintableMap;
  1964. }
  1965. @Deprecated
  1966. public void unregisterPaintable(VPaintable p) {
  1967. paintableMap.unregisterPaintable(p);
  1968. }
  1969. public VTooltip getVTooltip() {
  1970. return tooltip;
  1971. }
  1972. @Deprecated
  1973. public void handleWidgetTooltipEvent(Event event, Widget owner, Object key) {
  1974. handleTooltipEvent(event, getPaintableMap().getPaintable(owner), key);
  1975. }
  1976. @Deprecated
  1977. public void handleWidgetTooltipEvent(Event event, Widget owner) {
  1978. handleTooltipEvent(event, getPaintableMap().getPaintable(owner));
  1979. }
  1980. @Deprecated
  1981. public void registerWidgetTooltip(Widget owner, Object key, TooltipInfo info) {
  1982. registerTooltip(getPaintableMap().getPaintable(owner), key, info);
  1983. }
  1984. @Deprecated
  1985. public boolean hasWidgetEventListeners(Widget widget, String eventIdentifier) {
  1986. return hasEventListeners(getPaintableMap().getPaintable(widget),
  1987. eventIdentifier);
  1988. }
  1989. private boolean layoutScheduled = false;
  1990. private ScheduledCommand layoutCommand = new ScheduledCommand() {
  1991. public void execute() {
  1992. layoutScheduled = false;
  1993. measureManager.doLayout(ApplicationConnection.this);
  1994. }
  1995. };
  1996. public void doLayout(boolean lazy) {
  1997. if (!lazy) {
  1998. layoutCommand.execute();
  1999. } else if (!layoutScheduled) {
  2000. layoutScheduled = true;
  2001. Scheduler.get().scheduleDeferred(layoutCommand);
  2002. }
  2003. }
  2004. public MeasureManager.MeasuredSize getMeasuredSize(
  2005. VPaintableWidget paintable) {
  2006. return paintableMap.getMeasuredSize(paintable);
  2007. }
  2008. }