Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

InviteLabel.java 5.9KB

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