Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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