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.

TimedCommand.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.addons.timeplugin;
  23. import com.dmdirc.commandparser.CommandParser;
  24. import com.dmdirc.commandparser.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. this.repetitions = repetitions;
  50. this.command = command;
  51. this.origin = origin;
  52. timer = new Timer();
  53. timer.scheduleAtFixedRate(this, delay * 1000, delay * 1000);
  54. }
  55. /** {@inheritDoc} */
  56. public void run() {
  57. CommandParser parser;
  58. if (origin == null) {
  59. parser = GlobalCommandParser.getGlobalCommandParser();
  60. } else {
  61. parser = origin.getCommandParser();
  62. }
  63. parser.parseCommand(origin, command);
  64. if (--repetitions <= 0) {
  65. timer.cancel();
  66. }
  67. }
  68. }