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.

InviteLabel.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.EdtHandlerInvocation;
  19. import com.dmdirc.addons.ui_swing.MainFrame;
  20. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  21. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  22. import com.dmdirc.events.ServerInviteExpiredEvent;
  23. import com.dmdirc.events.ServerInviteReceivedEvent;
  24. import com.dmdirc.interfaces.Connection;
  25. import com.dmdirc.events.eventbus.EventBus;
  26. import com.dmdirc.interfaces.InviteManager;
  27. import com.dmdirc.addons.ui_swing.components.IconManager;
  28. import java.awt.Window;
  29. import java.awt.event.MouseEvent;
  30. import java.util.List;
  31. import java.util.Optional;
  32. import javax.inject.Inject;
  33. import javax.swing.BorderFactory;
  34. import javax.swing.JLabel;
  35. import javax.swing.JMenuItem;
  36. import javax.swing.JPopupMenu;
  37. import javax.swing.JSeparator;
  38. import net.engio.mbassy.listener.Handler;
  39. /**
  40. * A status bar component to show invites to the user and enable them to accept or dismiss them.
  41. */
  42. public class InviteLabel extends StatusbarPopupPanel<JLabel> {
  43. /** A version number for this class. */
  44. private static final long serialVersionUID = 1;
  45. /** Invite popup menu. */
  46. private final JPopupMenu menu;
  47. /** Dismiss invites menu item. */
  48. private final JMenuItem dismiss;
  49. /** Accept invites menu item. */
  50. private final JMenuItem accept;
  51. /** Parent window that will own popup windows. */
  52. private final Window parentWindow;
  53. /** The client event bus to use for invite events. */
  54. private final EventBus eventBus;
  55. /** The swing event bus to use for selection events. */
  56. private final SwingEventBus swingEventBus;
  57. /** Active connection. */
  58. private Optional<Connection> activeConnection;
  59. @Inject
  60. public InviteLabel(final EventBus eventBus, final IconManager iconManager,
  61. final MainFrame mainFrame, final SwingEventBus swingEventBus) {
  62. super(new JLabel());
  63. this.parentWindow = mainFrame;
  64. this.eventBus = eventBus;
  65. this.swingEventBus = swingEventBus;
  66. this.activeConnection = Optional.empty();
  67. setBorder(BorderFactory.createEtchedBorder());
  68. label.setIcon(iconManager.getIcon("invite"));
  69. menu = new JPopupMenu();
  70. dismiss = new JMenuItem("Dismiss all invites");
  71. dismiss.addActionListener(e -> activeConnection.map(Connection::getInviteManager)
  72. .ifPresent(InviteManager::removeInvites));
  73. accept = new JMenuItem("Accept all invites");
  74. accept.addActionListener(e -> activeConnection.map(Connection::getInviteManager)
  75. .ifPresent(InviteManager::acceptInvites));
  76. }
  77. /**
  78. * Initialises the invite label, adding appropriate listeners.
  79. */
  80. public void init() {
  81. swingEventBus.subscribe(this);
  82. eventBus.subscribe(this);
  83. update();
  84. }
  85. @Override
  86. protected StatusbarPopupWindow getWindow() {
  87. return new InvitePopup(this, activeConnection, parentWindow);
  88. }
  89. /**
  90. * Populates the menu.
  91. */
  92. private void popuplateMenu() {
  93. menu.removeAll();
  94. activeConnection
  95. .map(Connection::getInviteManager)
  96. .map(InviteManager::getInvites)
  97. .ifPresent(invites -> invites.stream()
  98. .map(InviteAction::new)
  99. .map(JMenuItem::new)
  100. .map(menu::add));
  101. menu.add(new JSeparator());
  102. menu.add(accept);
  103. menu.add(dismiss);
  104. }
  105. /**
  106. * Updates the invite label for the currently active server.
  107. */
  108. private void update() {
  109. if (activeConnection
  110. .map(Connection::getInviteManager)
  111. .map(InviteManager::getInvites)
  112. .map(List::isEmpty).orElse(true)) {
  113. setVisible(false);
  114. closeDialog();
  115. } else {
  116. refreshDialog();
  117. setVisible(true);
  118. }
  119. }
  120. @Handler(invocation = EdtHandlerInvocation.class)
  121. public void handleInviteReceived(final ServerInviteReceivedEvent event) {
  122. update();
  123. }
  124. @Handler(invocation = EdtHandlerInvocation.class)
  125. public void handleInviteExpired(final ServerInviteExpiredEvent event) {
  126. update();
  127. }
  128. @Override
  129. public void mouseReleased(final MouseEvent e) {
  130. mouseClicked(e);
  131. popuplateMenu();
  132. if (menu.getComponentCount() > 0) {
  133. menu.show(this, e.getX(), e.getY());
  134. }
  135. }
  136. @Handler(invocation = EdtHandlerInvocation.class)
  137. public void selectionChanged(final SwingWindowSelectedEvent event) {
  138. if (event.getWindow().isPresent()) {
  139. activeConnection = event.getWindow().get().getContainer().getConnection();
  140. } else {
  141. activeConnection = Optional.empty();
  142. }
  143. update();
  144. }
  145. }