選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

GroupListManager.java 4.0KB

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