PseudoCodeNode.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package animation;
  2. import javax.swing.JTree;
  3. import javax.swing.tree.DefaultMutableTreeNode;
  4. import javax.swing.tree.MutableTreeNode;
  5. import javax.swing.tree.TreePath;
  6. /**
  7. * represents a line of pseudocode
  8. * @author kolja
  9. *
  10. */
  11. public class PseudoCodeNode extends DefaultMutableTreeNode {
  12. private static final long serialVersionUID = 1L;
  13. private boolean selected;
  14. private JTree tree;
  15. private boolean breakPoint;
  16. private AnimationController controller;
  17. public PseudoCodeNode( String description, JTree tree )
  18. {
  19. super( description );
  20. selected = false;
  21. this.tree = tree;
  22. breakPoint = false;
  23. }
  24. public void setController( AnimationController c )
  25. {
  26. if( children != null )
  27. {
  28. for( Object ch : children )
  29. {
  30. ((PseudoCodeNode)ch).setController( c );
  31. }
  32. }
  33. controller = c;
  34. }
  35. @Override
  36. public void add( MutableTreeNode node )
  37. {
  38. ((PseudoCodeNode)node).setController( controller );
  39. super.add( node );
  40. }
  41. /**
  42. *
  43. * @return the tree that this node belongs to
  44. */
  45. public JTree getTree()
  46. {
  47. return tree;
  48. }
  49. /**
  50. * checks if this node should be highlighted
  51. * @return true if it should, false otherwise
  52. */
  53. public boolean isSelected()
  54. {
  55. return selected;
  56. }
  57. /**
  58. * checks if one of the subnodes of this node is selected.
  59. * @return true if one is, false otherwise
  60. */
  61. public boolean hasSelectedSubnode()
  62. {
  63. if( children != null )
  64. {
  65. for( Object ch : children )
  66. {
  67. if( ((PseudoCodeNode)ch).isSelected() || ((PseudoCodeNode)ch).hasSelectedSubnode() )
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**
  74. * highlight this line of pseudocode.
  75. * should be called when the line is entered, as it triggers breakpoints
  76. * @param selected whether to select or deselect this line
  77. * @return false iff a breakpoint was reached and the node was set to be selected.
  78. */
  79. public boolean setSelected( boolean selected )
  80. {
  81. if( selected && breakPoint )
  82. controller.setContinuous( false );
  83. this.selected = selected;
  84. if( selected )
  85. {
  86. tree.collapsePath( new TreePath( this.getPath() ) );
  87. tree.expandPath( new TreePath( this.getPath() ) );
  88. }
  89. else
  90. {
  91. tree.expandPath( new TreePath( this.getPath() ) );
  92. tree.collapsePath( new TreePath( this.getPath() ) );
  93. }
  94. return !breakPoint || !selected;
  95. }
  96. /**
  97. * set a breakpoint at this line of code
  98. * @param breakPoint whether there should be a breakpoint or node
  99. */
  100. public void setBreakPoint( boolean breakPoint )
  101. {
  102. this.breakPoint = breakPoint;
  103. }
  104. /**
  105. * check if there is a breakpoint set at this line of code
  106. * @return true, iff there is a breakpoint
  107. */
  108. public boolean hasBreakPoint()
  109. {
  110. return breakPoint;
  111. }
  112. }