/*
Copyright (c) 2002 David Drysdale

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the author shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the author.
*/

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

public class Converter extends Applet {
    public boolean debugMode = false;

    float currentValue = (float)0.0;

    TextField hexField;
    TextField floatField;

    Component theFrame;
    
    public String[][] getParameterInfo() {
        String[][] info = {
            {"debug",  "true false", "whether to display debug info"}
        };
        return info;
    }

    public String getAppletInfo() {
        return "Converter v1.0 (c) David Drysdale 2002";
    }

    public void init() {
        if (debugMode) System.out.println("Init applet");
        // read in parameters
        Boolean dm = new Boolean(this.getParameter("debug"));
        debugMode = dm.booleanValue();

        // find an owning frame
        if (debugMode) System.out.println("Find owning frame");
        theFrame = getParent();
        while ((theFrame != null) && !(theFrame instanceof Frame)) {
            theFrame = theFrame.getParent();
        }
        setLayout(new BorderLayout());

        Panel hexPanel = new Panel();
        hexPanel.add(new Label("Hex value:   0x"));
        hexField = new TextField(9);
        hexPanel.add(hexField);

        Panel floatPanel = new Panel();
        floatPanel.add(new Label("Float value:"));
        floatField = new TextField(20);
        floatPanel.add(floatField);

        if (debugMode) System.out.println("Add top and bottom panels");
        add("North", hexPanel);
        add("South", floatPanel);

        if (debugMode) System.out.println("Time to show()");
        updateValues();
        show();
    }

    public boolean action(Event e, Object what) {
        boolean processed = true;

        if (debugMode) System.out.println("Examine event "+e+" on object "+what);
        if (e.target == floatField) {
            Float d = new Float(floatField.getText());
            currentValue = d.floatValue();
        } else if (e.target == hexField) {
            String s = hexField.getText();
            if (debugMode) System.out.println("Hex value: "+s);
            try {
                int ii = Integer.parseInt(s, 16);
                currentValue = Float.intBitsToFloat(ii);
            } catch (NumberFormatException exception) {}
        } else {
            processed = false;
        }
        if (processed) updateValues();
        return(processed);
    }
    public void updateValues() {
        floatField.setText(String.valueOf(currentValue));
        String s = Integer.toHexString(Float.floatToIntBits(currentValue)); 
        while (s.length() < 8) s = "0" + s;
        hexField.setText(s);
    }
}

