您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractConnector.java 8.6KB

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