You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Help.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2006-2008 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. package com.dmdirc.commandparser.commands.global;
  23. import com.dmdirc.Server;
  24. import com.dmdirc.commandparser.commands.Command;
  25. import com.dmdirc.commandparser.CommandManager;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.commands.ServerCommand;
  28. import com.dmdirc.ui.interfaces.ChannelWindow;
  29. import com.dmdirc.ui.interfaces.InputWindow;
  30. import com.dmdirc.ui.interfaces.QueryWindow;
  31. import com.dmdirc.ui.interfaces.ServerWindow;
  32. import com.dmdirc.ui.messages.Styliser;
  33. import java.util.ArrayList;
  34. import java.util.Collections;
  35. import java.util.List;
  36. /**
  37. * The help command shows the user a list of available commands, along with
  38. * their arguments, and a description. It is context-aware, so channel commands
  39. * are only displayed when in a channel window, for example.
  40. * @author chris
  41. */
  42. public final class Help extends ServerCommand {
  43. /**
  44. * Creates a new instance of Help.
  45. */
  46. public Help() {
  47. super();
  48. CommandManager.registerCommand(this);
  49. }
  50. /**
  51. * Executes this command.
  52. * @param origin The frame in which this command was issued
  53. * @param server The server object that this command is associated with
  54. * @param isSilent Whether this command is silenced or not
  55. * @param args The user supplied arguments
  56. */
  57. public void execute(final InputWindow origin, final Server server,
  58. final boolean isSilent, final String... args) {
  59. if (args.length == 0) {
  60. showAllCommands(origin, isSilent);
  61. } else {
  62. showCommand(origin, isSilent, args[0]);
  63. }
  64. }
  65. private void showAllCommands(final InputWindow origin, final boolean isSilent) {
  66. final List<Command> commands = new ArrayList<Command>();
  67. commands.addAll(CommandManager.getCommands(CommandType.TYPE_GLOBAL));
  68. if (origin instanceof ServerWindow) {
  69. commands.addAll(CommandManager.getCommands(CommandType.TYPE_SERVER));
  70. } else if (origin instanceof ChannelWindow) {
  71. commands.addAll(CommandManager.getCommands(CommandType.TYPE_CHANNEL));
  72. commands.addAll(CommandManager.getCommands(CommandType.TYPE_CHAT));
  73. commands.addAll(CommandManager.getCommands(CommandType.TYPE_SERVER));
  74. } else if (origin instanceof QueryWindow) {
  75. commands.addAll(CommandManager.getCommands(CommandType.TYPE_QUERY));
  76. commands.addAll(CommandManager.getCommands(CommandType.TYPE_CHAT));
  77. commands.addAll(CommandManager.getCommands(CommandType.TYPE_SERVER));
  78. }
  79. Collections.sort(commands);
  80. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  81. + "----------------------- Available commands -------");
  82. final StringBuilder builder = new StringBuilder();
  83. for (Command command : commands) {
  84. if (builder.length() + command.getName().length() + 1 > 50) {
  85. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
  86. builder.delete(0, builder.length());
  87. } else if (builder.length() > 0) {
  88. builder.append(' ');
  89. }
  90. builder.append(command.getName());
  91. }
  92. if (builder.length() > 0) {
  93. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
  94. }
  95. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  96. + "--------------------------------------------------");
  97. }
  98. private void showCommand(final InputWindow origin, final boolean isSilent,
  99. final String name) {
  100. final Command command = CommandManager.getCommand(name);
  101. if (command == null) {
  102. sendLine(origin, isSilent, FORMAT_ERROR, "Command '" + name + "' not found.");
  103. } else {
  104. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  105. + "---------------------- Command information -------");
  106. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  107. + " Name: " + name);
  108. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  109. + " Type: " + command.getType());
  110. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  111. + "Usage: " + command.getHelp());
  112. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  113. + "--------------------------------------------------");
  114. }
  115. }
  116. /** {@inheritDoc}. */
  117. public String getName() {
  118. return "help";
  119. }
  120. /** {@inheritDoc}. */
  121. public boolean showInHelp() {
  122. return true;
  123. }
  124. /** {@inheritDoc}. */
  125. public String getHelp() {
  126. return "help [command] - shows client command help";
  127. }
  128. }