Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AbstractConnector.java 5.5KB

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