|
@@ -1,162 +1,15 @@
|
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
|
1
|
package com.dmdirc.commandparser.commands;
|
24
|
2
|
|
25
|
|
-import com.dmdirc.commandparser.CommandArguments;
|
26
|
|
-import com.dmdirc.commandparser.commands.context.CommandContext;
|
27
|
|
-import com.dmdirc.events.CommandErrorEvent;
|
28
|
|
-import com.dmdirc.events.CommandOutputEvent;
|
29
|
3
|
import com.dmdirc.interfaces.CommandController;
|
30
|
|
-import com.dmdirc.interfaces.WindowModel;
|
31
|
|
-import com.dmdirc.ui.messages.IRCControlCodes;
|
32
|
|
-
|
33
|
|
-import javax.annotation.Nonnull;
|
34
|
|
-import javax.annotation.Nullable;
|
35
|
4
|
|
36
|
5
|
/**
|
37
|
|
- * Represents a generic command.
|
|
6
|
+ * @deprecated Use {@link BaseCommand} directly.
|
38
|
7
|
*/
|
39
|
|
-public abstract class Command {
|
40
|
|
-
|
41
|
|
- /** The controller this command is associated with. */
|
42
|
|
- private final CommandController controller;
|
|
8
|
+@Deprecated
|
|
9
|
+public abstract class Command extends BaseCommand {
|
43
|
10
|
|
44
|
|
- public Command(final CommandController controller) {
|
45
|
|
- this.controller = controller;
|
|
11
|
+ public Command(CommandController controller) {
|
|
12
|
+ super(controller);
|
46
|
13
|
}
|
47
|
14
|
|
48
|
|
- protected CommandController getController() {
|
49
|
|
- return controller;
|
50
|
|
- }
|
51
|
|
-
|
52
|
|
- /**
|
53
|
|
- * Sends an output line, if appropriate, to the specified target.
|
54
|
|
- *
|
55
|
|
- * @param target The command window to send the line to
|
56
|
|
- * @param isSilent Whether this command is being silenced or not
|
57
|
|
- * @param message The output to send
|
58
|
|
- */
|
59
|
|
- protected final void showOutput(@Nullable final WindowModel target,
|
60
|
|
- final boolean isSilent, final String message) {
|
61
|
|
- if (!isSilent && target != null) {
|
62
|
|
- target.getEventBus().publishAsync(new CommandOutputEvent(target, message));
|
63
|
|
- }
|
64
|
|
- }
|
65
|
|
-
|
66
|
|
- /**
|
67
|
|
- * Sends an error line, if appropriate, to the specified target.
|
68
|
|
- *
|
69
|
|
- * @param target The command window to send the line to
|
70
|
|
- * @param isSilent Whether this command is being silenced or not
|
71
|
|
- * @param message The error message to send
|
72
|
|
- */
|
73
|
|
- protected final void showError(@Nullable final WindowModel target,
|
74
|
|
- final boolean isSilent, final String message) {
|
75
|
|
- if (!isSilent && target != null) {
|
76
|
|
- target.getEventBus().publishAsync(new CommandErrorEvent(target, message));
|
77
|
|
- }
|
78
|
|
- }
|
79
|
|
-
|
80
|
|
- /**
|
81
|
|
- * Sends a usage line, if appropriate, to the specified target.
|
82
|
|
- *
|
83
|
|
- * @param target The command window to send the line to
|
84
|
|
- * @param isSilent Whether this command is being silenced or not
|
85
|
|
- * @param name The name of the command that's raising the error
|
86
|
|
- * @param args The arguments that the command accepts or expects
|
87
|
|
- */
|
88
|
|
- protected final void showUsage(@Nullable final WindowModel target,
|
89
|
|
- final boolean isSilent, final String name, final String args) {
|
90
|
|
- if (!isSilent && target != null) {
|
91
|
|
- target.getEventBus().publishAsync(new CommandErrorEvent(target,
|
92
|
|
- "Usage: " + controller.getCommandChar() + name + ' ' + args));
|
93
|
|
- }
|
94
|
|
- }
|
95
|
|
-
|
96
|
|
- /**
|
97
|
|
- * Formats the specified data into a table suitable for output in the textpane. It is expected
|
98
|
|
- * that each String[] in data has the same number of elements as the headers array.
|
99
|
|
- *
|
100
|
|
- * @param headers The headers of the table.
|
101
|
|
- * @param data The contents of the table.
|
102
|
|
- *
|
103
|
|
- * @return A string containing an ASCII table
|
104
|
|
- */
|
105
|
|
- protected static String doTable(final String[] headers, final String[][] data) {
|
106
|
|
- final StringBuilder res = new StringBuilder();
|
107
|
|
- res.append(IRCControlCodes.FIXED);
|
108
|
|
- res.append(IRCControlCodes.BOLD);
|
109
|
|
-
|
110
|
|
- final int[] maxsizes = new int[headers.length];
|
111
|
|
-
|
112
|
|
- for (int i = 0; i < headers.length; i++) {
|
113
|
|
- maxsizes[i] = headers[i].length() + 3;
|
114
|
|
-
|
115
|
|
- for (String[] row : data) {
|
116
|
|
- maxsizes[i] = Math.max(maxsizes[i], row[i].length() + 3);
|
117
|
|
- }
|
118
|
|
-
|
119
|
|
- doPadding(res, headers[i], maxsizes[i]);
|
120
|
|
- }
|
121
|
|
-
|
122
|
|
- for (String[] source : data) {
|
123
|
|
- res.append('\n');
|
124
|
|
- res.append(IRCControlCodes.FIXED);
|
125
|
|
-
|
126
|
|
- for (int i = 0; i < source.length; i++) {
|
127
|
|
- doPadding(res, source[i], maxsizes[i]);
|
128
|
|
- }
|
129
|
|
- }
|
130
|
|
-
|
131
|
|
- return res.toString();
|
132
|
|
- }
|
133
|
|
-
|
134
|
|
- /**
|
135
|
|
- * Adds the specified data to the stringbuilder, padding with spaces to the specified size.
|
136
|
|
- *
|
137
|
|
- * @param builder The stringbuilder to append data to
|
138
|
|
- * @param data The data to be added
|
139
|
|
- * @param size The minimum size that should be used
|
140
|
|
- */
|
141
|
|
- private static void doPadding(final StringBuilder builder, final String data,
|
142
|
|
- final int size) {
|
143
|
|
- builder.append(data);
|
144
|
|
-
|
145
|
|
- for (int i = 0; i < size - data.length(); i++) {
|
146
|
|
- builder.append(' ');
|
147
|
|
- }
|
148
|
|
- }
|
149
|
|
-
|
150
|
|
- /**
|
151
|
|
- * Executes this command.
|
152
|
|
- *
|
153
|
|
- * @param origin The container which received the command
|
154
|
|
- * @param args Arguments passed to this command
|
155
|
|
- * @param context The context the command was executed in
|
156
|
|
- *
|
157
|
|
- * @since 0.6.4
|
158
|
|
- */
|
159
|
|
- public abstract void execute(@Nonnull WindowModel origin, CommandArguments args,
|
160
|
|
- CommandContext context);
|
161
|
|
-
|
162
|
15
|
}
|