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.

UIDL.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import java.util.HashSet;
  18. import java.util.Iterator;
  19. import java.util.Set;
  20. import com.google.gwt.core.client.JavaScriptObject;
  21. import com.google.gwt.core.client.JsArrayString;
  22. import com.vaadin.server.PaintTarget;
  23. import com.vaadin.ui.AbstractComponent;
  24. import com.vaadin.ui.Component;
  25. /**
  26. * When a component is updated, it's client side widget's
  27. * {@link ComponentConnector#updateFromUIDL(UIDL, ApplicationConnection)
  28. * updateFromUIDL()} will be called with the updated ("changes") UIDL received
  29. * from the server.
  30. * <p>
  31. * UIDL is hierarchical, and there are a few methods to retrieve the children,
  32. * {@link #getChildCount()}, {@link #getChildIterator()}
  33. * {@link #getChildString(int)}, {@link #getChildUIDL(int)}.
  34. * </p>
  35. * <p>
  36. * It can be helpful to keep in mind that UIDL was originally modeled in XML, so
  37. * it's structure is very XML -like. For instance, the first to children in the
  38. * underlying UIDL representation will contain the "tag" name and attributes,
  39. * but will be skipped by the methods mentioned above.
  40. * </p>
  41. */
  42. public final class UIDL extends JavaScriptObject {
  43. protected UIDL() {
  44. }
  45. /**
  46. * Shorthand for getting the attribute named "id", which for Paintables is
  47. * the essential paintableId which binds the server side component to the
  48. * client side widget.
  49. *
  50. * @return the value of the id attribute, if available
  51. */
  52. public String getId() {
  53. return getStringAttribute("id");
  54. }
  55. /**
  56. * Gets the name of this UIDL section, as created with
  57. * {@link PaintTarget#startTag(String) PaintTarget.startTag()} in the
  58. * server-side {@link Component#paint(PaintTarget) Component.paint()} or
  59. * (usually) {@link AbstractComponent#paintContent(PaintTarget)
  60. * AbstractComponent.paintContent()}. Note that if the UIDL corresponds to a
  61. * Paintable, a component identifier will be returned instead - this is used
  62. * internally and is not needed within
  63. * {@link ComponentConnector#updateFromUIDL(UIDL, ApplicationConnection)
  64. * updateFromUIDL()}.
  65. *
  66. * @return the name for this section
  67. */
  68. public native String getTag()
  69. /*-{
  70. return this[0];
  71. }-*/;
  72. private native ValueMap attr()
  73. /*-{
  74. return this[1];
  75. }-*/;
  76. private native ValueMap var()
  77. /*-{
  78. return this[1]["v"];
  79. }-*/;
  80. private native boolean hasVariables()
  81. /*-{
  82. return Boolean(this[1]["v"]);
  83. }-*/;
  84. /**
  85. * Gets the named attribute as a String.
  86. *
  87. * @param name
  88. * the name of the attribute to get
  89. * @return the attribute value
  90. */
  91. public String getStringAttribute(String name) {
  92. return attr().getString(name);
  93. }
  94. /**
  95. * Gets the names of the attributes available.
  96. *
  97. * @return the names of available attributes
  98. */
  99. public Set<String> getAttributeNames() {
  100. Set<String> keySet = attr().getKeySet();
  101. keySet.remove("v");
  102. return keySet;
  103. }
  104. /**
  105. * Gets the names of variables available.
  106. *
  107. * @return the names of available variables
  108. */
  109. public Set<String> getVariableNames() {
  110. if (!hasVariables()) {
  111. return new HashSet<String>();
  112. } else {
  113. Set<String> keySet = var().getKeySet();
  114. return keySet;
  115. }
  116. }
  117. /**
  118. * Gets the named attribute as an int.
  119. *
  120. * @param name
  121. * the name of the attribute to get
  122. * @return the attribute value
  123. */
  124. public int getIntAttribute(String name) {
  125. return attr().getInt(name);
  126. }
  127. /**
  128. * Gets the named attribute as a long.
  129. *
  130. * @param name
  131. * the name of the attribute to get
  132. * @return the attribute value
  133. */
  134. public long getLongAttribute(String name) {
  135. return (long) attr().getRawNumber(name);
  136. }
  137. /**
  138. * Gets the named attribute as a float.
  139. *
  140. * @param name
  141. * the name of the attribute to get
  142. * @return the attribute value
  143. */
  144. public float getFloatAttribute(String name) {
  145. return (float) attr().getRawNumber(name);
  146. }
  147. /**
  148. * Gets the named attribute as a double.
  149. *
  150. * @param name
  151. * the name of the attribute to get
  152. * @return the attribute value
  153. */
  154. public double getDoubleAttribute(String name) {
  155. return attr().getRawNumber(name);
  156. }
  157. /**
  158. * Gets the named attribute as a boolean.
  159. *
  160. * @param name
  161. * the name of the attribute to get
  162. * @return the attribute value
  163. */
  164. public boolean getBooleanAttribute(String name) {
  165. return attr().getBoolean(name);
  166. }
  167. /**
  168. * Gets the named attribute as a Map of named values (key/value pairs).
  169. *
  170. * @param name
  171. * the name of the attribute to get
  172. * @return the attribute Map
  173. */
  174. public ValueMap getMapAttribute(String name) {
  175. return attr().getValueMap(name);
  176. }
  177. /**
  178. * Gets the named attribute as an array of Strings.
  179. *
  180. * @param name
  181. * the name of the attribute to get
  182. * @return the attribute value
  183. */
  184. public String[] getStringArrayAttribute(String name) {
  185. return attr().getStringArray(name);
  186. }
  187. /**
  188. * Gets the named attribute as an int array.
  189. *
  190. * @param name
  191. * the name of the attribute to get
  192. * @return the attribute value
  193. */
  194. public int[] getIntArrayAttribute(final String name) {
  195. return attr().getIntArray(name);
  196. }
  197. /**
  198. * Get attributes value as string whatever the type is
  199. *
  200. * @param name
  201. * @return string presentation of attribute
  202. */
  203. native String getAttribute(String name)
  204. /*-{
  205. return '' + this[1][name];
  206. }-*/;
  207. native String getVariable(String name)
  208. /*-{
  209. return '' + this[1]['v'][name];
  210. }-*/;
  211. /**
  212. * Indicates whether or not the named attribute is available.
  213. *
  214. * @param name
  215. * the name of the attribute to check
  216. * @return true if the attribute is available, false otherwise
  217. */
  218. public boolean hasAttribute(final String name) {
  219. return attr().containsKey(name);
  220. }
  221. /**
  222. * Gets the UIDL for the child at the given index.
  223. *
  224. * @param i
  225. * the index of the child to get
  226. * @return the UIDL of the child if it exists
  227. */
  228. public native UIDL getChildUIDL(int i)
  229. /*-{
  230. return this[i + 2];
  231. }-*/;
  232. /**
  233. * Gets the child at the given index as a String.
  234. *
  235. * @param i
  236. * the index of the child to get
  237. * @return the String representation of the child if it exists
  238. */
  239. public native String getChildString(int i)
  240. /*-{
  241. return this[i + 2];
  242. }-*/;
  243. private native XML getChildXML(int index)
  244. /*-{
  245. return this[index + 2];
  246. }-*/;
  247. /**
  248. * Gets an iterator that can be used to iterate trough the children of this
  249. * UIDL.
  250. * <p>
  251. * The Object returned by <code>next()</code> will be appropriately typed -
  252. * if it's UIDL, {@link #getTag()} can be used to check which section is in
  253. * question.
  254. * </p>
  255. * <p>
  256. * The basic use case is to iterate over the children of an UIDL update, and
  257. * update the appropriate part of the widget for each child encountered, e.g
  258. * if <code>getTag()</code> returns "color", one would update the widgets
  259. * color to reflect the value of the "color" section.
  260. * </p>
  261. *
  262. * @return an iterator for iterating over UIDL children
  263. */
  264. public Iterator<Object> getChildIterator() {
  265. return new Iterator<Object>() {
  266. int index = -1;
  267. @Override
  268. public void remove() {
  269. throw new UnsupportedOperationException();
  270. }
  271. @Override
  272. public Object next() {
  273. if (hasNext()) {
  274. int typeOfChild = typeOfChild(++index);
  275. switch (typeOfChild) {
  276. case CHILD_TYPE_UIDL:
  277. UIDL childUIDL = getChildUIDL(index);
  278. return childUIDL;
  279. case CHILD_TYPE_STRING:
  280. return getChildString(index);
  281. case CHILD_TYPE_XML:
  282. return getChildXML(index);
  283. default:
  284. throw new IllegalStateException("Illegal child in tag "
  285. + getTag() + " at index " + index);
  286. }
  287. }
  288. return null;
  289. }
  290. @Override
  291. public boolean hasNext() {
  292. int count = getChildCount();
  293. return count > index + 1;
  294. }
  295. };
  296. }
  297. private static final int CHILD_TYPE_STRING = 0;
  298. private static final int CHILD_TYPE_UIDL = 1;
  299. private static final int CHILD_TYPE_XML = 2;
  300. private native int typeOfChild(int index)
  301. /*-{
  302. var t = typeof this[index + 2];
  303. if(t == "object") {
  304. if(typeof(t.length) == "number") {
  305. return 1;
  306. } else {
  307. return 2;
  308. }
  309. } else if (t == "string") {
  310. return 0;
  311. }
  312. return -1;
  313. }-*/;
  314. /**
  315. * @deprecated
  316. */
  317. @Deprecated
  318. public String getChildrenAsXML() {
  319. return toString();
  320. }
  321. /**
  322. * Checks if the named variable is available.
  323. *
  324. * @param name
  325. * the name of the variable desired
  326. * @return true if the variable exists, false otherwise
  327. */
  328. public boolean hasVariable(String name) {
  329. return hasVariables() && var().containsKey(name);
  330. }
  331. /**
  332. * Gets the value of the named variable.
  333. *
  334. * @param name
  335. * the name of the variable
  336. * @return the value of the variable
  337. */
  338. public String getStringVariable(String name) {
  339. return var().getString(name);
  340. }
  341. /**
  342. * Gets the value of the named variable.
  343. *
  344. * @param name
  345. * the name of the variable
  346. * @return the value of the variable
  347. */
  348. public int getIntVariable(String name) {
  349. return var().getInt(name);
  350. }
  351. /**
  352. * Gets the value of the named variable.
  353. *
  354. * @param name
  355. * the name of the variable
  356. * @return the value of the variable
  357. */
  358. public long getLongVariable(String name) {
  359. return (long) var().getRawNumber(name);
  360. }
  361. /**
  362. * Gets the value of the named variable.
  363. *
  364. * @param name
  365. * the name of the variable
  366. * @return the value of the variable
  367. */
  368. public float getFloatVariable(String name) {
  369. return (float) var().getRawNumber(name);
  370. }
  371. /**
  372. * Gets the value of the named variable.
  373. *
  374. * @param name
  375. * the name of the variable
  376. * @return the value of the variable
  377. */
  378. public double getDoubleVariable(String name) {
  379. return var().getRawNumber(name);
  380. }
  381. /**
  382. * Gets the value of the named variable.
  383. *
  384. * @param name
  385. * the name of the variable
  386. * @return the value of the variable
  387. */
  388. public boolean getBooleanVariable(String name) {
  389. return var().getBoolean(name);
  390. }
  391. /**
  392. * Gets the value of the named variable.
  393. *
  394. * @param name
  395. * the name of the variable
  396. * @return the value of the variable
  397. */
  398. public String[] getStringArrayVariable(String name) {
  399. return var().getStringArray(name);
  400. }
  401. /**
  402. * Gets the value of the named String[] variable as a Set of Strings.
  403. *
  404. * @param name
  405. * the name of the variable
  406. * @return the value of the variable
  407. */
  408. public Set<String> getStringArrayVariableAsSet(final String name) {
  409. final HashSet<String> s = new HashSet<String>();
  410. JsArrayString a = var().getJSStringArray(name);
  411. for (int i = 0; i < a.length(); i++) {
  412. s.add(a.get(i));
  413. }
  414. return s;
  415. }
  416. /**
  417. * Gets the value of the named variable.
  418. *
  419. * @param name
  420. * the name of the variable
  421. * @return the value of the variable
  422. */
  423. public int[] getIntArrayVariable(String name) {
  424. return var().getIntArray(name);
  425. }
  426. /**
  427. * @deprecated should not be used anymore
  428. */
  429. @Deprecated
  430. public final static class XML extends JavaScriptObject {
  431. protected XML() {
  432. }
  433. public native String getXMLAsString()
  434. /*-{
  435. var buf = new Array();
  436. var self = this;
  437. for(j in self) {
  438. buf.push("<");
  439. buf.push(j);
  440. buf.push(">");
  441. buf.push(self[j]);
  442. buf.push("</");
  443. buf.push(j);
  444. buf.push(">");
  445. }
  446. return buf.join("");
  447. }-*/;
  448. }
  449. /**
  450. * Returns the number of children.
  451. *
  452. * @return the number of children
  453. */
  454. public native int getChildCount()
  455. /*-{
  456. return this.length - 2;
  457. }-*/;
  458. native boolean isMapAttribute(String name)
  459. /*-{
  460. return typeof this[1][name] == "object";
  461. }-*/;
  462. /**
  463. * Gets the Paintable with the id found in the named attributes's value.
  464. *
  465. * @param name
  466. * the name of the attribute
  467. * @return the Paintable referenced by the attribute, if it exists
  468. */
  469. public ServerConnector getPaintableAttribute(String name,
  470. ApplicationConnection connection) {
  471. return ConnectorMap.get(connection)
  472. .getConnector(getStringAttribute(name));
  473. }
  474. /**
  475. * Gets the Paintable with the id found in the named variable's value.
  476. *
  477. * @param name
  478. * the name of the variable
  479. * @return the Paintable referenced by the variable, if it exists
  480. */
  481. public ServerConnector getPaintableVariable(String name,
  482. ApplicationConnection connection) {
  483. return ConnectorMap.get(connection)
  484. .getConnector(getStringVariable(name));
  485. }
  486. /**
  487. * Returns the child UIDL by its name. If several child nodes exist with the
  488. * given name, the first child UIDL will be returned.
  489. *
  490. * @param tagName
  491. * @return the child UIDL or null if child wit given name was not found
  492. */
  493. public UIDL getChildByTagName(String tagName) {
  494. Iterator<Object> childIterator = getChildIterator();
  495. while (childIterator.hasNext()) {
  496. Object next = childIterator.next();
  497. if (next instanceof UIDL) {
  498. UIDL childUIDL = (UIDL) next;
  499. if (childUIDL.getTag().equals(tagName)) {
  500. return childUIDL;
  501. }
  502. }
  503. }
  504. return null;
  505. }
  506. }