Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractConnector.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright 2011 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.ui;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import com.google.gwt.event.shared.GwtEvent;
  24. import com.google.gwt.event.shared.HandlerManager;
  25. import com.google.web.bindery.event.shared.HandlerRegistration;
  26. import com.vaadin.client.ApplicationConnection;
  27. import com.vaadin.client.ServerConnector;
  28. import com.vaadin.client.Util;
  29. import com.vaadin.client.VConsole;
  30. import com.vaadin.client.communication.StateChangeEvent;
  31. import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
  32. import com.vaadin.client.metadata.NoDataException;
  33. import com.vaadin.client.metadata.Type;
  34. import com.vaadin.client.metadata.TypeData;
  35. import com.vaadin.shared.communication.ClientRpc;
  36. import com.vaadin.shared.communication.SharedState;
  37. /**
  38. * An abstract implementation of Connector.
  39. *
  40. * @author Vaadin Ltd
  41. * @since 7.0.0
  42. *
  43. */
  44. public abstract class AbstractConnector implements ServerConnector,
  45. StateChangeHandler {
  46. private ApplicationConnection connection;
  47. private String id;
  48. private HandlerManager handlerManager;
  49. private Map<String, HandlerManager> statePropertyHandlerManagers;
  50. private Map<String, Collection<ClientRpc>> rpcImplementations;
  51. private final boolean debugLogging = false;
  52. private SharedState state;
  53. private ServerConnector parent;
  54. /**
  55. * Temporary storage for last enabled state to be able to see if it has
  56. * changed. Can be removed once we are able to listen specifically for
  57. * enabled changes in the state. Widget.isEnabled() cannot be used as all
  58. * Widgets do not implement HasEnabled
  59. */
  60. private boolean lastEnabledState = true;
  61. private List<ServerConnector> children;
  62. /*
  63. * (non-Javadoc)
  64. *
  65. * @see com.vaadin.client.VPaintable#getConnection()
  66. */
  67. @Override
  68. public final ApplicationConnection getConnection() {
  69. return connection;
  70. }
  71. /*
  72. * (non-Javadoc)
  73. *
  74. * @see com.vaadin.client.Connector#getId()
  75. */
  76. @Override
  77. public String getConnectorId() {
  78. return id;
  79. }
  80. /**
  81. * Called once by the framework to initialize the connector.
  82. * <p>
  83. * Note that the shared state is not yet available when this method is
  84. * called.
  85. * <p>
  86. * Connector classes should override {@link #init()} instead of this method.
  87. */
  88. @Override
  89. public final void doInit(String connectorId,
  90. ApplicationConnection connection) {
  91. this.connection = connection;
  92. id = connectorId;
  93. addStateChangeHandler(this);
  94. init();
  95. }
  96. /**
  97. * Called when the connector has been initialized. Override this method to
  98. * perform initialization of the connector.
  99. */
  100. // FIXME: It might make sense to make this abstract to force users to
  101. // use init instead of constructor, where connection and id has not yet been
  102. // set.
  103. protected void init() {
  104. }
  105. /**
  106. * Registers an implementation for a server to client RPC interface.
  107. *
  108. * Multiple registrations can be made for a single interface, in which case
  109. * all of them receive corresponding RPC calls.
  110. *
  111. * @param rpcInterface
  112. * RPC interface
  113. * @param implementation
  114. * implementation that should receive RPC calls
  115. * @param <T>
  116. * The type of the RPC interface that is being registered
  117. */
  118. protected <T extends ClientRpc> void registerRpc(Class<T> rpcInterface,
  119. T implementation) {
  120. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  121. if (null == rpcImplementations) {
  122. rpcImplementations = new HashMap<String, Collection<ClientRpc>>();
  123. }
  124. if (null == rpcImplementations.get(rpcInterfaceId)) {
  125. rpcImplementations.put(rpcInterfaceId, new ArrayList<ClientRpc>());
  126. }
  127. rpcImplementations.get(rpcInterfaceId).add(implementation);
  128. }
  129. /**
  130. * Unregisters an implementation for a server to client RPC interface.
  131. *
  132. * @param rpcInterface
  133. * RPC interface
  134. * @param implementation
  135. * implementation to unregister
  136. */
  137. protected <T extends ClientRpc> void unregisterRpc(Class<T> rpcInterface,
  138. T implementation) {
  139. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  140. if (null != rpcImplementations
  141. && null != rpcImplementations.get(rpcInterfaceId)) {
  142. rpcImplementations.get(rpcInterfaceId).remove(implementation);
  143. }
  144. }
  145. @Override
  146. public <T extends ClientRpc> Collection<T> getRpcImplementations(
  147. String rpcInterfaceId) {
  148. if (null == rpcImplementations) {
  149. return Collections.emptyList();
  150. }
  151. return (Collection<T>) rpcImplementations.get(rpcInterfaceId);
  152. }
  153. @Override
  154. public void fireEvent(GwtEvent<?> event) {
  155. if (handlerManager != null) {
  156. handlerManager.fireEvent(event);
  157. }
  158. if (statePropertyHandlerManagers != null
  159. && event instanceof StateChangeEvent) {
  160. for (String property : ((StateChangeEvent) event)
  161. .getChangedProperties()) {
  162. HandlerManager manager = statePropertyHandlerManagers
  163. .get(property);
  164. if (manager != null) {
  165. manager.fireEvent(event);
  166. }
  167. }
  168. }
  169. }
  170. protected HandlerManager ensureHandlerManager() {
  171. if (handlerManager == null) {
  172. handlerManager = new HandlerManager(this);
  173. }
  174. return handlerManager;
  175. }
  176. @Override
  177. public HandlerRegistration addStateChangeHandler(StateChangeHandler handler) {
  178. return ensureHandlerManager()
  179. .addHandler(StateChangeEvent.TYPE, handler);
  180. }
  181. @Override
  182. public HandlerRegistration addStateChangeHandler(String propertyName,
  183. StateChangeHandler handler) {
  184. return ensureHandlerManager(propertyName).addHandler(
  185. StateChangeEvent.TYPE, handler);
  186. }
  187. private HandlerManager ensureHandlerManager(String propertyName) {
  188. if (statePropertyHandlerManagers == null) {
  189. statePropertyHandlerManagers = new HashMap<String, HandlerManager>();
  190. }
  191. HandlerManager manager = statePropertyHandlerManagers.get(propertyName);
  192. if (manager == null) {
  193. manager = new HandlerManager(this);
  194. statePropertyHandlerManagers.put(propertyName, manager);
  195. }
  196. return manager;
  197. }
  198. @Override
  199. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  200. if (debugLogging) {
  201. VConsole.log("State change event for "
  202. + Util.getConnectorString(stateChangeEvent.getConnector())
  203. + " received by " + Util.getConnectorString(this));
  204. }
  205. updateEnabledState(isEnabled());
  206. }
  207. /*
  208. * (non-Javadoc)
  209. *
  210. * @see com.vaadin.client.ServerConnector#onUnregister()
  211. */
  212. @Override
  213. public void onUnregister() {
  214. if (debugLogging) {
  215. VConsole.log("Unregistered connector "
  216. + Util.getConnectorString(this));
  217. }
  218. }
  219. /**
  220. * Returns the shared state object for this connector.
  221. *
  222. * Override this method to define the shared state type for your connector.
  223. *
  224. * @return the current shared state (never null)
  225. */
  226. @Override
  227. public SharedState getState() {
  228. if (state == null) {
  229. state = createState();
  230. }
  231. return state;
  232. }
  233. /**
  234. * Creates a state object with default values for this connector. The
  235. * created state object must be compatible with the return type of
  236. * {@link #getState()}. The default implementation creates a state object
  237. * using GWT.create() using the defined return type of {@link #getState()}.
  238. *
  239. * @return A new state object
  240. */
  241. protected SharedState createState() {
  242. try {
  243. Type stateType = getStateType(this);
  244. Object stateInstance = stateType.createInstance();
  245. return (SharedState) stateInstance;
  246. } catch (NoDataException e) {
  247. throw new IllegalStateException(
  248. "There is no information about the state for "
  249. + Util.getSimpleName(this)
  250. + ". Did you remember to compile the right widgetset?",
  251. e);
  252. }
  253. }
  254. public static Type getStateType(ServerConnector connector) {
  255. try {
  256. return TypeData.getType(connector.getClass()).getMethod("getState")
  257. .getReturnType();
  258. } catch (NoDataException e) {
  259. throw new IllegalStateException(
  260. "There is no information about the state for "
  261. + Util.getSimpleName(connector)
  262. + ". Did you remember to compile the right widgetset?",
  263. e);
  264. }
  265. }
  266. @Override
  267. public ServerConnector getParent() {
  268. return parent;
  269. }
  270. @Override
  271. public void setParent(ServerConnector parent) {
  272. this.parent = parent;
  273. }
  274. @Override
  275. public List<ServerConnector> getChildren() {
  276. if (children == null) {
  277. return Collections.emptyList();
  278. }
  279. return children;
  280. }
  281. @Override
  282. public void setChildren(List<ServerConnector> children) {
  283. this.children = children;
  284. }
  285. @Override
  286. public boolean isEnabled() {
  287. if (!getState().isEnabled()) {
  288. return false;
  289. }
  290. if (getParent() == null) {
  291. return true;
  292. } else {
  293. return getParent().isEnabled();
  294. }
  295. }
  296. @Override
  297. public void updateEnabledState(boolean enabledState) {
  298. if (lastEnabledState == enabledState) {
  299. return;
  300. }
  301. lastEnabledState = enabledState;
  302. for (ServerConnector c : getChildren()) {
  303. // Update children as they might be affected by the enabled state of
  304. // their parent
  305. c.updateEnabledState(c.isEnabled());
  306. }
  307. }
  308. }