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 17KB

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