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

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