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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.Invite;
  26. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  27. import com.dmdirc.addons.ui_swing.MainFrame;
  28. import com.dmdirc.addons.ui_swing.UIUtilities;
  29. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  30. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  31. import com.dmdirc.events.ServerConnectErrorEvent;
  32. import com.dmdirc.events.ServerConnectedEvent;
  33. import com.dmdirc.events.ServerDisconnectedEvent;
  34. import com.dmdirc.interfaces.Connection;
  35. import com.dmdirc.interfaces.ConnectionManager;
  36. import com.dmdirc.interfaces.InviteListener;
  37. import com.dmdirc.ui.IconManager;
  38. import java.awt.Window;
  39. import java.awt.event.ActionEvent;
  40. import java.awt.event.ActionListener;
  41. import java.awt.event.MouseEvent;
  42. import java.util.Collection;
  43. import javax.inject.Inject;
  44. import javax.swing.BorderFactory;
  45. import javax.swing.JLabel;
  46. import javax.swing.JMenuItem;
  47. import javax.swing.JPopupMenu;
  48. import javax.swing.JSeparator;
  49. import net.engio.mbassy.listener.Handler;
  50. import net.engio.mbassy.listener.Invoke;
  51. /**
  52. * A status bar component to show invites to the user and enable them to accept or dismiss them.
  53. */
  54. public class InviteLabel extends StatusbarPopupPanel<JLabel>
  55. implements InviteListener, ActionListener {
  56. /** A version number for this class. */
  57. private static final long serialVersionUID = 1;
  58. /** Invite popup menu. */
  59. private final JPopupMenu menu;
  60. /** Dismiss invites menu item. */
  61. private final JMenuItem dismiss;
  62. /** Accept invites menu item. */
  63. private final JMenuItem accept;
  64. /** Parent window that will own popup windows. */
  65. private final Window parentWindow;
  66. /** Active connection. */
  67. private Connection activeConnection;
  68. @Inject
  69. public InviteLabel(
  70. final DMDircMBassador eventBus,
  71. @GlobalConfig final IconManager iconManager,
  72. final ConnectionManager connectionManager,
  73. final MainFrame mainFrame,
  74. final SwingEventBus swingEventBus) {
  75. super(new JLabel());
  76. this.parentWindow = mainFrame;
  77. setBorder(BorderFactory.createEtchedBorder());
  78. label.setIcon(iconManager.getIcon("invite"));
  79. menu = new JPopupMenu();
  80. dismiss = new JMenuItem("Dismiss all invites");
  81. dismiss.setActionCommand("dismissAll");
  82. dismiss.addActionListener(this);
  83. accept = new JMenuItem("Accept all invites");
  84. accept.setActionCommand("acceptAll");
  85. accept.addActionListener(this);
  86. for (final Connection connection : connectionManager.getConnections()) {
  87. connection.addInviteListener(this);
  88. }
  89. swingEventBus.subscribe(this);
  90. eventBus.subscribe(this);
  91. update();
  92. }
  93. @Override
  94. protected StatusbarPopupWindow getWindow() {
  95. return new InvitePopup(this, activeConnection, parentWindow);
  96. }
  97. /**
  98. * Populates the menu.
  99. */
  100. private void popuplateMenu() {
  101. menu.removeAll();
  102. final Collection<Invite> invites = activeConnection.getInvites();
  103. for (final Invite invite : invites) {
  104. menu.add(new JMenuItem(new InviteAction(invite)));
  105. }
  106. menu.add(new JSeparator());
  107. menu.add(accept);
  108. menu.add(dismiss);
  109. }
  110. /**
  111. * Updates the invite label for the currently active server.
  112. */
  113. private void update() {
  114. UIUtilities.invokeLater(() -> {
  115. if (activeConnection == null || activeConnection.getInvites().isEmpty()) {
  116. setVisible(false);
  117. closeDialog();
  118. } else {
  119. refreshDialog();
  120. setVisible(true);
  121. }
  122. });
  123. }
  124. @Override
  125. public void inviteReceived(final Connection connection, final Invite invite) {
  126. update();
  127. }
  128. @Override
  129. public void inviteExpired(final Connection connection, final Invite invite) {
  130. update();
  131. }
  132. @Handler
  133. public void handleServerConnected(final ServerConnectedEvent event) {
  134. event.getConnection().addInviteListener(this);
  135. }
  136. @Handler
  137. public void handleServerDisconnected(final ServerDisconnectedEvent event) {
  138. handleServerRemoved(event.getConnection());
  139. }
  140. @Handler
  141. public void handleServerConnectError(final ServerConnectErrorEvent event) {
  142. handleServerRemoved(event.getConnection());
  143. }
  144. private void handleServerRemoved(final Connection connection) {
  145. connection.removeInviteListener(this);
  146. }
  147. @Override
  148. public void mouseReleased(final MouseEvent e) {
  149. mouseClicked(e);
  150. popuplateMenu();
  151. if (menu.getComponentCount() > 0) {
  152. menu.show(this, e.getX(), e.getY());
  153. }
  154. }
  155. @Override
  156. public void actionPerformed(final ActionEvent e) {
  157. switch (e.getActionCommand()) {
  158. case "acceptAll":
  159. activeConnection.acceptInvites();
  160. break;
  161. case "dismissAll":
  162. activeConnection.removeInvites();
  163. break;
  164. }
  165. }
  166. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  167. public void selectionChanged(final SwingWindowSelectedEvent event) {
  168. if (event.getWindow().isPresent()) {
  169. activeConnection = event.getWindow().get().getContainer().getConnection();
  170. } else {
  171. activeConnection = null;
  172. }
  173. update();
  174. }
  175. }