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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.interfaces.WindowModel;
  24. import java.util.Timer;
  25. import java.util.TimerTask;
  26. /**
  27. * Timed command represents a command that has been scheduled by the user.
  28. */
  29. public class TimedCommand extends TimerTask {
  30. /** The number of repetitions remaining. */
  31. private int repetitions;
  32. /** The command to execute. */
  33. private final String command;
  34. /** The container to use for executing commands. */
  35. private final WindowModel origin;
  36. /** The number of seconds between each execution. */
  37. private final int delay;
  38. /** The key for this timer in the Timer Manager. */
  39. private final int timerKey;
  40. /** The manager for this timer. */
  41. private final TimerManager manager;
  42. /** The timer we're using for scheduling this command. */
  43. private Timer timer;
  44. /**
  45. * Creates a new instance of TimedCommand.
  46. *
  47. * @param manager The manager that is controlling this command.
  48. * @param timerKey The key for this timer in the Timer Manager.
  49. * @param repetitions The number of times this command will be executed
  50. * @param delay The number of seconds between each execution
  51. * @param command The command to be executed
  52. * @param origin The frame container to use for the execution
  53. */
  54. public TimedCommand(
  55. final TimerManager manager,
  56. final int timerKey,
  57. final int repetitions,
  58. final int delay,
  59. final String command,
  60. final WindowModel origin) {
  61. this.timerKey = timerKey;
  62. this.repetitions = repetitions;
  63. this.command = command;
  64. this.origin = origin;
  65. this.manager = manager;
  66. this.delay = delay;
  67. }
  68. public void schedule(final TimerFactory timerFactory) {
  69. timer = timerFactory.getTimer("Timed Command Timer");
  70. timer.schedule(this, delay * 1000L, delay * 1000L);
  71. }
  72. /**
  73. * Returns the command this timer is due to execute.
  74. *
  75. * @return Command the timer will run
  76. */
  77. public String getCommand() {
  78. return command;
  79. }
  80. /**
  81. * Cancels this timer and removes it from the Timer Manager
  82. */
  83. public void cancelTimer() {
  84. manager.removeTimer(timerKey);
  85. if (timer != null) {
  86. timer.cancel();
  87. }
  88. }
  89. @Override
  90. public void run() {
  91. if (timer == null) {
  92. return;
  93. }
  94. origin.getCommandParser().parseCommand(origin, command);
  95. if (--repetitions <= 0) {
  96. manager.removeTimer(timerKey);
  97. timer.cancel();
  98. }
  99. }
  100. }