選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Debug.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2006-2011 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.addons.debug;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandInfo;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.commands.Command;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.commandparser.commands.context.CommandContext;
  30. import com.dmdirc.ui.input.AdditionalTabTargets;
  31. import java.util.Arrays;
  32. /**
  33. * Provides various handy ways to test or debug the client.
  34. */
  35. public class Debug extends Command implements IntelligentCommand, CommandInfo {
  36. /** Parent debug plugin. */
  37. private final DebugPlugin plugin;
  38. /**
  39. * Creates a new debug command with the specified parent plugin.
  40. *
  41. * @param plugin Parent debug plugin
  42. */
  43. public Debug(final DebugPlugin plugin) {
  44. super();
  45. this.plugin = plugin;
  46. }
  47. /** {@inheritDoc} */
  48. @Override
  49. public void execute(final FrameContainer<?> origin,
  50. final CommandArguments args, final CommandContext context) {
  51. if (args.getArguments().length == 0) {
  52. showUsage(origin, args.isSilent(), "debug",
  53. "<debug command> [options]");
  54. } else {
  55. final DebugCommand command = plugin.getCommand(
  56. args.getArguments()[0]);
  57. if (command == null) {
  58. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  59. "Unknown debug action.");
  60. } else {
  61. final CommandArguments newArgs = new CommandArguments(
  62. Arrays.asList(("/" + command.getName() + " "
  63. + args.getArgumentsAsString(1)).split(" ")));
  64. command.execute(origin, newArgs, context);
  65. }
  66. }
  67. }
  68. /**
  69. * Sends a line, if appropriate, to the specified target.
  70. *
  71. * @param target The command window to send the line to
  72. * @param isSilent Whether this command is being silenced or not
  73. * @param type The type of message to send
  74. * @param args The arguments of the message
  75. */
  76. public void proxySendLine(final FrameContainer<?> target,
  77. final boolean isSilent, final String type, final Object ... args) {
  78. sendLine(target, isSilent, type, args);
  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. public void proxyShowUsage(final FrameContainer<?> target,
  89. final boolean isSilent, final String name, final String args) {
  90. showUsage(target, isSilent, getName(), name + " " + args);
  91. }
  92. /**
  93. * Formats the specified data into a table suitable for output in the
  94. * textpane. It is expected that each String[] in data has the same number
  95. * of elements as the headers array.
  96. *
  97. * @param headers The headers of the table.
  98. * @param data The contents of the table.
  99. * @return A string containing an ASCII table
  100. */
  101. public String proxyDoTable(final String[] headers, final String[][] data) {
  102. return doTable(headers, data);
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public String getName() {
  107. return "debug";
  108. }
  109. /** {@inheritDoc} */
  110. @Override
  111. public boolean showInHelp() {
  112. return false;
  113. }
  114. /** {@inheritDoc} */
  115. @Override
  116. public CommandType getType() {
  117. return CommandType.TYPE_GLOBAL;
  118. }
  119. /** {@inheritDoc} */
  120. @Override
  121. public String getHelp() {
  122. return null;
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. public AdditionalTabTargets getSuggestions(final int arg,
  127. final IntelligentCommandContext context) {
  128. AdditionalTabTargets res = new AdditionalTabTargets();
  129. res.excludeAll();
  130. if (arg == 0) {
  131. res.addAll(plugin.getCommandNames());
  132. } else {
  133. final DebugCommand command = plugin.getCommand(
  134. context.getPreviousArgs().get(0));
  135. if (command instanceof IntelligentCommand) {
  136. final IntelligentCommandContext newContext =
  137. new IntelligentCommandContext(context.getWindow(),
  138. context.getPreviousArgs().subList(1,
  139. context.getPreviousArgs().size()),
  140. context.getPartial());
  141. res = ((IntelligentCommand) command).getSuggestions(
  142. arg, newContext);
  143. }
  144. }
  145. return res;
  146. }
  147. }