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.8KB

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