|
@@ -39,10 +39,9 @@ import com.dmdirc.interfaces.InviteListener;
|
39
|
39
|
import com.dmdirc.ui.IconManager;
|
40
|
40
|
|
41
|
41
|
import java.awt.Window;
|
42
|
|
-import java.awt.event.ActionEvent;
|
43
|
|
-import java.awt.event.ActionListener;
|
44
|
42
|
import java.awt.event.MouseEvent;
|
45
|
43
|
import java.util.Collection;
|
|
44
|
+import java.util.Optional;
|
46
|
45
|
|
47
|
46
|
import javax.inject.Inject;
|
48
|
47
|
import javax.swing.BorderFactory;
|
|
@@ -52,13 +51,11 @@ import javax.swing.JPopupMenu;
|
52
|
51
|
import javax.swing.JSeparator;
|
53
|
52
|
|
54
|
53
|
import net.engio.mbassy.listener.Handler;
|
55
|
|
-import net.engio.mbassy.listener.Invoke;
|
56
|
54
|
|
57
|
55
|
/**
|
58
|
56
|
* A status bar component to show invites to the user and enable them to accept or dismiss them.
|
59
|
57
|
*/
|
60
|
|
-public class InviteLabel extends StatusbarPopupPanel<JLabel>
|
61
|
|
- implements InviteListener, ActionListener {
|
|
58
|
+public class InviteLabel extends StatusbarPopupPanel<JLabel> implements InviteListener {
|
62
|
59
|
|
63
|
60
|
/** A version number for this class. */
|
64
|
61
|
private static final long serialVersionUID = 1;
|
|
@@ -70,8 +67,14 @@ public class InviteLabel extends StatusbarPopupPanel<JLabel>
|
70
|
67
|
private final JMenuItem accept;
|
71
|
68
|
/** Parent window that will own popup windows. */
|
72
|
69
|
private final Window parentWindow;
|
|
70
|
+ /** The client event bus to use for invite events. */
|
|
71
|
+ private final DMDircMBassador eventBus;
|
|
72
|
+ /** The swing event bus to use for selection events. */
|
|
73
|
+ private final SwingEventBus swingEventBus;
|
73
|
74
|
/** Active connection. */
|
74
|
|
- private Connection activeConnection;
|
|
75
|
+ private Optional<Connection> activeConnection;
|
|
76
|
+ /** Connection manager. */
|
|
77
|
+ private final ConnectionManager connectionManager;
|
75
|
78
|
|
76
|
79
|
@Inject
|
77
|
80
|
public InviteLabel(
|
|
@@ -83,25 +86,28 @@ public class InviteLabel extends StatusbarPopupPanel<JLabel>
|
83
|
86
|
super(new JLabel());
|
84
|
87
|
|
85
|
88
|
this.parentWindow = mainFrame;
|
|
89
|
+ this.connectionManager = connectionManager;
|
|
90
|
+ this.eventBus = eventBus;
|
|
91
|
+ this.swingEventBus = swingEventBus;
|
|
92
|
+ this.activeConnection = Optional.empty();
|
86
|
93
|
|
87
|
94
|
setBorder(BorderFactory.createEtchedBorder());
|
88
|
95
|
label.setIcon(iconManager.getIcon("invite"));
|
89
|
96
|
|
90
|
97
|
menu = new JPopupMenu();
|
91
|
98
|
dismiss = new JMenuItem("Dismiss all invites");
|
92
|
|
- dismiss.setActionCommand("dismissAll");
|
93
|
|
- dismiss.addActionListener(this);
|
|
99
|
+ dismiss.addActionListener(e -> activeConnection.ifPresent(Connection::removeInvites));
|
94
|
100
|
accept = new JMenuItem("Accept all invites");
|
95
|
|
- accept.setActionCommand("acceptAll");
|
96
|
|
- accept.addActionListener(this);
|
97
|
|
-
|
98
|
|
- for (final Connection connection : connectionManager.getConnections()) {
|
99
|
|
- connection.addInviteListener(this);
|
100
|
|
- }
|
|
101
|
+ accept.addActionListener(e -> activeConnection.ifPresent(Connection::acceptInvites));
|
|
102
|
+ }
|
101
|
103
|
|
|
104
|
+ /**
|
|
105
|
+ * Initialises the invite label, adding appropriate listeners.
|
|
106
|
+ */
|
|
107
|
+ public void init() {
|
|
108
|
+ connectionManager.getConnections().forEach(c-> c.addInviteListener(this));
|
102
|
109
|
swingEventBus.subscribe(this);
|
103
|
110
|
eventBus.subscribe(this);
|
104
|
|
-
|
105
|
111
|
update();
|
106
|
112
|
}
|
107
|
113
|
|
|
@@ -116,9 +122,11 @@ public class InviteLabel extends StatusbarPopupPanel<JLabel>
|
116
|
122
|
private void popuplateMenu() {
|
117
|
123
|
menu.removeAll();
|
118
|
124
|
|
119
|
|
- final Collection<Invite> invites = activeConnection.getInvites();
|
120
|
|
- for (final Invite invite : invites) {
|
121
|
|
- menu.add(new JMenuItem(new InviteAction(invite)));
|
|
125
|
+ if (activeConnection.isPresent()) {
|
|
126
|
+ final Collection<Invite> invites = activeConnection.get().getInvites();
|
|
127
|
+ for (final Invite invite : invites) {
|
|
128
|
+ menu.add(new JMenuItem(new InviteAction(invite)));
|
|
129
|
+ }
|
122
|
130
|
}
|
123
|
131
|
menu.add(new JSeparator());
|
124
|
132
|
menu.add(accept);
|
|
@@ -130,7 +138,7 @@ public class InviteLabel extends StatusbarPopupPanel<JLabel>
|
130
|
138
|
*/
|
131
|
139
|
private void update() {
|
132
|
140
|
UIUtilities.invokeLater(() -> {
|
133
|
|
- if (activeConnection == null || activeConnection.getInvites().isEmpty()) {
|
|
141
|
+ if (!activeConnection.isPresent() || activeConnection.get().getInvites().isEmpty()) {
|
134
|
142
|
setVisible(false);
|
135
|
143
|
closeDialog();
|
136
|
144
|
} else {
|
|
@@ -178,26 +186,13 @@ public class InviteLabel extends StatusbarPopupPanel<JLabel>
|
178
|
186
|
}
|
179
|
187
|
}
|
180
|
188
|
|
181
|
|
- @Override
|
182
|
|
- public void actionPerformed(final ActionEvent e) {
|
183
|
|
- switch (e.getActionCommand()) {
|
184
|
|
- case "acceptAll":
|
185
|
|
- activeConnection.acceptInvites();
|
186
|
|
- break;
|
187
|
|
- case "dismissAll":
|
188
|
|
- activeConnection.removeInvites();
|
189
|
|
- break;
|
190
|
|
- }
|
191
|
|
- }
|
192
|
|
-
|
193
|
189
|
@Handler(invocation = EdtHandlerInvocation.class)
|
194
|
190
|
public void selectionChanged(final SwingWindowSelectedEvent event) {
|
195
|
191
|
if (event.getWindow().isPresent()) {
|
196
|
|
- activeConnection = event.getWindow().get().getContainer().getConnection();
|
|
192
|
+ activeConnection = event.getWindow().get().getContainer().getOptionalConnection();
|
197
|
193
|
} else {
|
198
|
194
|
activeConnection = null;
|
199
|
195
|
}
|
200
|
196
|
update();
|
201
|
197
|
}
|
202
|
|
-
|
203
|
198
|
}
|