Просмотр исходного кода

Tidy WindowStatusManager.

pull/223/head
Greg Holmes 9 лет назад
Родитель
Сommit
9892d51a90
1 измененных файлов: 53 добавлений и 57 удалений
  1. 53
    57
      windowstatus/src/com/dmdirc/addons/windowstatus/WindowStatusManager.java

+ 53
- 57
windowstatus/src/com/dmdirc/addons/windowstatus/WindowStatusManager.java Просмотреть файл

@@ -22,26 +22,25 @@
22 22
 
23 23
 package com.dmdirc.addons.windowstatus;
24 24
 
25
-import com.dmdirc.Channel;
25
+import com.dmdirc.ClientModule.GlobalConfig;
26 26
 import com.dmdirc.DMDircMBassador;
27 27
 import com.dmdirc.FrameContainer;
28
-import com.dmdirc.Query;
28
+import com.dmdirc.addons.ui_swing.EDTInvocation;
29 29
 import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
30 30
 import com.dmdirc.addons.ui_swing.UIUtilities;
31 31
 import com.dmdirc.addons.ui_swing.events.SwingEventBus;
32 32
 import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
33 33
 import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
34
+import com.dmdirc.config.ConfigBinder;
35
+import com.dmdirc.config.ConfigBinding;
34 36
 import com.dmdirc.events.StatusBarComponentAddedEvent;
35 37
 import com.dmdirc.events.StatusBarComponentRemovedEvent;
36 38
 import com.dmdirc.interfaces.Connection;
37
-import com.dmdirc.interfaces.config.ConfigChangeListener;
38
-import com.dmdirc.interfaces.config.IdentityController;
39
-import com.dmdirc.parser.interfaces.ChannelClientInfo;
40
-import com.dmdirc.parser.interfaces.ChannelInfo;
39
+import com.dmdirc.interfaces.GroupChat;
40
+import com.dmdirc.interfaces.PrivateChat;
41
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
41 42
 import com.dmdirc.plugins.PluginDomain;
42 43
 
43
-import java.util.Optional;
44
-
45 44
 import javax.inject.Inject;
46 45
 
47 46
 import net.engio.mbassy.listener.Handler;
@@ -49,14 +48,12 @@ import net.engio.mbassy.listener.Handler;
49 48
 /**
50 49
  * Displays information related to the current window in the status bar.
51 50
  */
52
-public class WindowStatusManager implements ConfigChangeListener {
51
+public class WindowStatusManager {
53 52
 
54 53
     /** Active frame manager. */
55 54
     private final ActiveFrameManager activeFrameManager;
56
-    /** Identity controller to read settings from. */
57
-    private final IdentityController identityController;
58
-    /** Plugin settings domain. */
59
-    private final String domain;
55
+    /** Config to read settings from. */
56
+    private final ConfigBinder configBinder;
60 57
     /** The event bus to post events to. */
61 58
     private final DMDircMBassador eventBus;
62 59
     /** The swing event bus to register for events on. */
@@ -72,13 +69,12 @@ public class WindowStatusManager implements ConfigChangeListener {
72 69
 
73 70
     @Inject
74 71
     public WindowStatusManager(final ActiveFrameManager activeFrameManager,
75
-            final IdentityController identityController,
72
+            @GlobalConfig final AggregateConfigProvider config,
76 73
             @PluginDomain(WindowStatusPlugin.class) final String domain,
77 74
             final DMDircMBassador eventBus,
78 75
             final SwingEventBus swingEventBus) {
79
-        this.domain = domain;
80 76
         this.activeFrameManager = activeFrameManager;
81
-        this.identityController = identityController;
77
+        this.configBinder = config.getBinder().withDefaultDomain(domain);
82 78
         this.eventBus = eventBus;
83 79
         this.swingEventBus = swingEventBus;
84 80
     }
@@ -90,8 +86,8 @@ public class WindowStatusManager implements ConfigChangeListener {
90 86
         panel = UIUtilities.invokeAndWait(WindowStatusPanel::new);
91 87
         eventBus.publishAsync(new StatusBarComponentAddedEvent(panel));
92 88
         swingEventBus.subscribe(this);
93
-        identityController.getGlobalConfiguration().addChangeListener(domain, this);
94
-        updateCache();
89
+        configBinder.bind(this, WindowStatusManager.class);
90
+        UIUtilities.invokeLater(this::updateStatus);
95 91
     }
96 92
 
97 93
     /**
@@ -100,25 +96,13 @@ public class WindowStatusManager implements ConfigChangeListener {
100 96
     public void onUnload() {
101 97
         swingEventBus.unsubscribe(this);
102 98
         eventBus.publishAsync(new StatusBarComponentRemovedEvent(panel));
99
+        configBinder.unbind(this);
103 100
         panel = null;
104 101
     }
105 102
 
106 103
     @Handler(invocation = EdtHandlerInvocation.class)
107 104
     public void selectionChanged(final SwingWindowSelectedEvent event) {
108
-        if (event.getWindow().isPresent()) {
109
-            updateStatus(event.getWindow().get().getContainer());
110
-        }
111
-    }
112
-
113
-    /** Updates the cached config settings. */
114
-    private void updateCache() {
115
-        showname = identityController.getGlobalConfiguration()
116
-                .getOptionBool(domain, "client.showname");
117
-        shownone = identityController.getGlobalConfiguration()
118
-                .getOptionBool(domain, "channel.shownone");
119
-        nonePrefix = identityController.getGlobalConfiguration()
120
-                .getOption(domain, "channel.noneprefix");
121
-        updateStatus();
105
+        event.getWindow().ifPresent(w -> updateStatus(event.getWindow().get().getContainer()));
122 106
     }
123 107
 
124 108
     /** Update the window status using the current active window. */
@@ -135,42 +119,43 @@ public class WindowStatusManager implements ConfigChangeListener {
135 119
         if (current == null) {
136 120
             return;
137 121
         }
122
+        if (panel == null) {
123
+            return;
124
+        }
138 125
         final String textString;
139 126
 
140 127
         if (current instanceof Connection) {
141 128
             textString = updateStatusConnection((Connection) current);
142
-        } else if (current instanceof Channel) {
143
-            textString = updateStatusChannel((Channel) current);
144
-        } else if (current instanceof Query) {
145
-            textString = updateStatusQuery((Query) current);
129
+        } else if (current instanceof GroupChat) {
130
+            textString = updateStatusChannel((GroupChat) current);
131
+        } else if (current instanceof PrivateChat) {
132
+            textString = updateStatusQuery((PrivateChat) current);
146 133
         } else {
147 134
             textString = "???";
148 135
         }
149
-        if (panel != null) {
150
-            panel.setText(textString);
151
-        }
136
+        panel.setText(textString);
152 137
     }
153 138
 
154 139
     private String updateStatusConnection(final Connection connection) {
155 140
         return connection.getAddress();
156 141
     }
157 142
 
158
-    private String updateStatusChannel(final Channel frame) {
143
+    private String updateStatusChannel(final GroupChat frame) {
159 144
         final StringBuilder textString = new StringBuilder();
160
-        final ChannelInfo chan = frame.getChannelInfo();
161 145
 
162
-        textString.append(chan.getName());
146
+        textString.append(frame.getName());
163 147
         textString.append(" - Nicks: ");
164
-        textString.append(chan.getChannelClientCount());
148
+        textString.append(frame.getUsers().size());
165 149
         textString.append(" (");
166 150
 
167
-        final String channelUserModes = ' ' + chan.getParser().getChannelUserModes();
151
+        final String channelUserModes = ' ' + frame.getConnection()
152
+                .map(Connection::getUserModes).orElse("");
168 153
         final int[] usersWithMode = new int[channelUserModes.length()];
169
-        for (ChannelClientInfo client : chan.getChannelClients()) {
170
-            final String mode = client.getImportantModePrefix();
154
+        frame.getUsers().forEach(user -> {
155
+            final String mode = user.getImportantMode();
171 156
             final int index = channelUserModes.indexOf(mode);
172 157
             usersWithMode[index]++;
173
-        }
158
+        });
174 159
 
175 160
         boolean isFirst = true;
176 161
         for (int i = channelUserModes.length() - 1; i >= 0; i--) {
@@ -190,21 +175,32 @@ public class WindowStatusManager implements ConfigChangeListener {
190 175
         return textString.toString();
191 176
     }
192 177
 
193
-    private String updateStatusQuery(final Query frame) {
178
+    private String updateStatusQuery(final PrivateChat frame) {
194 179
         final StringBuilder textString = new StringBuilder();
195 180
         textString.append(frame.getHost());
196
-        final Optional<Connection> connection = frame.getConnection();
197
-        if (showname && connection.isPresent()) {
198
-            frame.getUser().getRealname().ifPresent(s -> {
199
-                textString.append(" - ").append(s);
200
-            });
201
-        }
181
+        frame.getConnection().ifPresent(c -> {
182
+            if (showname) {
183
+                frame.getUser().getRealname().ifPresent(s -> textString.append(" - ").append(s));
184
+            }
185
+        });
202 186
         return textString.toString();
203 187
     }
204 188
 
205
-    @Override
206
-    public void configChanged(final String domain, final String key) {
207
-        updateCache();
189
+    @ConfigBinding(key = "client.showname", invocation = EDTInvocation.class)
190
+    public void handleShowName(final String value) {
191
+        showname = Boolean.valueOf(value);
192
+        updateStatus();
193
+    }
194
+
195
+    @ConfigBinding(key = "client.shownone", invocation = EDTInvocation.class)
196
+    public void handleShowNone(final String value) {
197
+        shownone = Boolean.valueOf(value);
198
+        updateStatus();
208 199
     }
209 200
 
201
+    @ConfigBinding(key = "client.noneprefix", invocation = EDTInvocation.class)
202
+    public void handleShowPrefix(final String value) {
203
+        nonePrefix = value;
204
+        updateStatus();
205
+    }
210 206
 }

Загрузка…
Отмена
Сохранить