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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.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.Util;
  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
  33. extends AbstractServerConnectorEvent<StateChangeHandler> {
  34. /**
  35. * Type of this event, used by the event bus.
  36. */
  37. public static final Type<StateChangeHandler> TYPE = new Type<>();
  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. * @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<>();
  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(
  162. "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(
  171. "StateChangeEvent.getChangedPropertiesFastSet populate");
  172. }
  173. return changedProperties;
  174. }
  175. /**
  176. * Checks whether the give property has changed.
  177. *
  178. * @param property
  179. * the name of the property to check
  180. * @return <code>true</code> if the property has changed, else
  181. * <code>false></code>
  182. */
  183. public boolean hasPropertyChanged(String property) {
  184. if (isInitialStateChange()) {
  185. // Everything has changed for a new connector
  186. return true;
  187. } else if (stateJson != null) {
  188. // Check whether it's in the json object
  189. return isInJson(property, Util.json2jso(stateJson));
  190. } else {
  191. // Legacy cases
  192. if (changedProperties != null) {
  193. // Check legacy stuff
  194. return changedProperties.contains(property);
  195. } else if (changedPropertiesSet != null) {
  196. // Check legacy stuff
  197. return changedPropertiesSet.contains(property);
  198. } else {
  199. throw new IllegalStateException(
  200. "StateChangeEvent should have either stateJson, changedProperties or changePropertiesSet");
  201. }
  202. }
  203. }
  204. /**
  205. * Checks whether the given property name (which might contains dots) is
  206. * defined in some JavaScript object.
  207. *
  208. * @param property
  209. * the name of the property, might include dots to reference
  210. * inner objects
  211. * @param target
  212. * the JavaScript object to check
  213. * @return true if the property is defined
  214. */
  215. private static final native boolean isInJson(String property,
  216. JavaScriptObject target)
  217. /*-{
  218. var segments = property.split('.');
  219. while (typeof target == 'object') {
  220. var nextSegment = segments.shift();
  221. if (!(nextSegment in target)) {
  222. // Abort if segment is not found
  223. return false;
  224. } else if (segments.length == 0) {
  225. // Done if there are no more segments
  226. return true;
  227. } else {
  228. // Else just go deeper
  229. target = target[nextSegment];
  230. }
  231. }
  232. // Not defined if we reach something that isn't an object
  233. return false;
  234. }-*/;
  235. /**
  236. * Recursively adds the names of all properties in the provided state type.
  237. *
  238. * @param type
  239. * the type to process
  240. * @param changedProperties
  241. * a set of all currently added properties
  242. * @param context
  243. * the base name of the current object
  244. */
  245. @Deprecated
  246. private static void addAllStateFields(com.vaadin.client.metadata.Type type,
  247. FastStringSet changedProperties, String context) {
  248. try {
  249. JsArrayObject<Property> properties = type.getPropertiesAsArray();
  250. int size = properties.size();
  251. for (int i = 0; i < size; i++) {
  252. Property property = properties.get(i);
  253. String propertyName = context + property.getName();
  254. changedProperties.add(propertyName);
  255. com.vaadin.client.metadata.Type propertyType = property
  256. .getType();
  257. if (propertyType.hasProperties()) {
  258. addAllStateFields(propertyType, changedProperties,
  259. propertyName + ".");
  260. }
  261. }
  262. } catch (NoDataException e) {
  263. throw new IllegalStateException("No property info for " + type
  264. + ". Did you remember to compile the right widgetset?", e);
  265. }
  266. }
  267. /**
  268. * Recursively adds the names of all fields in all objects in the provided
  269. * json object.
  270. *
  271. * @param json
  272. * the json object to process
  273. * @param changedProperties
  274. * a set of all currently added fields
  275. * @param context
  276. * the base name of the current object
  277. */
  278. @Deprecated
  279. private static void addJsonFields(JsonObject json,
  280. FastStringSet changedProperties, String context) {
  281. for (String key : json.keys()) {
  282. String fieldName = context + key;
  283. changedProperties.add(fieldName);
  284. JsonObject object = json.get(key);
  285. if (object != null) {
  286. addJsonFields(object, changedProperties, fieldName + ".");
  287. }
  288. }
  289. }
  290. /**
  291. * Checks if the state change event is the first one for the given
  292. * connector.
  293. *
  294. * @since 7.1
  295. * @return true if this is the first state change event for the connector,
  296. * false otherwise
  297. */
  298. public boolean isInitialStateChange() {
  299. return initialStateChange;
  300. }
  301. }