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.

MessageLabel.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.addons.ui_swing.components.statusbar;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.IconManager;
  20. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  21. import com.dmdirc.config.GlobalConfig;
  22. import com.dmdirc.events.StatusBarMessageClearEvent;
  23. import com.dmdirc.events.StatusBarMessageEvent;
  24. import com.dmdirc.events.eventbus.EventBus;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import com.dmdirc.interfaces.ui.StatusBarComponent;
  27. import com.dmdirc.ui.StatusMessage;
  28. import java.awt.Window;
  29. import java.awt.event.MouseEvent;
  30. import java.awt.event.MouseListener;
  31. import java.util.Date;
  32. import java.util.Queue;
  33. import java.util.Timer;
  34. import java.util.TimerTask;
  35. import java.util.concurrent.ConcurrentLinkedQueue;
  36. import javax.inject.Inject;
  37. import javax.swing.JLabel;
  38. import javax.swing.JPanel;
  39. import javax.swing.SwingUtilities;
  40. import net.engio.mbassy.listener.Handler;
  41. import net.miginfocom.swing.MigLayout;
  42. import org.slf4j.Logger;
  43. import org.slf4j.LoggerFactory;
  44. /**
  45. * Message label handles showing messages in the status bar.
  46. */
  47. public class MessageLabel extends JPanel implements StatusBarComponent,
  48. MouseListener {
  49. private static final Logger LOG = LoggerFactory.getLogger(MessageLabel.class);
  50. /** Serial version UID. */
  51. private static final long serialVersionUID = 1;
  52. /** Default status bar message. */
  53. private final StatusMessage defaultMessage;
  54. /** Message queue. */
  55. private final Queue<StatusMessage> queue;
  56. /** Message label. */
  57. private final JLabel label;
  58. /** History label. */
  59. private final MessagePopup historyLabel;
  60. /** Current status message. */
  61. private StatusMessage currentMessage;
  62. /** Timer to clear the message. */
  63. private transient TimerTask messageTimer;
  64. /** Icon manager to retrieve icons from. */
  65. private final IconManager iconManager;
  66. /** The event bus to post status messages on. */
  67. private final EventBus eventBus;
  68. /**
  69. * Instantiates a new message label.
  70. *
  71. * @param iconManager Icon manager to retrieve icons from
  72. * @param config Config to read settings from
  73. * @param parentWindow Parent window
  74. * @param eventBus The event bus to post events on.
  75. */
  76. @Inject
  77. public MessageLabel(
  78. @GlobalConfig final AggregateConfigProvider config,
  79. final IconManager iconManager,
  80. @MainWindow final Window parentWindow,
  81. final EventBus eventBus) {
  82. super(new MigLayout("fill, ins 0, gap 0 0"));
  83. this.iconManager = iconManager;
  84. this.eventBus = eventBus;
  85. queue = new ConcurrentLinkedQueue<>();
  86. defaultMessage = new StatusMessage(null, "Ready.", null, -1, config);
  87. currentMessage = defaultMessage;
  88. label = new JLabel();
  89. historyLabel = new MessagePopup(this, parentWindow, iconManager);
  90. label.setText("Ready.");
  91. label.setBorder(new SidelessEtchedBorder(SidelessEtchedBorder.Side.RIGHT));
  92. label.addMouseListener(this);
  93. add(label, "grow, push");
  94. add(historyLabel, "grow, gapleft 0");
  95. }
  96. /**
  97. * Sets the message for this message label.
  98. *
  99. * @param event Event containing message object to show
  100. */
  101. @Handler
  102. public void setMessage(final StatusBarMessageEvent event) {
  103. LOG.info("Adding message to queue {}", event.getMessage());
  104. queue.add(event.getMessage());
  105. LOG.debug("Queue size: {}", queue.size());
  106. if (queue.size() == 1) {
  107. LOG.info("Showing only message {}", event.getMessage());
  108. currentMessage = event.getMessage();
  109. updateCurrentMessage();
  110. }
  111. }
  112. /**
  113. * Updates the message label to show the current message info.
  114. */
  115. private void updateCurrentMessage() {
  116. SwingUtilities.invokeLater(() -> {
  117. LOG.info("Updating current message: {}", currentMessage);
  118. if (currentMessage.getIconType() == null) {
  119. label.setIcon(null);
  120. } else {
  121. label.setIcon(iconManager.getIcon(currentMessage.getIconType()));
  122. }
  123. label.setText(UIUtilities.clipStringifNeeded(MessageLabel.this,
  124. currentMessage.getMessage(), getWidth()));
  125. if (messageTimer != null && System.currentTimeMillis()
  126. - messageTimer.scheduledExecutionTime() <= 0) {
  127. LOG.debug("Cancelling message timer.");
  128. messageTimer.cancel();
  129. }
  130. if (!defaultMessage.equals(currentMessage)) {
  131. LOG.debug("Starting new message timer.");
  132. messageTimer = new MessageTimerTask(eventBus);
  133. new Timer("SwingStatusBar messageTimer").schedule(
  134. messageTimer, new Date(System.currentTimeMillis()
  135. + 250 + currentMessage.getTimeout() * 1000L));
  136. }
  137. });
  138. }
  139. /**
  140. * Removes the message from the status bar.
  141. *
  142. * @param event The message clear event to handle
  143. */
  144. @Handler
  145. public void clearMessage(final StatusBarMessageClearEvent event) {
  146. LOG.info("Adding message to history {}", currentMessage);
  147. historyLabel.addMessage(currentMessage);
  148. LOG.debug("Queue size: {}", queue.size());
  149. if (queue.size() <= 1) {
  150. queue.remove();
  151. LOG.info("Reverting to default message.");
  152. currentMessage = defaultMessage;
  153. } else {
  154. currentMessage = queue.poll();
  155. LOG.info("Showing next message in queue: {}", currentMessage);
  156. }
  157. updateCurrentMessage();
  158. }
  159. @Override
  160. public void mouseClicked(final MouseEvent e) {
  161. if (currentMessage != null
  162. && currentMessage.getMessageNotifier() != null) {
  163. currentMessage.getMessageNotifier().clickReceived(
  164. e.getButton(), e.getClickCount());
  165. }
  166. }
  167. @Override
  168. public void mousePressed(final MouseEvent e) {
  169. //Ignore
  170. }
  171. @Override
  172. public void mouseReleased(final MouseEvent e) {
  173. //Ignore
  174. }
  175. @Override
  176. public void mouseEntered(final MouseEvent e) {
  177. //Ignore
  178. }
  179. @Override
  180. public void mouseExited(final MouseEvent e) {
  181. //Ignore
  182. }
  183. }