123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /*
- * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.commandparser.commands;
-
- import com.dmdirc.commandparser.CommandInfo;
- import com.dmdirc.commandparser.CommandManager;
- import com.dmdirc.commandparser.CommandType;
- import com.dmdirc.ui.interfaces.InputWindow;
-
- /**
- * Represents a generic command.
- * @author chris
- */
- public abstract class Command extends CommandInfo implements Comparable<Command> {
-
- /** The format name used for command output. */
- protected static final String FORMAT_OUTPUT = "commandOutput";
-
- /** The format name used for command errors. */
- protected static final String FORMAT_ERROR = "commandError";
-
- /**
- * Implodes the given list of arguments.
- * @param offset The index to start at
- * @param args The arguments to implode
- * @return A string containing each argument seperated by a space
- */
- protected final String implodeArgs(final int offset, final String... args) {
- String res = "";
- for (int i = offset; i < args.length; i++) {
- if (res.isEmpty()) {
- res = args[i];
- } else {
- res = res.concat(" " + args[i]);
- }
- }
- return res;
- }
-
- /**
- * Implodes the given list of arguments.
- * @param args The arguments to implode
- * @return A string containing each argument seperated by a space
- */
- protected final String implodeArgs(final String... args) {
- return implodeArgs(0, args);
- }
-
- /**
- * Sends a line, if appropriate, to the specified target.
- * @param target The command window to send the line to
- * @param isSilent Whether this command is being silenced or not
- * @param type The type of message to send
- * @param args The arguments of the message
- */
- protected final void sendLine(final InputWindow target,
- final boolean isSilent, final String type, final Object ... args) {
- if (!isSilent && target != null) {
- target.addLine(type, args);
- }
- }
-
- /**
- * Sends a usage line, if appropriate, to the specified target.
- *
- * @param target The command window to send the line to
- * @param isSilent Whether this command is being silenced or not
- * @param name The name of the command that's raising the error
- * @param args The arguments that the command accepts or expects
- */
- protected final void showUsage(final InputWindow target,
- final boolean isSilent, final String name, final String args) {
- sendLine(target, isSilent, "commandUsage", CommandManager.getCommandChar(),
- name, args);
- }
-
- /** {@inheritDoc} */
- @Override
- public final int compareTo(final Command o) {
- return getSignature().compareTo(o.getSignature());
- }
-
- /** {@inheritDoc} */
- @Override
- public CommandType getType() {
- return CommandType.fromCommand(this);
- }
-
- }
|