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.

Command.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.commands.context.CommandContext;
  26. import com.dmdirc.interfaces.CommandController;
  27. import com.dmdirc.ui.messages.Styliser;
  28. import javax.annotation.Nonnull;
  29. /**
  30. * Represents a generic command.
  31. */
  32. public abstract class Command {
  33. /** The format name used for command output. */
  34. protected static final String FORMAT_OUTPUT = "commandOutput";
  35. /** The format name used for command errors. */
  36. protected static final String FORMAT_ERROR = "commandError";
  37. /** The controller this command is associated with. */
  38. private final CommandController controller;
  39. public Command(final CommandController controller) {
  40. this.controller = controller;
  41. }
  42. protected CommandController getController() {
  43. return controller;
  44. }
  45. /**
  46. * Sends a line, if appropriate, to the specified target.
  47. *
  48. * @param target The command window to send the line to
  49. * @param isSilent Whether this command is being silenced or not
  50. * @param type The type of message to send
  51. * @param args The arguments of the message
  52. */
  53. protected final void sendLine(final FrameContainer target,
  54. final boolean isSilent, final String type, final Object... args) {
  55. if (!isSilent && target != null) {
  56. target.addLine(type, args);
  57. }
  58. }
  59. /**
  60. * Sends a usage line, if appropriate, to the specified target.
  61. *
  62. * @param target The command window to send the line to
  63. * @param isSilent Whether this command is being silenced or not
  64. * @param name The name of the command that's raising the error
  65. * @param args The arguments that the command accepts or expects
  66. */
  67. protected final void showUsage(final FrameContainer target,
  68. final boolean isSilent, final String name, final String args) {
  69. sendLine(target, isSilent, "commandUsage",
  70. controller.getCommandChar(),
  71. name, args);
  72. }
  73. /**
  74. * Formats the specified data into a table suitable for output in the textpane. It is expected
  75. * that each String[] in data has the same number of elements as the headers array.
  76. *
  77. * @param headers The headers of the table.
  78. * @param data The contents of the table.
  79. *
  80. * @return A string containing an ASCII table
  81. */
  82. protected static String doTable(final String[] headers, final String[][] data) {
  83. final StringBuilder res = new StringBuilder();
  84. res.append(Styliser.CODE_FIXED);
  85. res.append(Styliser.CODE_BOLD);
  86. final int[] maxsizes = new int[headers.length];
  87. for (int i = 0; i < headers.length; i++) {
  88. maxsizes[i] = headers[i].length() + 3;
  89. for (String[] row : data) {
  90. maxsizes[i] = Math.max(maxsizes[i], row[i].length() + 3);
  91. }
  92. doPadding(res, headers[i], maxsizes[i]);
  93. }
  94. for (String[] source : data) {
  95. res.append('\n');
  96. res.append(Styliser.CODE_FIXED);
  97. for (int i = 0; i < source.length; i++) {
  98. doPadding(res, source[i], maxsizes[i]);
  99. }
  100. }
  101. return res.toString();
  102. }
  103. /**
  104. * Adds the specified data to the stringbuilder, padding with spaces to the specified size.
  105. *
  106. * @param builder The stringbuilder to append data to
  107. * @param data The data to be added
  108. * @param size The minimum size that should be used
  109. */
  110. private static void doPadding(final StringBuilder builder, final String data,
  111. final int size) {
  112. builder.append(data);
  113. for (int i = 0; i < size - data.length(); i++) {
  114. builder.append(' ');
  115. }
  116. }
  117. /**
  118. * Executes this command.
  119. *
  120. * @param origin The container which received the command
  121. * @param args Arguments passed to this command
  122. * @param context The context the command was executed in
  123. *
  124. * @since 0.6.4
  125. */
  126. public abstract void execute(@Nonnull FrameContainer origin, CommandArguments args,
  127. CommandContext context);
  128. }