소스 검색

Initial global commands support (may be broken, can't test until svn removes the moved files...)

git-svn-id: http://svn.dmdirc.com/trunk@1239 00569f92-eb28-0410-84fd-f71c24880f
tags/0.4
Chris Smith 17 년 전
부모
커밋
ec1aece30c

+ 2
- 1
src/uk/org/ownage/dmdirc/commandparser/ChannelCommandParser.java 파일 보기

@@ -55,8 +55,9 @@ public final class ChannelCommandParser extends CommandParser {
55 55
     
56 56
     /** Loads the relevant commands into the parser. */
57 57
     protected void loadCommands() {
58
-        CommandManager.loadChannelCommands(this);
58
+        CommandManager.loadGlobalCommands(this);
59 59
         CommandManager.loadServerCommands(this);
60
+        CommandManager.loadChannelCommands(this);
60 61
     }
61 62
     
62 63
     /**

+ 59
- 4
src/uk/org/ownage/dmdirc/commandparser/CommandManager.java 파일 보기

@@ -29,6 +29,7 @@ import uk.org.ownage.dmdirc.Config;
29 29
 import uk.org.ownage.dmdirc.Server;
30 30
 import uk.org.ownage.dmdirc.ServerManager;
31 31
 import uk.org.ownage.dmdirc.commandparser.commands.channel.*;
32
+import uk.org.ownage.dmdirc.commandparser.commands.global.*;
32 33
 import uk.org.ownage.dmdirc.commandparser.commands.query.*;
33 34
 import uk.org.ownage.dmdirc.commandparser.commands.server.*;
34 35
 import uk.org.ownage.dmdirc.logger.ErrorLevel;
@@ -41,6 +42,10 @@ import uk.org.ownage.dmdirc.logger.Logger;
41 42
  */
42 43
 public final class CommandManager {
43 44
     
45
+    /**
46
+     * The global commands that have been instansiated.
47
+     */
48
+    private static List<Command> globalCommands;
44 49
     /**
45 50
      * The server commands that have been instansiated.
46 51
      */
@@ -53,6 +58,10 @@ public final class CommandManager {
53 58
      * The query commands that have been instansiated.
54 59
      */
55 60
     private static List<Command> queryCommands;
61
+    /**
62
+     * The parsers that have requested global commands.
63
+     */
64
+    private static List<CommandParser> globalParsers;
56 65
     /**
57 66
      * The parsers that have requested server commands.
58 67
      */
@@ -99,6 +108,9 @@ public final class CommandManager {
99 108
         } else if (command instanceof QueryCommand) {
100 109
             target = queryParsers;
101 110
             queryCommands.add(command);
111
+        } else if (command instanceof GlobalCommand) {
112
+            target = globalParsers;
113
+            globalCommands.add(command);
102 114
         } else {
103 115
             Logger.error(ErrorLevel.ERROR, "Attempted to register an invalid command: "
104 116
                     + command.getClass().getName());
@@ -116,7 +128,7 @@ public final class CommandManager {
116 128
             final String commandName = Config.getCommandChar() + command.getName();
117 129
             
118 130
             for (Server server : ServerManager.getServerManager().getServers()) {
119
-                if (command instanceof ServerCommand) {
131
+                if (command instanceof ServerCommand || command instanceof GlobalCommand) {
120 132
                     server.getTabCompleter().addEntry(commandName);
121 133
                 } else if (command instanceof ChannelCommand) {
122 134
                     for (String channelName : server.getChannels()) {
@@ -151,6 +163,9 @@ public final class CommandManager {
151 163
         } else if (command instanceof QueryCommand) {
152 164
             target = queryParsers;
153 165
             queryCommands.remove(command);
166
+        } else if (command instanceof GlobalCommand) {
167
+            target = globalParsers;
168
+            globalCommands.remove(command);
154 169
         } else {
155 170
             Logger.error(ErrorLevel.ERROR, "Attempted to unregister an invalid command: "
156 171
                     + command.getClass().getName());
@@ -168,7 +183,7 @@ public final class CommandManager {
168 183
             final String commandName = Config.getCommandChar() + command.getName();
169 184
             
170 185
             for (Server server : ServerManager.getServerManager().getServers()) {
171
-                if (command instanceof ServerCommand) {
186
+                if (command instanceof ServerCommand || command instanceof GlobalCommand) {
172 187
                     server.getTabCompleter().removeEntry(commandName);
173 188
                 } else if (command instanceof ChannelCommand) {
174 189
                     for (String channelName : server.getChannels()) {
@@ -214,10 +229,12 @@ public final class CommandManager {
214 229
         channelCommands = new ArrayList<Command>();
215 230
         serverCommands = new ArrayList<Command>();
216 231
         queryCommands = new ArrayList<Command>();
232
+        globalCommands = new ArrayList<Command>();
217 233
         
218 234
         channelParsers = new ArrayList<CommandParser>();
219 235
         serverParsers = new ArrayList<CommandParser>();
220 236
         queryParsers = new ArrayList<CommandParser>();
237
+        globalParsers = new ArrayList<CommandParser>();
221 238
         
222 239
         channelPopupCommands = new ArrayList<Command>();
223 240
         
@@ -266,8 +283,6 @@ public final class CommandManager {
266 283
         new Nick();
267 284
         new Notice();
268 285
         new Query();
269
-        new Exit();
270
-        new ExitDefault();
271 286
         new Raw();
272 287
         new Reconnect();
273 288
         new ReloadActions();
@@ -279,6 +294,10 @@ public final class CommandManager {
279 294
         // Query commands
280 295
         new QueryMe();
281 296
         new QueryMeEmpty();
297
+        
298
+        // Global commands
299
+        new Exit();
300
+        new ExitDefault();
282 301
     }
283 302
     
284 303
     /**
@@ -313,6 +332,22 @@ public final class CommandManager {
313 332
         serverParsers.add(parser);
314 333
     }
315 334
     
335
+    /**
336
+     * Loads all global commands into the specified parser.
337
+     * @param parser The parser to load commands into
338
+     */
339
+    public static void loadGlobalCommands(final CommandParser parser) {
340
+        if (globalCommands == null) {
341
+            initLists();
342
+        }
343
+        
344
+        for (Command com : globalCommands) {
345
+            parser.registerCommand(com);
346
+        }
347
+        
348
+        globalParsers.add(parser);
349
+    }
350
+    
316 351
     /**
317 352
      * Loads all query commands into the specified parser.
318 353
      * @param parser The parser to load commands into
@@ -349,6 +384,26 @@ public final class CommandManager {
349 384
         return null;
350 385
     }
351 386
     
387
+    /**
388
+     * Retrieves the global command identified by the specified signature.
389
+     * @param signature The signature to look for
390
+     * @return A global command with a matching signature, or null if none
391
+     * were found.
392
+     */
393
+    public static GlobalCommand getGlobalCommand(final String signature) {
394
+        if (globalCommands == null) {
395
+            initLists();
396
+        }
397
+        
398
+        for (Command com : globalCommands) {
399
+            if (com.getSignature().equalsIgnoreCase(signature)) {
400
+                return (GlobalCommand) com;
401
+            }
402
+        }
403
+        
404
+        return null;
405
+    }
406
+    
352 407
     /**
353 408
      * Retrieves the channel command identified by the specified signature.
354 409
      * @param signature The signature to look for

+ 39
- 0
src/uk/org/ownage/dmdirc/commandparser/GlobalCommand.java 파일 보기

@@ -0,0 +1,39 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
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 uk.org.ownage.dmdirc.commandparser;
24
+
25
+/**
26
+ * Represents a generic global command. Global commands are associated with
27
+ * no servers.
28
+ * @author chris
29
+ */
30
+public abstract class GlobalCommand extends Command {
31
+        
32
+    /**
33
+     * Executes this command. Note that for global commands, origin may be
34
+     * null.
35
+     * @param origin The window in which the command was typed
36
+     * @param args Arguments passed to this command
37
+     */
38
+    public abstract void execute(CommandWindow origin, String ... args);
39
+}

+ 77
- 0
src/uk/org/ownage/dmdirc/commandparser/GlobalCommandParser.java 파일 보기

@@ -0,0 +1,77 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
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 uk.org.ownage.dmdirc.commandparser;
24
+
25
+import uk.org.ownage.dmdirc.Server;
26
+import uk.org.ownage.dmdirc.logger.ErrorLevel;
27
+import uk.org.ownage.dmdirc.logger.Logger;
28
+
29
+/**
30
+ * The command parser used for global commands.
31
+ * @author chris
32
+ */
33
+public final class GlobalCommandParser extends CommandParser {
34
+    
35
+    /**
36
+     * The singleton instance of this command parser.
37
+     */
38
+    private static GlobalCommandParser me;
39
+    
40
+    /**
41
+     * Creates a new instance of the GlobalCommandParser.
42
+     */
43
+    private GlobalCommandParser() {
44
+        super();
45
+    }
46
+    
47
+    /** Loads the relevant commands into the parser. */
48
+    protected void loadCommands() {
49
+        CommandManager.loadGlobalCommands(this);
50
+    }
51
+    
52
+    /**
53
+     * Executes the specified command with the given arguments.
54
+     * @param origin The window in which the command was typed
55
+     * @param command The command to be executed
56
+     * @param args The arguments to the command
57
+     */
58
+    protected void executeCommand(final CommandWindow origin, 
59
+            final Command command, final String... args) {
60
+        ((GlobalCommand) command).execute(origin, args);
61
+    }
62
+        
63
+    /**
64
+     * Called when the input was a line of text that was not a command. This normally
65
+     * means it is sent to the server/channel/user as-is, with no further processing.
66
+     * @param origin The window in which the command was typed
67
+     * @param line The line input by the user
68
+     */
69
+    protected void handleNonCommand(final CommandWindow origin, final String line) {
70
+        if (origin == null) {
71
+            Logger.error(ErrorLevel.WARNING, "Invalid global command: " + line);
72
+        } else {
73
+            origin.addLine("commandError", "Invalid global command: " + line);
74
+        }
75
+    }
76
+    
77
+}

+ 4
- 3
src/uk/org/ownage/dmdirc/commandparser/QueryCommandParser.java 파일 보기

@@ -55,8 +55,9 @@ public final class QueryCommandParser extends CommandParser {
55 55
     
56 56
     /** Loads the relevant commands into the parser. */
57 57
     protected void loadCommands() {
58
-        CommandManager.loadQueryCommands(this);
58
+        CommandManager.loadGlobalCommands(this);
59 59
         CommandManager.loadServerCommands(this);
60
+        CommandManager.loadQueryCommands(this);
60 61
     }
61 62
     
62 63
     /**
@@ -65,7 +66,7 @@ public final class QueryCommandParser extends CommandParser {
65 66
      * @param command The command to be executed
66 67
      * @param args The arguments to the command
67 68
      */
68
-    protected void executeCommand(final CommandWindow origin, 
69
+    protected void executeCommand(final CommandWindow origin,
69 70
             final Command command, final String... args) {
70 71
         if (command instanceof QueryCommand) {
71 72
             ((QueryCommand) command).execute(origin, server, query, args);
@@ -73,7 +74,7 @@ public final class QueryCommandParser extends CommandParser {
73 74
             ((ServerCommand) command).execute(origin, server, args);
74 75
         }
75 76
     }
76
-        
77
+    
77 78
     /**
78 79
      * Called when the input was a line of text that was not a command. This normally
79 80
      * means it is sent to the server/channel/user as-is, with no further processing.

+ 1
- 0
src/uk/org/ownage/dmdirc/commandparser/ServerCommandParser.java 파일 보기

@@ -47,6 +47,7 @@ public final class ServerCommandParser extends CommandParser {
47 47
     
48 48
     /** Loads the relevant commands into the parser. */
49 49
     protected void loadCommands() {
50
+        CommandManager.loadGlobalCommands(this);
50 51
         CommandManager.loadServerCommands(this);
51 52
     }
52 53
     

src/uk/org/ownage/dmdirc/commandparser/commands/server/Exit.java → src/uk/org/ownage/dmdirc/commandparser/commands/global/Exit.java 파일 보기

@@ -20,13 +20,12 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package uk.org.ownage.dmdirc.commandparser.commands.server;
23
+package uk.org.ownage.dmdirc.commandparser.commands.global;
24 24
 
25 25
 import uk.org.ownage.dmdirc.Main;
26
-import uk.org.ownage.dmdirc.Server;
27 26
 import uk.org.ownage.dmdirc.commandparser.CommandManager;
28 27
 import uk.org.ownage.dmdirc.commandparser.CommandWindow;
29
-import uk.org.ownage.dmdirc.commandparser.ServerCommand;
28
+import uk.org.ownage.dmdirc.commandparser.GlobalCommand;
30 29
 
31 30
 /**
32 31
  * The exit command allows the user to quit DMDirc with a custom quit message.
@@ -34,7 +33,7 @@ import uk.org.ownage.dmdirc.commandparser.ServerCommand;
34 33
  * supplied) and saves the config file.
35 34
  * @author chris
36 35
  */
37
-public final class Exit extends ServerCommand {
36
+public final class Exit extends GlobalCommand {
38 37
     
39 38
     /**
40 39
      * Creates a new instance of Exit.
@@ -45,14 +44,8 @@ public final class Exit extends ServerCommand {
45 44
         CommandManager.registerCommand(this);
46 45
     }
47 46
     
48
-    /**
49
-     * Executes this command.
50
-     * @param origin The frame in which this command was issued
51
-     * @param server The server object that this command is associated with
52
-     * @param args The user supplied arguments
53
-     */
54
-    public void execute(final CommandWindow origin, final Server server,
55
-            final String... args) {
47
+    /** {@inheritDoc}  */
48
+    public void execute(final CommandWindow origin, final String... args) {
56 49
         Main.quit(implodeArgs(args));
57 50
     }
58 51
     

src/uk/org/ownage/dmdirc/commandparser/commands/server/ExitDefault.java → src/uk/org/ownage/dmdirc/commandparser/commands/global/ExitDefault.java 파일 보기

@@ -20,12 +20,12 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package uk.org.ownage.dmdirc.commandparser.commands.server;
23
+package uk.org.ownage.dmdirc.commandparser.commands.global;
24 24
 
25
-import uk.org.ownage.dmdirc.Server;
25
+import uk.org.ownage.dmdirc.Config;
26 26
 import uk.org.ownage.dmdirc.commandparser.CommandManager;
27 27
 import uk.org.ownage.dmdirc.commandparser.CommandWindow;
28
-import uk.org.ownage.dmdirc.commandparser.ServerCommand;
28
+import uk.org.ownage.dmdirc.commandparser.GlobalCommand;
29 29
 
30 30
 /**
31 31
  * Represents the exit/0 command (i.e., an exit with no arguments). Reads the
@@ -33,7 +33,7 @@ import uk.org.ownage.dmdirc.commandparser.ServerCommand;
33 33
  * with it as an argument.
34 34
  * @author chris
35 35
  */
36
-public final class ExitDefault extends ServerCommand {
36
+public final class ExitDefault extends GlobalCommand {
37 37
     
38 38
     /**
39 39
      * Creates a new instance of ExitDefault.
@@ -44,16 +44,17 @@ public final class ExitDefault extends ServerCommand {
44 44
         CommandManager.registerCommand(this);
45 45
     }
46 46
     
47
-    /**
48
-     * Executes this command.
49
-     * @param origin The frame in which this command was issued
50
-     * @param server The server object that this command is associated with
51
-     * @param args The user supplied arguments
52
-     */
53
-    public void execute(final CommandWindow origin, final Server server,
54
-            final String... args) {
55
-        final String def = origin.getConfigManager().getOption("general", "closemessage");
56
-        CommandManager.getServerCommand("exit").execute(origin, server, def);
47
+    /** {@inheritDoc}  */
48
+    public void execute(final CommandWindow origin, final String... args) {
49
+        String def;
50
+        
51
+        if (origin == null) {
52
+            def = Config.getOption("general", "closemessage");
53
+        } else {
54
+            def = origin.getConfigManager().getOption("general", "closemessage");
55
+        }
56
+        
57
+        CommandManager.getGlobalCommand("exit").execute(origin, def);
57 58
     }
58 59
     
59 60
     

Loading…
취소
저장