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.

ClientEventList.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.event;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. /**
  8. *
  9. * <code>ClientEventList</code> class used to store the registered events so a
  10. * list of the required client event identifiers (that the client-side should
  11. * listen for and send to the server-side) can be sent to the client-side via a
  12. * variable.
  13. *
  14. * @author davengo GmbH (Germany/Berlin, www.davengo.com)
  15. * @since 6.2
  16. *
  17. */
  18. public class ClientEventList {
  19. /**
  20. * the list containing the registered client events (as strings for
  21. * client-side transfer)
  22. *
  23. * @since 6.2
  24. */
  25. private Map<String, Integer> clientEvents = null;
  26. /**
  27. * initializes the list if necessary
  28. *
  29. * @since 6.2
  30. */
  31. private void checkList() {
  32. if (clientEvents == null) {
  33. clientEvents = new HashMap<String, Integer>();
  34. }
  35. }
  36. /**
  37. * listens for the event <br>
  38. * <br>
  39. * increments the internal counter for the event by one
  40. *
  41. * @param eventIdentifier
  42. * the identifier of the event to listen for
  43. *
  44. * @return true, if the event is newly added to the list<br>
  45. * false, if the list already contained the event (the internal
  46. * counter was incremented)
  47. *
  48. * @since 6.2
  49. */
  50. public boolean listenEvent(String eventIdentifier) {
  51. checkList();
  52. if (!clientEvents.keySet().contains(eventIdentifier)) {
  53. clientEvents.put(eventIdentifier, 1);
  54. return true;
  55. } else {
  56. clientEvents.put(eventIdentifier,
  57. clientEvents.get(eventIdentifier) + 1);
  58. return false;
  59. }
  60. }
  61. /**
  62. * unlistens for an event <br>
  63. * <br>
  64. * decrements the internal counter by one, if 0 is reached the event is
  65. * removed from the event-list
  66. *
  67. * @param eventIdentifier
  68. * the identifier of the event to stop listening for
  69. * @return true, if the event was removed from the list<br>
  70. * false, if the event is hold in list (the internal counter was
  71. * greater than zero)
  72. */
  73. public boolean unlistenEvent(String eventIdentifier) {
  74. checkList();
  75. if (clientEvents.keySet().contains(eventIdentifier)) {
  76. clientEvents.put(eventIdentifier,
  77. clientEvents.get(eventIdentifier) - 1);
  78. if (clientEvents.get(eventIdentifier) <= 0) {
  79. clientEvents.remove(eventIdentifier);
  80. return true;
  81. }
  82. return false;
  83. }
  84. return false;
  85. }
  86. /**
  87. * @return a string array containing all registered events
  88. *
  89. * @since 6.2
  90. */
  91. public String[] getEvents() {
  92. if (clientEvents == null) {
  93. return new String[] {};
  94. }
  95. return clientEvents.keySet().toArray(new String[] {});
  96. }
  97. }