Browse Source

DI window status plugin

Change-Id: I9c0d61b49aa86bbaa2dd592e98366f3800cc3c6d
Reviewed-on: http://gerrit.dmdirc.com/3100
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8
Greg Holmes 10 years ago
parent
commit
0ebc7634ab

+ 252
- 0
src/com/dmdirc/addons/windowstatus/WindowStatusManager.java View File

@@ -0,0 +1,252 @@
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.windowstatus;
23
+
24
+import com.dmdirc.Channel;
25
+import com.dmdirc.FrameContainer;
26
+import com.dmdirc.Query;
27
+import com.dmdirc.addons.ui_swing.MainFrame;
28
+import com.dmdirc.addons.ui_swing.SelectionListener;
29
+import com.dmdirc.addons.ui_swing.UIUtilities;
30
+import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
31
+import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
32
+import com.dmdirc.addons.windowstatus.WindowStatusModule.WindowStatusDomain;
33
+import com.dmdirc.config.prefs.PluginPreferencesCategory;
34
+import com.dmdirc.config.prefs.PreferencesCategory;
35
+import com.dmdirc.config.prefs.PreferencesDialogModel;
36
+import com.dmdirc.config.prefs.PreferencesSetting;
37
+import com.dmdirc.config.prefs.PreferencesType;
38
+import com.dmdirc.interfaces.Connection;
39
+import com.dmdirc.interfaces.config.ConfigChangeListener;
40
+import com.dmdirc.interfaces.config.IdentityController;
41
+import com.dmdirc.parser.interfaces.ChannelClientInfo;
42
+import com.dmdirc.parser.interfaces.ChannelInfo;
43
+import com.dmdirc.parser.interfaces.ClientInfo;
44
+import com.dmdirc.plugins.PluginInfo;
45
+
46
+import java.util.HashMap;
47
+import java.util.Map;
48
+import java.util.concurrent.Callable;
49
+
50
+import javax.inject.Inject;
51
+
52
+/**
53
+ * Displays information related to the current window in the status bar.
54
+ */
55
+public class WindowStatusManager implements ConfigChangeListener, SelectionListener {
56
+
57
+    /** Main frame. */
58
+    private final MainFrame mainFrame;
59
+    /** Status bar we're adding to. */
60
+    private final SwingStatusBar statusBar;
61
+    /** The panel we use in the status bar. */
62
+    private final WindowStatusPanel panel;
63
+    /** Identity controller to read settings from. */
64
+    private final IdentityController identityController;
65
+    /** Plugin settings domain. */
66
+    private final String domain;
67
+    /** Should we show the real name in queries? */
68
+    private boolean showname;
69
+    /** Should we show users without modes? */
70
+    private boolean shownone;
71
+    /** Prefix for users without modes. */
72
+    private String nonePrefix;
73
+
74
+    @Inject
75
+    public WindowStatusManager(final MainFrame mainFrame, final SwingStatusBar statusBar,
76
+            final IdentityController identityController,
77
+            @WindowStatusDomain final String domain) {
78
+        this.domain = domain;
79
+        this.mainFrame = mainFrame;
80
+        this.statusBar = statusBar;
81
+        this.identityController = identityController;
82
+
83
+        panel = UIUtilities.invokeAndWait(new Callable<WindowStatusPanel>() {
84
+
85
+            /** {@inheritDoc} */
86
+            @Override
87
+            public WindowStatusPanel call() {
88
+                return new WindowStatusPanel();
89
+            }
90
+        });
91
+    }
92
+
93
+    /**
94
+     * Loads the plugin.
95
+     */
96
+    public void onLoad() {
97
+        statusBar.addComponent(panel);
98
+        mainFrame.addSelectionListener(this);
99
+        identityController.getGlobalConfiguration().addChangeListener(domain, this);
100
+        updateCache();
101
+    }
102
+
103
+    /**
104
+     * Unloads the plugin.
105
+     */
106
+    public void onUnload() {
107
+        mainFrame.removeSelectionListener(this);
108
+        statusBar.removeComponent(panel);
109
+    }
110
+
111
+    /** {@inheritDoc} */
112
+    @Override
113
+    public void selectionChanged(final TextFrame window) {
114
+        if (window != null) {
115
+            updateStatus(window.getContainer());
116
+        }
117
+    }
118
+
119
+    /** Updates the cached config settings. */
120
+    private void updateCache() {
121
+        showname = identityController.getGlobalConfiguration()
122
+                .getOptionBool(domain, "client.showname");
123
+        shownone = identityController.getGlobalConfiguration()
124
+                .getOptionBool(domain, "channel.shownone");
125
+        nonePrefix = identityController.getGlobalConfiguration()
126
+                .getOption(domain, "channel.noneprefix");
127
+        updateStatus();
128
+    }
129
+
130
+    /** Update the window status using the current active window. */
131
+    public void updateStatus() {
132
+        final TextFrame active = mainFrame.getActiveFrame();
133
+
134
+        if (active != null) {
135
+            updateStatus(active.getContainer());
136
+        }
137
+    }
138
+
139
+    /**
140
+     * Update the window status using a given FrameContainer as the active frame.
141
+     *
142
+     * @param current Window to use when adding status.
143
+     */
144
+    public void updateStatus(final FrameContainer current) {
145
+        if (current == null) {
146
+            return;
147
+        }
148
+        final StringBuffer textString = new StringBuffer();
149
+
150
+        if (current instanceof Connection) {
151
+            textString.append(((Connection) current).getAddress());
152
+        } else if (current instanceof Channel) {
153
+            final ChannelInfo chan = ((Channel) current).getChannelInfo();
154
+            final Map<Integer, String> names = new HashMap<>();
155
+            final Map<Integer, Integer> types = new HashMap<>();
156
+
157
+            textString.append(chan.getName());
158
+            textString.append(" - Nicks: ");
159
+            textString.append(chan.getChannelClientCount());
160
+            textString.append(" (");
161
+
162
+            for (ChannelClientInfo client : chan.getChannelClients()) {
163
+                String mode = client.getImportantModePrefix();
164
+                final Integer im = client.getClient().getParser()
165
+                        .getChannelUserModes().indexOf(mode);
166
+
167
+                if (!names.containsKey(im)) {
168
+                    if (mode.isEmpty()) {
169
+                        if (shownone) {
170
+                            mode = nonePrefix;
171
+                        } else {
172
+                            continue;
173
+                        }
174
+                    }
175
+                    names.put(im, mode);
176
+                }
177
+
178
+                Integer count = types.get(im);
179
+
180
+                if (count == null) {
181
+                    count = Integer.valueOf(1);
182
+                } else {
183
+                    count++;
184
+                }
185
+                types.put(im, count);
186
+            }
187
+
188
+            boolean isFirst = true;
189
+
190
+            for (Map.Entry<Integer, Integer> entry : types.entrySet()) {
191
+                if (isFirst) {
192
+                    isFirst = false;
193
+                } else {
194
+                    textString.append(' ');
195
+                }
196
+                textString.append(names.get(entry.getKey()));
197
+                textString.append(entry.getValue());
198
+            }
199
+
200
+            textString.append(')');
201
+        } else if (current instanceof Query) {
202
+            final Query frame = (Query) current;
203
+
204
+            textString.append(frame.getHost());
205
+            if (showname && frame.getConnection().getParser() != null) {
206
+                final ClientInfo client = frame.getConnection().getParser()
207
+                        .getClient(frame.getHost());
208
+                final String realname = client.getRealname();
209
+                if (realname != null && !realname.isEmpty()) {
210
+                    textString.append(" - ");
211
+                    textString.append(client.getRealname());
212
+                }
213
+            }
214
+        } else {
215
+            textString.append("???");
216
+        }
217
+        panel.setText(textString.toString());
218
+    }
219
+
220
+    /**
221
+     * Shows the configuration page.
222
+     *
223
+     * @param manager Prefs dialog manager to add settings to
224
+     * @param pluginInfo Plugin info
225
+     */
226
+    public void showConfig(final PreferencesDialogModel manager,
227
+            final PluginInfo pluginInfo) {
228
+        final PreferencesCategory category = new PluginPreferencesCategory(
229
+                pluginInfo, "Window status", "");
230
+
231
+        category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
232
+                domain, "channel.shownone", "Show 'none' count",
233
+                "Should the count for users with no state be shown?",
234
+                manager.getConfigManager(), manager.getIdentity()));
235
+        category.addSetting(new PreferencesSetting(PreferencesType.TEXT,
236
+                domain, "channel.noneprefix", "'None' count prefix",
237
+                "The Prefix to use when showing the 'none' count",
238
+                manager.getConfigManager(), manager.getIdentity()));
239
+        category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
240
+                domain, "client.showname", "Show real name",
241
+                "Should the realname for clients be shown if known?",
242
+                manager.getConfigManager(), manager.getIdentity()));
243
+
244
+        manager.getCategory("Plugins").addSubCategory(category);
245
+    }
246
+
247
+    /** {@inheritDoc} */
248
+    @Override
249
+    public void configChanged(final String domain, final String key) {
250
+        updateCache();
251
+    }
252
+}

+ 59
- 0
src/com/dmdirc/addons/windowstatus/WindowStatusModule.java View File

@@ -0,0 +1,59 @@
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.windowstatus;
23
+
24
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
25
+import com.dmdirc.plugins.PluginInfo;
26
+
27
+import javax.inject.Qualifier;
28
+
29
+import dagger.Module;
30
+import dagger.Provides;
31
+
32
+/**
33
+ * DI module for this plugin.
34
+ */
35
+@Module(injects = {WindowStatusManager.class}, addsTo = SwingModule.class)
36
+public class WindowStatusModule {
37
+
38
+    @Qualifier
39
+    public @interface WindowStatusDomain {};
40
+
41
+    /** The plugin's plugin info. */
42
+    private final PluginInfo pluginInfo;
43
+
44
+    public WindowStatusModule(final PluginInfo pluginInfo) {
45
+        this.pluginInfo = pluginInfo;
46
+    }
47
+
48
+    /**
49
+     * Provides the domain that the settings should be stored under.
50
+     *
51
+     * @return The settings domain for the plugin.
52
+     */
53
+    @Provides
54
+    @WindowStatusDomain
55
+    public String getSettingsDomain() {
56
+        return pluginInfo.getDomain();
57
+    }
58
+
59
+}

+ 17
- 201
src/com/dmdirc/addons/windowstatus/WindowStatusPlugin.java View File

@@ -22,229 +22,45 @@
22 22
 
23 23
 package com.dmdirc.addons.windowstatus;
24 24
 
25
-import com.dmdirc.Channel;
26
-import com.dmdirc.FrameContainer;
27
-import com.dmdirc.Query;
28
-import com.dmdirc.addons.ui_swing.SelectionListener;
29
-import com.dmdirc.addons.ui_swing.SwingController;
30
-import com.dmdirc.addons.ui_swing.UIUtilities;
31
-import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
32
-import com.dmdirc.config.prefs.PluginPreferencesCategory;
33
-import com.dmdirc.config.prefs.PreferencesCategory;
34 25
 import com.dmdirc.config.prefs.PreferencesDialogModel;
35
-import com.dmdirc.config.prefs.PreferencesSetting;
36
-import com.dmdirc.config.prefs.PreferencesType;
37
-import com.dmdirc.interfaces.Connection;
38
-import com.dmdirc.interfaces.config.ConfigChangeListener;
39
-import com.dmdirc.interfaces.config.IdentityController;
40
-import com.dmdirc.parser.interfaces.ChannelClientInfo;
41
-import com.dmdirc.parser.interfaces.ChannelInfo;
42
-import com.dmdirc.parser.interfaces.ClientInfo;
43 26
 import com.dmdirc.plugins.PluginInfo;
44 27
 import com.dmdirc.plugins.implementations.BasePlugin;
45 28
 
46
-import java.util.HashMap;
47
-import java.util.Map;
48
-import java.util.Map.Entry;
49
-import java.util.concurrent.Callable;
29
+import dagger.ObjectGraph;
50 30
 
51 31
 /**
52 32
  * Displays information related to the current window in the status bar.
53 33
  */
54
-public final class WindowStatusPlugin extends BasePlugin
55
-        implements ConfigChangeListener, SelectionListener {
34
+public class WindowStatusPlugin extends BasePlugin {
56 35
 
57
-    /** The panel we use in the status bar. */
58
-    private final WindowStatusPanel panel;
59
-    /** Parent Swing UI. */
60
-    private final SwingController controller;
61
-    /** Identity controller to read settings from. */
62
-    private final IdentityController identityController;
63
-    /** Should we show the real name in queries? */
64
-    private boolean showname;
65
-    /** Should we show users without modes? */
66
-    private boolean shownone;
67
-    /** Prefix for users without modes. */
68
-    private String nonePrefix;
69
-    /** This plugin's plugin info. */
70
-    private final PluginInfo pluginInfo;
36
+    /** Nick colour manager. */
37
+    private WindowStatusManager windowStatusManager;
38
+    /** Plugin info. */
39
+    private PluginInfo pluginInfo;
71 40
 
72
-    /**
73
-     * Creates a new instance of WindowStatusPlugin.
74
-     *
75
-     * @param pluginInfo This plugin's plugin info.
76
-     * @param controller The parent {@link SwingController}.
77
-     * @param identityController The controller to read settings from.
78
-     */
79
-    public WindowStatusPlugin(
80
-            final PluginInfo pluginInfo,
81
-            final SwingController controller,
82
-            final IdentityController identityController) {
83
-        super();
41
+    @Override
42
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
43
+        super.load(pluginInfo, graph);
84 44
         this.pluginInfo = pluginInfo;
85
-        this.controller = controller;
86
-        this.identityController = identityController;
87
-
88
-        panel = UIUtilities.invokeAndWait(
89
-                new Callable<WindowStatusPanel>() {
90
-
91
-            /** {@inheritDoc} */
92
-            @Override
93
-            public WindowStatusPanel call() {
94
-                return new WindowStatusPanel();
95
-            }
96
-        });
45
+        setObjectGraph(graph.plus(new WindowStatusModule(pluginInfo)));
46
+        windowStatusManager = getObjectGraph().get(WindowStatusManager.class);
97 47
     }
98 48
 
99
-    /** {@inheritDoc} */
100 49
     @Override
101
-    public void onLoad() {
102
-        controller.getSwingStatusBar().addComponent(panel);
103
-        controller.getMainFrame().addSelectionListener(this);
104
-        identityController.getGlobalConfiguration().addChangeListener(getDomain(), this);
105
-        updateCache();
50
+        public void onLoad() {
51
+        super.onLoad();
52
+        windowStatusManager.onLoad();
106 53
     }
107 54
 
108
-    /** {@inheritDoc} */
109 55
     @Override
110 56
     public void onUnload() {
111
-        controller.getMainFrame().removeSelectionListener(this);
112
-        controller.getSwingStatusBar().removeComponent(panel);
57
+        super.onUnload();
58
+        windowStatusManager.onUnload();
113 59
     }
114 60
 
115
-    /** {@inheritDoc} */
116
-    @Override
117
-    public void selectionChanged(final TextFrame window) {
118
-        updateStatus(window == null ? null : window.getContainer());
119
-    }
120
-
121
-    /** Updates the cached config settings. */
122
-    private void updateCache() {
123
-        showname = identityController.getGlobalConfiguration()
124
-                .getOptionBool(getDomain(), "client.showname");
125
-        shownone = identityController.getGlobalConfiguration()
126
-                .getOptionBool(getDomain(), "channel.shownone");
127
-        nonePrefix = identityController.getGlobalConfiguration()
128
-                .getOption(getDomain(), "channel.noneprefix");
129
-        updateStatus();
130
-    }
131
-
132
-    /** Update the window status using the current active window. */
133
-    public void updateStatus() {
134
-        final TextFrame active = controller.getMainFrame().getActiveFrame();
135
-
136
-        if (active != null) {
137
-            updateStatus(active.getContainer());
138
-        }
139
-    }
140
-
141
-    /**
142
-     * Update the window status using a given FrameContainer as the
143
-     * active frame.
144
-     *
145
-     * @param current Window to use when adding status.
146
-     */
147
-    public void updateStatus(final FrameContainer current) {
148
-        if (current == null) {
149
-            return;
150
-        }
151
-        final StringBuffer textString = new StringBuffer();
152
-
153
-        if (current instanceof Connection) {
154
-            textString.append(((Connection) current).getAddress());
155
-        } else if (current instanceof Channel) {
156
-            final ChannelInfo chan = ((Channel) current).getChannelInfo();
157
-            final Map<Integer, String> names = new HashMap<>();
158
-            final Map<Integer, Integer> types = new HashMap<>();
159
-
160
-            textString.append(chan.getName());
161
-            textString.append(" - Nicks: ");
162
-            textString.append(chan.getChannelClientCount());
163
-            textString.append(" (");
164
-
165
-            for (ChannelClientInfo client : chan.getChannelClients()) {
166
-                String mode = client.getImportantModePrefix();
167
-                final Integer im = client.getClient().getParser()
168
-                        .getChannelUserModes().indexOf(mode);
169
-
170
-                if (!names.containsKey(im)) {
171
-                    if (mode.isEmpty()) {
172
-                        if (shownone) {
173
-                            mode = nonePrefix;
174
-                        } else {
175
-                            continue;
176
-                        }
177
-                    }
178
-                    names.put(im, mode);
179
-                }
180
-
181
-                Integer count = types.get(im);
182
-
183
-                if (count == null) {
184
-                    count = Integer.valueOf(1);
185
-                } else {
186
-                    count++;
187
-                }
188
-                types.put(im, count);
189
-            }
190
-
191
-            boolean isFirst = true;
192
-
193
-            for (Entry<Integer, Integer> entry : types.entrySet()) {
194
-                if (isFirst) {
195
-                    isFirst = false;
196
-                } else {
197
-                    textString.append(' ');
198
-                }
199
-                textString.append(names.get(entry.getKey()));
200
-                textString.append(entry.getValue());
201
-            }
202
-
203
-            textString.append(')');
204
-        } else if (current instanceof Query) {
205
-            final Query frame = (Query) current;
206
-
207
-            textString.append(frame.getHost());
208
-            if (showname && frame.getConnection().getParser() != null) {
209
-                final ClientInfo client = frame.getConnection().getParser()
210
-                        .getClient(frame.getHost());
211
-                final String realname = client.getRealname();
212
-                if (realname != null && !realname.isEmpty()) {
213
-                    textString.append(" - ");
214
-                    textString.append(client.getRealname());
215
-                }
216
-            }
217
-        } else {
218
-            textString.append("???");
219
-        }
220
-        panel.setText(textString.toString());
221
-    }
222
-
223
-    /** {@inheritDoc} */
224 61
     @Override
225 62
     public void showConfig(final PreferencesDialogModel manager) {
226
-        final PreferencesCategory category = new PluginPreferencesCategory(
227
-                pluginInfo, "Window status", "");
228
-
229
-        category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
230
-                getDomain(), "channel.shownone", "Show 'none' count",
231
-                "Should the count for users with no state be shown?",
232
-                manager.getConfigManager(), manager.getIdentity()));
233
-        category.addSetting(new PreferencesSetting(PreferencesType.TEXT,
234
-                getDomain(), "channel.noneprefix", "'None' count prefix",
235
-                "The Prefix to use when showing the 'none' count",
236
-                manager.getConfigManager(), manager.getIdentity()));
237
-        category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
238
-                getDomain(), "client.showname", "Show real name",
239
-                "Should the realname for clients be shown if known?",
240
-                manager.getConfigManager(), manager.getIdentity()));
241
-
242
-        manager.getCategory("Plugins").addSubCategory(category);
63
+        windowStatusManager.showConfig(manager, pluginInfo);
243 64
     }
244 65
 
245
-    /** {@inheritDoc} */
246
-    @Override
247
-    public void configChanged(final String domain, final String key) {
248
-        updateCache();
249
-    }
250 66
 }

Loading…
Cancel
Save