AnimatedAlgorithm.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package animation;
  2. import java.awt.event.ComponentEvent;
  3. import java.awt.event.ComponentListener;
  4. import java.util.Stack;
  5. import javax.swing.JFrame;
  6. import javax.swing.JTree;
  7. import javax.swing.SwingUtilities;
  8. import animation.Memory.MemoryType;
  9. import animation.PseudoCodeNode.CodeStatus;
  10. import animation.StackFrame.FrameType;
  11. import graph.LayeredGraphNode;
  12. public abstract class AnimatedAlgorithm extends Thread implements AlgorithmStage {
  13. protected AnimationController ac;
  14. protected LayeredGraphNode graph;
  15. private JFrame view;
  16. protected PseudoCodeNode root;
  17. protected Stack<PseudoCodeNode> activeFunction;
  18. protected Memory mem;
  19. public AnimatedAlgorithm( AnimationController controller, LayeredGraphNode graph, JFrame view )
  20. {
  21. this.ac = controller;
  22. this.graph = graph;
  23. this.view = view;
  24. root = null;
  25. mem = new Memory();
  26. mem.declare( "graph", graph, MemoryType.GLOBAL );
  27. mem.addFrame( new StackFrame( FrameType.FUNCTION ) );
  28. activeFunction = new Stack<PseudoCodeNode>();
  29. }
  30. public Memory getMemory()
  31. {
  32. return mem;
  33. }
  34. public void addActiveFunction( PseudoCodeNode n )
  35. {
  36. if( !activeFunction.empty() )
  37. activeFunction.peek().writeToStack( mem );
  38. activeFunction.push( n );
  39. }
  40. public PseudoCodeNode getActiveFunction()
  41. {
  42. if( activeFunction.empty() )
  43. return null;
  44. return activeFunction.peek();
  45. }
  46. public boolean isActiveFunction( PseudoCodeNode n )
  47. {
  48. if( activeFunction.empty() )
  49. return false;
  50. return activeFunction.peek() == n;
  51. }
  52. private void update()
  53. {
  54. SwingUtilities.invokeLater(new Runnable() {
  55. public void run() {
  56. view.repaint();
  57. for( ComponentListener l : view.getComponentListeners() )
  58. {
  59. l.componentResized( new ComponentEvent(view, 0) );
  60. }
  61. }
  62. });
  63. }
  64. @Override
  65. public void run()
  66. {
  67. while( true ) // if this loop would end we could not undo steps any more
  68. {
  69. CodeStatus status = null;
  70. try {
  71. while( activeFunction.size() == 0 )
  72. Thread.sleep( 100 );
  73. PseudoCodeNode current = activeFunction.peek();
  74. switch( ac.getNextAction() )
  75. {
  76. case FORWARD:
  77. status = current.forwardStep( mem );
  78. break;
  79. case FORWARD_OUT:
  80. status = current.forwardStepOut( mem );
  81. break;
  82. case FORWARD_OVER:
  83. status = current.forwardStepOver( mem );
  84. break;
  85. case BACKWARD:
  86. status = current.backwardStep( mem );
  87. break;
  88. case BACKWARD_OUT:
  89. status = current.backwardStepOut( mem );
  90. break;
  91. case BACKWARD_OVER:
  92. status = current.backwardStepOver( mem );
  93. break;
  94. }
  95. System.out.println( "Stack Frames: " + mem.getSize() );
  96. } catch (InterruptedException e) {
  97. e.printStackTrace();
  98. return;
  99. }
  100. update();
  101. if( status == CodeStatus.FINISHED )
  102. {
  103. if( activeFunction.size() > 1 )
  104. {
  105. activeFunction.pop();
  106. activeFunction.peek().loadFromStack( mem );
  107. }
  108. else
  109. {
  110. ac.setContinuous( false );
  111. ac.setNextAction( null );
  112. }
  113. }
  114. }
  115. }
  116. @Override
  117. public abstract PseudoCodeNode createPseudocodeTree( JTree tree );
  118. }