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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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(new Runnable() {
  115. @Override
  116. public void run() {
  117. if (activeConnection == null || activeConnection.getInvites().isEmpty()) {
  118. setVisible(false);
  119. closeDialog();
  120. } else {
  121. refreshDialog();
  122. setVisible(true);
  123. }
  124. }
  125. });
  126. }
  127. @Override
  128. public void inviteReceived(final Connection connection, final Invite invite) {
  129. update();
  130. }
  131. @Override
  132. public void inviteExpired(final Connection connection, final Invite invite) {
  133. update();
  134. }
  135. @Handler
  136. public void handleServerConnected(final ServerConnectedEvent event) {
  137. event.getConnection().addInviteListener(this);
  138. }
  139. @Handler
  140. public void handleServerDisconnected(final ServerDisconnectedEvent event) {
  141. handleServerRemoved(event.getConnection());
  142. }
  143. @Handler
  144. public void handleServerConnectError(final ServerConnectErrorEvent event) {
  145. handleServerRemoved(event.getConnection());
  146. }
  147. private void handleServerRemoved(final Connection connection) {
  148. connection.removeInviteListener(this);
  149. }
  150. @Override
  151. public void mouseReleased(final MouseEvent e) {
  152. mouseClicked(e);
  153. popuplateMenu();
  154. if (menu.getComponentCount() > 0) {
  155. menu.show(this, e.getX(), e.getY());
  156. }
  157. }
  158. @Override
  159. public void actionPerformed(final ActionEvent e) {
  160. switch (e.getActionCommand()) {
  161. case "acceptAll":
  162. activeConnection.acceptInvites();
  163. break;
  164. case "dismissAll":
  165. activeConnection.removeInvites();
  166. break;
  167. }
  168. }
  169. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  170. public void selectionChanged(final SwingWindowSelectedEvent event) {
  171. if (event.getWindow().isPresent()) {
  172. activeConnection = event.getWindow().get().getContainer().getConnection();
  173. } else {
  174. activeConnection = null;
  175. }
  176. update();
  177. }
  178. }