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

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