Bladeren bron

DI systray plugin.

Change-Id: I52991f4ea7fdbbe6d05633f434b9cdb2d78d2806
Reviewed-on: http://gerrit.dmdirc.com/3223
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.8
Greg Holmes 10 jaren geleden
bovenliggende
commit
4879f4866b

+ 9
- 7
src/com/dmdirc/addons/systray/PopupCommand.java Bestand weergeven

@@ -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
  * The /popup command allows the user to show a popup message from the system tray icon.
35 37
  */
@@ -39,19 +41,20 @@ public class PopupCommand extends Command {
39 41
     public static final BaseCommandInfo INFO = new BaseCommandInfo("popup",
40 42
             "popup <message> - shows the message as a system tray popup",
41 43
             CommandType.TYPE_GLOBAL);
42
-    /** The SystrayPlugin that we belong to. */
43
-    private final SystrayPlugin parent;
44
+    /** Systray manager, used to show notifications. */
45
+    private final SystrayManager manager;
44 46
 
45 47
     /**
46 48
      * Creates a new instance of PopupCommand.
47 49
      *
48
-     * @param newParent         The plugin that this command belongs to
50
+     * @param manager           Systray manager, used to show notifications
49 51
      * @param commandController The controller to use for command information.
50 52
      */
51
-    public PopupCommand(final SystrayPlugin newParent, final CommandController commandController) {
53
+    @Inject
54
+    public PopupCommand(final SystrayManager manager, final CommandController commandController) {
52 55
         super(commandController);
53 56
 
54
-        this.parent = newParent;
57
+        this.manager = manager;
55 58
     }
56 59
 
57 60
     /**
@@ -63,11 +66,10 @@ public class PopupCommand extends Command {
63 66
      * @return True if the notification was shown.
64 67
      */
65 68
     public boolean showPopup(final String title, final String message) {
66
-        parent.notify(title, message);
69
+        manager.notify(title, message);
67 70
         return true;
68 71
     }
69 72
 
70
-    /** {@inheritDoc} */
71 73
     @Override
72 74
     public void execute(final FrameContainer origin,
73 75
             final CommandArguments args, final CommandContext context) {

+ 181
- 0
src/com/dmdirc/addons/systray/SystrayManager.java Bestand weergeven

@@ -0,0 +1,181 @@
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.systray;
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.interfaces.ActionController;
29
+import com.dmdirc.interfaces.actions.ActionType;
30
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
31
+import com.dmdirc.plugins.PluginDomain;
32
+import com.dmdirc.ui.IconManager;
33
+import com.dmdirc.ui.messages.Styliser;
34
+
35
+import java.awt.AWTException;
36
+import java.awt.Frame;
37
+import java.awt.MenuItem;
38
+import java.awt.PopupMenu;
39
+import java.awt.SystemTray;
40
+import java.awt.TrayIcon;
41
+import java.awt.event.ActionEvent;
42
+import java.awt.event.ActionListener;
43
+import java.awt.event.MouseEvent;
44
+import java.awt.event.MouseListener;
45
+
46
+import javax.inject.Inject;
47
+
48
+public class SystrayManager implements ActionListener, MouseListener,
49
+        com.dmdirc.interfaces.ActionListener {
50
+
51
+    /** Main frame instance. */
52
+    private final MainFrame mainFrame;
53
+    /** This plugin's settings domain. */
54
+    private final String domain;
55
+    /** The config to read settings from. */
56
+    private final AggregateConfigProvider globalConfig;
57
+    /** Icon manager to get images from. */
58
+    private final IconManager iconManager;
59
+    /** Action Controller. */
60
+    private final ActionController actionController;
61
+    /** The tray icon we're currently using. */
62
+    private TrayIcon icon;
63
+
64
+    @Inject
65
+    public SystrayManager(@GlobalConfig final AggregateConfigProvider globalConfig,
66
+            @PluginDomain(SystrayPlugin.class) final String domain,
67
+            final MainFrame mainFrame, final ActionController actionController,
68
+            @GlobalConfig final IconManager iconManager) {
69
+        this.globalConfig = globalConfig;
70
+        this.domain = domain;
71
+        this.mainFrame = mainFrame;
72
+        this.iconManager = iconManager;
73
+        this.actionController = actionController;
74
+    }
75
+
76
+    public void load() {
77
+        final MenuItem show = new MenuItem("Show/hide");
78
+        final MenuItem quit = new MenuItem("Quit");
79
+
80
+        final PopupMenu menu = new PopupMenu();
81
+        menu.add(show);
82
+        menu.add(quit);
83
+
84
+        show.addActionListener(this);
85
+        quit.addActionListener(this);
86
+
87
+        icon = new TrayIcon(iconManager.getImage("logo"), "DMDirc", menu);
88
+        icon.setImageAutoSize(true);
89
+        icon.addMouseListener(this);
90
+
91
+        try {
92
+            SystemTray.getSystemTray().add(icon);
93
+            actionController.registerListener(this, CoreActionType.CLIENT_MINIMISED);
94
+        } catch (AWTException ex) {
95
+            throw new IllegalStateException("Unable to load plugin", ex);
96
+        }
97
+    }
98
+
99
+    public void unload() {
100
+        SystemTray.getSystemTray().remove(icon);
101
+        actionController.unregisterListener(this);
102
+        icon = null;
103
+    }
104
+
105
+    /**
106
+     * Sends a notification via the system tray icon.
107
+     *
108
+     * @param title   The title of the notification
109
+     * @param message The contents of the notification
110
+     * @param type    The type of notification
111
+     */
112
+    public void notify(final String title, final String message,
113
+            final TrayIcon.MessageType type) {
114
+        icon.displayMessage(title, Styliser.stipControlCodes(message), type);
115
+    }
116
+
117
+    /**
118
+     * Sends a notification via the system tray icon.
119
+     *
120
+     * @param title   The title of the notification
121
+     * @param message The contents of the notification
122
+     */
123
+    public void notify(final String title, final String message) {
124
+        notify(title, message, TrayIcon.MessageType.NONE);
125
+    }
126
+
127
+    @Override
128
+    public void actionPerformed(final ActionEvent e) {
129
+        switch (e.getActionCommand()) {
130
+            case "Show/hide":
131
+                mainFrame.setVisible(!mainFrame.isVisible());
132
+                break;
133
+            case "Quit":
134
+                mainFrame.quit();
135
+                break;
136
+        }
137
+    }
138
+
139
+    @Override
140
+    public void mouseClicked(final MouseEvent e) {
141
+        if (e.getButton() == MouseEvent.BUTTON1) {
142
+            if (mainFrame.isVisible()) {
143
+                mainFrame.setVisible(false);
144
+            } else {
145
+                mainFrame.setVisible(true);
146
+                mainFrame.setState(Frame.NORMAL);
147
+                mainFrame.toFront();
148
+            }
149
+        }
150
+    }
151
+
152
+    @Override
153
+    public void processEvent(final ActionType type, final StringBuffer format,
154
+            final Object... arguments) {
155
+        if (type == CoreActionType.CLIENT_MINIMISED
156
+                && globalConfig.getOptionBool(domain, "autominimise")) {
157
+            mainFrame.setVisible(false);
158
+        }
159
+    }
160
+
161
+    @Override
162
+    public void mousePressed(final MouseEvent e) {
163
+        //Ignore
164
+    }
165
+
166
+    @Override
167
+    public void mouseReleased(final MouseEvent e) {
168
+        //Ignore
169
+    }
170
+
171
+    @Override
172
+    public void mouseEntered(final MouseEvent e) {
173
+        //Ignore
174
+    }
175
+
176
+    @Override
177
+    public void mouseExited(final MouseEvent e) {
178
+        //Ignore
179
+    }
180
+
181
+}

+ 45
- 0
src/com/dmdirc/addons/systray/SystrayModule.java Bestand weergeven

@@ -0,0 +1,45 @@
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.systray;
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
+@Module(injects = {SystrayManager.class, PopupCommand.class}, addsTo = SwingModule.class)
33
+public class SystrayModule {
34
+    private final PluginInfo pluginInfo;
35
+
36
+    public SystrayModule(final PluginInfo pluginInfo) {
37
+        this.pluginInfo = pluginInfo;
38
+    }
39
+
40
+    @Provides
41
+    @PluginDomain(SystrayPlugin.class)
42
+    public String getSettingsDomain() {
43
+        return pluginInfo.getDomain();
44
+    }
45
+}

+ 16
- 185
src/com/dmdirc/addons/systray/SystrayPlugin.java Bestand weergeven

@@ -22,115 +22,46 @@
22 22
 
23 23
 package com.dmdirc.addons.systray;
24 24
 
25
-import com.dmdirc.actions.CoreActionType;
26
-import com.dmdirc.addons.ui_swing.MainFrame;
27
-import com.dmdirc.addons.ui_swing.SwingController;
28 25
 import com.dmdirc.config.prefs.PluginPreferencesCategory;
29 26
 import com.dmdirc.config.prefs.PreferencesCategory;
30 27
 import com.dmdirc.config.prefs.PreferencesDialogModel;
31 28
 import com.dmdirc.config.prefs.PreferencesSetting;
32 29
 import com.dmdirc.config.prefs.PreferencesType;
33
-import com.dmdirc.interfaces.ActionController;
34
-import com.dmdirc.interfaces.CommandController;
35
-import com.dmdirc.interfaces.actions.ActionType;
36
-import com.dmdirc.interfaces.config.IdentityController;
37 30
 import com.dmdirc.plugins.Exported;
38 31
 import com.dmdirc.plugins.PluginInfo;
39 32
 import com.dmdirc.plugins.implementations.BaseCommandPlugin;
40
-import com.dmdirc.ui.IconManager;
41
-import com.dmdirc.ui.messages.Styliser;
42
-import com.dmdirc.util.URLBuilder;
43 33
 import com.dmdirc.util.validators.ValidationResponse;
44 34
 
45
-import java.awt.AWTException;
46
-import java.awt.Frame;
47
-import java.awt.MenuItem;
48
-import java.awt.PopupMenu;
49 35
 import java.awt.SystemTray;
50
-import java.awt.TrayIcon;
51
-import java.awt.event.ActionEvent;
52
-import java.awt.event.ActionListener;
53
-import java.awt.event.MouseEvent;
54
-import java.awt.event.MouseListener;
36
+
37
+import dagger.ObjectGraph;
55 38
 
56 39
 /**
57 40
  * The Systray plugin shows DMDirc in the user's system tray, and allows notifications to be
58 41
  * disabled.
59 42
  */
60
-public class SystrayPlugin extends BaseCommandPlugin implements
61
-        ActionListener, MouseListener, com.dmdirc.interfaces.ActionListener {
43
+public class SystrayPlugin extends BaseCommandPlugin {
62 44
 
63
-    /** The tray icon we're currently using. */
64
-    private final TrayIcon icon;
65
-    /** Main frame instance. */
66
-    private final MainFrame mainFrame;
67 45
     /** This plugin's plugin info. */
68 46
     private final PluginInfo pluginInfo;
69
-    /** The action controller to use. */
70
-    private final ActionController actionController;
71
-    /** The controller to read/write settings with. */
72
-    private final IdentityController identityController;
47
+    /** Systray manager. */
48
+    private SystrayManager manager;
73 49
 
74 50
     /**
75 51
      * Creates a new system tray plugin.
76 52
      *
77
-     * @param swingController    The controller that owns this plugin.
78 53
      * @param pluginInfo         This plugin's plugin info.
79
-     * @param actionController   The action controller to use.
80
-     * @param identityController The identity manager to read settings from.
81
-     * @param commandController  The controller to use for command information.
82
-     * @param urlBuilder         URL builder to use to resolve icon paths.
83 54
      */
84
-    public SystrayPlugin(
85
-            final SwingController swingController,
86
-            final PluginInfo pluginInfo,
87
-            final ActionController actionController,
88
-            final IdentityController identityController,
89
-            final CommandController commandController,
90
-            final URLBuilder urlBuilder) {
55
+    public SystrayPlugin(final PluginInfo pluginInfo) {
91 56
         this.pluginInfo = pluginInfo;
92
-        this.actionController = actionController;
93
-        this.identityController = identityController;
94
-        this.mainFrame = swingController.getMainFrame();
95
-
96
-        final MenuItem show = new MenuItem("Show/hide");
97
-        final MenuItem quit = new MenuItem("Quit");
98
-
99
-        final PopupMenu menu = new PopupMenu();
100
-        menu.add(show);
101
-        menu.add(quit);
102
-
103
-        show.addActionListener(this);
104
-        quit.addActionListener(this);
105
-
106
-        icon = new TrayIcon(
107
-                new IconManager(identityController.getGlobalConfiguration(), urlBuilder)
108
-                .getImage("logo"), "DMDirc", menu);
109
-        icon.setImageAutoSize(true);
110
-        icon.addMouseListener(this);
111
-        registerCommand(new PopupCommand(this, commandController), PopupCommand.INFO);
112 57
     }
113 58
 
114
-    /**
115
-     * Sends a notification via the system tray icon.
116
-     *
117
-     * @param title   The title of the notification
118
-     * @param message The contents of the notification
119
-     * @param type    The type of notification
120
-     */
121
-    public void notify(final String title, final String message,
122
-            final TrayIcon.MessageType type) {
123
-        icon.displayMessage(title, Styliser.stipControlCodes(message), type);
124
-    }
125
-
126
-    /**
127
-     * Sends a notification via the system tray icon.
128
-     *
129
-     * @param title   The title of the notification
130
-     * @param message The contents of the notification
131
-     */
132
-    public void notify(final String title, final String message) {
133
-        notify(title, message, TrayIcon.MessageType.NONE);
59
+    @Override
60
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
61
+        super.load(pluginInfo, graph);
62
+        setObjectGraph(graph.plus(new SystrayModule(pluginInfo)));
63
+        registerCommand(PopupCommand.class, PopupCommand.INFO);
64
+        manager = getObjectGraph().get(SystrayManager.class);
134 65
     }
135 66
 
136 67
     /**
@@ -142,27 +73,9 @@ public class SystrayPlugin extends BaseCommandPlugin implements
142 73
      */
143 74
     @Exported
144 75
     public void showPopup(final String title, final String message) {
145
-        notify(title, message);
76
+        manager.notify(title, message);
146 77
     }
147 78
 
148
-    /**
149
-     * {@inheritDoc}
150
-     *
151
-     * @param e Action event
152
-     */
153
-    @Override
154
-    public void actionPerformed(final ActionEvent e) {
155
-        switch (e.getActionCommand()) {
156
-            case "Show/hide":
157
-                mainFrame.setVisible(!mainFrame.isVisible());
158
-                break;
159
-            case "Quit":
160
-                mainFrame.quit();
161
-                break;
162
-        }
163
-    }
164
-
165
-    /** {@inheritDoc} */
166 79
     @Override
167 80
     public ValidationResponse checkPrerequisites() {
168 81
         if (SystemTray.isSupported()) {
@@ -173,31 +86,18 @@ public class SystrayPlugin extends BaseCommandPlugin implements
173 86
         }
174 87
     }
175 88
 
176
-    /** {@inheritDoc} */
177 89
     @Override
178 90
     public void onLoad() {
179
-        boolean continueLoading = true;
180
-        try {
181
-            SystemTray.getSystemTray().add(icon);
182
-            actionController.registerListener(this, CoreActionType.CLIENT_MINIMISED);
183
-        } catch (AWTException ex) {
184
-            continueLoading = false;
185
-        }
186
-        if (!continueLoading || mainFrame == null) {
187
-            pluginInfo.unloadPlugin();
188
-        }
91
+        manager.load();
189 92
         super.onLoad();
190 93
     }
191 94
 
192
-    /** {@inheritDoc} */
193 95
     @Override
194 96
     public void onUnload() {
195
-        SystemTray.getSystemTray().remove(icon);
196
-        actionController.unregisterListener(this);
97
+        manager.unload();
197 98
         super.onUnload();
198 99
     }
199 100
 
200
-    /** {@inheritDoc} */
201 101
     @Override
202 102
     public void showConfig(final PreferencesDialogModel manager) {
203 103
         final PreferencesCategory category = new PluginPreferencesCategory(
@@ -205,7 +105,7 @@ public class SystrayPlugin extends BaseCommandPlugin implements
205 105
                 "General configuration settings");
206 106
 
207 107
         category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
208
-                getDomain(), "autominimise", "Auto-hide DMDirc when minimised",
108
+                pluginInfo.getDomain(), "autominimise", "Auto-hide DMDirc when minimised",
209 109
                 "If this option is enabled, the systray plugin will hide DMDirc"
210 110
                 + " to the system tray whenever DMDirc is minimised",
211 111
                 manager.getConfigManager(), manager.getIdentity()));
@@ -213,73 +113,4 @@ public class SystrayPlugin extends BaseCommandPlugin implements
213 113
         manager.getCategory("Plugins").addSubCategory(category);
214 114
     }
215 115
 
216
-    /**
217
-     * {@inheritDoc}
218
-     *
219
-     * @param e Mouse event
220
-     */
221
-    @Override
222
-    public void mouseClicked(final MouseEvent e) {
223
-        if (e.getButton() == MouseEvent.BUTTON1) {
224
-            if (mainFrame.isVisible()) {
225
-                mainFrame.setVisible(false);
226
-            } else {
227
-                mainFrame.setVisible(true);
228
-                mainFrame.setState(Frame.NORMAL);
229
-                mainFrame.toFront();
230
-            }
231
-        }
232
-    }
233
-
234
-    /**
235
-     * {@inheritDoc}
236
-     *
237
-     * @param e Mouse event
238
-     */
239
-    @Override
240
-    public void mousePressed(final MouseEvent e) {
241
-        //Ignore
242
-    }
243
-
244
-    /**
245
-     * {@inheritDoc}
246
-     *
247
-     * @param e Mouse event
248
-     */
249
-    @Override
250
-    public void mouseReleased(final MouseEvent e) {
251
-        //Ignore
252
-    }
253
-
254
-    /**
255
-     * {@inheritDoc}
256
-     *
257
-     * @param e Mouse event
258
-     */
259
-    @Override
260
-    public void mouseEntered(final MouseEvent e) {
261
-        //Ignore
262
-    }
263
-
264
-    /**
265
-     * {@inheritDoc}
266
-     *
267
-     * @param e Mouse event
268
-     */
269
-    @Override
270
-    public void mouseExited(final MouseEvent e) {
271
-        //Ignore
272
-    }
273
-
274
-    /** {@inheritDoc} */
275
-    @Override
276
-    public void processEvent(final ActionType type, final StringBuffer format,
277
-            final Object... arguments) {
278
-        if (type == CoreActionType.CLIENT_MINIMISED
279
-                && identityController.getGlobalConfiguration()
280
-                .getOptionBool(getDomain(), "autominimise")) {
281
-            mainFrame.setVisible(false);
282
-        }
283
-    }
284
-
285 116
 }

Laden…
Annuleren
Opslaan