//import java.awt.*;
import marimba.channel.*;
import marimba.gui.*;

public class calculators extends ApplicationPlayerFrame {

    // initialize widget object for event handler
    Widget w = null;

    // initialize values for calcOhms()
    String results = "";
    int multiplier[] = {0,1,2,3,4,5,6,7,8,9,-1,-2};
    String tolerance[] = {"+/-5%","+/-10%","+/-20%"};

    // perform calculation
    public String calcOhms() {
        int tens = ((DropDownListBoxWidget)util.getWidget("tens")).getIntegerValue();
        int ones = ((DropDownListBoxWidget)util.getWidget("ones")).getIntegerValue();
        int mult = ((DropDownListBoxWidget)util.getWidget("multiplier")).getIntegerValue();
        int toler = ((DropDownListBoxWidget)util.getWidget("tolerance")).getIntegerValue();

        double ohmage = ((double)((tens * 10) + ones)) * (Math.pow(10,multiplier[mult]));
        results = format(ohmage);
        results += ", " + tolerance[toler];
        return results;
    }

    // format ohmage value
    private String format(double ohmage) {
        if (ohmage >= 10e+006) {
            ohmage /= 10e+005;
            return ("" + ohmage + " Mohms");
        }
        if (ohmage >= 10e+003) {
            ohmage /= 10e+002;
            return ("" + ohmage + " Kohms");
        }
        return ("" + ohmage + " ohms");
    }

    // Handle events that occur in the presentation.
    public boolean handleEvent(Event evt) {
        switch (evt.id) {
            case Event.ACTION_EVENT:
                // get the container of the event object
                w = ((Widget)evt.target).getParent();
                // handle events for "Resistor Calculator" page
                if (w.getName() != null && (w.getName()).equals("Resistor Calculator")) {
            	    if (evt.target instanceof DropDownListBoxWidget) {
	                     ((TextBoxWidget)util.getWidget("results")).setText(calcOhms());
                    }
                    break;
                }
        }
	    return super.handleEvent(evt);
    }

}
