Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2006-2010 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.parsers.CommandParser;
  24. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  25. import com.dmdirc.ui.interfaces.InputWindow;
  26. import java.util.Timer;
  27. import java.util.TimerTask;
  28. /**
  29. * Timed command represents a command that has been scheduled by the user.
  30. */
  31. public final class TimedCommand extends TimerTask {
  32. /** The number of repetitions remaining. */
  33. private int repetitions;
  34. /** The command to execute. */
  35. private final String command;
  36. /** The window to use for executing commands. */
  37. private final InputWindow origin;
  38. /** The timer we're using for scheduling this command. */
  39. private final Timer timer;
  40. /**
  41. * Creates a new instance of TimedCommand.
  42. * @param repetitions The number of times this command will be executed
  43. * @param delay The number of seconds between each execution
  44. * @param command The command to be executed
  45. * @param origin The command window to use for the execution
  46. */
  47. public TimedCommand(final int repetitions, final int delay,
  48. final String command, final InputWindow origin) {
  49. super();
  50. this.repetitions = repetitions;
  51. this.command = command;
  52. this.origin = origin;
  53. timer = new Timer("Timed Command Timer");
  54. timer.schedule(this, delay * 1000L, delay * 1000L);
  55. }
  56. /** {@inheritDoc} */
  57. @Override
  58. public void run() {
  59. CommandParser parser;
  60. if (origin == null) {
  61. parser = GlobalCommandParser.getGlobalCommandParser();
  62. } else {
  63. parser = origin.getCommandParser();
  64. }
  65. parser.parseCommand(origin, command);
  66. if (--repetitions <= 0) {
  67. timer.cancel();
  68. }
  69. }
  70. }