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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2014 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. /**
  35. * Manages a group list request.
  36. */
  37. public class GroupListManager implements GroupListStartListener,
  38. GroupListEntryListener, GroupListEndListener {
  39. /** List of registered listeners. */
  40. private final ListenerList listenerList = new ListenerList();
  41. /** The connection to request group information from. */
  42. private final Connection connection;
  43. /** The cached groups. */
  44. private final ObservableList<GroupListEntry> groups = new ObservableListDecorator<>(
  45. new LinkedList<GroupListEntry>());
  46. public GroupListManager(final Connection connection) {
  47. this.connection = connection;
  48. }
  49. public ObservableList<GroupListEntry> getGroups() {
  50. return groups;
  51. }
  52. /**
  53. * Starts a search with the given search terms.
  54. *
  55. * @param searchTerm The term to search for
  56. *
  57. * @see Parser#requestGroupList(java.lang.String)
  58. */
  59. public void startSearch(final String searchTerm) {
  60. groups.clear();
  61. connection.getParser().getCallbackManager().addCallback(GroupListStartListener.class, this);
  62. connection.getParser().getCallbackManager().addCallback(GroupListEntryListener.class, this);
  63. connection.getParser().getCallbackManager().addCallback(GroupListEndListener.class, this);
  64. connection.getParser().requestGroupList(searchTerm);
  65. }
  66. @Override
  67. public void onGroupListStart(final Parser parser, final Date date) {
  68. listenerList.getCallable(GroupListObserver.class).onGroupListStarted();
  69. }
  70. @Override
  71. public void onGroupListEntry(final Parser parser, final Date date,
  72. final String name, final int users, final String topic) {
  73. groups.add(new GroupListEntry(name, users, topic));
  74. }
  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 manager.
  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. }