Pārlūkot izejas kodu

DI the time plugin.

Change-Id: I271d24e61936ddb3930339207f575af646598320
Reviewed-on: http://gerrit.dmdirc.com/3224
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.8
Greg Holmes 10 gadus atpakaļ
vecāks
revīzija
57ebe1b77d

+ 16
- 22
src/com/dmdirc/addons/time/TimeActionMetaType.java Parādīt failu

@@ -28,33 +28,27 @@ import java.util.Calendar;
28 28
 
29 29
 /**
30 30
  * Encapsulates the meta types used by the various time actions.
31
- *
32
- * @author chris
33 31
  */
34 32
 public enum TimeActionMetaType implements ActionMetaType {
35 33
 
36 34
     /** Time type. */
37 35
     TIME_TIME {
38
-        /** {@inheritDoc} */
39
-        @Override
40
-        public int getArity() {
41
-            return 1;
42
-        }
43
-
44
-        /** {@inheritDoc} */
45
-        @Override
46
-        public Class[] getArgTypes() {
47
-            return new Class[]{Calendar.class};
48
-        }
49
-
50
-        /** {@inheritDoc} */
51
-        @Override
52
-        public String[] getArgNames() {
53
-            return new String[]{"Date"};
54
-        }
55
-    };
56
-
57
-    /** {@inheritDoc} */
36
+                @Override
37
+                public int getArity() {
38
+                    return 1;
39
+                }
40
+
41
+                @Override
42
+                public Class[] getArgTypes() {
43
+                    return new Class<?>[]{Calendar.class};
44
+                }
45
+
46
+                @Override
47
+                public String[] getArgNames() {
48
+                    return new String[]{"Date"};
49
+                }
50
+            };
51
+
58 52
     @Override
59 53
     public String getGroup() {
60 54
         return "Time Events";

+ 0
- 4
src/com/dmdirc/addons/time/TimeActionType.java Parādīt failu

@@ -27,8 +27,6 @@ import com.dmdirc.interfaces.actions.ActionType;
27 27
 
28 28
 /**
29 29
  * Lists the actions that the time plugin will raise.
30
- *
31
- * @author chris
32 30
  */
33 31
 public enum TimeActionType implements ActionType {
34 32
 
@@ -50,13 +48,11 @@ public enum TimeActionType implements ActionType {
50 48
         this.name = name;
51 49
     }
52 50
 
53
-    /** {@inheritDoc} */
54 51
     @Override
55 52
     public ActionMetaType getType() {
56 53
         return TimeActionMetaType.TIME_TIME;
57 54
     }
58 55
 
59
-    /** {@inheritDoc} */
60 56
     @Override
61 57
     public String getName() {
62 58
         return name;

+ 32
- 0
src/com/dmdirc/addons/time/TimeModule.java Parādīt failu

@@ -0,0 +1,32 @@
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.time;
24
+
25
+import com.dmdirc.ClientModule;
26
+
27
+import dagger.Module;
28
+
29
+@Module(injects = {TimerCommand.class, TimerManager.class}, addsTo = ClientModule.class)
30
+public class TimeModule {
31
+
32
+}

+ 10
- 69
src/com/dmdirc/addons/time/TimePlugin.java Parādīt failu

@@ -22,98 +22,39 @@
22 22
 
23 23
 package com.dmdirc.addons.time;
24 24
 
25
-import com.dmdirc.interfaces.ActionController;
26
-import com.dmdirc.interfaces.CommandController;
25
+import com.dmdirc.plugins.PluginInfo;
27 26
 import com.dmdirc.plugins.implementations.BaseCommandPlugin;
28 27
 
29
-import java.util.Calendar;
30
-import java.util.Timer;
31
-import java.util.TimerTask;
28
+import dagger.ObjectGraph;
32 29
 
33 30
 /**
34 31
  * Provides various time-related features.
35 32
  */
36 33
 public class TimePlugin extends BaseCommandPlugin {
37 34
 
38
-    /** Have we registered our types already? */
39
-    private static boolean registered;
40
-    /** The timer to use for scheduling. */
41
-    private Timer timer;
42 35
     /** The Manager to use for managing timers. */
43 36
     private TimerManager manager;
44
-    /** The action controller to use. */
45
-    private final ActionController actionController;
46 37
 
47
-    /**
48
-     * Creates a new instance of this plugin.
49
-     *
50
-     * @param actionController  The action controller to register listeners with
51
-     * @param commandController Command controller to register commands
52
-     */
53
-    public TimePlugin(
54
-            final ActionController actionController,
55
-            final CommandController commandController) {
56
-        this.actionController = actionController;
57
-        manager = new TimerManager(commandController);
58
-        registerCommand(new TimerCommand(manager, commandController), TimerCommand.INFO);
38
+    @Override
39
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
40
+        super.load(pluginInfo, graph);
41
+        setObjectGraph(graph.plus(new TimeModule()));
42
+        registerCommand(TimerCommand.class, TimerCommand.INFO);
43
+        manager = getObjectGraph().get(TimerManager.class);
59 44
     }
60 45
 
61 46
     /** {@inheritDoc} */
62 47
     @Override
63 48
     public void onLoad() {
64
-        if (!registered) {
65
-            actionController.registerTypes(TimeActionType.values());
66
-            registered = true;
67
-        }
68
-
69
-        final int offset = 60 - Calendar.getInstance().get(Calendar.SECOND);
70
-
71
-        timer = new Timer("Time plugin timer");
72
-
73
-        timer.schedule(new TimerTask() {
74
-            /** {@inheritDoc} */
75
-            @Override
76
-            public void run() {
77
-                runTimer();
78
-            }
79
-        }, 1000 * offset, 1000 * 60);
49
+        manager.load();
80 50
         super.onLoad();
81 51
     }
82 52
 
83
-    /** Handles a timer event that occurs every minute. */
84
-    public void runTimer() {
85
-        final Calendar cal = Calendar.getInstance();
86
-
87
-        actionController.triggerEvent(TimeActionType.TIME_MINUTE, null, cal);
88
-
89
-        if (cal.get(Calendar.MINUTE) == 0) {
90
-            actionController.triggerEvent(TimeActionType.TIME_HOUR, null, cal);
91
-
92
-            if (cal.get(Calendar.HOUR_OF_DAY) == 0) {
93
-                actionController.triggerEvent(TimeActionType.TIME_DAY, null, cal);
94
-            }
95
-        }
96
-    }
97
-
98 53
     /** {@inheritDoc} */
99 54
     @Override
100 55
     public void onUnload() {
101
-        if (timer != null) {
102
-            timer.cancel();
103
-            timer = null;
104
-        }
105
-        manager = null;
106
-
56
+        manager.unload();
107 57
         super.onUnload();
108 58
     }
109 59
 
110
-    /**
111
-     * Returns the manager that is assigned to this Plugin.
112
-     *
113
-     * @return The Instance of TimeManager that is associated with this plugin
114
-     */
115
-    public TimerManager getTimerManager() {
116
-        return manager;
117
-    }
118
-
119 60
 }

+ 1
- 5
src/com/dmdirc/addons/time/TimedCommand.java Parādīt failu

@@ -31,6 +31,7 @@ import com.dmdirc.interfaces.CommandController;
31 31
 import java.util.Timer;
32 32
 import java.util.TimerTask;
33 33
 
34
+
34 35
 /**
35 36
  * Timed command represents a command that has been scheduled by the user.
36 37
  */
@@ -85,8 +86,6 @@ public class TimedCommand extends TimerTask {
85 86
      * Returns the command this timer is due to execute.
86 87
      *
87 88
      * @return Command the timer will run
88
-     *
89
-     * @since 0.6.5
90 89
      */
91 90
     public String getCommand() {
92 91
         return command;
@@ -94,15 +93,12 @@ public class TimedCommand extends TimerTask {
94 93
 
95 94
     /**
96 95
      * Cancels this timer and removes it from the Timer Manager
97
-     *
98
-     * @since 0.6.5
99 96
      */
100 97
     public void cancelTimer() {
101 98
         manager.removeTimer(timerKey);
102 99
         timer.cancel();
103 100
     }
104 101
 
105
-    /** {@inheritDoc} */
106 102
     @Override
107 103
     public void run() {
108 104
         CommandParser parser;

+ 4
- 2
src/com/dmdirc/addons/time/TimerCommand.java Parādīt failu

@@ -35,6 +35,9 @@ import com.dmdirc.ui.input.AdditionalTabTargets;
35 35
 import java.util.Map.Entry;
36 36
 import java.util.Set;
37 37
 
38
+import javax.inject.Inject;
39
+
40
+
38 41
 /**
39 42
  * The timer command allows users to schedule commands to occur after a certain interval, or to
40 43
  * repeatedly occur with a specified delay.
@@ -57,13 +60,13 @@ public class TimerCommand extends Command implements IntelligentCommand {
57 60
      * @param manager           The instance of TimerManager associated with this command
58 61
      * @param commandController The controller to use for command information.
59 62
      */
63
+    @Inject
60 64
     public TimerCommand(final TimerManager manager, final CommandController commandController) {
61 65
         super(commandController);
62 66
 
63 67
         this.manager = manager;
64 68
     }
65 69
 
66
-    /** {@inheritDoc} */
67 70
     @Override
68 71
     public void execute(final FrameContainer origin,
69 72
             final CommandArguments args, final CommandContext context) {
@@ -145,7 +148,6 @@ public class TimerCommand extends Command implements IntelligentCommand {
145 148
                 + "<repetitions> <interval> <command>]");
146 149
     }
147 150
 
148
-    /** {@inheritDoc} */
149 151
     @Override
150 152
     public AdditionalTabTargets getSuggestions(final int arg,
151 153
             final IntelligentCommandContext context) {

+ 57
- 3
src/com/dmdirc/addons/time/TimerManager.java Parādīt failu

@@ -23,17 +23,21 @@
23 23
 package com.dmdirc.addons.time;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26
+import com.dmdirc.interfaces.ActionController;
26 27
 import com.dmdirc.interfaces.CommandController;
27 28
 
29
+import java.util.Calendar;
28 30
 import java.util.HashMap;
29 31
 import java.util.Map;
30 32
 import java.util.Map.Entry;
31 33
 import java.util.Set;
34
+import java.util.Timer;
35
+import java.util.TimerTask;
36
+
37
+import javax.inject.Inject;
32 38
 
33 39
 /**
34 40
  * Class to manage Timers.
35
- *
36
- * @since 0.6.5
37 41
  */
38 42
 public class TimerManager {
39 43
 
@@ -41,9 +45,44 @@ public class TimerManager {
41 45
     private final Map<Integer, TimedCommand> timerList = new HashMap<>();
42 46
     /** The command controller to use when executing global commands. */
43 47
     private final CommandController commandController;
48
+    /** Action controller. */
49
+    private final ActionController actionController;
50
+    /** Have we registered our types already? */
51
+    private static boolean registered;
52
+    /** The timer to use for scheduling. */
53
+    private Timer timer;
44 54
 
45
-    public TimerManager(final CommandController commandController) {
55
+    @Inject
56
+    public TimerManager(final CommandController commandController,
57
+            final ActionController actionController) {
46 58
         this.commandController = commandController;
59
+        this.actionController = actionController;
60
+    }
61
+
62
+    public void load() {
63
+        if (!registered) {
64
+            actionController.registerTypes(TimeActionType.values());
65
+            registered = true;
66
+        }
67
+
68
+        final int offset = 60 - Calendar.getInstance().get(Calendar.SECOND);
69
+
70
+        timer = new Timer("Time plugin timer");
71
+
72
+        timer.schedule(new TimerTask() {
73
+            /** {@inheritDoc} */
74
+            @Override
75
+            public void run() {
76
+                runTimer();
77
+            }
78
+        }, 1000 * offset, 1000 * 60);
79
+    }
80
+
81
+    public void unload() {
82
+        if (timer != null) {
83
+            timer.cancel();
84
+            timer = null;
85
+        }
47 86
     }
48 87
 
49 88
     /**
@@ -128,4 +167,19 @@ public class TimerManager {
128 167
         return timerList.containsKey(id);
129 168
     }
130 169
 
170
+    /** Handles a timer event that occurs every minute. */
171
+    public void runTimer() {
172
+        final Calendar cal = Calendar.getInstance();
173
+
174
+        actionController.triggerEvent(TimeActionType.TIME_MINUTE, null, cal);
175
+
176
+        if (cal.get(Calendar.MINUTE) == 0) {
177
+            actionController.triggerEvent(TimeActionType.TIME_HOUR, null, cal);
178
+
179
+            if (cal.get(Calendar.HOUR_OF_DAY) == 0) {
180
+                actionController.triggerEvent(TimeActionType.TIME_DAY, null, cal);
181
+            }
182
+        }
183
+    }
184
+
131 185
 }

Notiek ielāde…
Atcelt
Saglabāt