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.

AbstractConnector.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright 2000-2013 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 java.util.Set;
  24. import com.google.gwt.core.client.JsArrayString;
  25. import com.google.gwt.event.shared.GwtEvent;
  26. import com.google.gwt.event.shared.HandlerManager;
  27. import com.google.web.bindery.event.shared.HandlerRegistration;
  28. import com.vaadin.client.ApplicationConnection;
  29. import com.vaadin.client.FastStringMap;
  30. import com.vaadin.client.Profiler;
  31. import com.vaadin.client.ServerConnector;
  32. import com.vaadin.client.Util;
  33. import com.vaadin.client.VConsole;
  34. import com.vaadin.client.communication.RpcProxy;
  35. import com.vaadin.client.communication.StateChangeEvent;
  36. import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
  37. import com.vaadin.client.metadata.NoDataException;
  38. import com.vaadin.client.metadata.Type;
  39. import com.vaadin.client.metadata.TypeData;
  40. import com.vaadin.shared.communication.ClientRpc;
  41. import com.vaadin.shared.communication.ServerRpc;
  42. import com.vaadin.shared.communication.SharedState;
  43. import com.vaadin.shared.communication.URLReference;
  44. /**
  45. * An abstract implementation of Connector.
  46. *
  47. * @author Vaadin Ltd
  48. * @since 7.0.0
  49. *
  50. */
  51. public abstract class AbstractConnector implements ServerConnector,
  52. StateChangeHandler {
  53. private ApplicationConnection connection;
  54. private String id;
  55. private HandlerManager handlerManager;
  56. private FastStringMap<HandlerManager> statePropertyHandlerManagers;
  57. private FastStringMap<Collection<ClientRpc>> rpcImplementations;
  58. private final boolean debugLogging = false;
  59. private SharedState state;
  60. private ServerConnector parent;
  61. /**
  62. * A map from client-to-server RPC interface class to the RPC proxy that
  63. * sends outgoing RPC calls for that interface.
  64. */
  65. private FastStringMap<ServerRpc> rpcProxyMap = FastStringMap.create();
  66. /**
  67. * Temporary storage for last enabled state to be able to see if it has
  68. * changed. Can be removed once we are able to listen specifically for
  69. * enabled changes in the state. Widget.isEnabled() cannot be used as all
  70. * Widgets do not implement HasEnabled
  71. */
  72. private boolean lastEnabledState = true;
  73. private List<ServerConnector> children;
  74. /*
  75. * (non-Javadoc)
  76. *
  77. * @see com.vaadin.client.VPaintable#getConnection()
  78. */
  79. @Override
  80. public final ApplicationConnection getConnection() {
  81. return connection;
  82. }
  83. /*
  84. * (non-Javadoc)
  85. *
  86. * @see com.vaadin.client.Connector#getId()
  87. */
  88. @Override
  89. public String getConnectorId() {
  90. return id;
  91. }
  92. /**
  93. * Called once by the framework to initialize the connector.
  94. * <p>
  95. * Note that the shared state is not yet available when this method is
  96. * called.
  97. * <p>
  98. * Connector classes should override {@link #init()} instead of this method.
  99. */
  100. @Override
  101. public final void doInit(String connectorId,
  102. ApplicationConnection connection) {
  103. Profiler.enter("AbstractConnector.doInit");
  104. this.connection = connection;
  105. id = connectorId;
  106. addStateChangeHandler(this);
  107. if (Profiler.isEnabled()) {
  108. Profiler.enter("AbstractConnector.init " + Util.getSimpleName(this));
  109. }
  110. init();
  111. if (Profiler.isEnabled()) {
  112. Profiler.leave("AbstractConnector.init " + Util.getSimpleName(this));
  113. }
  114. Profiler.leave("AbstractConnector.doInit");
  115. }
  116. /**
  117. * Called when the connector has been initialized. Override this method to
  118. * perform initialization of the connector.
  119. */
  120. // FIXME: It might make sense to make this abstract to force users to
  121. // use init instead of constructor, where connection and id has not yet been
  122. // set.
  123. protected void init() {
  124. }
  125. /**
  126. * Registers an implementation for a server to client RPC interface.
  127. *
  128. * Multiple registrations can be made for a single interface, in which case
  129. * all of them receive corresponding RPC calls.
  130. *
  131. * @param rpcInterface
  132. * RPC interface
  133. * @param implementation
  134. * implementation that should receive RPC calls
  135. * @param <T>
  136. * The type of the RPC interface that is being registered
  137. */
  138. protected <T extends ClientRpc> void registerRpc(Class<T> rpcInterface,
  139. T implementation) {
  140. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  141. if (null == rpcImplementations) {
  142. rpcImplementations = FastStringMap.create();
  143. }
  144. if (null == rpcImplementations.get(rpcInterfaceId)) {
  145. rpcImplementations.put(rpcInterfaceId, new ArrayList<ClientRpc>());
  146. }
  147. rpcImplementations.get(rpcInterfaceId).add(implementation);
  148. }
  149. /**
  150. * Unregisters an implementation for a server to client RPC interface.
  151. *
  152. * @param rpcInterface
  153. * RPC interface
  154. * @param implementation
  155. * implementation to unregister
  156. */
  157. protected <T extends ClientRpc> void unregisterRpc(Class<T> rpcInterface,
  158. T implementation) {
  159. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  160. if (null != rpcImplementations
  161. && null != rpcImplementations.get(rpcInterfaceId)) {
  162. rpcImplementations.get(rpcInterfaceId).remove(implementation);
  163. }
  164. }
  165. /**
  166. * Returns an RPC proxy object which can be used to invoke the RPC method on
  167. * the server.
  168. *
  169. * @param <T>
  170. * The type of the ServerRpc interface
  171. * @param rpcInterface
  172. * The ServerRpc interface to retrieve a proxy object for
  173. * @return A proxy object which can be used to invoke the RPC method on the
  174. * server.
  175. */
  176. protected <T extends ServerRpc> T getRpcProxy(Class<T> rpcInterface) {
  177. String name = rpcInterface.getName();
  178. if (!rpcProxyMap.containsKey(name)) {
  179. rpcProxyMap.put(name, RpcProxy.create(rpcInterface, this));
  180. }
  181. return (T) rpcProxyMap.get(name);
  182. }
  183. @Override
  184. public <T extends ClientRpc> Collection<T> getRpcImplementations(
  185. String rpcInterfaceId) {
  186. if (null == rpcImplementations) {
  187. return Collections.emptyList();
  188. }
  189. return (Collection<T>) rpcImplementations.get(rpcInterfaceId);
  190. }
  191. @Override
  192. public void fireEvent(GwtEvent<?> event) {
  193. String profilerKey = null;
  194. if (Profiler.isEnabled()) {
  195. profilerKey = "Fire " + Util.getSimpleName(event) + " for "
  196. + Util.getSimpleName(this);
  197. Profiler.enter(profilerKey);
  198. }
  199. if (handlerManager != null) {
  200. handlerManager.fireEvent(event);
  201. }
  202. if (statePropertyHandlerManagers != null
  203. && event instanceof StateChangeEvent) {
  204. StateChangeEvent stateChangeEvent = (StateChangeEvent) event;
  205. JsArrayString keys = statePropertyHandlerManagers.getKeys();
  206. for (int i = 0; i < keys.length(); i++) {
  207. String property = keys.get(i);
  208. if (stateChangeEvent.hasPropertyChanged(property)) {
  209. statePropertyHandlerManagers.get(property).fireEvent(event);
  210. }
  211. }
  212. }
  213. if (Profiler.isEnabled()) {
  214. Profiler.leave(profilerKey);
  215. }
  216. }
  217. protected HandlerManager ensureHandlerManager() {
  218. if (handlerManager == null) {
  219. handlerManager = new HandlerManager(this);
  220. }
  221. return handlerManager;
  222. }
  223. @Override
  224. public HandlerRegistration addStateChangeHandler(StateChangeHandler handler) {
  225. return ensureHandlerManager()
  226. .addHandler(StateChangeEvent.TYPE, handler);
  227. }
  228. @Override
  229. public void removeStateChangeHandler(StateChangeHandler handler) {
  230. ensureHandlerManager().removeHandler(StateChangeEvent.TYPE, handler);
  231. }
  232. @Override
  233. public HandlerRegistration addStateChangeHandler(String propertyName,
  234. StateChangeHandler handler) {
  235. return ensureHandlerManager(propertyName).addHandler(
  236. StateChangeEvent.TYPE, handler);
  237. }
  238. @Override
  239. public void removeStateChangeHandler(String propertyName,
  240. StateChangeHandler handler) {
  241. ensureHandlerManager(propertyName).removeHandler(StateChangeEvent.TYPE,
  242. handler);
  243. }
  244. private HandlerManager ensureHandlerManager(String propertyName) {
  245. if (statePropertyHandlerManagers == null) {
  246. statePropertyHandlerManagers = FastStringMap.create();
  247. }
  248. HandlerManager manager = statePropertyHandlerManagers.get(propertyName);
  249. if (manager == null) {
  250. manager = new HandlerManager(this);
  251. statePropertyHandlerManagers.put(propertyName, manager);
  252. }
  253. return manager;
  254. }
  255. @Override
  256. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  257. Profiler.enter("AbstractConnector.onStateChanged");
  258. if (debugLogging) {
  259. VConsole.log("State change event for "
  260. + Util.getConnectorString(stateChangeEvent.getConnector())
  261. + " received by " + Util.getConnectorString(this));
  262. }
  263. updateEnabledState(isEnabled());
  264. Profiler.leave("AbstractConnector.onStateChanged");
  265. }
  266. /*
  267. * (non-Javadoc)
  268. *
  269. * @see com.vaadin.client.ServerConnector#onUnregister()
  270. */
  271. @Override
  272. public void onUnregister() {
  273. if (debugLogging) {
  274. VConsole.log("Unregistered connector "
  275. + Util.getConnectorString(this));
  276. }
  277. }
  278. /**
  279. * Returns the shared state object for this connector.
  280. *
  281. * Override this method to define the shared state type for your connector.
  282. *
  283. * @return the current shared state (never null)
  284. */
  285. @Override
  286. public SharedState getState() {
  287. if (state == null) {
  288. Profiler.enter("AbstractConnector.createState()");
  289. state = createState();
  290. Profiler.leave("AbstractConnector.createState()");
  291. }
  292. return state;
  293. }
  294. /**
  295. * Creates a state object with default values for this connector. The
  296. * created state object must be compatible with the return type of
  297. * {@link #getState()}. The default implementation creates a state object
  298. * using GWT.create() using the defined return type of {@link #getState()}.
  299. *
  300. * @return A new state object
  301. */
  302. protected SharedState createState() {
  303. try {
  304. Type stateType = getStateType(this);
  305. Object stateInstance = stateType.createInstance();
  306. return (SharedState) stateInstance;
  307. } catch (NoDataException e) {
  308. throw new IllegalStateException(
  309. "There is no information about the state for "
  310. + Util.getSimpleName(this)
  311. + ". Did you remember to compile the right widgetset?",
  312. e);
  313. }
  314. }
  315. public static Type getStateType(ServerConnector connector) {
  316. try {
  317. return TypeData.getType(connector.getClass()).getMethod("getState")
  318. .getReturnType();
  319. } catch (NoDataException e) {
  320. throw new IllegalStateException(
  321. "There is no information about the state for "
  322. + Util.getSimpleName(connector)
  323. + ". Did you remember to compile the right widgetset?",
  324. e);
  325. }
  326. }
  327. @Override
  328. public ServerConnector getParent() {
  329. return parent;
  330. }
  331. @Override
  332. public void setParent(ServerConnector parent) {
  333. this.parent = parent;
  334. }
  335. @Override
  336. public List<ServerConnector> getChildren() {
  337. if (children == null) {
  338. return Collections.emptyList();
  339. }
  340. return children;
  341. }
  342. @Override
  343. public void setChildren(List<ServerConnector> children) {
  344. this.children = children;
  345. }
  346. @Override
  347. public boolean isEnabled() {
  348. if (!getState().enabled) {
  349. return false;
  350. }
  351. if (getParent() == null) {
  352. return true;
  353. } else {
  354. return getParent().isEnabled();
  355. }
  356. }
  357. @Override
  358. public void updateEnabledState(boolean enabledState) {
  359. if (lastEnabledState == enabledState) {
  360. return;
  361. }
  362. lastEnabledState = enabledState;
  363. for (ServerConnector c : getChildren()) {
  364. // Update children as they might be affected by the enabled state of
  365. // their parent
  366. c.updateEnabledState(c.isEnabled());
  367. }
  368. }
  369. /**
  370. * Gets the URL for a resource that has been added by the server-side
  371. * connector using
  372. * {@link com.vaadin.terminal.AbstractClientConnector#setResource(String, com.vaadin.terminal.Resource)}
  373. * with the same key. <code>null</code> is returned if no corresponding
  374. * resource is found.
  375. * <p>
  376. * To get an event when a resource changes, you can use
  377. * {@link #addStateChangeHandler(String, StateChangeHandler)} with
  378. * <code>resources.[key]</code> as the property name.
  379. *
  380. * @param key
  381. * a string identifying the resource.
  382. * @return the resource URL as a string, or <code>null</code> if no
  383. * corresponding resource is found.
  384. */
  385. public String getResourceUrl(String key) {
  386. URLReference urlReference = getState().resources.get(key);
  387. if (urlReference == null) {
  388. return null;
  389. } else {
  390. return urlReference.getURL();
  391. }
  392. }
  393. /*
  394. * (non-Javadoc)
  395. *
  396. * @see com.vaadin.client.ServerConnector#hasEventListener(java.lang.String)
  397. */
  398. public boolean hasEventListener(String eventIdentifier) {
  399. Set<String> reg = getState().registeredEventListeners;
  400. return (reg != null && reg.contains(eventIdentifier));
  401. }
  402. }