AnimationController.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package algorithm;
  2. /**
  3. * controls the execution of an {@link Algorithm}.
  4. * Do not use the same controller for multiple Algorithms!
  5. * @author kolja
  6. *
  7. */
  8. public class AnimationController {
  9. public static final int DEFAULT_DELAY = 100;
  10. private Action next;
  11. private boolean continuous;
  12. private long lastTime;
  13. private long delay;
  14. private int stepOption;
  15. /**
  16. * creates a new {@link AnimationController} that can be used to control an {@link Algorithm}
  17. */
  18. public AnimationController()
  19. {
  20. stepOption = 0;
  21. next = null;
  22. continuous = false;
  23. lastTime = 0;
  24. delay = DEFAULT_DELAY;
  25. }
  26. /**
  27. * Ask the controller, which kind of action should be done next.
  28. * The controller waits until it knows this for sure.
  29. * @return The action.
  30. * @throws InterruptedException if any thread interrupted the
  31. * current thread before or while the current thread
  32. * was waiting for a notification. The <i>interrupted
  33. * status</i> of the current thread is cleared when
  34. * this exception is thrown.
  35. */
  36. public Action getNextAction() throws InterruptedException
  37. {
  38. long old = lastTime;
  39. Action ret = null;
  40. synchronized( this ) {
  41. while( next == null )
  42. wait();
  43. lastTime = System.currentTimeMillis();
  44. ret = next;
  45. }
  46. if( !continuous )
  47. next = null;
  48. else
  49. {
  50. if( lastTime - old < delay )
  51. {
  52. Thread.sleep( delay - ( lastTime - old ) );
  53. lastTime = System.currentTimeMillis();
  54. }
  55. }
  56. return ret;
  57. }
  58. /**
  59. * activates or deactivates automatic execution
  60. * @param c automatic execution will be activated if c is true
  61. * and deactivated otherwise
  62. */
  63. public void setContinuous( boolean c )
  64. {
  65. if( !c && continuous )
  66. {
  67. synchronized( this ) {
  68. next = null;
  69. }
  70. }
  71. this.continuous = c;
  72. }
  73. /**
  74. * sets the delay of the automatic execution.
  75. * You might also want to enable it by calling setContinuous()
  76. * @param delay the delay in milliseconds
  77. */
  78. public void setDelay( long delay )
  79. {
  80. this.delay = delay;
  81. }
  82. /**
  83. * sets the next action and wakes up one thread waiting for an answer of the controller.
  84. * @param a
  85. * the action
  86. */
  87. public void setNextAction( Action a )
  88. {
  89. synchronized( this ) {
  90. next = a;
  91. notify();
  92. }
  93. }
  94. public void setStepOption( int opt )
  95. {
  96. stepOption = opt;
  97. }
  98. public int getStepOption()
  99. {
  100. return stepOption;
  101. }
  102. /**
  103. * checks if automatic execution is active
  104. * @return true if it is
  105. */
  106. public boolean isContinuous()
  107. {
  108. return continuous;
  109. }
  110. }