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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import java.util.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(
  285. "Illegal child in tag " + getTag()
  286. + " at index " + index);
  287. }
  288. }
  289. return null;
  290. }
  291. @Override
  292. public boolean hasNext() {
  293. int count = getChildCount();
  294. return count > index + 1;
  295. }
  296. };
  297. }
  298. private static final int CHILD_TYPE_STRING = 0;
  299. private static final int CHILD_TYPE_UIDL = 1;
  300. private static final int CHILD_TYPE_XML = 2;
  301. private native int typeOfChild(int index)
  302. /*-{
  303. var t = typeof this[index + 2];
  304. if(t == "object") {
  305. if(typeof(t.length) == "number") {
  306. return 1;
  307. } else {
  308. return 2;
  309. }
  310. } else if (t == "string") {
  311. return 0;
  312. }
  313. return -1;
  314. }-*/;
  315. /**
  316. * @deprecated
  317. */
  318. @Deprecated
  319. public String getChildrenAsXML() {
  320. return toString();
  321. }
  322. /**
  323. * Checks if the named variable is available.
  324. *
  325. * @param name
  326. * the name of the variable desired
  327. * @return true if the variable exists, false otherwise
  328. */
  329. public boolean hasVariable(String name) {
  330. return hasVariables() && var().containsKey(name);
  331. }
  332. /**
  333. * Gets the value of the named variable.
  334. *
  335. * @param name
  336. * the name of the variable
  337. * @return the value of the variable
  338. */
  339. public String getStringVariable(String name) {
  340. return var().getString(name);
  341. }
  342. /**
  343. * Gets the value of the named variable.
  344. *
  345. * @param name
  346. * the name of the variable
  347. * @return the value of the variable
  348. */
  349. public int getIntVariable(String name) {
  350. return var().getInt(name);
  351. }
  352. /**
  353. * Gets the value of the named variable.
  354. *
  355. * @param name
  356. * the name of the variable
  357. * @return the value of the variable
  358. */
  359. public long getLongVariable(String name) {
  360. return (long) var().getRawNumber(name);
  361. }
  362. /**
  363. * Gets the value of the named variable.
  364. *
  365. * @param name
  366. * the name of the variable
  367. * @return the value of the variable
  368. */
  369. public float getFloatVariable(String name) {
  370. return (float) var().getRawNumber(name);
  371. }
  372. /**
  373. * Gets the value of the named variable.
  374. *
  375. * @param name
  376. * the name of the variable
  377. * @return the value of the variable
  378. */
  379. public double getDoubleVariable(String name) {
  380. return var().getRawNumber(name);
  381. }
  382. /**
  383. * Gets the value of the named variable.
  384. *
  385. * @param name
  386. * the name of the variable
  387. * @return the value of the variable
  388. */
  389. public boolean getBooleanVariable(String name) {
  390. return var().getBoolean(name);
  391. }
  392. /**
  393. * Gets the value of the named variable.
  394. *
  395. * @param name
  396. * the name of the variable
  397. * @return the value of the variable
  398. */
  399. public String[] getStringArrayVariable(String name) {
  400. return var().getStringArray(name);
  401. }
  402. /**
  403. * Gets the value of the named String[] variable as a Set of Strings.
  404. *
  405. * @param name
  406. * the name of the variable
  407. * @return the value of the variable
  408. */
  409. public Set<String> getStringArrayVariableAsSet(final String name) {
  410. final HashSet<String> s = new HashSet<String>();
  411. JsArrayString a = var().getJSStringArray(name);
  412. for (int i = 0; i < a.length(); i++) {
  413. s.add(a.get(i));
  414. }
  415. return s;
  416. }
  417. /**
  418. * Gets the value of the named variable.
  419. *
  420. * @param name
  421. * the name of the variable
  422. * @return the value of the variable
  423. */
  424. public int[] getIntArrayVariable(String name) {
  425. return var().getIntArray(name);
  426. }
  427. /**
  428. * @deprecated should not be used anymore
  429. */
  430. @Deprecated
  431. public final static class XML extends JavaScriptObject {
  432. protected XML() {
  433. }
  434. public native String getXMLAsString()
  435. /*-{
  436. var buf = new Array();
  437. var self = this;
  438. for(j in self) {
  439. buf.push("<");
  440. buf.push(j);
  441. buf.push(">");
  442. buf.push(self[j]);
  443. buf.push("</");
  444. buf.push(j);
  445. buf.push(">");
  446. }
  447. return buf.join("");
  448. }-*/;
  449. }
  450. /**
  451. * Returns the number of children.
  452. *
  453. * @return the number of children
  454. */
  455. public native int getChildCount()
  456. /*-{
  457. return this.length - 2;
  458. }-*/;
  459. native boolean isMapAttribute(String name)
  460. /*-{
  461. return typeof this[1][name] == "object";
  462. }-*/;
  463. /**
  464. * Gets the Paintable with the id found in the named attributes's value.
  465. *
  466. * @param name
  467. * the name of the attribute
  468. * @return the Paintable referenced by the attribute, if it exists
  469. */
  470. public ServerConnector getPaintableAttribute(String name,
  471. ApplicationConnection connection) {
  472. return ConnectorMap.get(connection).getConnector(
  473. getStringAttribute(name));
  474. }
  475. /**
  476. * Gets the Paintable with the id found in the named variable's value.
  477. *
  478. * @param name
  479. * the name of the variable
  480. * @return the Paintable referenced by the variable, if it exists
  481. */
  482. public ServerConnector getPaintableVariable(String name,
  483. ApplicationConnection connection) {
  484. return ConnectorMap.get(connection).getConnector(
  485. getStringVariable(name));
  486. }
  487. /**
  488. * Returns the child UIDL by its name. If several child nodes exist with the
  489. * given name, the first child UIDL will be returned.
  490. *
  491. * @param tagName
  492. * @return the child UIDL or null if child wit given name was not found
  493. */
  494. public UIDL getChildByTagName(String tagName) {
  495. Iterator<Object> childIterator = getChildIterator();
  496. while (childIterator.hasNext()) {
  497. Object next = childIterator.next();
  498. if (next instanceof UIDL) {
  499. UIDL childUIDL = (UIDL) next;
  500. if (childUIDL.getTag().equals(tagName)) {
  501. return childUIDL;
  502. }
  503. }
  504. }
  505. return null;
  506. }
  507. }