/*
    SimpleTrees-brainDead.java

    A version of SimpleTrees.java by burt rosenberg,
    for use in Math220/971 Univ. of Miami Sept 1996.

    The student is to supply the missing code.

*/
 
// warning: critical race between viewer and mutator on tree.
//   not only is the viewer clumsy but there is no obvious synchronization
//   point.

import java.awt.* ;
import java.applet.Applet ;

class TreeActions extends Panel
{
    Button bOne ;
    Button bTwo ;
    Label lOne ;
    TextField tfOne ;
    
    Tree t ;
    
    TreeActions(Tree t)
    {
       super() ;   // construct the Panel
       this.t = t ; 
        
       bOne = new Button("Clear Tree" ) ;
       bTwo = new Button("Insert to Tree" ) ;
       lOne = new Label("Value to Insert:") ;
       tfOne = new TextField(15) ;
       
       this.add(bOne) ;
       this.add(bTwo) ;
       this.add(lOne) ;
       this.add(tfOne) ;
     
    }
    
    public boolean action( Event evt, Object arg ) 
    {
       if ( evt.target == bOne )
       {
          // Clear Tree
          t.delete() ;
          tfOne.requestFocus() ;
          return true ;
       }
       if ( evt.target == bTwo )
       {
          // Insert to Tree
           String s = tfOne.getText() ;
           s = s.trim() ;
           if ( s.length() > 0 )
           {
             // ignore empty string inserts 
             t.insert( s ) ;
           }
           tfOne.setText("") ;
           tfOne.requestFocus() ;
           return true ;
       }
       return false ;
    }
    
    public boolean mouseDown(Event evt, int x, int y )
    {
       //System.out.println("ButtonPanel.mouseDown: entered") ;
       return true ;
    }
    
    public void paint(Graphics gc)
    {
       //System.out.println("ButtonPanel.paint: entered") ;
    }    
    
}

public class SimpleTrees extends Applet
{
   Tree treeOne ;
   
   public void init()
   {
      treeOne = new Tree() ;
      setLayout( new BorderLayout() ) ;
      Panel pOne = new TreeActions(treeOne) ;
      Panel pTwo = new TreeView(treeOne) ;
      treeOne.setTreeViewer( (TreeView) pTwo ) ;
      add("Center", pTwo ) ;
      add("North", pOne ) ;
      
      validate() ;
      
      //System.out.println( size().toString() ) ;
      //System.out.println( pOne.size().toString() ) ;
      //System.out.println( pTwo.size().toString() ) ;
      
   }
      
   public void paint(Graphics gc)
   {
      //System.out.println("PanelTest.paint: entered") ;
   }
      
   public boolean mouseDown(Event evt, int x, int y )
   {
      //System.out.println("PanelTest.mouseDown: entered") ;
      return true ;
   }
  
}

class TreeNode
{

   private TreeNode leftChild = null ;
   private TreeNode rightChild = null ;
   private String content = "" ;

   TreeNode getLeftChild() 
      { return leftChild ; }
      
   TreeNode getRightChild() 
      { return rightChild ; }
      
   String getContent() 
      { return content ; }

   void setLeftChild( TreeNode n ) 
      { leftChild = n ; }
      
   void setRightChild( TreeNode n ) 
      { rightChild = n ; }
      
   void setContent( String s ) 
      { content = s ; }
       
}

class Tree
{
   private TreeNode theRoot ;
   private TreeView tv ;
   
   Tree() 
   { 
      theRoot = null ; 
      tv = null ;
   }
   
   public void setTreeViewer(TreeView tv)
   {
      this.tv = tv ;
   }
   
   private void repaint()
   {  
       if ( tv != null )
       {
            tv.repaint() ;
       }
   }
  
   void delete()
   {
      theRoot = null ; 
      this.repaint() ;  // tell observers that something has changed
   }
   
   TreeNode getRoot ()
   {
      return theRoot ;
   }

   TreeNode insert( String content )
   {
   
      TreeNode n = theRoot ;
      TreeNode np = n ;     // np is the parent node of n,
      boolean lastWentLeft = true ;

      while ( n!=null )
      {

      // Lots-O-code deleted

      }

      if ( np==null )
      {
         
      // Code deleted

      }
      else
      {
         
      // Lots-O-code deleted
      
      }

   this.repaint() ; // tell observers that something has changed
   return n ;
   }
}

class TreeView extends Panel
{
   Tree t ;
   
   final int X_SKIP = 20 ;
   final int Y_SKIP = 20 ;
   final int X_OFFSET = 3 ;
   final int Y_OFFSET = 3 ;
   final int X_ORIG = 10 ;
   final int Y_ORIG = 10 ;
   
   final Color lineColor = Color.blue ;
   final Color textColor = Color.black ;
   final Color leafColor = Color.blue ;
   
   TreeView( Tree t )
   {
       super() ;    // construct the Panel
       this.t = t ;
   }
   
   public void repaint()
   {  
       super.repaint() ;
   }
      
   public boolean mouseDown(Event evt, int x, int y )
   {
      //System.out.println("TreeView.mouseDown: entered") ;
      return false ;
   }
      
   int recDraw( Graphics gc, TreeNode n, int xp, int yp, int x, int y )
   {
      // draw line from (xp,yp) -> (x,y)
         // code deleted

      if ( n == null )  // basis case for recursion
      {
         // code deleted
      }
      else 
      {
         // recurse on left and right children
         
      // Lots-O-Code deleted

      }	  
      return y ;
   }

   void draw( Graphics gc )
   {
      int x = X_ORIG ;
      int y = Y_ORIG ;
      
      TreeNode n = t.getRoot() ;

      if ( n == null )
      {
         gc.setColor( textColor ) ;
         gc.drawString( "The tree is empty!", x+2*X_OFFSET, y+Y_OFFSET ) ;
      }
      else 
      {     
         // tree is not null 
         gc.setColor( lineColor ) ;
         gc.drawLine( x-X_OFFSET, y, x+X_OFFSET, y ) ;
         gc.setColor( textColor ) ;
	     gc.drawString( n.getContent(), x+2*X_OFFSET, y+Y_OFFSET ) ;
	     y += Y_SKIP ; 
	     y = recDraw( gc, n.getLeftChild(), x, y, x+X_SKIP, y ) ;
 	     y = recDraw( gc, n.getRightChild(), x, Y_ORIG, x+X_SKIP, y ) ;
 	  }	  
   }

   public void paint(Graphics gc)
   {
      //System.out.println("TreeView.paint: entered") ;
      draw(gc) ;
   }   
  
}

