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.

Set.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.Config;
  24. import com.dmdirc.commandparser.CommandManager;
  25. import com.dmdirc.commandparser.GlobalCommand;
  26. import com.dmdirc.commandparser.IntelligentCommand;
  27. import com.dmdirc.ui.input.AdditionalTabTargets;
  28. import com.dmdirc.ui.interfaces.InputWindow;
  29. import java.util.List;
  30. /**
  31. * The set command allows the user to inspect and change global config settings.
  32. * @author chris
  33. */
  34. public final class Set extends GlobalCommand implements IntelligentCommand {
  35. /**
  36. * Creates a new instance of Set.
  37. */
  38. public Set() {
  39. super();
  40. CommandManager.registerCommand(this);
  41. }
  42. /** {@inheritDoc} */
  43. public void execute(final InputWindow origin, final boolean isSilent,
  44. final String... args) {
  45. switch (args.length) {
  46. case 0:
  47. doDomainList(origin, isSilent);
  48. break;
  49. case 1:
  50. doOptionsList(origin, isSilent, args[0]);
  51. break;
  52. case 2:
  53. doShowOption(origin, isSilent, args[0], args[1]);
  54. break;
  55. default:
  56. doSetOption(origin, isSilent, args[0], args[1], implodeArgs(2, args));
  57. }
  58. }
  59. /**
  60. * Shows the user a list of valid domains.
  61. * @param origin The window the command was issued from
  62. * @param isSilent Whether or not the command is being silenced or not
  63. */
  64. private void doDomainList(final InputWindow origin, final boolean isSilent) {
  65. final StringBuffer output = new StringBuffer(67);
  66. output.append("Valid domains (use ");
  67. output.append(Config.getCommandChar());
  68. output.append("set <domain> to see options within a domain): ");
  69. for (String domain : Config.getDomains()) {
  70. output.append(domain);
  71. output.append(", ");
  72. }
  73. sendLine(origin, isSilent, "commandOutput", output.substring(0, output.length() - 2));
  74. }
  75. /**
  76. * Shows the user a list of valid options within a domain.
  77. * @param origin The window the command was issued from
  78. * @param isSilent Whether or not the command is being silenced or not
  79. * @param domain The domain to be inspected
  80. */
  81. private void doOptionsList(final InputWindow origin,
  82. final boolean isSilent, final String domain) {
  83. final StringBuffer output = new StringBuffer(24);
  84. output.append("Options in domain '");
  85. output.append(domain);
  86. output.append("': ");
  87. boolean found = false;
  88. for (String option : Config.getOptions(domain)) {
  89. output.append(option);
  90. output.append(", ");
  91. found = true;
  92. }
  93. if (found) {
  94. sendLine(origin, isSilent, "commandOutput", output.substring(0, output.length() - 2));
  95. } else {
  96. sendLine(origin, isSilent, "commandError", "There are no options in the domain '" + domain + "'.");
  97. }
  98. }
  99. /**
  100. * Shows the user the current value of one option.
  101. * @param origin The window the command was issued from
  102. * @param isSilent Whether or not the command is being silenced or not
  103. * @param domain The domain of the option
  104. * @param option The name of the option
  105. */
  106. private void doShowOption(final InputWindow origin,
  107. final boolean isSilent, final String domain, final String option) {
  108. if (Config.hasOption(domain, option)) {
  109. sendLine(origin, isSilent, "commandOutput", "The current value of " + domain + "." + option
  110. + " is: " + Config.getOption(domain, option));
  111. } else {
  112. sendLine(origin, isSilent, "commandError", "Option not found: " + domain + "." + option);
  113. }
  114. }
  115. /**
  116. * Sets the value of the specified option.
  117. * @param origin The window the command was issued from
  118. * @param isSilent Whether or not the command is being silenced or not
  119. * @param domain The domain of the option
  120. * @param option The name of the option
  121. * @param newvalue The value the option should be set to
  122. */
  123. private void doSetOption(final InputWindow origin,
  124. final boolean isSilent, final String domain, final String option,
  125. final String newvalue) {
  126. Config.setOption(domain, option, newvalue);
  127. sendLine(origin, isSilent, "commandOutput", domain + "." + option + " has been set to: " + newvalue);
  128. }
  129. /** {@inheritDoc} */
  130. public String getName() {
  131. return "set";
  132. }
  133. /** {@inheritDoc} */
  134. public boolean showInHelp() {
  135. return true;
  136. }
  137. /** {@inheritDoc} */
  138. public boolean isPolyadic() {
  139. return true;
  140. }
  141. /** {@inheritDoc} */
  142. public int getArity() {
  143. return 0;
  144. }
  145. /** {@inheritDoc} */
  146. public String getHelp() {
  147. return "set [domain [option [newvalue]]] - inspect or change configuration settings";
  148. }
  149. /** {@inheritDoc} */
  150. public AdditionalTabTargets getSuggestions(final int arg, final List<String> previousArgs) {
  151. final AdditionalTabTargets res = new AdditionalTabTargets();
  152. if (arg == 0) {
  153. res.addAll(Config.getDomains());
  154. res.setIncludeNormal(false);
  155. } else if (arg == 1 && previousArgs.size() >= 1) {
  156. res.addAll(Config.getOptions(previousArgs.get(0)));
  157. res.setIncludeNormal(false);
  158. }
  159. return res;
  160. }
  161. }