Переглянути джерело

DI the window flashing plugin.

Change-Id: I891b7a42d0d924ab518006fb297f4ffeb4807f85
Reviewed-on: http://gerrit.dmdirc.com/3113
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.8
Greg Holmes 10 роки тому
джерело
коміт
9f74ad213f

+ 9
- 6
src/com/dmdirc/addons/windowflashing/FlashWindow.java Переглянути файл

@@ -30,6 +30,8 @@ import com.dmdirc.commandparser.commands.Command;
30 30
 import com.dmdirc.commandparser.commands.context.CommandContext;
31 31
 import com.dmdirc.interfaces.CommandController;
32 32
 
33
+import javax.inject.Inject;
34
+
33 35
 /**
34 36
  * Command to flash an inactive window under Windows.
35 37
  */
@@ -39,24 +41,25 @@ public class FlashWindow extends Command {
39 41
     public static final BaseCommandInfo INFO = new BaseCommandInfo(
40 42
             "flashwindow", "flashwindow - Flashes the window until activated",
41 43
             CommandType.TYPE_GLOBAL);
42
-    /** Parent plugin. */
43
-    private final WindowFlashing plugin;
44
+    /** Window flashing manager. */
45
+    private final WindowFlashingManager manager;
44 46
 
45 47
     /**
46 48
      * Creates a new flash window command.
47 49
      *
48
-     * @param plugin Parent plugin to trigger window flashing
50
+     * @param manager Window flashing manager
49 51
      * @param commandController The controller to use for command information.
50 52
      */
51
-    public FlashWindow(final WindowFlashing plugin, final CommandController commandController) {
53
+    @Inject
54
+    public FlashWindow(final WindowFlashingManager manager, final CommandController commandController) {
52 55
         super(commandController);
53
-        this.plugin = plugin;
56
+        this.manager = manager;
54 57
     }
55 58
 
56 59
     /** {@inheritDoc} */
57 60
     @Override
58 61
     public void execute(final FrameContainer origin,
59 62
             final CommandArguments args, final CommandContext context) {
60
-        plugin.flashWindow();
63
+        manager.flashWindow();
61 64
     }
62 65
 }

+ 21
- 153
src/com/dmdirc/addons/windowflashing/WindowFlashing.java Переглянути файл

@@ -22,93 +22,39 @@
22 22
 
23 23
 package com.dmdirc.addons.windowflashing;
24 24
 
25
-import com.dmdirc.actions.ActionManager;
26
-import com.dmdirc.actions.CoreActionType;
27
-import com.dmdirc.addons.ui_swing.MainFrame;
28
-import com.dmdirc.addons.ui_swing.SwingController;
29
-import com.dmdirc.config.ConfigBinder;
30
-import com.dmdirc.config.ConfigBinding;
31 25
 import com.dmdirc.config.prefs.PluginPreferencesCategory;
32 26
 import com.dmdirc.config.prefs.PreferencesCategory;
33 27
 import com.dmdirc.config.prefs.PreferencesDialogModel;
34 28
 import com.dmdirc.config.prefs.PreferencesSetting;
35 29
 import com.dmdirc.config.prefs.PreferencesType;
36
-import com.dmdirc.interfaces.ActionListener;
37
-import com.dmdirc.interfaces.CommandController;
38
-import com.dmdirc.interfaces.actions.ActionType;
39
-import com.dmdirc.interfaces.config.IdentityController;
40 30
 import com.dmdirc.plugins.Exported;
41 31
 import com.dmdirc.plugins.PluginInfo;
42 32
 import com.dmdirc.plugins.implementations.BaseCommandPlugin;
43 33
 
44
-import com.sun.jna.Native;
45
-import com.sun.jna.NativeLibrary;
46
-import com.sun.jna.Pointer;
47
-import com.sun.jna.platform.win32.User32;
48
-import com.sun.jna.platform.win32.WinDef.HWND;
49
-import com.sun.jna.platform.win32.WinUser;
50
-import com.sun.jna.platform.win32.WinUser.FLASHWINFO;
34
+import dagger.ObjectGraph;
51 35
 
52 36
 /**
53 37
  * Native notification plugin to make DMDirc support windows task bar flashing.
54 38
  */
55
-public class WindowFlashing extends BaseCommandPlugin implements ActionListener {
39
+public class WindowFlashing extends BaseCommandPlugin {
56 40
 
41
+    /** Window flashing manager. */
42
+    private WindowFlashingManager manager;
57 43
     /** This plugin's plugin info. */
58
-    private final PluginInfo pluginInfo;
59
-    /** Config binder. */
60
-    private final ConfigBinder binder;
61
-    /** Parent swing controller. */
62
-    private final SwingController controller;
63
-    /** Action manager used for getting client events. */
64
-    private final ActionManager actionManager;
65
-    /** Library instance. */
66
-    private User32 user32;
67
-    /** Swing main frame. */
68
-    private MainFrame mainFrame;
69
-    /** Cached blink rate setting. */
70
-    @ConfigBinding(domain="plugin-windowflashing", key="blinkrate",
71
-            fallbacks={"plugin-windowflashing", "blinkratefallback"})
72
-    private int blinkrate;
73
-    /** Cached count setting. */
74
-    @ConfigBinding(domain="plugin-windowflashing", key="flashcount",
75
-            fallbacks={"plugin-windowflashing", "flashcountfallback"})
76
-    private int flashcount;
77
-    /** Cached flash taskbar setting. */
78
-    @ConfigBinding(domain="plugin-windowflashing", key="flashtaskbar")
79
-    private boolean flashtaskbar;
80
-    /** Cached flash caption setting. */
81
-    @ConfigBinding(domain="plugin-windowflashing", key="flashcaption")
82
-    private boolean flashcaption;
44
+    private PluginInfo pluginInfo;
83 45
 
84
-    /**
85
-     * Creates a new instance of this plugin.
86
-     *
87
-     * @param pluginInfo This plugin's plugin info
88
-     * @param identityController Identity Manager
89
-     * @param controller Parent swing controller
90
-     * @param actionManager Action manager
91
-     * @param commandController command controller to register commands
92
-     */
93
-    public WindowFlashing(final PluginInfo pluginInfo,
94
-            final IdentityController identityController,
95
-            final SwingController controller,
96
-            final ActionManager actionManager,
97
-            final CommandController commandController) {
46
+    public WindowFlashing(final PluginInfo pluginInfo) {
98 47
         this.pluginInfo = pluginInfo;
99
-        this.controller = controller;
100
-        this.actionManager = actionManager;
101
-        binder = identityController.getGlobalConfiguration().getBinder();
102
-        registerCommand(new FlashWindow(this, commandController), FlashWindow.INFO);
103 48
     }
104 49
 
105
-    /**
106
-     * Flashes an inactive window under windows.
107
-     */
108
-    public void flashWindow() {
109
-        if (!mainFrame.isFocused()) {
110
-            user32.FlashWindowEx(setupFlashObject());
111
-        }
50
+    @Override
51
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
52
+        super.load(pluginInfo, graph);
53
+        this.pluginInfo = pluginInfo;
54
+
55
+        setObjectGraph(graph.plus(new WindowFlashingModule()));
56
+        registerCommand(FlashWindow.class, FlashWindow.INFO);
57
+        manager = getObjectGraph().get(WindowFlashingManager.class);
112 58
     }
113 59
 
114 60
     /**
@@ -120,31 +66,21 @@ public class WindowFlashing extends BaseCommandPlugin implements ActionListener
120 66
      */
121 67
     @Exported
122 68
     public void flashNotification(final String title, final String message) {
123
-        flashWindow();
69
+        manager.flashWindow();
124 70
     }
125 71
 
126
-    /** {@inheritDoc} */
127 72
     @Override
128 73
     public void onLoad() {
129
-        mainFrame = controller.getMainFrame();
130
-        user32 = (User32) Native.loadLibrary("user32", User32.class);
131
-        binder.bind(this, WindowFlashing.class);
132
-        actionManager.registerListener(this, CoreActionType.CLIENT_FOCUS_GAINED);
133 74
         super.onLoad();
75
+        manager.onLoad();
134 76
     }
135 77
 
136
-    /** {@inheritDoc} */
137 78
     @Override
138 79
     public void onUnload() {
139 80
         super.onUnload();
140
-        actionManager.unregisterListener(this);
141
-        binder.unbind(this);
142
-        mainFrame = null;
143
-        user32 = null;
144
-        NativeLibrary.getInstance("user32").dispose();
81
+        manager.onUnload();
145 82
     }
146 83
 
147
-    /** {@inheritDoc} */
148 84
     @Override
149 85
     public void showConfig(final PreferencesDialogModel manager) {
150 86
         final PreferencesCategory category = new PluginPreferencesCategory(
@@ -152,93 +88,25 @@ public class WindowFlashing extends BaseCommandPlugin implements ActionListener
152 88
                 "General configuration for window flashing plugin.");
153 89
 
154 90
         category.addSetting(new PreferencesSetting(
155
-                PreferencesType.OPTIONALINTEGER, getDomain(), "blinkrate",
91
+                PreferencesType.OPTIONALINTEGER, pluginInfo.getDomain(), "blinkrate",
156 92
                 "Blink rate", "Specifies the rate at which the taskbar and or "
157 93
                 + "caption will blink, if unspecified this will be your cursor "
158 94
                 + "blink rate.",
159 95
                 manager.getConfigManager(), manager.getIdentity()));
160 96
         category.addSetting(new PreferencesSetting(
161
-                PreferencesType.OPTIONALINTEGER, getDomain(), "flashcount",
97
+                PreferencesType.OPTIONALINTEGER, pluginInfo.getDomain(), "flashcount",
162 98
                 "Flash count", "Specifies the number of times to blink, if "
163 99
                 + "unspecified this will blink indefinitely",
164 100
                 manager.getConfigManager(), manager.getIdentity()));
165 101
         category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
166
-                getDomain(), "flashtaskbar", "Flash taskbar",
102
+                pluginInfo.getDomain(), "flashtaskbar", "Flash taskbar",
167 103
                 "Should the taskbar entry flash?",
168 104
                 manager.getConfigManager(), manager.getIdentity()));
169 105
         category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
170
-                getDomain(), "flashcaption", "Flash caption",
106
+                pluginInfo.getDomain(), "flashcaption", "Flash caption",
171 107
                 "Should the window caption flash?",
172 108
                 manager.getConfigManager(), manager.getIdentity()));
173 109
 
174 110
         manager.getCategory("Plugins").addSubCategory(category);
175 111
     }
176
-
177
-    /**
178
-     * Creates a new flash info object that starts flashing with the configured
179
-     * settings.
180
-     */
181
-    private FLASHWINFO setupFlashObject() {
182
-        final FLASHWINFO flashInfo = new FLASHWINFO();
183
-        flashInfo.dwFlags = getFlags();
184
-        flashInfo.dwTimeout = blinkrate;
185
-        flashInfo.uCount = flashcount;
186
-        flashInfo.hWnd = getHWND();
187
-        flashInfo.cbSize = flashInfo.size();
188
-        return flashInfo;
189
-    }
190
-
191
-    /**
192
-     * Creates a new flash object that stops the flashing.
193
-     */
194
-    private FLASHWINFO stopFlashObject() {
195
-        final FLASHWINFO flashInfo = new FLASHWINFO();
196
-        flashInfo.dwFlags = WinUser.FLASHW_STOP;
197
-        flashInfo.dwTimeout = 0;
198
-        flashInfo.uCount = 0;
199
-        flashInfo.hWnd = getHWND();
200
-        flashInfo.cbSize = flashInfo.size();
201
-        return flashInfo;
202
-    }
203
-
204
-    /**
205
-     * Returns the native handle object for the main frame.
206
-     * @return
207
-     */
208
-    private HWND getHWND() {
209
-        final HWND hwnd = new HWND();
210
-        final Pointer pointer = Native.getWindowPointer(mainFrame);
211
-        hwnd.setPointer(pointer);
212
-        return hwnd;
213
-    }
214
-
215
-    /**
216
-     * Calculates the flags for the flash object based on config settings.
217
-     *
218
-     * @return Flash info flags
219
-     */
220
-    private int getFlags() {
221
-        int returnValue = 0;
222
-        if (flashtaskbar) {
223
-            returnValue |= WinUser.FLASHW_TRAY;
224
-        }
225
-        if (flashcaption) {
226
-            returnValue |= WinUser.FLASHW_CAPTION;
227
-        }
228
-        if (flashcount >= 0) {
229
-            returnValue |= WinUser.FLASHW_TIMER;
230
-        } else {
231
-            returnValue |= WinUser.FLASHW_TIMERNOFG;
232
-        }
233
-        return returnValue;
234
-    }
235
-
236
-    /** {@inheritDoc} */
237
-    @Override
238
-    public void processEvent(final ActionType type, final StringBuffer format,
239
-            final Object... arguments) {
240
-        if (mainFrame != null) {
241
-            user32.FlashWindowEx(stopFlashObject());
242
-        }
243
-    }
244 112
 }

+ 165
- 0
src/com/dmdirc/addons/windowflashing/WindowFlashingManager.java Переглянути файл

@@ -0,0 +1,165 @@
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
+
23
+package com.dmdirc.addons.windowflashing;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.actions.CoreActionType;
27
+import com.dmdirc.addons.ui_swing.MainFrame;
28
+import com.dmdirc.config.ConfigBinder;
29
+import com.dmdirc.config.ConfigBinding;
30
+import com.dmdirc.interfaces.ActionController;
31
+import com.dmdirc.interfaces.ActionListener;
32
+import com.dmdirc.interfaces.actions.ActionType;
33
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
34
+
35
+import javax.inject.Inject;
36
+
37
+import com.sun.jna.Native;
38
+import com.sun.jna.NativeLibrary;
39
+import com.sun.jna.Pointer;
40
+import com.sun.jna.platform.win32.User32;
41
+import com.sun.jna.platform.win32.WinDef;
42
+import com.sun.jna.platform.win32.WinUser;
43
+
44
+public class WindowFlashingManager implements ActionListener {
45
+
46
+    /** Swing main frame. */
47
+    private final MainFrame mainFrame;
48
+    /** Action controller. */
49
+    private final ActionController actionController;
50
+    /** Config binder. */
51
+    private final ConfigBinder binder;
52
+    /** Cached blink rate setting. */
53
+    @ConfigBinding(domain="plugin-windowflashing", key="blinkrate",
54
+            fallbacks={"plugin-windowflashing", "blinkratefallback"})
55
+    private int blinkrate;
56
+    /** Cached count setting. */
57
+    @ConfigBinding(domain="plugin-windowflashing", key="flashcount",
58
+            fallbacks={"plugin-windowflashing", "flashcountfallback"})
59
+    private int flashcount;
60
+    /** Cached flash taskbar setting. */
61
+    @ConfigBinding(domain="plugin-windowflashing", key="flashtaskbar")
62
+    private boolean flashtaskbar;
63
+    /** Cached flash caption setting. */
64
+    @ConfigBinding(domain="plugin-windowflashing", key="flashcaption")
65
+    private boolean flashcaption;
66
+    /** Library instance. */
67
+    private User32 user32;
68
+
69
+    @Inject
70
+    public WindowFlashingManager(@GlobalConfig final AggregateConfigProvider config,
71
+            final MainFrame mainFrame, final ActionController actionController) {
72
+        this.mainFrame = mainFrame;
73
+        this.actionController = actionController;
74
+        binder = config.getBinder();
75
+    }
76
+
77
+    public void onLoad() {
78
+        user32 = (User32) Native.loadLibrary("user32", User32.class);
79
+        binder.bind(this, WindowFlashing.class);
80
+        actionController.registerListener(this, CoreActionType.CLIENT_FOCUS_GAINED);
81
+    }
82
+
83
+    public void onUnload() {
84
+        actionController.unregisterListener(this);
85
+        binder.unbind(this);
86
+        user32 = null;
87
+        NativeLibrary.getInstance("user32").dispose();
88
+    }
89
+
90
+    /**
91
+     * Flashes an inactive window under windows.
92
+     */
93
+    public void flashWindow() {
94
+        if (!mainFrame.isFocused()) {
95
+            user32.FlashWindowEx(setupFlashObject());
96
+        }
97
+    }
98
+
99
+    /**
100
+     * Creates a new flash info object that starts flashing with the configured
101
+     * settings.
102
+     */
103
+    private WinUser.FLASHWINFO setupFlashObject() {
104
+        final WinUser.FLASHWINFO flashInfo = new WinUser.FLASHWINFO();
105
+        flashInfo.dwFlags = getFlags();
106
+        flashInfo.dwTimeout = blinkrate;
107
+        flashInfo.uCount = flashcount;
108
+        flashInfo.hWnd = getHWND();
109
+        flashInfo.cbSize = flashInfo.size();
110
+        return flashInfo;
111
+    }
112
+
113
+    /**
114
+     * Creates a new flash object that stops the flashing.
115
+     */
116
+    private WinUser.FLASHWINFO stopFlashObject() {
117
+        final WinUser.FLASHWINFO flashInfo = new WinUser.FLASHWINFO();
118
+        flashInfo.dwFlags = WinUser.FLASHW_STOP;
119
+        flashInfo.dwTimeout = 0;
120
+        flashInfo.uCount = 0;
121
+        flashInfo.hWnd = getHWND();
122
+        flashInfo.cbSize = flashInfo.size();
123
+        return flashInfo;
124
+    }
125
+
126
+    /**
127
+     * Returns the native handle object for the main frame.
128
+     * @return
129
+     */
130
+    private WinDef.HWND getHWND() {
131
+        final WinDef.HWND hwnd = new WinDef.HWND();
132
+        final Pointer pointer = Native.getWindowPointer(mainFrame);
133
+        hwnd.setPointer(pointer);
134
+        return hwnd;
135
+    }
136
+
137
+    /**
138
+     * Calculates the flags for the flash object based on config settings.
139
+     *
140
+     * @return Flash info flags
141
+     */
142
+    private int getFlags() {
143
+        int returnValue = 0;
144
+        if (flashtaskbar) {
145
+            returnValue |= WinUser.FLASHW_TRAY;
146
+        }
147
+        if (flashcaption) {
148
+            returnValue |= WinUser.FLASHW_CAPTION;
149
+        }
150
+        if (flashcount >= 0) {
151
+            returnValue |= WinUser.FLASHW_TIMER;
152
+        } else {
153
+            returnValue |= WinUser.FLASHW_TIMERNOFG;
154
+        }
155
+        return returnValue;
156
+    }
157
+
158
+    @Override
159
+    public void processEvent(final ActionType type, final StringBuffer format,
160
+            final Object... arguments) {
161
+        if (mainFrame != null) {
162
+            user32.FlashWindowEx(stopFlashObject());
163
+        }
164
+    }
165
+}

+ 35
- 0
src/com/dmdirc/addons/windowflashing/WindowFlashingModule.java Переглянути файл

@@ -0,0 +1,35 @@
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
+
23
+package com.dmdirc.addons.windowflashing;
24
+
25
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
26
+
27
+import dagger.Module;
28
+
29
+/**
30
+ * DI Module for the window flashing module.
31
+ */
32
+@Module(injects = {WindowFlashingManager.class, FlashWindow.class}, addsTo = SwingModule.class)
33
+public class WindowFlashingModule {
34
+
35
+}

Завантаження…
Відмінити
Зберегти