Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Set.java 5.5KB

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