Przeglądaj źródła

And the OSD plugin

pull/289/head
Greg Holmes 9 lat temu
rodzic
commit
b572dc6673

+ 164
- 10
osd/src/com/dmdirc/addons/osd/OsdManager.java Wyświetl plik

@@ -23,39 +23,80 @@
23 23
 package com.dmdirc.addons.osd;
24 24
 
25 25
 import com.dmdirc.addons.ui_swing.UIUtilities;
26
+import com.dmdirc.addons.ui_swing.injection.MainWindow;
27
+import com.dmdirc.config.prefs.CategoryChangeListener;
28
+import com.dmdirc.config.prefs.PluginPreferencesCategory;
29
+import com.dmdirc.config.prefs.PreferencesCategory;
30
+import com.dmdirc.config.prefs.PreferencesDialogModel;
31
+import com.dmdirc.config.prefs.PreferencesInterface;
32
+import com.dmdirc.config.prefs.PreferencesSetting;
33
+import com.dmdirc.config.prefs.PreferencesType;
34
+import com.dmdirc.config.prefs.SettingChangeListener;
35
+import com.dmdirc.events.ClientPrefsOpenedEvent;
26 36
 import com.dmdirc.interfaces.config.IdentityController;
37
+import com.dmdirc.plugins.PluginDomain;
27 38
 import com.dmdirc.plugins.PluginInfo;
28 39
 import com.dmdirc.ui.messages.ColourManager;
40
+import com.dmdirc.ui.messages.ColourManagerFactory;
41
+import com.dmdirc.util.validators.NumericalValidator;
42
+import com.dmdirc.util.validators.OptionalValidator;
29 43
 
30 44
 import java.awt.Window;
31 45
 import java.util.ArrayList;
46
+import java.util.HashMap;
32 47
 import java.util.LinkedList;
33 48
 import java.util.List;
49
+import java.util.Map;
34 50
 import java.util.Queue;
35 51
 
52
+import javax.inject.Inject;
53
+
54
+import net.engio.mbassy.listener.Handler;
55
+
36 56
 /**
37 57
  * Class to manage OSD Windows.
38 58
  */
39
-public class OsdManager {
59
+public class OsdManager implements CategoryChangeListener, PreferencesInterface,
60
+        SettingChangeListener {
40 61
 
41 62
     /** The frame the OSD will be associated with. */
42 63
     private final Window mainFrame;
43
-    /** The controller to read/write settings with. */
44
-    private final IdentityController identityController;
45
-    /** The colour manager to use to parse colours. */
46
-    private final ColourManager colourManager;
47 64
     /** List of OSD Windows. */
48 65
     private final List<OsdWindow> windowList = new ArrayList<>();
49 66
     /** List of messages to be queued. */
50 67
     private final Queue<QueuedMessage> windowQueue = new LinkedList<>();
51 68
     /** This plugin's settings domain. */
52 69
     private final String domain;
70
+    /** Config OSD Window. */
71
+    private OsdWindow osdWindow;
72
+    /** X-axis position of OSD. */
73
+    private int x;
74
+    /** Y-axis potion of OSD. */
75
+    private int y;
76
+    /** Setting objects with registered change listeners. */
77
+    private PreferencesSetting fontSizeSetting;
78
+    private PreferencesSetting backgroundSetting;
79
+    private PreferencesSetting foregroundSetting;
80
+    private PreferencesSetting widthSetting;
81
+    private PreferencesSetting timeoutSetting;
82
+    private PreferencesSetting maxWindowsSetting;
83
+    /** This plugin's plugin info. */
84
+    private final PluginInfo pluginInfo;
85
+    /** The controller to read/write settings with. */
86
+    private final IdentityController identityController;
87
+    /** The manager to use to parse colours. */
88
+    private final ColourManager colourManager;
53 89
 
54
-    public OsdManager(final Window mainFrame, final IdentityController identityController,
55
-            final ColourManager colourManager, final PluginInfo pluginInfo) {
90
+    @Inject
91
+    public OsdManager(
92
+            @MainWindow final Window mainFrame,
93
+            final IdentityController identityController,
94
+            final ColourManagerFactory colourManagerFactory,
95
+            @PluginDomain(OsdPlugin.class) final PluginInfo pluginInfo) {
56 96
         this.mainFrame = mainFrame;
57 97
         this.identityController = identityController;
58
-        this.colourManager = colourManager;
98
+        this.colourManager = colourManagerFactory.getColourManager(identityController.getGlobalConfiguration());
99
+        this.pluginInfo = pluginInfo;
59 100
         this.domain = pluginInfo.getDomain();
60 101
     }
61 102
 
@@ -97,8 +138,9 @@ public class OsdManager {
97 138
      * @param message Text to display in the OSD window.
98 139
      */
99 140
     private synchronized void displayWindow(final int timeout, final String message) {
100
-        final OsdPolicy policy = OsdPolicy.valueOf(identityController.getGlobalConfiguration()
101
-                .getOption(domain, "newbehaviour").toUpperCase());
141
+        final OsdPolicy policy = OsdPolicy.valueOf(
142
+                identityController.getGlobalConfiguration().getOption(domain, "newbehaviour")
143
+                        .toUpperCase());
102 144
         final int startY = identityController.getGlobalConfiguration()
103 145
                 .getOptionInt(domain, "locationY");
104 146
 
@@ -167,4 +209,116 @@ public class OsdManager {
167 209
         return windowList.size();
168 210
     }
169 211
 
212
+    @Handler
213
+    public void showConfig(final ClientPrefsOpenedEvent event) {
214
+        final PreferencesDialogModel manager = event.getModel();
215
+        x = identityController.getGlobalConfiguration()
216
+                .getOptionInt(domain, "locationX");
217
+        y = identityController.getGlobalConfiguration()
218
+                .getOptionInt(domain, "locationY");
219
+
220
+        final PreferencesCategory category = new PluginPreferencesCategory(
221
+                pluginInfo, "OSD",
222
+                "General configuration for OSD plugin.", "category-osd");
223
+
224
+        fontSizeSetting = new PreferencesSetting(PreferencesType.INTEGER,
225
+                domain, "fontSize", "Font size", "Changes the font " + "size of the OSD",
226
+                manager.getConfigManager(),
227
+                manager.getIdentity()).registerChangeListener(this);
228
+        backgroundSetting = new PreferencesSetting(PreferencesType.COLOUR,
229
+                domain, "bgcolour", "Background colour",
230
+                "Background colour for the OSD", manager.getConfigManager(),
231
+                manager.getIdentity()).registerChangeListener(this);
232
+        foregroundSetting = new PreferencesSetting(PreferencesType.COLOUR,
233
+                domain, "fgcolour", "Foreground colour",
234
+                "Foreground colour for the OSD", manager.getConfigManager(),
235
+                manager.getIdentity()).registerChangeListener(this);
236
+        widthSetting = new PreferencesSetting(PreferencesType.INTEGER,
237
+                domain, "width", "OSD Width", "Width of the OSD Window",
238
+                manager.getConfigManager(), manager.getIdentity())
239
+                .registerChangeListener(this);
240
+        timeoutSetting = new PreferencesSetting(PreferencesType.OPTIONALINTEGER,
241
+                new OptionalValidator(new NumericalValidator(1, Integer.MAX_VALUE)),
242
+                domain, "timeout", "Timeout", "Length of time in "
243
+                + "seconds before the OSD window closes", manager.getConfigManager(),
244
+                manager.getIdentity());
245
+        maxWindowsSetting = new PreferencesSetting(PreferencesType.OPTIONALINTEGER,
246
+                new OptionalValidator(new NumericalValidator(1, Integer.MAX_VALUE)),
247
+                domain, "maxWindows", "Maximum open windows",
248
+                "Maximum number of OSD windows that will be displayed at any given time",
249
+                manager.getConfigManager(), manager.getIdentity());
250
+
251
+        category.addSetting(fontSizeSetting);
252
+        category.addSetting(backgroundSetting);
253
+        category.addSetting(foregroundSetting);
254
+        category.addSetting(widthSetting);
255
+        category.addSetting(timeoutSetting);
256
+        category.addSetting(maxWindowsSetting);
257
+
258
+        final Map<String, String> posOptions = new HashMap<>();
259
+
260
+        //Populate policy MULTICHOICE
261
+        for (OsdPolicy policy : OsdPolicy.values()) {
262
+            posOptions.put(policy.name(), policy.getDescription());
263
+        }
264
+
265
+        category.addSetting(new PreferencesSetting(domain, "newbehaviour",
266
+                "New window policy", "What to do when an OSD Window "
267
+                + "is opened when there are other, existing windows open",
268
+                posOptions, manager.getConfigManager(), manager.getIdentity()));
269
+
270
+        category.addChangeListener(this);
271
+        manager.getCategory("Plugins").addSubCategory(category);
272
+        manager.registerSaveListener(this);
273
+    }
274
+
275
+    @Override
276
+    public void categorySelected(final PreferencesCategory category) {
277
+        osdWindow = new OsdWindow(mainFrame, identityController, this, colourManager,
278
+                -1, "Please drag this OSD to position", true, x, y, domain);
279
+        osdWindow.setBackgroundColour(backgroundSetting.getValue());
280
+        osdWindow.setForegroundColour(foregroundSetting.getValue());
281
+        osdWindow.setFontSize(Integer.parseInt(fontSizeSetting.getValue()));
282
+    }
283
+
284
+    @Override
285
+    public void categoryDeselected(final PreferencesCategory category) {
286
+        x = osdWindow.getLocationOnScreen().x;
287
+        y = osdWindow.getLocationOnScreen().y;
288
+
289
+        osdWindow.dispose();
290
+        osdWindow = null;
291
+    }
292
+
293
+    @Override
294
+    public void save() {
295
+        identityController.getUserSettings().setOption(domain, "locationX", x);
296
+        identityController.getUserSettings().setOption(domain, "locationY", y);
297
+    }
298
+
299
+    @Override
300
+    public void settingChanged(final PreferencesSetting setting) {
301
+        if (osdWindow == null) {
302
+            // They've changed categories but are somehow poking settings.
303
+            // Ignore the request.
304
+            return;
305
+        }
306
+
307
+        if (setting.equals(fontSizeSetting)) {
308
+            osdWindow.setFontSize(Integer.parseInt(setting.getValue()));
309
+        } else if (setting.equals(backgroundSetting)) {
310
+            osdWindow.setBackgroundColour(setting.getValue());
311
+        } else if (setting.equals(foregroundSetting)) {
312
+            osdWindow.setForegroundColour(setting.getValue());
313
+        } else if (setting.equals(widthSetting)) {
314
+            int width = 500;
315
+            try {
316
+                width = Integer.parseInt(setting.getValue());
317
+            } catch (NumberFormatException e) {
318
+                //Ignore
319
+            }
320
+            osdWindow.setSize(width, osdWindow.getHeight());
321
+        }
322
+    }
323
+
170 324
 }

+ 49
- 0
osd/src/com/dmdirc/addons/osd/OsdModule.java Wyświetl plik

@@ -0,0 +1,49 @@
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
+
23
+package com.dmdirc.addons.osd;
24
+
25
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
26
+import com.dmdirc.plugins.PluginDomain;
27
+import com.dmdirc.plugins.PluginInfo;
28
+
29
+import dagger.Module;
30
+import dagger.Provides;
31
+
32
+/**
33
+ * Dependency injection module for the OSD plugin.
34
+ */
35
+@Module(injects = {OsdManager.class, OsdCommand.class}, addsTo = SwingModule.class)
36
+public class OsdModule {
37
+
38
+    private final PluginInfo pluginInfo;
39
+
40
+    public OsdModule(final PluginInfo pluginInfo) {
41
+        this.pluginInfo = pluginInfo;
42
+    }
43
+
44
+    @Provides
45
+    @PluginDomain(OsdPlugin.class)
46
+    public PluginInfo getPluginInfo() {
47
+        return pluginInfo;
48
+    }
49
+}

+ 9
- 173
osd/src/com/dmdirc/addons/osd/OsdPlugin.java Wyświetl plik

@@ -22,193 +22,29 @@
22 22
 
23 23
 package com.dmdirc.addons.osd;
24 24
 
25
-import com.dmdirc.addons.ui_swing.SwingController;
26
-import com.dmdirc.config.prefs.CategoryChangeListener;
27
-import com.dmdirc.config.prefs.PluginPreferencesCategory;
28
-import com.dmdirc.config.prefs.PreferencesCategory;
29
-import com.dmdirc.config.prefs.PreferencesDialogModel;
30
-import com.dmdirc.config.prefs.PreferencesInterface;
31
-import com.dmdirc.config.prefs.PreferencesSetting;
32
-import com.dmdirc.config.prefs.PreferencesType;
33
-import com.dmdirc.config.prefs.SettingChangeListener;
34
-import com.dmdirc.interfaces.CommandController;
35
-import com.dmdirc.interfaces.config.IdentityController;
36 25
 import com.dmdirc.plugins.Exported;
37 26
 import com.dmdirc.plugins.PluginInfo;
38 27
 import com.dmdirc.plugins.implementations.BaseCommandPlugin;
39
-import com.dmdirc.ui.messages.ColourManager;
40
-import com.dmdirc.util.validators.NumericalValidator;
41
-import com.dmdirc.util.validators.OptionalValidator;
42 28
 
43
-import java.awt.Frame;
44
-import java.util.HashMap;
45
-import java.util.Map;
29
+import dagger.ObjectGraph;
46 30
 
47 31
 /**
48 32
  * Allows the user to display on-screen-display messages.
49 33
  */
50
-public class OsdPlugin extends BaseCommandPlugin implements
51
-        CategoryChangeListener, PreferencesInterface, SettingChangeListener {
34
+public class OsdPlugin extends BaseCommandPlugin {
52 35
 
53
-    /** Config OSD Window. */
54
-    private OsdWindow osdWindow;
55
-    /** The OSD Manager that this plugin is using. */
56
-    private final OsdManager osdManager;
57
-    /** X-axis position of OSD. */
58
-    private int x;
59
-    /** Y-axis potion of OSD. */
60
-    private int y;
61
-    /** Setting objects with registered change listeners. */
62
-    private PreferencesSetting fontSizeSetting;
63
-    private PreferencesSetting backgroundSetting;
64
-    private PreferencesSetting foregroundSetting;
65
-    private PreferencesSetting widthSetting;
66
-    private PreferencesSetting timeoutSetting;
67
-    private PreferencesSetting maxWindowsSetting;
68
-    /** This plugin's plugin info. */
69
-    private final PluginInfo pluginInfo;
70
-    /** The controller to read/write settings with. */
71
-    private final IdentityController identityController;
72
-    /** The manager to use to parse colours. */
73
-    private final ColourManager colourManager;
74
-    /** The frame that the OSDs will be associated with. */
75
-    private final Frame mainFrame;
76
-    /** This plugin's settings domain. */
77
-    private final String domain;
78
-
79
-    /**
80
-     * Creates a new instance of this plugin.
81
-     *
82
-     * @param swingController    The controller that owns this plugin.
83
-     * @param pluginInfo         This plugin's plugin info
84
-     * @param identityController The controller to use to read and write settings.
85
-     * @param commandController  Command controller to register commands
86
-     * @param colourManager      The manager to use to parse colours.
87
-     */
88
-    public OsdPlugin(
89
-            final SwingController swingController,
90
-            final PluginInfo pluginInfo,
91
-            final IdentityController identityController,
92
-            final CommandController commandController,
93
-            final ColourManager colourManager) {
94
-        this.pluginInfo = pluginInfo;
95
-        this.identityController = identityController;
96
-        this.colourManager = colourManager;
97
-        this.mainFrame = swingController.getMainFrame();
98
-        this.domain = pluginInfo.getDomain();
99
-        osdManager = new OsdManager(mainFrame, identityController, colourManager, pluginInfo);
100
-        registerCommand(new OsdCommand(commandController, osdManager), OsdCommand.INFO);
101
-    }
102
-
103
-    @Override
104
-    public void showConfig(final PreferencesDialogModel manager) {
105
-        x = identityController.getGlobalConfiguration()
106
-                .getOptionInt(domain, "locationX");
107
-        y = identityController.getGlobalConfiguration()
108
-                .getOptionInt(domain, "locationY");
109
-
110
-        final PreferencesCategory category = new PluginPreferencesCategory(
111
-                pluginInfo, "OSD",
112
-                "General configuration for OSD plugin.", "category-osd");
113
-
114
-        fontSizeSetting = new PreferencesSetting(PreferencesType.INTEGER,
115
-                domain, "fontSize", "Font size", "Changes the font " + "size of the OSD",
116
-                manager.getConfigManager(),
117
-                manager.getIdentity()).registerChangeListener(this);
118
-        backgroundSetting = new PreferencesSetting(PreferencesType.COLOUR,
119
-                domain, "bgcolour", "Background colour",
120
-                "Background colour for the OSD", manager.getConfigManager(),
121
-                manager.getIdentity()).registerChangeListener(this);
122
-        foregroundSetting = new PreferencesSetting(PreferencesType.COLOUR,
123
-                domain, "fgcolour", "Foreground colour",
124
-                "Foreground colour for the OSD", manager.getConfigManager(),
125
-                manager.getIdentity()).registerChangeListener(this);
126
-        widthSetting = new PreferencesSetting(PreferencesType.INTEGER,
127
-                domain, "width", "OSD Width", "Width of the OSD Window",
128
-                manager.getConfigManager(), manager.getIdentity())
129
-                .registerChangeListener(this);
130
-        timeoutSetting = new PreferencesSetting(PreferencesType.OPTIONALINTEGER,
131
-                new OptionalValidator(new NumericalValidator(1, Integer.MAX_VALUE)),
132
-                domain, "timeout", "Timeout", "Length of time in "
133
-                + "seconds before the OSD window closes", manager.getConfigManager(),
134
-                manager.getIdentity());
135
-        maxWindowsSetting = new PreferencesSetting(PreferencesType.OPTIONALINTEGER,
136
-                new OptionalValidator(new NumericalValidator(1, Integer.MAX_VALUE)),
137
-                domain, "maxWindows", "Maximum open windows",
138
-                "Maximum number of OSD windows that will be displayed at any given time",
139
-                manager.getConfigManager(), manager.getIdentity());
140
-
141
-        category.addSetting(fontSizeSetting);
142
-        category.addSetting(backgroundSetting);
143
-        category.addSetting(foregroundSetting);
144
-        category.addSetting(widthSetting);
145
-        category.addSetting(timeoutSetting);
146
-        category.addSetting(maxWindowsSetting);
147
-
148
-        final Map<String, String> posOptions = new HashMap<>();
149
-
150
-        //Populate policy MULTICHOICE
151
-        for (OsdPolicy policy : OsdPolicy.values()) {
152
-            posOptions.put(policy.name(), policy.getDescription());
153
-        }
154 36
 
155
-        category.addSetting(new PreferencesSetting(domain, "newbehaviour",
156
-                "New window policy", "What to do when an OSD Window "
157
-                + "is opened when there are other, existing windows open",
158
-                posOptions, manager.getConfigManager(), manager.getIdentity()));
159
-
160
-        category.addChangeListener(this);
161
-        manager.getCategory("Plugins").addSubCategory(category);
162
-        manager.registerSaveListener(this);
163
-    }
164
-
165
-    @Override
166
-    public void categorySelected(final PreferencesCategory category) {
167
-        osdWindow = new OsdWindow(mainFrame, identityController, osdManager, colourManager,
168
-                -1, "Please drag this OSD to position", true, x, y, domain);
169
-        osdWindow.setBackgroundColour(backgroundSetting.getValue());
170
-        osdWindow.setForegroundColour(foregroundSetting.getValue());
171
-        osdWindow.setFontSize(Integer.parseInt(fontSizeSetting.getValue()));
172
-    }
173
-
174
-    @Override
175
-    public void categoryDeselected(final PreferencesCategory category) {
176
-        x = osdWindow.getLocationOnScreen().x;
177
-        y = osdWindow.getLocationOnScreen().y;
178
-
179
-        osdWindow.dispose();
180
-        osdWindow = null;
181
-    }
37
+    /** The OSD Manager that this plugin is using. */
38
+    private OsdManager osdManager;
182 39
 
183 40
     @Override
184
-    public void save() {
185
-        identityController.getUserSettings().setOption(domain, "locationX", x);
186
-        identityController.getUserSettings().setOption(domain, "locationY", y);
187
-    }
41
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
42
+        super.load(pluginInfo, graph);
188 43
 
189
-    @Override
190
-    public void settingChanged(final PreferencesSetting setting) {
191
-        if (osdWindow == null) {
192
-            // They've changed categories but are somehow poking settings.
193
-            // Ignore the request.
194
-            return;
195
-        }
44
+        setObjectGraph(graph.plus(new OsdModule(pluginInfo)));
45
+        osdManager = getObjectGraph().get(OsdManager.class);
196 46
 
197
-        if (setting.equals(fontSizeSetting)) {
198
-            osdWindow.setFontSize(Integer.parseInt(setting.getValue()));
199
-        } else if (setting.equals(backgroundSetting)) {
200
-            osdWindow.setBackgroundColour(setting.getValue());
201
-        } else if (setting.equals(foregroundSetting)) {
202
-            osdWindow.setForegroundColour(setting.getValue());
203
-        } else if (setting.equals(widthSetting)) {
204
-            int width = 500;
205
-            try {
206
-                width = Integer.parseInt(setting.getValue());
207
-            } catch (NumberFormatException e) {
208
-                //Ignore
209
-            }
210
-            osdWindow.setSize(width, osdWindow.getHeight());
211
-        }
47
+        registerCommand(OsdCommand.class, OsdCommand.INFO);
212 48
     }
213 49
 
214 50
     /**

Ładowanie…
Anuluj
Zapisz