/**

+++++++++++++ DownTime applet 1.4 Copyright 2000, 2001 virtig01 +++++++++++++

!!!  DownTime is protected under the GNU General Public License  !!!
!!!  See http://www.gnu.org/copyleft/gpl.html for details  !!!

-----------------------------------------------------------------------------
Description: Downtime is a simple applet that calculates file download times.

DownTime Main Page:  http://virtig01.cjb.net/applets/downTime/
-----------------------------------------------------------------------------

[2002.01.07 9:01 EST]

*/

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.applet.*;
import java.text.*;

public class DownTime extends Applet implements ActionListener, ItemListener {

  float size, modsize;
  DecimalFormat df = new DecimalFormat("0.00");
  String fileName, incsize;
  Color bgColor = Color.gray;
  Color fgColor = Color.white;

  double divisor;
  double sec, min, hour;

  Panel Top = new Panel(new GridLayout(1, 2));
  Panel Bottom = new Panel(new BorderLayout());
  Panel BottomInside = new Panel(new GridLayout(1, 3));
  Panel vPanel = new Panel(new BorderLayout());
  GridLayout grid = new GridLayout(1, 3);
  Choice SpeedBox = new Choice();
  Label Disclaim = new Label("* Times approximate");
  Label NameLabel = new Label();
  Label HourLabel = new Label("? Hours");
  Label MinLabel = new Label("? Minutes");
  Label SecLabel = new Label("? Seconds");
  Button vButton = new Button("About");
  String HourWord, MinWord, SecWord, HourText, MinText, SecText;

  Frame popup = new Frame("About DownTime");
  Panel PopPanel = new Panel(new GridLayout(3, 1));
  Label byLabel = new Label("DownTime applet 1.4", Label.CENTER);
  Label byLabel2 = new Label("(c) 2000, 2001 virtig01", Label.CENTER);
  Label byLabel3 = new Label("Released Under the GPL", Label.CENTER);
  Button visitButton = new Button("Visit virtig01 Main");
  Button closeButton = new Button("Close");

  public void init() {

//Get filesize (in Kilobytes)
	String sizeValue = getParameter("size");
	if (sizeValue != null)
		size = Integer.parseInt(sizeValue);

//Change file size; 1024 KB per MB
	incsize = (size >= 1024) ? "MB" : "KB";
	modsize = (size >= 1024) ? size / 1024 : size;

//Get file name & make label
	String fileNameValue = getParameter("filename");
	fileName = (fileNameValue != null) ? fileNameValue : "this file";
	NameLabel.setText("Download Times for " + fileName + ", " + df.format(modsize) + incsize);

//File size is changed from kilobytes to kilobits to do the math
	size = size*8;

//Do colors
	String bgValue = getParameter("bgcolor");
	if (bgValue != null)
		bgColor = new Color(Integer.parseInt(bgValue,16));

	String fgValue = getParameter("fgcolor");
	if (fgValue != null)
		fgColor = new Color(Integer.parseInt(fgValue,16));

	setBackground(bgColor);
	setForeground(fgColor);


//Do GUI
	//set layout for applet window
	setLayout(new BorderLayout());

	SpeedBox.add("<Select>");
	SpeedBox.add("14.4 Modem");
	SpeedBox.add("28.8 Modem");
	SpeedBox.add("33.6 Modem");
	SpeedBox.add("56k Modem");
	SpeedBox.add("DSL");
	SpeedBox.add("ISDN");
	SpeedBox.add("Cable");
	SpeedBox.add("Satellite");
	SpeedBox.add("T1");
	SpeedBox.add("T3");
	SpeedBox.addItemListener(this);
	SpeedBox.setBackground(bgColor);
	SpeedBox.setForeground(fgColor);
	Top.setBackground(bgColor);
	Top.setForeground(fgColor);

	Top.add(new Label("Your Connection: "));
	Top.add(SpeedBox);

	Bottom.setBackground(fgColor);
	Bottom.setForeground(bgColor);

	BottomInside.add(HourLabel);
	BottomInside.add(MinLabel);
	BottomInside.add(SecLabel);

	Bottom.add(NameLabel, "North");
	Bottom.add(BottomInside, "Center");

	Disclaim.setFont(new Font("Helvetica", Font.ITALIC, 10));
	Bottom.add(Disclaim, "South");

//Add "About" button
	vButton.addActionListener(this);
	vButton.setForeground(fgColor);
	vButton.setBackground(bgColor);
	vPanel.add(vButton, "East");

	//For About popup
	popup.setLayout(new BorderLayout());
	popup.setSize(230, 150);
	popup.setBackground(bgColor);
	popup.setForeground(fgColor);
	popup.setResizable(false);
	byLabel.setFont(new Font("Helvetica", Font.BOLD, 16));
	byLabel2.setFont(new Font("Helvetica", Font.PLAIN, 13));
	byLabel3.setFont(new Font("Helvetica", Font.PLAIN, 11));
	visitButton.setBackground(bgColor);
	visitButton.setForeground(fgColor);
	closeButton.setBackground(bgColor);
	closeButton.setForeground(fgColor);
	visitButton.addActionListener(this);
	closeButton.addActionListener(this);
	PopPanel.add(byLabel);
	PopPanel.add(byLabel2);
	PopPanel.add(byLabel3);
	popup.add(PopPanel, "North");
	popup.add(visitButton, "Center");
	popup.add(closeButton, "East");

//Add all panels to applet window
	add(Top, "North");
	add(Bottom, "Center");
	add(vPanel, "South");

  }


//About Box
  public void actionPerformed(ActionEvent evt) {

		Object source = evt.getSource();

		if (source == vButton) {
			if (popup.isVisible())
				popup.dispose();
			else {
				popup.show();
				popup.setLocation(200, 150);
			}
		}

		else if (source == closeButton) {
			popup.dispose();
		}

		else {
			popup.dispose();
			try {
			URL vir = new URL("http://virtig01.cjb.net");
			AppletContext context = getAppletContext();
			context.showDocument(vir, "_blank");
			}
			catch (MalformedURLException e) { };
		}

  }


//For speed selection	
  public void itemStateChanged(ItemEvent event) {

	String item = (String)event.getItem();

	if (item.equals("14.4 Modem"))
		divisor = 14.062;

	else if (item.equals("28.8 Modem"))
		divisor = 28.125;

	else if (item.equals("33.6 Modem"))
		divisor = 32.812;

	else if (item.equals("56k Modem"))
		divisor = 51.75;

	else if (item.equals("ISDN"))
		divisor = 125;

	else if (item.equals("DSL"))
		divisor = 781.25;

	else if (item.equals("Cable"))
		divisor = 1000;

	else if (item.equals("Satellite"))
		divisor = 390.62;

	else if (item.equals("T1"))
		divisor = 1464.84;

	else if (item.equals("T3"))
		divisor = 41992.18;

	setLables(divisor);

	}


  public void setLables(double div) {

	hour = 0;
	min = 0;

	//Find download time in seconds
	sec = size / div;

	//1 min = 60 secs
	while (sec >=60) {
		min++;
		sec -= 60;
	}

	//1 hour = 60 mins
	while (min >=60) {
		hour++;
		min -= 60;
	}

	//check if singular or plural is needed
	HourWord = (hour > 1) ? " Hours" : " Hour";
	MinWord = (min > 1) ? " Minutes" : " Minute";
	SecWord = (sec > 1) ? " Seconds" : " Second";

	//If # of hours and/or mins is 0, display blank labels
	HourText = ((int)hour == 0) ? " " : (int)hour + HourWord;
	MinText = ((int)min == 0) ? " " : (int)min + MinWord;

	//set the text
	HourLabel.setText(HourText);
	MinLabel.setText(MinText);
	SecLabel.setText((int)sec + SecWord);
  }


}