Browse Source

Tidy up invitelabel.

pull/140/head
Greg Holmes 9 years ago
parent
commit
960a0f702f

+ 28
- 33
ui_swing/src/com/dmdirc/addons/ui_swing/components/statusbar/InviteLabel.java View File

@@ -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
 }

+ 14
- 11
ui_swing/src/com/dmdirc/addons/ui_swing/components/statusbar/InvitePopup.java View File

@@ -27,9 +27,11 @@ import com.dmdirc.interfaces.Connection;
27 27
 import com.dmdirc.util.DateUtils;
28 28
 
29 29
 import java.awt.Window;
30
+import java.util.Optional;
30 31
 
31 32
 import javax.swing.JLabel;
32 33
 import javax.swing.JPanel;
34
+import javax.swing.SwingConstants;
33 35
 
34 36
 /**
35 37
  * Shows information about received invites.
@@ -41,7 +43,7 @@ public class InvitePopup extends StatusbarPopupWindow {
41 43
     /** A version number for this class. */
42 44
     private static final long serialVersionUID = 1;
43 45
     /** The connection to show invites for. */
44
-    private final Connection connection;
46
+    private final Optional<Connection> connection;
45 47
 
46 48
     /**
47 49
      * Creates a new InvitePopup for the specified panel and connection.
@@ -51,22 +53,23 @@ public class InvitePopup extends StatusbarPopupWindow {
51 53
      * @param parentWindow Parent window
52 54
      */
53 55
     public InvitePopup(final JPanel parent,
54
-            final Connection connection, final Window parentWindow) {
56
+            final Optional<Connection> connection, final Window parentWindow) {
55 57
         super(parent, parentWindow);
56 58
         this.connection = connection;
57 59
     }
58 60
 
59 61
     @Override
60 62
     protected void initContent(final JPanel panel) {
61
-        for (final Invite invite : connection.getInvites()) {
62
-            panel.add(new JLabel(invite.getChannel()), "growx, pushx");
63
-            panel.add(new JLabel(invite.getSource()[0], JLabel.CENTER),
64
-                    "growx, pushx, al center");
65
-            panel.add(new JLabel(DateUtils.formatDuration((int) (System.currentTimeMillis()
66
-                    - invite.getTimestamp())
67
-                    / 1000) + " ago", JLabel.RIGHT),
68
-                    "growx, pushx, al right, wrap");
63
+        if (connection.isPresent()) {
64
+            for (final Invite invite : connection.get().getInvites()) {
65
+                panel.add(new JLabel(invite.getChannel()), "growx, pushx");
66
+                panel.add(new JLabel(invite.getSource()[0], SwingConstants.CENTER),
67
+                        "growx, pushx, al center");
68
+                panel.add(new JLabel(DateUtils.formatDuration(
69
+                                (int) (System.currentTimeMillis() - invite.getTimestamp()) / 1000)
70
+                                + " ago", SwingConstants.RIGHT),
71
+                        "growx, pushx, al right, wrap");
72
+            }
69 73
         }
70 74
     }
71
-
72 75
 }

+ 2
- 6
ui_swing/src/com/dmdirc/addons/ui_swing/components/statusbar/SwingStatusBar.java View File

@@ -22,7 +22,6 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.components.statusbar;
24 24
 
25
-import com.dmdirc.DMDircMBassador;
26 25
 import com.dmdirc.events.StatusBarComponentAddedEvent;
27 26
 import com.dmdirc.events.StatusBarComponentRemovedEvent;
28 27
 
@@ -47,8 +46,6 @@ public class SwingStatusBar extends JPanel {
47 46
 
48 47
     /** A version number for this class. */
49 48
     private static final long serialVersionUID = 5;
50
-    /** Event bus to post events to. */
51
-    private final DMDircMBassador eventBus;
52 49
     /** Mig layout component restraints. */
53 50
     private final String componentConstraints;
54 51
     /** error panel. */
@@ -61,14 +58,12 @@ public class SwingStatusBar extends JPanel {
61 58
     /**
62 59
      * Creates a new instance of SwingStatusBar.
63 60
      *
64
-     * @param eventBus     The event bus to post events to (should be removed soon)
65 61
      * @param inviteLabel  The invite label to add to the status bar.
66 62
      * @param updaterLabel The updater label to add to the status bar.
67 63
      * @param errorLabel   The error label to add to the status bar.
68 64
      * @param messageLabel The message label to add to the status bar.
69 65
      */
70 66
     public SwingStatusBar(
71
-            final DMDircMBassador eventBus,
72 67
             final InviteLabel inviteLabel,
73 68
             final UpdaterLabel updaterLabel,
74 69
             final ErrorPanel errorLabel,
@@ -81,11 +76,12 @@ public class SwingStatusBar extends JPanel {
81 76
         componentConstraints = "sgy components, hmax " + height + ", hmin " + height
82 77
                 + ", wmin 20, shrink 0";
83 78
 
84
-        this.eventBus = eventBus;
85 79
         this.errorLabel = errorLabel;
86 80
         this.updaterLabel = updaterLabel;
87 81
         this.inviteLabel = inviteLabel;
88 82
 
83
+        inviteLabel.init();
84
+
89 85
         setLayout(new MigLayout("fill, ins 0, hidemode 3"));
90 86
 
91 87
         add(messageLabel, "grow, push, sgy components, hmax " + height

Loading…
Cancel
Save