Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

StateChangeEvent.java 11KB

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