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.

ComponentEventHandler.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. *
  9. * class for event handlers used by ComponentEventHandler
  10. *
  11. * @author davengo GmbH (Germany/Berlin, www.davengo.com)
  12. * @since 6.2
  13. *
  14. */
  15. public class ComponentEventHandler {
  16. public static final String HANDLER_LISTEN_ATTRIBUTE = "listenEvents";
  17. public static final String HANDLER_TRIGGER_VARIABLE = "fireEvent";
  18. private List<String> eventRegistrations;
  19. private ComponentDetail detail;
  20. private ApplicationConnection client;
  21. /**
  22. * creates a new <code>ComponentEventHandler</code> instance for the given
  23. * <code>ComponentDetail</code> and <code>ApplicationConntection</code>
  24. * instance.
  25. *
  26. * @param detail
  27. * the attached ComponentDetail
  28. * @param client
  29. * the <code>ApplicationConnection</code> for sending events
  30. *
  31. * @see ApplicationConnection
  32. * @see ComponentDetail
  33. * @since 6.2
  34. */
  35. public ComponentEventHandler(ComponentDetail detail,
  36. ApplicationConnection client) {
  37. this.detail = detail;
  38. this.client = client;
  39. this.eventRegistrations = null;
  40. }
  41. /**
  42. * Fires a event which is transmitted to the server and passed on the the
  43. * components handleEvent method provided listeners have been registered on
  44. * the server side.
  45. *
  46. * @param eventIdentifier
  47. * the unique identifier for the event
  48. * @param parameters
  49. * the parameters for the event (can be null)
  50. * @since 6.2
  51. */
  52. public void fireEvent(String eventIdentifier, String... parameters) {
  53. fireEvent(eventIdentifier, false, parameters);
  54. }
  55. /**
  56. * Fires a component event which is transmitted to the server and passed on
  57. * the the components handleEvent method. The event is sent to the server
  58. * even though there are no explicit listeners registered on the server
  59. * side.
  60. *
  61. * @param eventIdentifier
  62. * the unique identifier for the event
  63. * @param parameters
  64. * the parameters for the event (can be null)
  65. * @since 6.2
  66. */
  67. public void fireComponentEvent(String eventIdentifier, String... parameters) {
  68. fireEvent(eventIdentifier, true, parameters);
  69. }
  70. /**
  71. * Transmit the event to the Server (Fires a event which is transmitted to
  72. * the server and passed on the the components handleEvent method)
  73. *
  74. * @param eventIdentifier
  75. * the unique identifier for the event
  76. * @param forceTransmission
  77. * enforce the transmission to the server
  78. * @param parameters
  79. * the parameters for the event (can be null)
  80. * @since 6.2
  81. */
  82. private void fireEvent(String eventIdentifier, boolean forceTransmission,
  83. String... parameters) {
  84. String[] event;
  85. // filter events which are not listened on the server-side right here
  86. boolean transmit = forceTransmission
  87. || ((!(eventRegistrations == null)) && eventRegistrations
  88. .contains(eventIdentifier));
  89. if (transmit) {
  90. if (parameters != null) {
  91. event = new String[parameters.length + 1];
  92. event[0] = eventIdentifier;
  93. for (int i = 0; i < parameters.length; i++) {
  94. event[i + 1] = parameters[i];
  95. }
  96. } else {
  97. event = new String[] { eventIdentifier };
  98. }
  99. // transmit the event to the server-side
  100. client.updateVariable(detail.getPid(), HANDLER_TRIGGER_VARIABLE,
  101. event, true);
  102. }
  103. }
  104. /**
  105. * Registers the Events listened on the server-side from the UIDL
  106. *
  107. * @param componentUIDL
  108. * @since 6.2
  109. */
  110. void registerEventsFromUIDL(UIDL componentUIDL) {
  111. // read out the request event handlers
  112. if (componentUIDL.hasAttribute(HANDLER_LISTEN_ATTRIBUTE)) {
  113. String[] requestedEvents = componentUIDL
  114. .getStringArrayAttribute(HANDLER_LISTEN_ATTRIBUTE);
  115. // create the eventRegistrations list if necessary
  116. if ((requestedEvents.length > 0) && (eventRegistrations == null)) {
  117. eventRegistrations = new ArrayList<String>();
  118. }
  119. // parse the requested event handlers
  120. for (String reqEvent : requestedEvents) {
  121. if (!eventRegistrations.contains(reqEvent)) {
  122. eventRegistrations.add(reqEvent);
  123. }
  124. }
  125. }
  126. }
  127. }