File Source: Help.java

         /* 
    P/P   *  Method: com.dmdirc.commandparser.commands.global.Help__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 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 com.dmdirc.commandparser.commands.global;
    24  
    25  import com.dmdirc.commandparser.CommandArguments;
    26  import com.dmdirc.commandparser.CommandInfo;
    27  import com.dmdirc.commandparser.CommandManager;
    28  import com.dmdirc.commandparser.commands.Command;
    29  import com.dmdirc.commandparser.commands.GlobalCommand;
    30  import com.dmdirc.commandparser.commands.IntelligentCommand;
    31  import com.dmdirc.ui.input.AdditionalTabTargets;
    32  import com.dmdirc.ui.input.TabCompletionType;
    33  import com.dmdirc.ui.interfaces.InputWindow;
    34  import com.dmdirc.ui.messages.Styliser;
    35  
    36  import java.util.ArrayList;
    37  import java.util.Collections;
    38  import java.util.List;
    39  import java.util.Map;
    40  
    41  /**
    42   * The help command shows the user a list of available commands, along with
    43   * their arguments, and a description. It is context-aware, so channel commands
    44   * are only displayed when in a channel window, for example.
    45   * @author chris
    46   */
    47  public final class Help extends GlobalCommand implements IntelligentCommand {
    48      
    49      /**
    50       * Creates a new instance of Help.
    51       */
    52      public Help() {
                 /* 
    P/P           *  Method: void com.dmdirc.commandparser.commands.global.Help()
                  * 
                  *  Preconditions:
                  *    init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
                  */
    53          super();
    54          
    55          CommandManager.registerCommand(this);
    56      }
    57      
    58      /** {@inheritDoc} */
    59      @Override
    60      public void execute(final InputWindow origin, final boolean isSilent,
    61              final CommandArguments args) {
                 /* 
    P/P           *  Method: void execute(InputWindow, bool, CommandArguments)
                  * 
                  *  Preconditions:
                  *    args != null
                  *    init'ed(args.words)
                  *    (soft) args.line != null
                  *    (soft) init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
                  *    (soft) origin != null
                  * 
                  *  Presumptions:
                  *    getArguments(...).length@65 >= 1
                  *    getArguments(...)[0]@65 != null
                  *    java.util.Arrays:copyOfRange(...)@99 != null
                  * 
                  *  Postconditions:
                  *    args.words != null
                  *    init'ed(java.lang.String:split(...)._tainted)
                  *    java.lang.String:split(...)._tainted == 0
                  *    init'ed(java.lang.String:split(...).length)
                  * 
                  *  Test Vectors:
                  *    getArguments(...).length@62: {1..+Inf}, {0}
                  */
    62          if (args.getArguments().length == 0) {
    63              showAllCommands(origin, isSilent);
    64          } else {
    65              showCommand(origin, isSilent, args.getArguments()[0]);
    66          }
    67      }
    68      
    69      /**
    70       * Shows a list of all commands valid for the current window.
    71       * 
    72       * @param origin The window the command was executed in
    73       * @param isSilent Whether this command has been silenced or not
    74       */
    75      private void showAllCommands(final InputWindow origin, final boolean isSilent) {
                 /* 
    P/P           *  Method: void showAllCommands(InputWindow, bool)
                  * 
                  *  Preconditions:
                  *    origin != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.ui.interfaces.InputWindow:getCommandParser(...)@76 != null
                  *    java.util.Iterator:next(...)@86 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.StringBuilder:length(...)@90: {-231..0}, {1..232-1}
                  *    java.lang.StringBuilder:length(...)@97: {-231..0}, {1..232-1}
                  *    java.util.Iterator:hasNext(...)@86: {0}, {1}
                  */
    76          final List<String> commands = new ArrayList<String>(origin.getCommandParser()
    77                  .getCommands().keySet());
    78  
    79          Collections.sort(commands);
    80          
    81          sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
    82                  + "----------------------- Available commands -------");
    83          
    84          final StringBuilder builder = new StringBuilder();
    85          
    86          for (String command : commands) {
    87              if (builder.length() + command.length() + 1 > 50) {
    88                  sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
    89                  builder.delete(0, builder.length());
    90              } else if (builder.length() > 0) {
    91                  builder.append(' ');
    92              }
    93              
    94              builder.append(command);
    95          }
    96          
    97          if (builder.length() > 0) {
    98              sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
    99          }
   100          
   101          sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
   102                  + "--------------------------------------------------");
   103      }
   104      
   105      /**
   106       * Shows information about the specified command.
   107       * 
   108       * @param origin The window the command was executed in
   109       * @param isSilent Whether this command has been silenced or not
   110       * @param name The name of the command to display info for
   111       */
   112      private void showCommand(final InputWindow origin, final boolean isSilent,
   113              final String name) {
                 /* 
    P/P           *  Method: void showCommand(InputWindow, bool, String)
                  * 
                  *  Preconditions:
                  *    name != null
                  *    (soft) init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
                  * 
                  *  Presumptions:
                  *    java.util.Map_Entry:getKey(...)@129 != null
                  *    java.util.Map_Entry:getKey(...)@131 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:length(...)@116: {0}, {1..232-1}
                  */
   114          Map.Entry<CommandInfo, Command> command = null;
   115  
   116          if (name.length() > 0 && name.charAt(0) == CommandManager.getCommandChar()) {
   117              command = CommandManager.getCommand(name.substring(1));
   118          } else {
   119              command = CommandManager.getCommand(name);
   120          }
   121          
   122          if (command == null) {
   123              sendLine(origin, isSilent, FORMAT_ERROR, "Command '" + name + "' not found.");
   124          } else {
   125              sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
   126                      + "---------------------- Command information -------");            
   127              sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
   128                      + " Name: " + name);
   129              sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
   130                      + " Type: " + command.getKey().getType());
   131              sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
   132                      + "Usage: " + command.getKey().getHelp());
   133              sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
   134                      + "--------------------------------------------------");            
   135          }
   136      }
   137      
   138      /** {@inheritDoc}. */
   139      @Override
   140      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;"help"
                  */
   141          return "help";
   142      }
   143      
   144      /** {@inheritDoc}. */
   145      @Override
   146      public boolean showInHelp() {
                 /* 
    P/P           *  Method: bool showInHelp()
                  * 
                  *  Postconditions:
                  *    return_value == 1
                  */
   147          return true;
   148      }
   149      
   150      /** {@inheritDoc}. */
   151      @Override
   152      public String getHelp() {
                 /* 
    P/P           *  Method: String getHelp()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;"help [command] - shows client command help"
                  */
   153          return "help [command] - shows client command help";
   154      }
   155  
   156      /** {@inheritDoc} */
   157      @Override
   158      public AdditionalTabTargets getSuggestions(final int arg, final List<String> previousArgs) {
                 /* 
    P/P           *  Method: AdditionalTabTargets getSuggestions(int, List)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.ui.input.AdditionalTabTargets:excludeAll(...)@159 != null
                  *    init'ed(com.dmdirc.ui.input.TabCompletionType.COMMAND)
                  * 
                  *  Postconditions:
                  *    return_value != null
                  * 
                  *  Test Vectors:
                  *    arg: {-231..-1, 1..232-1}, {0}
                  */
   159          final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
   160  
   161          if (arg == 0) {
   162              res.include(TabCompletionType.COMMAND);
   163          }
   164          
   165          return res;
   166      } 
   167      
   168  }








SofCheck Inspector Build Version : 2.17854
Help.java 2009-Jun-25 01:54:24
Help.class 2009-Sep-02 17:04:16