Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TimerCommand.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2006-2009 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.time;
  23. import com.dmdirc.commandparser.CommandArguments;
  24. import com.dmdirc.commandparser.CommandManager;
  25. import com.dmdirc.commandparser.commands.GlobalCommand;
  26. import com.dmdirc.commandparser.commands.IntelligentCommand;
  27. import com.dmdirc.ui.input.AdditionalTabTargets;
  28. import com.dmdirc.ui.input.TabCompleter;
  29. import com.dmdirc.ui.interfaces.InputWindow;
  30. import java.util.List;
  31. /**
  32. * The timer command allows users to schedule commands to occur after a certain
  33. * interval, or to repeatedly occur with a specified delay.
  34. * @author chris
  35. */
  36. public final class TimerCommand extends GlobalCommand implements IntelligentCommand {
  37. /**
  38. * Creates a new instance of TimerCommand.
  39. */
  40. public TimerCommand() {
  41. CommandManager.registerCommand(this);
  42. }
  43. /** {@inheritDoc} */
  44. @Override
  45. public void execute(final InputWindow origin, final boolean isSilent,
  46. final CommandArguments args) {
  47. if (args.getArguments().length < 3) {
  48. doUsage(origin, isSilent);
  49. } else {
  50. int repetitions = 0;
  51. int interval = 0;
  52. final String command = args.getArgumentsAsString(2);
  53. try {
  54. repetitions = Integer.parseInt(args.getArguments()[0]);
  55. interval = Integer.parseInt(args.getArguments()[1]);
  56. } catch (NumberFormatException ex) {
  57. doUsage(origin, isSilent);
  58. return;
  59. }
  60. new TimedCommand(repetitions, interval, command, origin);
  61. sendLine(origin, isSilent, FORMAT_OUTPUT, "Command scheduled.");
  62. }
  63. }
  64. /**
  65. * Displays usage information for this command.
  66. * @param origin The window that the command was entered in
  67. * @param isSilent Whether this command is being silenced or not
  68. */
  69. private void doUsage(final InputWindow origin, final boolean isSilent) {
  70. showUsage(origin, isSilent, "timer", "<repetitions> <interval> <command>");
  71. }
  72. /** {@inheritDoc} */
  73. @Override
  74. public String getName() {
  75. return "timer";
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public boolean showInHelp() {
  80. return true;
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public String getHelp() {
  85. return "timer <repetitions> <interval> <command> " +
  86. "- schedules a command to be executed after a certain time";
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public AdditionalTabTargets getSuggestions(final int arg, final List<String> previousArgs) {
  91. AdditionalTabTargets res;
  92. if (arg <= 1) {
  93. res = new AdditionalTabTargets().excludeAll();
  94. } else {
  95. res = TabCompleter.getIntelligentResults(arg, previousArgs, 2);
  96. }
  97. return res;
  98. }
  99. }