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.

GroupChatHandler.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.jpq;
  23. import com.dmdirc.config.ConfigBinder;
  24. import com.dmdirc.config.ConfigBinding;
  25. import com.dmdirc.events.ChannelJoinEvent;
  26. import com.dmdirc.events.ChannelPartEvent;
  27. import com.dmdirc.events.ChannelQuitEvent;
  28. import com.dmdirc.events.DisplayProperty;
  29. import com.dmdirc.events.DisplayableEvent;
  30. import com.dmdirc.interfaces.GroupChat;
  31. import com.google.common.annotations.VisibleForTesting;
  32. import net.engio.mbassy.listener.Handler;
  33. /**
  34. * Handles {@link ChannelJoinEvent}, {@link ChannelPartEvent}, {@link ChannelQuitEvent} events and
  35. * hides them if the required.
  36. */
  37. public class GroupChatHandler {
  38. private final GroupChat groupChat;
  39. private final ConfigBinder binder;
  40. private boolean hideEvents;
  41. public GroupChatHandler(final String domain, final GroupChat groupChat) {
  42. this.groupChat = groupChat;
  43. binder = groupChat.getWindowModel().getConfigManager().getBinder()
  44. .withDefaultDomain(domain);
  45. }
  46. /**
  47. * Loads this handler, adds required listeners and bindings.
  48. */
  49. public void load() {
  50. groupChat.getEventBus().subscribe(this);
  51. binder.bind(this, GroupChatHandler.class);
  52. }
  53. /**
  54. * Unloads this handler, removes required listeners and bindings.
  55. */
  56. public void unload() {
  57. groupChat.getEventBus().unsubscribe(this);
  58. binder.unbind(this);
  59. }
  60. @VisibleForTesting
  61. @ConfigBinding(key = "hidejpq")
  62. void handleSettingChange(final boolean value) {
  63. hideEvents = value;
  64. }
  65. @SuppressWarnings("TypeMayBeWeakened")
  66. @VisibleForTesting
  67. @Handler
  68. void handleJoin(final ChannelJoinEvent event) {
  69. hideEvent(event);
  70. }
  71. @SuppressWarnings("TypeMayBeWeakened")
  72. @VisibleForTesting
  73. @Handler
  74. void handlePart(final ChannelPartEvent event) {
  75. hideEvent(event);
  76. }
  77. @SuppressWarnings("TypeMayBeWeakened")
  78. @VisibleForTesting
  79. @Handler
  80. void handleQuit(final ChannelQuitEvent event) {
  81. hideEvent(event);
  82. }
  83. private void hideEvent(final DisplayableEvent event) {
  84. if (hideEvents) {
  85. event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  86. }
  87. }
  88. }