PseudoCodeNode.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package animation;
  2. import java.awt.Rectangle;
  3. import javax.swing.JTree;
  4. import javax.swing.SwingUtilities;
  5. import javax.swing.tree.DefaultMutableTreeNode;
  6. import javax.swing.tree.MutableTreeNode;
  7. import javax.swing.tree.TreePath;
  8. import lib.TextLayoutHelper;
  9. /**
  10. * represents a line of pseudocode
  11. * @author kolja
  12. *
  13. */
  14. public class PseudoCodeNode extends DefaultMutableTreeNode {
  15. public static enum CodeAction
  16. {
  17. SKIP,
  18. STOP,
  19. CONTINUE
  20. }
  21. private static final long serialVersionUID = 1L;
  22. private static int nextNodeId = 0;
  23. private final int nodeId;
  24. private AnimationController controller;
  25. private JTree tree;
  26. private CodeLine code;
  27. private boolean selected;
  28. private boolean breakPoint;
  29. public PseudoCodeNode( String description, String[] vars, JTree tree, CodeLine line )
  30. {
  31. super( TextLayoutHelper.setupPseudoCode( description, vars ) );
  32. synchronized( PseudoCodeNode.class )
  33. {
  34. nodeId = nextNodeId++;
  35. }
  36. selected = false;
  37. this.tree = tree;
  38. breakPoint = false;
  39. code = line;
  40. }
  41. public int getId()
  42. {
  43. return nodeId;
  44. }
  45. public void setController( AnimationController c )
  46. {
  47. if( children != null )
  48. {
  49. for( Object ch : children )
  50. {
  51. ((PseudoCodeNode)ch).setController( c );
  52. }
  53. }
  54. controller = c;
  55. }
  56. @Override
  57. public void add( MutableTreeNode node )
  58. {
  59. ((PseudoCodeNode)node).setController( controller );
  60. super.add( node );
  61. }
  62. /**
  63. *
  64. * @return the tree that this node belongs to
  65. */
  66. public JTree getTree()
  67. {
  68. return tree;
  69. }
  70. /**
  71. * checks if this node should be highlighted
  72. * @return true if it should, false otherwise
  73. */
  74. public boolean isSelected()
  75. {
  76. return selected;
  77. }
  78. /**
  79. * checks if one of the subnodes of this node is selected.
  80. * @return true if one is, false otherwise
  81. */
  82. public boolean hasSelectedSubnode()
  83. {
  84. if( children != null )
  85. {
  86. for( Object ch : children )
  87. {
  88. if( ((PseudoCodeNode)ch).isSelected() || ((PseudoCodeNode)ch).hasSelectedSubnode() )
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. private void expandToRoot()
  95. {
  96. if( parent != null )
  97. ((PseudoCodeNode)parent).expandToRoot();
  98. tree.expandPath( new TreePath( this.getPath() ) );
  99. }
  100. /**
  101. * highlight this line of pseudocode.
  102. * should be called when the line is entered, as it triggers breakpoints
  103. * @param selected whether to select or deselect this line
  104. * @return if the automatic execution should continue.
  105. */
  106. public CodeAction setSelected( boolean selected )
  107. {
  108. if( selected && breakPoint )
  109. controller.setContinuous( false );
  110. this.selected = selected;
  111. if( selected )
  112. {
  113. if( tree != null ) {
  114. TreePath path = new TreePath( getPath() );
  115. Rectangle bounds = tree.getPathBounds(path);
  116. if( bounds!= null )
  117. {
  118. bounds.height = (int) (tree.getVisibleRect().height - tree.getVisibleRect().getHeight() / 2);
  119. bounds.x = 0;
  120. SwingUtilities.invokeLater( new Runnable() {
  121. @Override
  122. public void run() {
  123. tree.scrollRectToVisible(bounds);
  124. }
  125. });
  126. }
  127. }
  128. if( controller == null || controller.getStepOption() != 1 || breakPoint )
  129. {
  130. SwingUtilities.invokeLater( new Runnable() {
  131. @Override
  132. public void run() {
  133. expandToRoot();
  134. }
  135. });
  136. }
  137. }
  138. else
  139. {
  140. if( controller == null || controller.getStepOption() != 1 )
  141. {
  142. SwingUtilities.invokeLater( new Runnable() {
  143. @Override
  144. public void run() {
  145. tree.collapsePath( new TreePath( getPath() ) );
  146. }
  147. });
  148. }
  149. }
  150. if( breakPoint && selected )
  151. return CodeAction.STOP; // Breakpoint
  152. if( controller != null && controller.getStepOption() == 1 && !tree.isVisible( new TreePath( this.getPath() ) ) )
  153. return CodeAction.SKIP; // Step would be to detailed
  154. return CodeAction.CONTINUE; // Normal
  155. }
  156. /**
  157. * set a breakpoint at this line of code
  158. * @param breakPoint whether there should be a breakpoint or node
  159. */
  160. public void setBreakPoint( boolean breakPoint )
  161. {
  162. this.breakPoint = breakPoint;
  163. }
  164. /**
  165. * check if there is a breakpoint set at this line of code
  166. * @return true, iff there is a breakpoint
  167. */
  168. public boolean hasBreakPoint()
  169. {
  170. return breakPoint;
  171. }
  172. public ControlFlow forwardStep( Memory m )
  173. {
  174. ControlFlow cf = code.runForward( m );
  175. cf.setJumpBack( this );
  176. return cf;
  177. }
  178. public ControlFlow emptyForwardStep( Memory m )
  179. {
  180. code.actions.push( (Memory mem) -> {} ); // add empty reverse function
  181. ControlFlow cf = new ControlFlow( ControlFlow.STEP_OVER );
  182. cf.setJumpBack( this );
  183. return cf;
  184. }
  185. public void backwardStep( Memory m )
  186. {
  187. code.runBackward( m );
  188. }
  189. public String getDebugOutput( Memory m )
  190. {
  191. if( parent == null )
  192. return "";
  193. return ((PseudoCodeNode)parent).getDebugOutput( m );
  194. }
  195. }