Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UnreadStatusManagerImpl.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.ui.messages;
  18. import com.dmdirc.config.ConfigBinding;
  19. import com.dmdirc.events.BaseChannelTextEvent;
  20. import com.dmdirc.events.BaseQueryTextEvent;
  21. import com.dmdirc.events.ChannelHighlightEvent;
  22. import com.dmdirc.events.DisplayProperty;
  23. import com.dmdirc.events.DisplayableEvent;
  24. import com.dmdirc.events.QueryHighlightEvent;
  25. import com.dmdirc.events.UnreadStatusChangedEvent;
  26. import com.dmdirc.events.eventbus.EventBus;
  27. import com.dmdirc.interfaces.WindowModel;
  28. import com.dmdirc.util.colours.Colour;
  29. import java.util.Optional;
  30. import net.engio.mbassy.listener.Handler;
  31. /**
  32. * Tracks unread messages and other notifications.
  33. */
  34. public class UnreadStatusManagerImpl implements UnreadStatusManager {
  35. private final EventBus eventBus;
  36. private final WindowModel container;
  37. private final ColourManager colourManager;
  38. private int unreadLines;
  39. private Optional<Colour> notificationColour = Optional.empty();
  40. private Optional<Colour> miscellaneousColour = Optional.of(Colour.GREEN);
  41. private Optional<Colour> messageColour = Optional.of(Colour.BLUE);
  42. private Optional<Colour> highlightColour = Optional.of(Colour.RED);
  43. public UnreadStatusManagerImpl(final WindowModel container) {
  44. this.container = container;
  45. this.eventBus = container.getEventBus();
  46. this.colourManager = new ColourManagerImpl(container.getConfigManager());
  47. }
  48. @Handler
  49. public void handleDisplayableEvent(final DisplayableEvent event) {
  50. if (includeEvent(event)) {
  51. updateStatus(miscellaneousColour, unreadLines + 1);
  52. }
  53. }
  54. @Handler
  55. public void handleChannelTextEvent(final BaseChannelTextEvent event) {
  56. if (includeEvent(event)) {
  57. updateStatus(messageColour);
  58. }
  59. }
  60. @Handler
  61. public void handleQueryTextEvent(final BaseQueryTextEvent event) {
  62. if (includeEvent(event)) {
  63. updateStatus(messageColour);
  64. }
  65. }
  66. @Handler
  67. public void handleChannelHighlightEvent(final ChannelHighlightEvent event) {
  68. if (includeEvent(event)) {
  69. updateStatus(highlightColour);
  70. }
  71. }
  72. @Handler
  73. public void handleQueryHighlightEvent(final QueryHighlightEvent event) {
  74. if (includeEvent(event)) {
  75. updateStatus(highlightColour);
  76. }
  77. }
  78. private boolean includeEvent(final DisplayableEvent event) {
  79. return event.getSource().equals(container)
  80. && !event.getDisplayProperty(DisplayProperty.DO_NOT_DISPLAY).orElse(false);
  81. }
  82. @Override
  83. public int getUnreadLines() {
  84. return unreadLines;
  85. }
  86. @Override
  87. public Optional<Colour> getNotificationColour() {
  88. return notificationColour;
  89. }
  90. @Override
  91. public void clearStatus() {
  92. updateStatus(Optional.empty(), 0);
  93. }
  94. private void updateStatus(final Optional<Colour> desiredColour) {
  95. updateStatus(desiredColour, unreadLines);
  96. }
  97. private void updateStatus(final Optional<Colour> desiredColour, final int newUnreadCount) {
  98. final Optional<Colour> newColour = getBestColour(desiredColour, notificationColour);
  99. final boolean updated = !newColour.equals(notificationColour)
  100. || newUnreadCount != unreadLines;
  101. notificationColour = newColour;
  102. unreadLines = newUnreadCount;
  103. if (updated) {
  104. publishChangedEvent();
  105. }
  106. }
  107. private Optional<Colour> getBestColour(
  108. final Optional<Colour> desiredColour,
  109. final Optional<Colour> existingColour) {
  110. if (!desiredColour.isPresent()) {
  111. // If we're trying to explicitly reset, go with the empty one.
  112. return desiredColour;
  113. }
  114. if (desiredColour.equals(highlightColour)
  115. || !existingColour.isPresent()
  116. || existingColour.equals(miscellaneousColour)) {
  117. return desiredColour;
  118. } else {
  119. return existingColour;
  120. }
  121. }
  122. @ConfigBinding(domain = "ui", key = "miscellaneousNotificationColour")
  123. void handleMiscellaneousColour(final String colour) {
  124. final Optional<Colour> newColour = Optional.ofNullable(
  125. colourManager.getColourFromString(colour, Colour.GREEN));
  126. if (notificationColour.equals(miscellaneousColour)) {
  127. notificationColour = newColour;
  128. publishChangedEvent();
  129. }
  130. miscellaneousColour = newColour;
  131. }
  132. @ConfigBinding(domain = "ui", key = "messageNotificationColour")
  133. void handleMessageColour(final String colour) {
  134. final Optional<Colour> newColour = Optional.ofNullable(
  135. colourManager.getColourFromString(colour, Colour.BLUE));
  136. if (notificationColour.equals(messageColour)) {
  137. notificationColour = newColour;
  138. publishChangedEvent();
  139. }
  140. messageColour = newColour;
  141. }
  142. @ConfigBinding(domain = "ui", key = "highlightNotificationColour")
  143. void handleHighlightColour(final String colour) {
  144. final Optional<Colour> newColour = Optional.ofNullable(
  145. colourManager.getColourFromString(colour, Colour.RED));
  146. if (notificationColour.equals(highlightColour)) {
  147. notificationColour = newColour;
  148. publishChangedEvent();
  149. }
  150. highlightColour = newColour;
  151. }
  152. private void publishChangedEvent() {
  153. eventBus.publishAsync(new UnreadStatusChangedEvent(container, this, notificationColour,
  154. unreadLines));
  155. }
  156. }