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.7KB

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