package animation;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

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 );
    }
    
    public JTree getTree()
    {
        return tree;
    }
    
    public boolean isSelected()
    {
        return selected;
    }
    
    public void setSelected( boolean selected )
    {
        if( selected && breakPoint )
            controller.setContinuous( false );
        if( selected )
            tree.expandPath( new TreePath( this.getPath() ) );
        else
            tree.collapsePath( new TreePath( this.getPath() ) );
        this.selected = selected;
    }
    
    public void setBreakPoint( boolean breakPoint )
    {
        this.breakPoint = breakPoint;
    }
    
    public boolean hasBreakPoint()
    {
        return breakPoint;
    }
}
