package animation; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.MutableTreeNode; import javax.swing.tree.TreePath; /** * represents a line of pseudocode * @author kolja * */ public class PseudoCodeNode extends DefaultMutableTreeNode { private static final long serialVersionUID = 1L; private boolean selected; private JTree tree; private boolean breakPoint; private AnimationController controller; public PseudoCodeNode( String description, JTree tree ) { super( description ); selected = false; this.tree = tree; breakPoint = false; } public void setController( AnimationController c ) { if( children != null ) { for( Object ch : children ) { ((PseudoCodeNode)ch).setController( c ); } } controller = c; } @Override public void add( MutableTreeNode node ) { ((PseudoCodeNode)node).setController( controller ); super.add( node ); } /** * * @return the tree that this node belongs to */ public JTree getTree() { return tree; } /** * checks if this node should be highlighted * @return true if it should, false otherwise */ public boolean isSelected() { return selected; } /** * checks if one of the subnodes of this node is selected. * @return true if one is, false otherwise */ public boolean hasSelectedSubnode() { if( children != null ) { for( Object ch : children ) { if( ((PseudoCodeNode)ch).isSelected() || ((PseudoCodeNode)ch).hasSelectedSubnode() ) return true; } } return false; } /** * highlight this line of pseudocode. * should be called when the line is entered, as it triggers breakpoints * @param selected whether to select or deselect this line * @return false iff a breakpoint was reached and the node was set to be selected. */ public boolean setSelected( boolean selected ) { if( selected && breakPoint ) controller.setContinuous( false ); this.selected = selected; if( selected ) { tree.collapsePath( new TreePath( this.getPath() ) ); tree.expandPath( new TreePath( this.getPath() ) ); } else { tree.expandPath( new TreePath( this.getPath() ) ); tree.collapsePath( new TreePath( this.getPath() ) ); } return !breakPoint || !selected; } /** * set a breakpoint at this line of code * @param breakPoint whether there should be a breakpoint or node */ public void setBreakPoint( boolean breakPoint ) { this.breakPoint = breakPoint; } /** * check if there is a breakpoint set at this line of code * @return true, iff there is a breakpoint */ public boolean hasBreakPoint() { return breakPoint; } }