/*
 * Copyright (c) 1997, Subrahmanyam Allamaraju. All Rights Reserved.
 * 
 * Permission to use, copy, modify, and distribute this software for
 * NON-COMMERCIAL purposes and without fee is hereby granted provided that this
 * copyright notice appears in all copies.
 *
 * This software is intended for demonstration purposes only, and comes without
 * any explicit or implicit warranty.
 *
 * Please report any bugs to subrahmanyam@geocities.com
 *
 */


import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.util.*;
import java.net.*;
import java.io.*;

    
public class ButtonBar extends Applet 
{

    SleekButton autoButtons[], mailButton, homeButton;
    TitleBar title;
    boolean doHint = false;

    String hint;
    Rectangle hintRect;
    int xpos, ypos;
    Font sFont;
    

    public void init() 
	{
	    Vector urlVector = new Vector();
	    String url, name, s;
	    Color tcolor = Color.black;
	    Color bcolor = Color.gray;
	    Color backColor = Color.white;
	    String textFont = "Helvetica";
	    int textSize = 12;
	    String titleFont = "Helvetica";
	    int titleSize = 15; 
	    int fill = 2;

	    String position = "left";
	    String align = "left";
	   
	    URL codebase = this.getCodeBase();
	    	    
	    int borderWidth = Integer.parseInt(getParameter("borderWidth"));
	    
	    s = getParameter("buttonColor");
	    if(s != null) bcolor = parseColor(s);
	    
	    s = getParameter("textColor");
	    if(s != null) tcolor = parseColor(s);
	    
	    s = getParameter("backColor");
	    if(s != null) backColor = parseColor(s);
	    
	    s = getParameter("textFont");
	    if(s != null) {
		int end = s.indexOf(",");
		textFont = s.substring(0, end);
		textSize = Integer.parseInt(s.substring(end+1));
	    }
	    
	    s = getParameter("fill");
	    if(s != null) {
		fill = Integer.parseInt(s);
	    }

	    s = getParameter("position");
	    if (s != null) 
		if(s.equals("left") || s.equals("right") || s.equals("top"))
		    position = s; 

	    s = getParameter("align");
	    if (s != null) 
		if(s.equals("left") || s.equals("right") || s.equals("center"))
		    align = s; 

	    String target = getParameter("target");
	    
	    int num = 0;
	    while(true) {
		s = getParameter("url" + num);
		if(s == null) {
		    break;
		}
		else {
		    int end = s.indexOf(",");
		    String hint = null;
		    name = s.substring(0, end);
		    s = s.substring(end + 1);
		    end = s.indexOf(",");
		    
		    if(end != -1 && s.length() > end) {
			url = s.substring(0, end).trim();
			url = codebase.toString().trim() + url;
			if(s.length() > end) hint= s.substring(end+1);
			urlVector.addElement(new SleekButton(name, url, true,
							 hint)); 
		    } else {
			url = s.substring(end + 1).trim();
			url = codebase.toString().trim() + url;
			urlVector.addElement(new SleekButton(name, url, true,
							 hint)); 
		    }
		    num++;
		}
	    }

	    
	    s = getParameter("mailto");
	    
	    if(s != null) {
		int end = s.indexOf(",");
		String hint = null;
		name = s.substring(0, end);
		s = s.substring(end + 1);
		end = s.indexOf(",");
		if(end != -1 && s.length() > end) {
		    url = s.substring(0, end).trim();
		    if(s.length() > end) hint = s.substring(end + 1);
		    urlVector.addElement(new SleekButton(name, url, true,
							 hint)); 
		}
		else {
		    url = s.substring(end + 1).trim();
		    urlVector.addElement(new SleekButton(name, url, true,
							 hint));
		}
	    }
	    

	    autoButtons = new SleekButton[urlVector.size()];
	    urlVector.copyInto(autoButtons);	    

	    /*
	     * Set all buttons.
	     */
	    autoButtons[0].setFont(textFont, textSize);
	    autoButtons[0].setParent(this);
	    autoButtons[0].setTarget(target);
	    autoButtons[0].setBorderWidth(borderWidth);
	    autoButtons[0].setBorderColor(bcolor);
	    autoButtons[0].setTextColor(tcolor);
	    autoButtons[0].setAlign(align);
	    
	    for(int i = 0; i < autoButtons.length; i++) {
		autoButtons[i].setButton(); 
		autoButtons[i].setShadow(true);
	    }
	    
	     
	    boolean hasTitle = false;

	    String st = getParameter("title");
	    if(st != null) {
		s = getParameter("titleFont");
		if(s != null) {
		    int end = s.indexOf(",");
		    titleFont = s.substring(0, end);
		    titleSize = Integer.parseInt(s.substring(end+1));
		}

		s = getParameter("titleMessage");

		title = new TitleBar(this, st, titleFont, titleSize, s, position);
		title.resize(40,200);
		title.createImages();
		
		hasTitle = true;
		
	    }

	    if(hasTitle) {
		int h = title.getHeight();
		h = (int) ((new Integer(h).doubleValue()) / 
			   (new Integer(autoButtons.length).doubleValue()));
		
		h = (autoButtons[0].getHeight() > h) ?
		    autoButtons[0].getHeight() : h;
		
		h = (int) ((new Integer(this.size().height).doubleValue()) / 
			   (new Integer(autoButtons.length).doubleValue()));
		for(int i = 0; i < autoButtons.length; i++)
		    autoButtons[i].resize(autoButtons[0].getWidth(), h);
	    }
	    else {
		for(int i = 0; i < autoButtons.length; i++)
		    autoButtons[i].resize(autoButtons[0].getWidth(), 30); 
	    }

	    /*
	     * Add the components now.
	     */

	    if(position.equals("top")) {
		setLayout(new GridLayout(1, autoButtons.length + 2, 0, 0));
		
		if(homeButton != null) add(homeButton);
		
		for(int i = 0; i < autoButtons.length; i++) add(autoButtons[i]);
		
		if(mailButton != null) add(mailButton);
	    }
	    else {
		setLayout(new BorderLayout());
		if(hasTitle) {
		    if(position.equals("left")) add("West", title);
		    else if(position.equals("right")) add("East", title);
		}
		
		int h = 0;
		for(int i = 0; i < autoButtons.length; i++)
		    h += autoButtons[i].getHeight();

		Panel p = new Panel();
		p.setLayout(new GridLayout(0, 1));
		
		for(int i = 0; i < autoButtons.length; i++) 
		    p.add(autoButtons[i]);
		add("Center", p);
		
	    }
	}

    Color parseColor(String s) 
	{
	    if(s != null) {
		int end = s.indexOf(",");
		int sr = Integer.parseInt(s.substring(0, end));
		s = s.substring(end+1);
		end = s.indexOf(",");
		int sg = Integer.parseInt(s.substring(0, end));
		int sb = Integer.parseInt(s.substring(end+1));
		return new Color(sr, sg, sb);
	    }
	    else return null;
	}

   
    public void paint(Graphics g) 
	{
	    for(int i = 0; i < autoButtons.length; i++) {
		autoButtons[i].setButton();
		autoButtons[i].repaint();
	    }
	    
	    if(doHint) {
		if(hintRect != null && hint != null) {
		    g.setColor(Color.black);
		    g.drawRect(20, 20, 30, 30);
		    
		    g.drawRect(hintRect.x - 1, hintRect.y - 1, hintRect.width +
			       1, hintRect.height + 1);
		    g.setColor(Color.yellow);
		    g.fillRect(hintRect.x, hintRect.y, hintRect.width,
			       hintRect.height); 
		    g.setColor(Color.black);
		    g.setFont(sFont);
		    
		    g.drawString(hint, xpos, ypos);
		}
		
	    }
	}

     public void showHint(String hint, Rectangle hintRect, int xpos, int ypos,
			   Font sFont, Graphics g) 
	{
	    this.hintRect = hintRect;
	    this.hint = hint;
	    this.xpos = xpos;
	    this.ypos = ypos;
	    this.sFont = sFont;
	    
	    doHint = true;
	}
}

	    
    
