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

MessageLabel.java 7.3KB

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