
package polarDust.calculator;

/*
 * Copyright (c) 1996 Sorin Lazareanu, All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies.
 *
 */

import java.applet.*;
import java.awt.*;
import java.net.*;

/**
 * CalculatorApplet is a applet wrapper around calculators.<br>
 * Accepts one parameter: CALCCODE the code of the calculator.<br>
 * Values for the parameter: <br>
 * "01" simple calculator.<br>
 * "02" simple calculator with paper tape.<br>
 * "03" calculator with more operations.<br>
 *
 * <br><br><a href=../source/polarDust/calculator/CalculatoApplet.java>
 * The source code.</a>
 *
 * @version 0.9, 1996.06.04
 * @author Sorin Lazareanu
 */

public class CalculatorApplet extends Applet implements CalcConstants {
	String zeroFile = "zero.au";
	String oneFile = "one.au";
	String twoFile = "two.au";
	String threeFile = "three.au";
	String fourFile = "four.au";
	String fiveFile = "five.au";
	String sixFile = "six.au";
	String sevenFile = "seven.au";
	String eightFile = "eight.au";
	String nineFile = "nine.au";
	String addFile = "add.au";
	String subFile = "sub.au";
	String mulFile = "mul.au";
	String divFile = "div.au";
	String equFile = "equ.au";
	String prcFile = "prc.au";

	SoundList soundList;
	URL anURL;
	CalculatorPanel calcPanel;
	Thread windowThread;

	/**
	* Initializes the applet. Contains one component CalculatorPanelxx.
	* Gets the CALCODE applet parameter and instantiates
    * from CalculatorPanelxx classes - where xx is passed by CALCODE.
	* Starts a thread that loads sounds.
	*/
	public void init() {
		String code;
		AudioClip aClip;

		new CalcHashtable();
		code = getParameter("CALCCODE");
		if (code.equals("01")) calcPanel = new CalculatorPanel01(this);
		if (code.equals("02")) calcPanel = new CalculatorPanel02(this);
		if (code.equals("03")) calcPanel = new CalculatorPanel03(this);
		if (calcPanel == null) calcPanel = new CalculatorPanel01(this);
		configure();
		try {
			anURL = new URL(getCodeBase().toString() + "audio/");
		} catch (MalformedURLException e) { }
		soundList = new SoundList(this, anURL);
		showStatus("Loading sounds");
		soundList.startLoading(zeroFile);
		soundList.startLoading(oneFile);
		soundList.startLoading(twoFile);
		soundList.startLoading(threeFile);
		soundList.startLoading(fourFile);
		soundList.startLoading(fiveFile);
		soundList.startLoading(sixFile);
		soundList.startLoading(sevenFile);
		soundList.startLoading(eightFile);
		soundList.startLoading(nineFile);
		soundList.startLoading(addFile);
		soundList.startLoading(subFile);
		soundList.startLoading(mulFile);
		soundList.startLoading(divFile);
		soundList.startLoading(equFile);
		soundList.startLoading(prcFile);
	}
	/**
	* Configures a CalculatorPanelxx.
	*/
	public void configure() {
		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		GridBagLayoutHelper aHelper;

		setLayout(gridBag);
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.CENTER;
		c.insets = new Insets(0, 0, 0, 0);
		aHelper = new GridBagLayoutHelper(this, gridBag, c, 1, 1);

		aHelper.addComponent(0, 0, 1, 1, calcPanel);
		calcPanel.configure();
	}

	/**
	* Plays a sound for a digit between 0 and 9.
	* @param iDigit the digit.
	*/
	public void digit(int iDigit) {
		AudioClip aClip = null;
		String aFileName = null;
		switch (iDigit) {
			case 1: aFileName = oneFile; break;
			case 2: aFileName = twoFile; break;
			case 3: aFileName = threeFile; break;
			case 4: aFileName = fourFile; break;
			case 5: aFileName = fiveFile; break;
			case 6: aFileName = sixFile; break;
			case 7: aFileName = sevenFile; break;
			case 8: aFileName = eightFile; break;
			case 9: aFileName = nineFile; break;
			case 0: aFileName = zeroFile;
		}
		if (aFileName != null) aClip = soundList.getClip(aFileName);
		if (aClip != null) aClip.play();
	}

	/**
	* Plays a sound for an operator code.
	* @param iDigit the operator code (ADD, SUB, MUL, DIV, EQU, PRC)
	* @see polarDust.calculator.CalcConstants
	*/
	public void op(int iDigit) {
		AudioClip aClip = null;
		String aFileName = null;
		switch (iDigit) {
			case ADD: aFileName = addFile; break;
			case SUB: aFileName = subFile; break;
			case MUL: aFileName = mulFile; break;
			case DIV: aFileName = divFile; break;
			case EQU: aFileName = equFile; break;
			case PRC: aFileName = prcFile;
		}
		if (aFileName != null) aClip = soundList.getClip(aFileName);
		if (aClip != null) aClip.play();
	}

	/**
	* Plays a sound for dot(.) character.
	*/
	public void dot() {
		AudioClip aClip;
		aClip = getAudioClip(anURL, "dot.au");
		if (aClip != null) aClip.play();
	}

	/**
	* Stops the threads in the embedded panels.
	*/
	public void stop() {
		calcPanel.stop();
		super.stop();
	}

	public String toString() {
		return "CalculatorApplet extends " + super.toString();
	}

}

