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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import com.google.gwt.event.shared.GwtEvent;
  11. import com.google.gwt.event.shared.HandlerManager;
  12. import com.google.gwt.event.shared.HandlerRegistration;
  13. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  14. import com.vaadin.terminal.gwt.client.ServerConnector;
  15. import com.vaadin.terminal.gwt.client.Util;
  16. import com.vaadin.terminal.gwt.client.communication.ClientRpc;
  17. import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
  18. import com.vaadin.terminal.gwt.client.communication.StateChangeEvent.StateChangeHandler;
  19. /**
  20. * An abstract implementation of Connector.
  21. *
  22. * @author Vaadin Ltd
  23. * @version @VERSION@
  24. * @since 7.0.0
  25. *
  26. */
  27. public abstract class AbstractConnector implements ServerConnector,
  28. StateChangeHandler {
  29. private ApplicationConnection connection;
  30. private String id;
  31. private HandlerManager handlerManager;
  32. private Map<String, Collection<ClientRpc>> rpcImplementations;
  33. /*
  34. * (non-Javadoc)
  35. *
  36. * @see com.vaadin.terminal.gwt.client.VPaintable#getConnection()
  37. */
  38. public final ApplicationConnection getConnection() {
  39. return connection;
  40. }
  41. /*
  42. * (non-Javadoc)
  43. *
  44. * @see com.vaadin.terminal.gwt.client.Connector#getId()
  45. */
  46. public String getConnectorId() {
  47. return id;
  48. }
  49. /**
  50. * Called once by the framework to initialize the connector.
  51. * <p>
  52. * Note that the shared state is not yet available when this method is
  53. * called.
  54. * <p>
  55. * Connector classes should override {@link #init()} instead of this method.
  56. */
  57. public final void doInit(String connectorId,
  58. ApplicationConnection connection) {
  59. this.connection = connection;
  60. id = connectorId;
  61. addStateChangeHandler(this);
  62. init();
  63. }
  64. /**
  65. * Called when the connector has been initialized. Override this method to
  66. * perform initialization of the connector.
  67. */
  68. // FIXME: It might make sense to make this abstract to force users to
  69. // use init instead of constructor, where connection and id has not yet been
  70. // set.
  71. protected void init() {
  72. }
  73. /**
  74. * Registers an implementation for a server to client RPC interface.
  75. *
  76. * Multiple registrations can be made for a single interface, in which case
  77. * all of them receive corresponding RPC calls.
  78. *
  79. * @param rpcInterface
  80. * RPC interface
  81. * @param implementation
  82. * implementation that should receive RPC calls
  83. * @param <T>
  84. * The type of the RPC interface that is being registered
  85. */
  86. protected <T extends ClientRpc> void registerRpc(Class<T> rpcInterface,
  87. T implementation) {
  88. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  89. if (null == rpcImplementations) {
  90. rpcImplementations = new HashMap<String, Collection<ClientRpc>>();
  91. }
  92. if (null == rpcImplementations.get(rpcInterfaceId)) {
  93. rpcImplementations.put(rpcInterfaceId, new ArrayList<ClientRpc>());
  94. }
  95. rpcImplementations.get(rpcInterfaceId).add(implementation);
  96. }
  97. /**
  98. * Unregisters an implementation for a server to client RPC interface.
  99. *
  100. * @param rpcInterface
  101. * RPC interface
  102. * @param implementation
  103. * implementation to unregister
  104. */
  105. protected <T extends ClientRpc> void unregisterRpc(Class<T> rpcInterface,
  106. T implementation) {
  107. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  108. if (null != rpcImplementations
  109. && null != rpcImplementations.get(rpcInterfaceId)) {
  110. rpcImplementations.get(rpcInterfaceId).remove(implementation);
  111. }
  112. }
  113. public <T extends ClientRpc> Collection<T> getRpcImplementations(
  114. String rpcInterfaceId) {
  115. if (null == rpcImplementations) {
  116. return Collections.emptyList();
  117. }
  118. return (Collection<T>) rpcImplementations.get(rpcInterfaceId);
  119. }
  120. /*
  121. * (non-Javadoc)
  122. *
  123. * @see com.vaadin.terminal.gwt.client.Connector#isConnectorEnabled()
  124. */
  125. public boolean isConnectorEnabled() {
  126. // Client side can always receive message from the server
  127. return true;
  128. }
  129. public void fireEvent(GwtEvent<?> event) {
  130. if (handlerManager != null) {
  131. handlerManager.fireEvent(event);
  132. }
  133. }
  134. protected HandlerManager ensureHandlerManager() {
  135. if (handlerManager == null) {
  136. handlerManager = new HandlerManager(this);
  137. }
  138. return handlerManager;
  139. }
  140. public HandlerRegistration addStateChangeHandler(StateChangeHandler handler) {
  141. return ensureHandlerManager()
  142. .addHandler(StateChangeEvent.TYPE, handler);
  143. }
  144. // TODO Should be abstract as all connectors need it
  145. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  146. System.out.println("State change event for "
  147. + Util.getConnectorString(stateChangeEvent.getConnector())
  148. + " received by " + Util.getConnectorString(this));
  149. }
  150. }