AnimatedAlgorithm.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 void addActiveFunction( PseudoCodeNode n )
  31. {
  32. if( !activeFunction.empty() )
  33. activeFunction.peek().writeToStack( mem );
  34. activeFunction.push( n );
  35. }
  36. private void update()
  37. {
  38. SwingUtilities.invokeLater(new Runnable() {
  39. public void run() {
  40. view.repaint();
  41. for( ComponentListener l : view.getComponentListeners() )
  42. {
  43. l.componentResized( new ComponentEvent(view, 0) );
  44. }
  45. }
  46. });
  47. }
  48. @Override
  49. public void run()
  50. {
  51. while( true ) // if this loop would end we could not undo steps any more
  52. {
  53. CodeStatus status = null;
  54. try {
  55. while( activeFunction.size() == 0 )
  56. Thread.sleep( 100 );
  57. PseudoCodeNode current = activeFunction.peek();
  58. switch( ac.getNextAction() )
  59. {
  60. case FORWARD:
  61. status = current.forwardStep( mem );
  62. break;
  63. case FORWARD_OUT:
  64. status = current.forwardStepOut( mem );
  65. break;
  66. case FORWARD_OVER:
  67. status = current.forwardStepOver( mem );
  68. break;
  69. case BACKWARD:
  70. status = current.backwardStep( mem );
  71. break;
  72. case BACKWARD_OUT:
  73. status = current.backwardStepOut( mem );
  74. break;
  75. case BACKWARD_OVER:
  76. status = current.backwardStepOver( mem );
  77. break;
  78. }
  79. System.out.println( "Stack Frames: " + mem.getSize() );
  80. } catch (InterruptedException e) {
  81. e.printStackTrace();
  82. return;
  83. }
  84. update();
  85. if( status == CodeStatus.FINISHED )
  86. {
  87. if( activeFunction.size() > 1 )
  88. {
  89. activeFunction.pop();
  90. activeFunction.peek().loadFromStack( mem );
  91. }
  92. else
  93. {
  94. ac.setContinuous( false );
  95. ac.setNextAction( null );
  96. }
  97. }
  98. }
  99. }
  100. @Override
  101. public abstract PseudoCodeNode createPseudocodeTree( JTree tree );
  102. }