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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2006-2011 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.Server;
  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.ObservableList;
  30. import com.dmdirc.util.collections.ObservableListDecorator;
  31. import java.util.Date;
  32. import java.util.LinkedList;
  33. import lombok.Getter;
  34. import lombok.ListenerSupport;
  35. import lombok.RequiredArgsConstructor;
  36. /**
  37. * Manages a group list request.
  38. */
  39. @RequiredArgsConstructor
  40. @ListenerSupport(GroupListObserver.class)
  41. public class GroupListManager implements GroupListStartListener,
  42. GroupListEntryListener, GroupListEndListener {
  43. /** The server to request group information from. */
  44. private final Server server;
  45. /** The cached groups. */
  46. @Getter
  47. private final ObservableList<GroupListEntry> groups
  48. = new ObservableListDecorator<GroupListEntry>(new LinkedList<GroupListEntry>());
  49. /**
  50. * Starts a search with the given search terms.
  51. *
  52. * @param searchTerm The term to search for
  53. * @see Parser#requestGroupList(java.lang.String)
  54. */
  55. public void startSearch(final String searchTerm) {
  56. groups.clear();
  57. server.getParser().getCallbackManager().addCallback(GroupListStartListener.class, this);
  58. server.getParser().getCallbackManager().addCallback(GroupListEntryListener.class, this);
  59. server.getParser().getCallbackManager().addCallback(GroupListEndListener.class, this);
  60. server.getParser().requestGroupList(searchTerm);
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. public void onGroupListStart(final Parser parser, final Date date) {
  65. fireOnGroupListStarted();
  66. }
  67. /** {@inheritDoc} */
  68. @Override
  69. public void onGroupListEntry(final Parser parser, final Date date,
  70. final String name, final int users, final String topic) {
  71. groups.add(new GroupListEntry(name, users, topic));
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public void onGroupListEnd(final Parser parser, final Date date) {
  76. parser.getCallbackManager().delAllCallback(this);
  77. fireOnGroupListFinished();
  78. }
  79. /**
  80. * Joins a group list entry.
  81. *
  82. * @param entry Group list entry to join
  83. */
  84. public void joinGroupListEntry(final GroupListEntry entry) {
  85. server.join(new ChannelJoinRequest(entry.getName()));
  86. }
  87. }