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.

MessagePopup.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.components.IconManager;
  19. import com.dmdirc.ui.StatusMessage;
  20. import com.dmdirc.util.collections.RollingList;
  21. import java.awt.Color;
  22. import java.awt.Point;
  23. import java.awt.Window;
  24. import java.awt.event.MouseEvent;
  25. import javax.swing.JLabel;
  26. import javax.swing.JPanel;
  27. import javax.swing.SwingConstants;
  28. import javax.swing.UIManager;
  29. import net.miginfocom.swing.MigLayout;
  30. /**
  31. * Previous status bar messages popup.
  32. */
  33. class MessagePopup extends StatusbarTogglePanel<JLabel> {
  34. /** A version number for this class. */
  35. private static final long serialVersionUID = 2;
  36. /** Parent window. */
  37. private final Window parentWindow;
  38. /** List of historical messages. */
  39. private final RollingList<StatusMessage> messages;
  40. /** Parent panel. */
  41. private final JPanel parent;
  42. /** Icon manager to retrieve icons from. */
  43. private final IconManager iconManager;
  44. /**
  45. * Creates a new message history popup.
  46. *
  47. * @param parent Parent to size against
  48. * @param parentWindow Parent window
  49. * @param iconManager Icon manager to retrieve icons from
  50. */
  51. public MessagePopup(final JPanel parent, final Window parentWindow,
  52. final IconManager iconManager) {
  53. super(new JLabel("^"),
  54. new SidelessEtchedBorder(SidelessEtchedBorder.Side.LEFT),
  55. new SidelessEtchedBorder(SidelessEtchedBorder.Side.TOP));
  56. this.parentWindow = parentWindow;
  57. this.parent = parent;
  58. this.iconManager = iconManager;
  59. messages = new RollingList<>(5);
  60. }
  61. /* {@inheritDoc} */
  62. @Override
  63. protected StatusbarPopupWindow getWindow() {
  64. return new MessageHistoryPanel(this);
  65. }
  66. /* {@inheritDoc} */
  67. @Override
  68. public void mouseEntered(final MouseEvent e) {
  69. super.mouseEntered(e);
  70. if (!isDialogOpen()) {
  71. setBorder(nonSelectedBorder);
  72. setBackground(UIManager.getColor("ToolTip.background"));
  73. setForeground(UIManager.getColor("ToolTip.foreground"));
  74. }
  75. }
  76. /* {@inheritDoc} */
  77. @Override
  78. public void mouseExited(final MouseEvent e) {
  79. super.mouseExited(e);
  80. if (!isDialogOpen()) {
  81. setBorder(new SidelessEtchedBorder(SidelessEtchedBorder.Side.LEFT));
  82. setBackground(null);
  83. setForeground(null);
  84. }
  85. }
  86. /**
  87. * Adds a message to this history window.
  88. *
  89. * @param message to add
  90. */
  91. public void addMessage(final StatusMessage message) {
  92. synchronized (messages) {
  93. messages.add(message);
  94. }
  95. }
  96. /* {@inheritDoc} */
  97. @Override
  98. protected Color getPopupBackground() {
  99. return parent.getBackground();
  100. }
  101. /* {@inheritDoc} */
  102. @Override
  103. protected Color getPopupForeground() {
  104. return getForeground();
  105. }
  106. /** Message history status bar popup window. */
  107. private class MessageHistoryPanel extends StatusbarPopupWindow {
  108. /** A version number for this class. */
  109. private static final long serialVersionUID = 2;
  110. /**
  111. * Creates a new message history window.
  112. *
  113. * @param parent Parent window
  114. */
  115. public MessageHistoryPanel(final JPanel parent) {
  116. super(parent, parentWindow);
  117. }
  118. /* {@inheritDoc} */
  119. @Override
  120. protected void initPanel(final JPanel panel) {
  121. panel.setLayout(new MigLayout("ins 0 0 0 rel, fill, wmin "
  122. + (parent.getWidth() - 5) + ", wmax "
  123. + (parent.getWidth() - 5)));
  124. panel.setBackground(parent.getBackground());
  125. panel.setForeground(parent.getForeground());
  126. panel.setBorder(new GappedEtchedBorder(this));
  127. }
  128. /* {@inheritDoc} */
  129. @Override
  130. protected void initContent(final JPanel panel) {
  131. if (messages.isEmpty()) {
  132. panel.add(new JLabel("No previous messages."), "grow, push");
  133. return;
  134. }
  135. for (final StatusMessage message : messages.getList()) {
  136. panel.add(new JLabel(message.getMessage(), message.getIconType()
  137. == null ? null : iconManager.getIcon(message.getIconType()),
  138. SwingConstants.LEFT), "grow, push, wrap");
  139. }
  140. }
  141. /* {@inheritDoc} */
  142. @Override
  143. protected Point getPopupLocation() {
  144. final Point point = parent.getLocationOnScreen();
  145. point.y = point.y - getHeight() + 2;
  146. return point;
  147. }
  148. }
  149. }