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.

StateChangeEvent.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.communication;
  17. import java.io.Serializable;
  18. import java.util.HashSet;
  19. import java.util.Set;
  20. import com.google.gwt.core.client.JavaScriptObject;
  21. import com.google.gwt.event.shared.EventHandler;
  22. import com.google.gwt.json.client.JSONObject;
  23. import com.vaadin.client.FastStringSet;
  24. import com.vaadin.client.JsArrayObject;
  25. import com.vaadin.client.Profiler;
  26. import com.vaadin.client.ServerConnector;
  27. import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
  28. import com.vaadin.client.metadata.NoDataException;
  29. import com.vaadin.client.metadata.Property;
  30. import com.vaadin.client.ui.AbstractConnector;
  31. public class StateChangeEvent extends
  32. AbstractServerConnectorEvent<StateChangeHandler> {
  33. /**
  34. * Type of this event, used by the event bus.
  35. */
  36. public static final Type<StateChangeHandler> TYPE = new Type<StateChangeHandler>();
  37. /**
  38. * Used to cache a FastStringSet representation of the properties that have
  39. * changed if one is needed.
  40. */
  41. @Deprecated
  42. private FastStringSet changedProperties;
  43. /**
  44. * Used to cache a Set representation of the changedProperties if one is
  45. * needed.
  46. */
  47. @Deprecated
  48. private Set<String> changedPropertiesSet;
  49. private boolean initialStateChange = false;
  50. private JSONObject stateJson;
  51. @Override
  52. public Type<StateChangeHandler> getAssociatedType() {
  53. return TYPE;
  54. }
  55. /**
  56. * Creates a new state change event.
  57. *
  58. * @param connector
  59. * the event whose state has changed
  60. * @param changedPropertiesSet
  61. * a set of names of the changed properties
  62. * @deprecated As of 7.0.1, use
  63. * {@link #StateChangeEvent(ServerConnector, JSONObject, boolean)}
  64. * instead for improved performance.
  65. */
  66. @Deprecated
  67. public StateChangeEvent(ServerConnector connector,
  68. Set<String> changedPropertiesSet) {
  69. setConnector(connector);
  70. // Keep instance around for caching
  71. this.changedPropertiesSet = changedPropertiesSet;
  72. changedProperties = FastStringSet.create();
  73. for (String property : changedPropertiesSet) {
  74. changedProperties.add(property);
  75. }
  76. }
  77. /**
  78. * Creates a new state change event.
  79. *
  80. * @param connector
  81. * the event whose state has changed
  82. * @param changedProperties
  83. * a set of names of the changed properties
  84. * @deprecated As of 7.0.2, use
  85. * {@link #StateChangeEvent(ServerConnector, JSONObject, boolean)}
  86. * instead for improved performance.
  87. */
  88. @Deprecated
  89. public StateChangeEvent(ServerConnector connector,
  90. FastStringSet changedProperties) {
  91. setConnector(connector);
  92. this.changedProperties = changedProperties;
  93. }
  94. /**
  95. * /** Creates a new state change event.
  96. *
  97. * @param connector
  98. * the event whose state has changed
  99. * @param stateJson
  100. * the JSON representation of the state change
  101. * @param initialStateChange
  102. * <code>true</code> if the state change is for a new connector,
  103. * otherwise <code>false</code>
  104. */
  105. public StateChangeEvent(ServerConnector connector, JSONObject stateJson,
  106. boolean initialStateChange) {
  107. setConnector(connector);
  108. this.stateJson = stateJson;
  109. this.initialStateChange = initialStateChange;
  110. }
  111. @Override
  112. public void dispatch(StateChangeHandler listener) {
  113. listener.onStateChanged(this);
  114. }
  115. /**
  116. * Event handler that gets notified whenever any part of the state has been
  117. * updated by the server.
  118. *
  119. * @author Vaadin Ltd
  120. * @version @VERSION@
  121. * @since 7.0.0
  122. */
  123. public interface StateChangeHandler extends Serializable, EventHandler {
  124. /**
  125. * Notifies the event handler that the state has changed.
  126. *
  127. * @param stateChangeEvent
  128. * the state change event with details about the change
  129. */
  130. public void onStateChanged(StateChangeEvent stateChangeEvent);
  131. }
  132. /**
  133. * Gets the properties that have changed.
  134. *
  135. * @return a set of names of the changed properties
  136. *
  137. * @deprecated As of 7.0.1, use {@link #hasPropertyChanged(String)} instead
  138. * for improved performance.
  139. */
  140. @Deprecated
  141. public Set<String> getChangedProperties() {
  142. if (changedPropertiesSet == null) {
  143. Profiler.enter("StateChangeEvent.getChangedProperties populate");
  144. changedPropertiesSet = new HashSet<String>();
  145. getChangedPropertiesFastSet().addAllTo(changedPropertiesSet);
  146. Profiler.leave("StateChangeEvent.getChangedProperties populate");
  147. }
  148. return changedPropertiesSet;
  149. }
  150. /**
  151. * Gets the properties that have changed.
  152. *
  153. * @return a set of names of the changed properties
  154. *
  155. * @deprecated As of 7.0.1, use {@link #hasPropertyChanged(String)} instead
  156. * for improved performance.
  157. */
  158. @Deprecated
  159. public FastStringSet getChangedPropertiesFastSet() {
  160. if (changedProperties == null) {
  161. Profiler.enter("StateChangeEvent.getChangedPropertiesFastSet populate");
  162. changedProperties = FastStringSet.create();
  163. addJsonFields(stateJson, changedProperties, "");
  164. if (isInitialStateChange()) {
  165. addAllStateFields(
  166. AbstractConnector.getStateType(getConnector()),
  167. changedProperties, "");
  168. }
  169. Profiler.leave("StateChangeEvent.getChangedPropertiesFastSet populate");
  170. }
  171. return changedProperties;
  172. }
  173. /**
  174. * Checks whether the give property has changed.
  175. *
  176. * @param property
  177. * the name of the property to check
  178. * @return <code>true</code> if the property has changed, else
  179. * <code>false></code>
  180. */
  181. public boolean hasPropertyChanged(String property) {
  182. if (isInitialStateChange()) {
  183. // Everything has changed for a new connector
  184. return true;
  185. } else if (stateJson != null) {
  186. // Check whether it's in the json object
  187. return isInJson(property, stateJson.getJavaScriptObject());
  188. } else {
  189. // Legacy cases
  190. if (changedProperties != null) {
  191. // Check legacy stuff
  192. return changedProperties.contains(property);
  193. } else if (changedPropertiesSet != null) {
  194. // Check legacy stuff
  195. return changedPropertiesSet.contains(property);
  196. } else {
  197. throw new IllegalStateException(
  198. "StateChangeEvent should have either stateJson, changedProperties or changePropertiesSet");
  199. }
  200. }
  201. }
  202. /**
  203. * Checks whether the given property name (which might contains dots) is
  204. * defined in some JavaScript object.
  205. *
  206. * @param property
  207. * the name of the property, might include dots to reference
  208. * inner objects
  209. * @param target
  210. * the JavaScript object to check
  211. * @return true if the property is defined
  212. */
  213. private static native final boolean isInJson(String property,
  214. JavaScriptObject target)
  215. /*-{
  216. var segments = property.split('.');
  217. while (typeof target == 'object') {
  218. var nextSegment = segments.shift();
  219. if (!(nextSegment in target)) {
  220. // Abort if segment is not found
  221. return false;
  222. } else if (segments.length == 0) {
  223. // Done if there are no more segments
  224. return true;
  225. } else {
  226. // Else just go deeper
  227. target = target[nextSegment];
  228. }
  229. }
  230. // Not defined if we reach something that isn't an object
  231. return false;
  232. }-*/;
  233. /**
  234. * Recursively adds the names of all properties in the provided state type.
  235. *
  236. * @param type
  237. * the type to process
  238. * @param changedProperties
  239. * a set of all currently added properties
  240. * @param context
  241. * the base name of the current object
  242. */
  243. @Deprecated
  244. private static void addAllStateFields(com.vaadin.client.metadata.Type type,
  245. FastStringSet changedProperties, String context) {
  246. try {
  247. JsArrayObject<Property> properties = type.getPropertiesAsArray();
  248. int size = properties.size();
  249. for (int i = 0; i < size; i++) {
  250. Property property = properties.get(i);
  251. String propertyName = context + property.getName();
  252. changedProperties.add(propertyName);
  253. com.vaadin.client.metadata.Type propertyType = property
  254. .getType();
  255. if (propertyType.hasProperties()) {
  256. addAllStateFields(propertyType, changedProperties,
  257. propertyName + ".");
  258. }
  259. }
  260. } catch (NoDataException e) {
  261. throw new IllegalStateException("No property info for " + type
  262. + ". Did you remember to compile the right widgetset?", e);
  263. }
  264. }
  265. /**
  266. * Recursively adds the names of all fields in all objects in the provided
  267. * json object.
  268. *
  269. * @param json
  270. * the json object to process
  271. * @param changedProperties
  272. * a set of all currently added fields
  273. * @param context
  274. * the base name of the current object
  275. */
  276. @Deprecated
  277. private static void addJsonFields(JSONObject json,
  278. FastStringSet changedProperties, String context) {
  279. for (String key : json.keySet()) {
  280. String fieldName = context + key;
  281. changedProperties.add(fieldName);
  282. JSONObject object = json.get(key).isObject();
  283. if (object != null) {
  284. addJsonFields(object, changedProperties, fieldName + ".");
  285. }
  286. }
  287. }
  288. /**
  289. * Checks if the state change event is the first one for the given
  290. * connector.
  291. *
  292. * @since 7.1
  293. * @return true if this is the first state change event for the connector,
  294. * false otherwise
  295. */
  296. public boolean isInitialStateChange() {
  297. return initialStateChange;
  298. }
  299. }