/*-------------------------------------------------------------------
  Sorter class
  -----------------------------------------------------------------*/

import java.lang.*;
import java.applet.*;
import java.awt.*;
import java.util.*;


public class Sorter extends Applet {

    public static final int MAX = 10;

    Random random = new Random();
    Vector blocks = new Vector();

    Button randomizeButton = new Button( "randomize" );
    Button selectionButton = new Button( "selection sort" );
    Button insertionButton = new Button( "insertion sort" );
    Button bubbleButton    = new Button( "bubble sort" );

    BlocksCanvas    blocksCanvas    = null;
    SelectionCanvas selectionCanvas = null;
    InsertionCanvas insertionCanvas = null;
    BubbleCanvas    bubbleCanvas    = null;

    
    /*---------------------------------------------------------------
      init()

      This method overrides the standard init() method in the
      Applet class.

      It constructs the following components:
      (1) a Vector of 10 Blocks, each with a different color and
          a random numeric value
      (2) a Canvas for displaying the blocks, in unsorted order
      (3) three Canvases for displaying each of three sorting
          algorithms as the blocks are sorted

      It then creates a layout manager and lays out all the
      graphical components in the Applet.
      -------------------------------------------------------------*/
    public void init() {

	blocks.addElement( new Block( random,Color.blue ));
	blocks.addElement( new Block( random,Color.cyan ));
	blocks.addElement( new Block( random,Color.lightGray ));
	blocks.addElement( new Block( random,Color.green ));
	blocks.addElement( new Block( random,Color.magenta ));
	blocks.addElement( new Block( random,Color.orange ));
	blocks.addElement( new Block( random,Color.pink ));
	blocks.addElement( new Block( random,Color.red ));
	blocks.addElement( new Block( random,Color.white ));
	blocks.addElement( new Block( random,Color.yellow ));

	blocksCanvas = new BlocksCanvas( this );
	selectionCanvas = new SelectionCanvas( this );
	insertionCanvas = new InsertionCanvas( this );
	bubbleCanvas = new BubbleCanvas( this );

	selectionButton.disable();
	insertionButton.disable();
	bubbleButton.disable();

	this.setFont( new Font( "Helvetica",Font.PLAIN,10 ));
	GridBagLayout layout = new GridBagLayout();
	setLayout( layout );
	addComponent( randomizeButton,this,layout,
		      0,0,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	addComponent( blocksCanvas,this,layout,
		      0,1,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	addComponent( selectionButton,this,layout,
		      1,0,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	addComponent( selectionCanvas,this,layout,
		      1,1,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	Label spacer2 = new Label( " " );
	addComponent( spacer2,this,layout,
		      2,1,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	addComponent( insertionButton,this,layout,
		      3,0,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	addComponent( insertionCanvas,this,layout,
		      3,1,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	Label spacer4 = new Label( " " );
	addComponent( spacer4,this,layout,
		      4,1,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	addComponent( bubbleButton,this,layout,
		      5,0,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	addComponent( bubbleCanvas,this,layout,
		      5,1,1,1,GridBagConstraints.NONE,
		      GridBagConstraints.CENTER,0,0 );
	validate();
	randomize();

    } // end of init() method


    /*---------------------------------------------------------------
      randomize()
      -------------------------------------------------------------*/
    public void randomize() {
	Block block;
	for ( int i=0; i<MAX; i++ ) {
	    block = (Block)blocks.elementAt( i );
	    block.setValue();
	} // end for i
	blocksCanvas.copy( blocks );
	selectionCanvas.clear();
	insertionCanvas.clear();
	bubbleCanvas.clear();
	selectionButton.enable();
	insertionButton.enable();
	bubbleButton.enable();
	update( getGraphics() );
    } // end of randomize() method


    /*---------------------------------------------------------------
      paint()
      -------------------------------------------------------------*/
    public void paint( Graphics g ) {
	if (( blocksCanvas != null ) &&
	    ( blocksCanvas.getGraphics() != null )) {
	    blocksCanvas.update( blocksCanvas.getGraphics() );
	}
	if (( selectionCanvas != null ) &&
	    ( selectionCanvas.getGraphics() != null )) {
	    selectionCanvas.update( selectionCanvas.getGraphics() );
	}
	if (( insertionCanvas != null ) &&
	    ( insertionCanvas.getGraphics() != null )) {
	    insertionCanvas.update( insertionCanvas.getGraphics() );
	}
	if (( bubbleCanvas != null ) &&
	    ( bubbleCanvas.getGraphics() != null )) {
	    bubbleCanvas.update( bubbleCanvas.getGraphics() );
	}
    } // end of paint() method


    /*---------------------------------------------------------------
      addComponent()
      -------------------------------------------------------------*/
    public void addComponent( Component     component,
			      Container     container,
			      GridBagLayout layout,
			      int           gridx,
			      int           gridy,
			      int           gridwidth,
			      int           gridheight,
			      int           fill,
			      int           anchor,
			      double        weightx,
			      double        weighty ) {
	GridBagConstraints gbc = new GridBagConstraints();
	gbc.gridx      = gridx; 
	gbc.gridy      = gridy;
	gbc.gridwidth  = gridwidth; 
	gbc.gridheight = gridheight;
	gbc.fill       = fill; 
	gbc.anchor     = anchor;
	gbc.weightx    = weightx;
	gbc.weighty    = weighty;
	layout.setConstraints( component,gbc );
	container.add( component );
    } /* end of addComponent() */
    
    
    /*---------------------------------------------------------------
      handleEvent()
      -------------------------------------------------------------*/
    public boolean handleEvent( Event evt ) {
	if ( evt.id == Event.ACTION_EVENT ) {
	    if ( evt.target == randomizeButton ) {
		randomize();
		return( true );
	    }
	    else if ( evt.target == selectionButton ) {
		selectionCanvas.copy( blocks );
		selectionCanvas.update( selectionCanvas.getGraphics() );
		selectionButton.disable();
		return( true );
	    }
	    else if ( evt.target == insertionButton ) {
		insertionCanvas.copy( blocks );
		insertionCanvas.update( insertionCanvas.getGraphics() );
		insertionButton.disable();
		return( true );
	    }
	    else if ( evt.target == bubbleButton ) {
		bubbleCanvas.copy( blocks );
		bubbleCanvas.update( bubbleCanvas.getGraphics() );
		bubbleButton.disable();
		return( true );
	    }
	}
	return( super.handleEvent( evt ));
    } /* end of handleEvent() */
    


} /* end of class Sorter */
