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.

FeedbackNag.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.addons.ui_swing.dialogs.feedback.FeedbackDialog;
  24. import com.dmdirc.addons.ui_swing.injection.DialogProvider;
  25. import com.dmdirc.events.StatusBarComponentAddedEvent;
  26. import com.dmdirc.events.StatusBarComponentRemovedEvent;
  27. import com.dmdirc.interfaces.EventBus;
  28. import com.dmdirc.interfaces.ui.StatusBarComponent;
  29. import com.dmdirc.addons.ui_swing.components.IconManager;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.awt.event.MouseEvent;
  33. import java.awt.event.MouseListener;
  34. import javax.inject.Inject;
  35. import javax.swing.BorderFactory;
  36. import javax.swing.JLabel;
  37. import javax.swing.JMenuItem;
  38. import javax.swing.JPopupMenu;
  39. /**
  40. * Feedback nag icon.
  41. */
  42. public class FeedbackNag extends JLabel implements StatusBarComponent,
  43. MouseListener, ActionListener {
  44. /** A version number for this class. */
  45. private static final long serialVersionUID = 1;
  46. /** Dismiss menu. */
  47. private final JPopupMenu menu;
  48. /** Show menu item. */
  49. private final JMenuItem show;
  50. /** Provider of feedback dialogs. */
  51. private final DialogProvider<FeedbackDialog> feedbackDialogProvider;
  52. /** The event bus to post events to. */
  53. private final EventBus eventBus;
  54. /**
  55. * Creates a new feedback nag.
  56. *
  57. * @param iconManager The icon manager to use to find the feedback nag icon.
  58. * @param feedbackDialogProvider Provider of feedback dialogs.
  59. * @param eventBus The event bus to post messages to
  60. */
  61. @Inject
  62. public FeedbackNag(
  63. final IconManager iconManager,
  64. final DialogProvider<FeedbackDialog> feedbackDialogProvider,
  65. final EventBus eventBus) {
  66. this.feedbackDialogProvider = feedbackDialogProvider;
  67. this.eventBus = eventBus;
  68. menu = new JPopupMenu();
  69. show = new JMenuItem("Open");
  70. final JMenuItem dismiss = new JMenuItem("Dismiss");
  71. setIcon(iconManager.getIcon("feedback"));
  72. setBorder(BorderFactory.createEtchedBorder());
  73. setToolTipText("We would appreciate any feedback you may have about "
  74. + "DMDirc.");
  75. menu.add(show);
  76. menu.add(dismiss);
  77. show.addActionListener(this);
  78. dismiss.addActionListener(this);
  79. addMouseListener(this);
  80. // TODO: There should be some other class which adds the nag to the status bar
  81. eventBus.publishAsync(new StatusBarComponentAddedEvent(this));
  82. }
  83. @Override
  84. public void mouseClicked(final MouseEvent e) {
  85. checkMouseEvent(e);
  86. }
  87. @Override
  88. public void mousePressed(final MouseEvent e) {
  89. checkMouseEvent(e);
  90. }
  91. @Override
  92. public void mouseReleased(final MouseEvent e) {
  93. if (e.getButton() == 1) {
  94. feedbackDialogProvider.displayOrRequestFocus();
  95. eventBus.publishAsync(new StatusBarComponentRemovedEvent(this));
  96. }
  97. checkMouseEvent(e);
  98. }
  99. @Override
  100. public void mouseEntered(final MouseEvent e) {
  101. checkMouseEvent(e);
  102. }
  103. @Override
  104. public void mouseExited(final MouseEvent e) {
  105. checkMouseEvent(e);
  106. }
  107. /**
  108. * Checks a mouse event for a popup trigger.
  109. *
  110. * @param e Mouse event
  111. */
  112. private void checkMouseEvent(final MouseEvent e) {
  113. if (e.isPopupTrigger()) {
  114. menu.show(this, e.getX(), e.getY());
  115. }
  116. }
  117. @Override
  118. public void actionPerformed(final ActionEvent e) {
  119. if (e.getSource() == show) {
  120. feedbackDialogProvider.displayOrRequestFocus();
  121. }
  122. eventBus.publishAsync(new StatusBarComponentRemovedEvent(this));
  123. }
  124. }