AnimatedAlgorithm.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.PseudoCodeNode.CodeStatus;
  9. import animation.StackFrame.FrameType;
  10. import graph.LayeredGraphNode;
  11. public abstract class AnimatedAlgorithm extends Thread implements AlgorithmStage {
  12. protected AnimationController ac;
  13. protected LayeredGraphNode graph;
  14. private JFrame view;
  15. protected PseudoCodeNode root;
  16. protected Stack<PseudoCodeNode> activeFunction;
  17. protected Memory mem;
  18. private Memory fstack;
  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, true );
  27. mem.addFrame( new StackFrame( FrameType.FUNCTION ) );
  28. activeFunction = new Stack<PseudoCodeNode>();
  29. fstack = new Memory();
  30. }
  31. public void addActiveFunction( PseudoCodeNode n )
  32. {
  33. if( !activeFunction.empty() )
  34. activeFunction.peek().writeToStack( fstack );
  35. fstack.addFrame( new StackFrame( FrameType.FUNCTION ) );
  36. activeFunction.push( n );
  37. }
  38. private void update()
  39. {
  40. SwingUtilities.invokeLater(new Runnable() {
  41. public void run() {
  42. view.repaint();
  43. for( ComponentListener l : view.getComponentListeners() )
  44. {
  45. l.componentResized( new ComponentEvent(view, 0) );
  46. }
  47. }
  48. });
  49. }
  50. @Override
  51. public void run()
  52. {
  53. while( true ) // if this loop would end we could not undo steps any more
  54. {
  55. CodeStatus status = null;
  56. try {
  57. while( activeFunction.size() == 0 )
  58. Thread.sleep( 100 );
  59. PseudoCodeNode current = activeFunction.peek();
  60. switch( ac.getNextAction() )
  61. {
  62. case FORWARD:
  63. status = current.forwardStep( mem );
  64. break;
  65. case FORWARD_OUT:
  66. status = current.forwardStepOut( mem );
  67. break;
  68. case FORWARD_OVER:
  69. status = current.forwardStepOver( mem );
  70. break;
  71. case BACKWARD:
  72. status = current.backwardStep( mem );
  73. break;
  74. case BACKWARD_OUT:
  75. status = current.backwardStepOut( mem );
  76. break;
  77. case BACKWARD_OVER:
  78. status = current.backwardStepOver( mem );
  79. break;
  80. }
  81. } catch (InterruptedException e) {
  82. e.printStackTrace();
  83. return;
  84. }
  85. update();
  86. if( status == CodeStatus.FINISHED )
  87. {
  88. if( activeFunction.size() > 1 )
  89. {
  90. fstack.removeFrame();
  91. activeFunction.pop();
  92. activeFunction.peek().loadFromStack( fstack );
  93. }
  94. else
  95. {
  96. ac.setContinuous( false );
  97. ac.setNextAction( null );
  98. }
  99. }
  100. }
  101. }
  102. @Override
  103. public abstract PseudoCodeNode createPseudocodeTree( JTree tree );
  104. }