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.

GroupListManager.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.lists;
  23. import com.dmdirc.interfaces.Connection;
  24. import com.dmdirc.parser.common.ChannelJoinRequest;
  25. import com.dmdirc.parser.interfaces.Parser;
  26. import com.dmdirc.parser.interfaces.callbacks.GroupListEndListener;
  27. import com.dmdirc.parser.interfaces.callbacks.GroupListEntryListener;
  28. import com.dmdirc.parser.interfaces.callbacks.GroupListStartListener;
  29. import com.dmdirc.util.collections.ListenerList;
  30. import com.dmdirc.util.collections.ObservableList;
  31. import com.dmdirc.util.collections.ObservableListDecorator;
  32. import java.util.Date;
  33. import java.util.LinkedList;
  34. import lombok.Getter;
  35. import lombok.RequiredArgsConstructor;
  36. /**
  37. * Manages a group list request.
  38. */
  39. @RequiredArgsConstructor
  40. public class GroupListManager implements GroupListStartListener,
  41. GroupListEntryListener, GroupListEndListener {
  42. /** List of registered listeners. */
  43. private final ListenerList listenerList = new ListenerList();
  44. /** The connection to request group information from. */
  45. private final Connection connection;
  46. /** The cached groups. */
  47. @Getter
  48. private final ObservableList<GroupListEntry> groups
  49. = new ObservableListDecorator<>(new LinkedList<GroupListEntry>());
  50. /**
  51. * Starts a search with the given search terms.
  52. *
  53. * @param searchTerm The term to search for
  54. * @see Parser#requestGroupList(java.lang.String)
  55. */
  56. public void startSearch(final String searchTerm) {
  57. groups.clear();
  58. connection.getParser().getCallbackManager().addCallback(GroupListStartListener.class, this);
  59. connection.getParser().getCallbackManager().addCallback(GroupListEntryListener.class, this);
  60. connection.getParser().getCallbackManager().addCallback(GroupListEndListener.class, this);
  61. connection.getParser().requestGroupList(searchTerm);
  62. }
  63. /** {@inheritDoc} */
  64. @Override
  65. public void onGroupListStart(final Parser parser, final Date date) {
  66. listenerList.getCallable(GroupListObserver.class).onGroupListStarted();
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public void onGroupListEntry(final Parser parser, final Date date,
  71. final String name, final int users, final String topic) {
  72. groups.add(new GroupListEntry(name, users, topic));
  73. }
  74. /** {@inheritDoc} */
  75. @Override
  76. public void onGroupListEnd(final Parser parser, final Date date) {
  77. parser.getCallbackManager().delAllCallback(this);
  78. listenerList.getCallable(GroupListObserver.class).onGroupListFinished();
  79. }
  80. /**
  81. * Joins a group list entry.
  82. *
  83. * @param entry Group list entry to join
  84. */
  85. public void joinGroupListEntry(final GroupListEntry entry) {
  86. connection.join(new ChannelJoinRequest(entry.getName()));
  87. }
  88. /**
  89. * Adds an observer to this mananger.
  90. *
  91. * @param observer The observer to be notified of group start/stop events.
  92. */
  93. public void addGroupListObserver(final GroupListObserver observer) {
  94. listenerList.add(GroupListObserver.class, observer);
  95. }
  96. /**
  97. * Removes an observer from this manager.
  98. *
  99. * @param observer The observer to be removed.
  100. */
  101. public void removeGroupListObserver(final GroupListObserver observer) {
  102. listenerList.remove(GroupListObserver.class, observer);
  103. }
  104. }