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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. 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 java.util.Collections;
  33. import java.util.List;
  34. /**
  35. * The help command shows the user a list of available commands, along with
  36. * their arguments, and a description. It is context-aware, so channel commands
  37. * are only displayed when in a channel window, for example.
  38. * @author chris
  39. */
  40. public final class Help extends ServerCommand {
  41. /**
  42. * Creates a new instance of Help.
  43. */
  44. public Help() {
  45. super();
  46. CommandManager.registerCommand(this);
  47. }
  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 isSilent Whether this command is silenced or not
  53. * @param args The user supplied arguments
  54. */
  55. public void execute(final InputWindow origin, final Server server,
  56. final boolean isSilent, final String... args) {
  57. sendLine(origin, isSilent, FORMAT_OUTPUT, "-- Global commands ----------------------------------");
  58. showCommands(CommandManager.getCommands(CommandType.TYPE_GLOBAL), origin, isSilent);
  59. if (origin instanceof ServerWindow || origin instanceof ChannelWindow
  60. || origin instanceof QueryWindow) {
  61. sendLine(origin, isSilent, FORMAT_OUTPUT, "-- Server commands ----------------------------------");
  62. showCommands(CommandManager.getCommands(CommandType.TYPE_SERVER), origin, isSilent);
  63. }
  64. if (origin instanceof ChannelWindow) {
  65. sendLine(origin, isSilent, FORMAT_OUTPUT, "-- Channel commands ---------------------------------");
  66. showCommands(CommandManager.getCommands(CommandType.TYPE_CHANNEL), origin, isSilent);
  67. }
  68. if (origin instanceof QueryWindow) {
  69. sendLine(origin, isSilent, FORMAT_OUTPUT, "-- Query commands -----------------------------------");
  70. showCommands(CommandManager.getCommands(CommandType.TYPE_QUERY), origin, isSilent);
  71. }
  72. sendLine(origin, isSilent, FORMAT_OUTPUT, "-----------------------------------------------------");
  73. }
  74. /**
  75. * Shows the user the commands from the specified list.
  76. * @param commands The commands to be displayed
  77. * @param origin The window to output to
  78. * @param isSilent Whether this command is silent or not
  79. */
  80. private void showCommands(final List<Command> commands,
  81. final InputWindow origin, final boolean isSilent) {
  82. Collections.sort(commands);
  83. for (Command com : commands) {
  84. if (com.showInHelp()) {
  85. sendLine(origin, isSilent, FORMAT_OUTPUT, com.getHelp());
  86. }
  87. }
  88. }
  89. /** {@inheritDoc}. */
  90. public String getName() {
  91. return "help";
  92. }
  93. /** {@inheritDoc}. */
  94. public boolean showInHelp() {
  95. return true;
  96. }
  97. /** {@inheritDoc}. */
  98. public boolean isPolyadic() {
  99. return false;
  100. }
  101. /** {@inheritDoc}. */
  102. public int getArity() {
  103. return 0;
  104. }
  105. /** {@inheritDoc}. */
  106. public String getHelp() {
  107. return "help - shows all available client commands";
  108. }
  109. }