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.

gwt-shared-state.asciidoc 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. ---
  2. title: Shared State
  3. order: 5
  4. layout: page
  5. ---
  6. [[gwt.shared-state]]
  7. = Shared State
  8. The basic communication from a server-side component to its the client-side
  9. widget counterpart is handled using a __shared state__. The shared state is
  10. serialized transparently. It should be considered read-only on the client-side,
  11. as it is not serialized back to the server-side.
  12. A shared state object simply needs to extend the
  13. [classname]#AbstractComponentState#. The member variables should normally be
  14. declared as public.
  15. ----
  16. public class MyComponentState extends AbstractComponentState {
  17. public String text;
  18. }
  19. ----
  20. A shared state should never contain any logic. If the members have private
  21. visibility for some reason, you can also use public setters and getters, in
  22. which case the property must not be public.
  23. [[gwt.shared-state.location]]
  24. == Location of Shared-State Classes
  25. The shared-state classes are used by both server- and client-side classes, but
  26. widget set compilation requires that they must be located in a client-side
  27. source package. The default location is under a [filename]#client# package under
  28. the package of the [filename]#.gwt.xml# descriptor. If you wish to organize the
  29. shared classes separately from other client-side code, you can define separate
  30. client-side source packages for pure client-side classes and any shared classes.
  31. In addition to shared state classes, shared classes could include enumerations
  32. and other classes needed by shared-state or RPC communication.
  33. For example, you could have the following definitions in the
  34. [filename]#.gwt.xml# descriptor:
  35. ----
  36. <source path="client" />
  37. <source path="shared" />
  38. ----
  39. The paths are relative to the package containing the descriptor.
  40. [[gwt.shared-state.component]]
  41. == Accessing Shared State on Server-Side
  42. A server-side component can access the shared state with the
  43. [methodname]#getState()# method. It is required that you override the base
  44. implementation with one that returns the shared state object cast to the proper
  45. type, as follows:
  46. ----
  47. @Override
  48. public MyComponentState getState() {
  49. return (MyComponentState) super.getState();
  50. }
  51. ----
  52. You can then use the [methodname]#getState()# to access the shared state object
  53. with the proper type.
  54. ----
  55. public MyComponent() {
  56. getState().setText("This is the initial state");
  57. ....
  58. }
  59. ----
  60. [[gwt.shared-state.connector]]
  61. == Handling Shared State in a Connector
  62. A connector can access a shared state with the [methodname]#getState()# method.
  63. The access should be read-only. It is required that you override the base
  64. implementation with one that returns the proper shared state type, as follows:
  65. ----
  66. @Override
  67. public MyComponentState getState() {
  68. return (MyComponentState) super.getState();
  69. }
  70. ----
  71. State changes made on the server-side are communicated transparently to the
  72. client-side. When a state change occurs, the [methodname]#onStateChanged()#
  73. method in the connector is called. You should should always call the superclass
  74. method before anything else to handle changes to common component properties.
  75. ----
  76. @Override
  77. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  78. super.onStateChanged(stateChangeEvent);
  79. // Copy the state properties to the widget properties
  80. final String text = getState().getText();
  81. getWidget().setText(text);
  82. }
  83. ----
  84. The crude [methodname]#onStateChanged()# method is called when any of the state
  85. properties is changed, allowing you to have even complex logic in how you
  86. manipulate the widget according to the state changes. In most cases, however,
  87. you can handle the property changes more easily and also more efficiently by
  88. using instead the [classname]#@OnStateChange# annotation on the handler methods
  89. for each property, as described next in <<gwt.shared-state.onstatechange>>, or
  90. by delegating the property value directly to the widget, as described in
  91. <<gwt.shared-state.delegatetowidget>>.
  92. ifdef::web[]
  93. The processing phases of state changes are described in more detail in
  94. <<dummy/../../../framework/gwt/gwt-advanced#gwt.advanced.phases,"Client-Side
  95. Processing Phases">>.
  96. endif::web[]
  97. [[gwt.shared-state.onstatechange]]
  98. == Handling Property State Changes with [classname]#@OnStateChange#
  99. The [classname]#@OnStateChange# annotation can be used to mark a connector
  100. method that handles state change on a particular property, given as parameter
  101. for the annotation. In addition to higher clarity, this avoids handling all
  102. property changes if a state change occurs in only one or some of them. However,
  103. if a state change can occur in multiple properties, you can only use this
  104. technique if the properties do not have interaction that prevents handling them
  105. separately in arbitrary order.
  106. We can replace the [methodname]#onStateChange()# method in the earlier connector
  107. example with the following:
  108. ----
  109. @OnStateChange("text")
  110. void updateText() {
  111. getWidget().setText(getState().text);
  112. }
  113. ----
  114. If the shared state property and the widget property have same name and do not
  115. require any type conversion, as is the case in the above example, you could
  116. simplify this even further by using the [classname]#@DelegateToWidget#
  117. annotation for the shared state property, as described in
  118. <<gwt.shared-state.delegatetowidget>>.
  119. [[gwt.shared-state.delegatetowidget]]
  120. == Delegating State Properties to Widget
  121. The [classname]#@DelegateToWidget# annotation for a shared state property
  122. defines automatic delegation of the property value to the corresponding widget
  123. property of the same name and type, by calling the respective setter for the
  124. property in the widget.
  125. ----
  126. public class MyComponentState extends AbstractComponentState {
  127. @DelegateToWidget
  128. public String text;
  129. }
  130. ----
  131. This is equivalent to handling the state change in the connector, as done in the
  132. example in <<gwt.shared-state.onstatechange>>.
  133. If you want to delegate a shared state property to a widget property of another
  134. name, you can give the property name as a string parameter for the annotation.
  135. ----
  136. public class MyComponentState extends AbstractComponentState {
  137. @DelegateToWidget("description")
  138. public String text;
  139. }
  140. ----
  141. [[gwt.shared-state.referring]]
  142. == Referring to Components in Shared State
  143. While you can pass any regular Java objects through a shared state, referring to
  144. another component requires special handling because on the server-side you can
  145. only refer to a server-side component, while on the client-side you only have
  146. widgets. References to components can be made by referring to their connectors
  147. (all server-side components implement the [interfacename]#Connector# interface).
  148. ----
  149. public class MyComponentState extends AbstractComponentState {
  150. public Connector otherComponent;
  151. }
  152. ----
  153. You could then access the component on the server-side as follows:
  154. ----
  155. public class MyComponent {
  156. public void MyComponent(Component otherComponent) {
  157. getState().otherComponent = otherComponent;
  158. }
  159. public Component getOtherComponent() {
  160. return (Component)getState().otherComponent;
  161. }
  162. // And the cast method
  163. @Override
  164. public MyComponentState getState() {
  165. return (MyComponentState) super.getState();
  166. }
  167. }
  168. ----
  169. On the client-side, you should cast it in a similar fashion to a
  170. [classname]#ComponentConnector#, or possibly to the specific connector type if
  171. it is known.
  172. [[gwt.shared-state.resource]]
  173. == Sharing Resources
  174. Resources, which commonly are references to icons or other images, are another
  175. case of objects that require special handling. A [interfacename]#Resource#
  176. object exists only on the server-side and on the client-side you have an URL to
  177. the resource. You need to use the [methodname]#setResource()# and
  178. [methodname]#getResource()# on the server-side to access a resource, which is
  179. serialized to the client-side separately.
  180. Let us begin with the server-side API:
  181. ----
  182. public class MyComponent extends AbstractComponent {
  183. ...
  184. public void setMyIcon(Resource myIcon) {
  185. setResource("myIcon", myIcon);
  186. }
  187. public Resource getMyIcon() {
  188. return getResource("myIcon");
  189. }
  190. }
  191. ----
  192. On the client-side, you can then get the URL of the resource with
  193. [methodname]#getResourceUrl()#.
  194. ----
  195. @Override
  196. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  197. super.onStateChanged(stateChangeEvent);
  198. ...
  199. // Get the resource URL for the icon
  200. getWidget().setMyIcon(getResourceUrl("myIcon"));
  201. }
  202. ----
  203. The widget could then use the URL, for example, as follows:
  204. ----
  205. public class MyWidget extends Label {
  206. ...
  207. Element imgElement = null;
  208. public void setMyIcon(String url) {
  209. if (imgElement == null) {
  210. imgElement = DOM.createImg();
  211. getElement().appendChild(imgElement);
  212. }
  213. DOM.setElementAttribute(imgElement, "src", url);
  214. }
  215. }
  216. ----