
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.awt.*;
import java.net.*;

/**
 * KeypadPanel01 is a button container for a simple calculator.
 *
 * <br><br><a href=../source/polarDust/calculator/KeypadPanel01.java>
 * The source code.</a>
 *
 * @version 0.9, 1996.06.04
 * @author Sorin Lazareanu
 */

public class KeypadPanel01
	extends Panel
	implements CalcConstants{
	Label fillArea;
	Button btnClr, btnCE;
	Button btnMC, btnMR, btnMM, btnMP, btnSqrt;
	Button btnSgn, btnPrc;
	Button btnMul, btnDiv;
	Button btnAdd, btnSub;
	Button btnEqu;
	NumKeypadPanel numKeypad;
	CalcCPU cpu;
	Dimension lastDimension = new Dimension(0,0);
	CalculatorApplet parentApplet = null;

	/**
	* Constructs a new panel. The original call is from an CalculatorApplet.
	* This information is used to send back sound related messages.
	* @see CalculatorApplet
	*/
	public KeypadPanel01(CalcCPU aCPU, CalculatorApplet parentApplet) {
		cpu = aCPU;
		this.parentApplet = parentApplet;
		numKeypad = new NumKeypadPanel(aCPU, parentApplet);
		commonInit();

	}

	/**
	* Constructs a new panel.
	*/
	public KeypadPanel01(CalcCPU aCPU) {
		cpu = aCPU;
		numKeypad = new NumKeypadPanel(aCPU);
		commonInit();

	}

	private void commonInit() {

		fillArea = new Label("");
		btnClr = new Button(STR_CLR);
		btnCE = new Button(STR_CE);

		btnMC = new Button(STR_MC);
		btnMR = new Button(STR_MR);
		btnMM = new Button(STR_MSUB);
		btnMP = new Button(STR_MADD);
		btnSqrt = new Button(STR_SQRT);

		btnSgn = new Button(STR_SGN);
		btnPrc = new Button(STR_PRC);

		btnMul = new Button(STR_MUL);
		btnDiv = new Button(STR_DIV);

		btnAdd = new Button(STR_ADD);
		btnSub = new Button(STR_SUB);

		btnEqu = new Button(STR_EQU);

	}

	/**
	* Prepares the layout of the panel
	*/
	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(2, 2, 2, 2);
		aHelper = new GridBagLayoutHelper(this, gridBag, c, 5, 7);
		setFont(new Font(HELVETICA, Font.PLAIN, 14));

		aHelper.addComponent(0, 0, 3, 1, fillArea);
		aHelper.addComponent(3, 0, 1, 1, btnClr);
		aHelper.addComponent(4, 0, 1, 1, btnCE);

		aHelper.addComponent(0, 1, 1, 1, btnMC);
		aHelper.addComponent(1, 1, 1, 1, btnMR);
		aHelper.addComponent(2, 1, 1, 1, btnMM);
		aHelper.addComponent(3, 1, 1, 1, btnMP);
		aHelper.addComponent(4, 1, 1, 1, btnSqrt);

		c.insets = new Insets(0, 0, 0, 0);
		numKeypad.configure();
		aHelper.addComponent(0, 2, 3, 4, numKeypad);
		c.insets = new Insets(2, 2, 2, 2);
		aHelper.addComponent(3, 2, 1, 1, btnSgn);
		aHelper.addComponent(4, 2, 1, 1, btnPrc);

		aHelper.addComponent(3, 3, 1, 1, btnMul);
		aHelper.addComponent(4, 3, 1, 1, btnDiv);

		aHelper.addComponent(3, 4, 1, 1, btnAdd);
		aHelper.addComponent(4, 4, 1, 1, btnSub);

		aHelper.addComponent(3, 5, 2, 1, btnEqu);
	}

	/**
	* Responds to button click events.
	*/
	public boolean action(Event evt, Object arg) {
		if (evt.target == btnClr) { cpu.c(); return true; };
		if (evt.target == btnCE) { cpu.ce(); return true; };
		if (evt.target == btnAdd) { op(ADD); cpu.add(); return true; };
		if (evt.target == btnSub) { op(SUB); cpu.sub(); return true; };
		if (evt.target == btnMul) { op(MUL); cpu.mul(); return true; };
		if (evt.target == btnDiv) { op(DIV); cpu.div(); return true; };
		if (evt.target == btnPrc) { op(PRC); cpu.prc(); return true; };
		if (evt.target == btnMM) { cpu.msub(); return true; };
		if (evt.target == btnMP) { cpu.madd(); return true; };
		if (evt.target == btnSgn) { cpu.sgn(); return true; };
		if (evt.target == btnMR) { cpu.mr(); return true; };
		if (evt.target == btnMC) { cpu.mc(); return true; };
		if (evt.target == btnSqrt) { cpu.sqrt(); return true; };
		if (evt.target == btnEqu) { op(EQU); cpu.equ(); return true; };
		return false;
	}

	private void op(int iOper) {
		if (parentApplet != null) {
			parentApplet.op(iOper);
		}
	}


	/**
	* Dispatches unintercepted key down events to the components.
	*/
	public boolean dispatchKeyDown(Event evt, int iKey) {
		boolean bDispatched;

		bDispatched = keyDown(evt, iKey);
		if (!bDispatched) bDispatched = numKeypad.dispatchKeyDown(evt, iKey);
		return bDispatched;
	}

	/**
	* Dispatches unintercepted key up events to the components.
	*/
	public boolean dispatchKeyUp(Event evt, int iKey) {
		boolean bDispatched;

		bDispatched = keyUp(evt, iKey);
		if (!bDispatched) bDispatched = numKeypad.dispatchKeyUp(evt, iKey);
		return bDispatched;
	}


	/**
	* Responds to key down events.
	*/
	public boolean keyDown(Event evt, int iKey) {
		if (evt.key == CALC_KEY_CLR) { cpu.c(); return true; };
		if (evt.key == CALC_KEY_CE) { cpu.ce(); return true; };
		if (evt.key == CALC_KEY_ADD) { op(ADD); cpu.add(); return true; };
		if (evt.key == CALC_KEY_SUB) { op(SUB); cpu.sub(); return true; };
		if (evt.key == CALC_KEY_MUL) { op(MUL); cpu.mul(); return true; };
		if (evt.key == CALC_KEY_DIV) { op(DIV); cpu.div(); return true; };
		if (evt.key == CALC_KEY_PRC) { op(PRC); cpu.prc(); return true; };
		if (evt.key == CALC_KEY_EQU) { op(EQU); cpu.equ(); return true; };
		if (evt.key == CALC_KEY_MSUB) { cpu.msub(); return true; };
		if (evt.key == CALC_KEY_MADD) { cpu.madd(); return true; };
		if (evt.key == CALC_KEY_MR) { cpu.mr(); return true; };
		if (evt.key == CALC_KEY_MC) { cpu.mc(); return true; };
		return false;
	}

	/**
	* Paints on a Graphics object.
	*/
	public void paint(Graphics aGraphics) {
		if (lastDimension.width != size().width || lastDimension.height != size().height) {
			int iFontHeight = Math.min( size().height / 12, size().width / 10 );
			Font stdFont = new Font(HELVETICA, Font.PLAIN, iFontHeight);
			btnClr.setFont(stdFont);
			btnCE.setFont(stdFont);
			btnMC.setFont(stdFont);
			btnMR.setFont(stdFont);
			btnMM.setFont(stdFont);
			btnMP.setFont(stdFont);
			btnSqrt.setFont(stdFont);
			btnSgn.setFont(stdFont);
			btnPrc.setFont(stdFont);
			btnMul.setFont(stdFont);
			btnDiv.setFont(stdFont);
			btnAdd.setFont(stdFont);
			btnSub.setFont(stdFont);
			btnEqu.setFont(stdFont);
			lastDimension = size();
		}
		super.paint(aGraphics);
	}

}


