/*************************************************
 * Author: 	Evelyn Lai-Tee Cheok
 *			Department of Electrical Engineering
 *			Center for Telecommunications Research
 *			Schapiro Research Building
 *			Columbia University
 *
 * Email: 	laitee@ctr.columbia.edu
 *
 **************************************************/
import java.lang.*;
import java.awt.*;

abstract class ChoiceBar extends Bar
{   
    /**
     * selected item index
     */
    int curSelected = 0;
  
    /**
     * Constructs a Bar with button count and direction
     * @param count : button's count
     * @param direction : bars direction (VERTICAL, HORIZONTAL)
     */
    public ChoiceBar(int count, int direction)
    {
        super(count, direction);
    }
   
    /**
     * implement's abstract method in Bar
     * @param i : button index in Bar
     * @see Bar
     */
    public boolean isButtonUp(int i)
    {
        return i != curSelected;
    }
   
    /**
     * implement's abstract method in Bar
     * @param i : button index in Bar
     * @see Bar
     */
    public void pressButton(int i)
    {
        if (curSelected != i)
        {
            Graphics g = getGraphics();
   
            clearCell(g, curSelected);
            drawUpCell(g, curSelected);
            curSelected = i;
            clearCell(g, curSelected);
            drawDownCell(g, curSelected);
   
            g.dispose();
            setSelectedItem(i); //call child's function
        }
    }
}  

abstract class Bar extends Canvas
{  
    /**
     * button's drawing area size
     */
    final int sizeCell = 30;
    /**
     * bar's drawing area size
     */
    final int sizeBar = 40;
    /**
     * gab between bar border and button border
     */
    int gabBarCell = (sizeBar - sizeCell) / 2;
   
    /**
     * help string array pointer
     */
    private String[] help = null; // showState
    /**
     * current displaying help pointer
     */
    private static String curHelp = null;
    
    /**
     * bar's dispaying stylw
     */
    int direction;
    static final int HORIZONTAL = 1;
    static final int VERTICAL = 2;
    
    /**
     * button count in bar
     */
    private int count;
    /**
     * getCellRect help variable
     */
    private int factorX, factorY;
    
    /**
     * draw button method called when paint button
     * @param g : graphics
     * @param i : button index in Bar
     * @param rc : drawing area
     */
    abstract void drawCell(Graphics g, int i, Rectangle rc);
    /**
     * called when mouse downd the button
     * @param g : graphics
     * @param i : button index in Bar
     * @param rc : drawing area
     */
    abstract public void pressButton(int i);
     
    /**
     * return the button state
     * @param i : button index in Bar
     * @return : true if button is up state
     */
    abstract public boolean isButtonUp(int i);
    /**
     * called when button down
     * set target's content variable
     * @param i : pressed button index in Bar
     */
    abstract void setSelectedItem(int i);
     
    /**
     * Constructs a Bar with button count and direction
     * @param count : button's count
     * @param direction : bars direction (VERTICAL, HORIZONTAL)
     */
    public Bar(int count, int direction)
    {
        this.count = count;
 
        setBackground(Color.lightGray);
        this.direction = direction;
 
        if (direction == HORIZONTAL)
        {
            factorX = 1;
            factorY = 0;
            resize(sizeCell*count + gabBarCell*2, sizeBar);
        }
        else
        {
            factorX = 0;
            factorY = 1;
            resize(sizeBar, sizeCell*count + gabBarCell*2);
        }
    }
    
	public Dimension getDimension() {
		Dimension dimension = new Dimension();
		Dimension dim = size();
		System.out.println(" Dimension of this component = "+dim.width+" "+dim.height);	
		if ( direction == HORIZONTAL ) {
			dimension.width = count*sizeCell + 2*gabBarCell;
			dimension.height = sizeCell + 2*gabBarCell;
		}
		else {
			dimension.width = sizeCell + 2*gabBarCell;
			dimension.height = count*sizeCell + 2*gabBarCell;
			System.out.println("COUNT = "+count);
			System.out.println("SIZECELL = "+sizeCell);
			System.out.println("GABBARCELL = "+gabBarCell);
		}
		System.out.println(" Dimension calculated based on count = "+dimension.width+
							" "+dimension.height);

		return dimension;

	}

    /**
     * set help string
     * @param help : help String
     */
    /*public void setHelp(String[] help) 
    {   
        if (help.length >= count)
            this.help = help;
    }
	*/

    public void paint(Graphics g)
    {
        int i;
        Dimension dm = size();
               
        // draw border
        g.setColor(Color.lightGray);
        g.draw3DRect(0, 0, dm.width - 1, dm.height - 1, true);
 
        // draw each cell
        for (i = 0; i < count; i++)
        {
            if (isButtonUp(i)) // child's method
                drawUpCell(g, i);
            else
                drawDownCell(g, i);
        }
        g.dispose();
    }
    /**
     * draw up shape button
     * it call child;s function drawCell
     * @param g : graphics
     * @param i : button index in Bar
     */
    void drawUpCell(Graphics g, int i)
    {
        Rectangle rc = getCellRect(i);
   
        g.setColor(Color.lightGray);
        g.draw3DRect(rc.x, rc.y, rc.width, rc.height, true);
        drawCell(g, i, getCellInterior(i, true));
    }
    
    /**
     * draw dwon shape button
     * it call child;s function drawCell
     * @param g : graphics
     * @param i : button index in Bar
     */
    void drawDownCell(Graphics g, int i)
    {
        Rectangle rc = getCellRect(i);
   
        g.setColor(Color.lightGray);
        g.draw3DRect(rc.x, rc.y, rc.width, rc.height, false);
        drawCell(g, i, getCellInterior(i, false));
    }
    
    /**
     * clear button
     * @param g : graphics
     * @param i : button index in Bar
     */
    public void clearCell(Graphics g, int i)
    {
        Rectangle rc = getCellRect(i);
 
        g.clearRect(rc.x, rc.y, rc.width, rc.height);
    }
    /**
     * get area of a button
     * @param i : button index in Bar
     * @return button's area
     */
    public Rectangle  getCellRect(int i)
    {
        int  startPos = i*sizeCell;
 
        return new Rectangle(factorX*startPos + gabBarCell, factorY*startPos + 
							gabBarCell, sizeCell - 1, sizeCell - 1);
    }

    /**
     * get drawing area of a button
     * @param i : button index in Bar
     * @param isUp : state of button
     * @return button's area
     */
    public Rectangle  getCellInterior(int i, boolean  isUp)
    {
        int  startPos = i*sizeCell;
        int  factor = (isUp) ? 0 : 1;
 
        return new Rectangle(factorX*startPos + gabBarCell + 4 + factor, 
							factorY*startPos + gabBarCell + 4 + factor,
                             sizeCell - 8, sizeCell - 8);
    }

    /**
     * get left top point of button's area
     * @param i : button index in Bar
     * @return left top point of button's area
     */
    public Point  getCellPos(int i)
    {
        int  startPos;
 
        startPos = i*sizeCell;
        return new Point(factorX*startPos, factorY*startPos);
    }

    /**
     * get button's index by position
     * @param x: x position
     * @param y: y position
     * @return button's index
     */
    protected int getButtonIndex(int x, int y)
    {
        if (direction == HORIZONTAL && x < gabBarCell + sizeCell*count &&
            y > gabBarCell && y < sizeBar - gabBarCell)
        {
            return  (x - gabBarCell) / sizeCell;
        }
        else if (direction == VERTICAL && y < gabBarCell + sizeCell*count &&
                 x > gabBarCell && x < sizeBar - gabBarCell)
        {
            return  (y - gabBarCell) / sizeCell;
        }
        return -1; 
    }

    public boolean mouseDown(Event e, int x, int y)
    {
        int  i = getButtonIndex(x, y);
        if (i != -1)
            pressButton(i);
 
        return true;
    }

    /*
     * Show help in status line
     */
    public boolean mouseMove(Event e, int x, int y)
    {
        /*if (help == null)
        {
            if (curHelp != null)
                WorkPanel.applet.showStatus("");
            return true;
        }
 
        int  i = getButtonIndex(x, y);
        if (i != -1)
        {
            if (curHelp != help[i])
                WorkPanel.applet.showStatus(help[i]);
            curHelp = help[i];
        }
        else
        {
            if (curHelp != null)
                WorkPanel.applet.showStatus("");
            curHelp = null;
        }
		*/
        return true;
    }
}   

