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

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