Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MessagePopup.java 5.7KB

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