/**
 * ============================================================================
 *                   Software License
 * ============================================================================
 *
 *    Copyright (C) 2001 Johannes Zemlin. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modifica-
 * tion, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. The end-user documentation included with the redistribution, if any, must
 *    include the following  acknowledgment: "This product includes software
 *    developed by Johannes Zemlin (http://zemlin-online.de/)."
 *    Alternately, this acknowledgment may appear in the software itself, if
 *    and wherever such third-party acknowledgments normally appear.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE  DISCLAIMED. IN NO  EVENT SHALL
 * JOHANNES ZEMLIN OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
 * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR  PROFITS; OR BUSINESS INTERRUPTION)  HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER  IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package zemlin.fritz;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;

/**
 *@author     <A href=mailto:Johannes@zemlin-online.de>Johannes Zemin</A>
 *@created    20. August 2001
 */
public class ParameterPanel extends Panel {

    // defocus (total)
    private TextField Tdelta = new TextField("2.7", 10);
    // two fold astigmatism (total)
    private TextField TdeltaA = new TextField("0.23", 10);
    // two fold astigmatism (deg.)
    private TextField TfiA = new TextField("-88.0", 10);
    // tilt
    private TextField TR0 = new TextField("1.1", 10);
    // coma (total)
    private TextField TB = new TextField("0.68", 10);
    // coma (deg.)
    private TextField TfiB = new TextField("-75.0", 10);
    // three fold astigmatism (total)
    private TextField TA3 = new TextField("0.06", 10);
    // three fold astigmatism (deg.)
    private TextField TfiA3 = new TextField("13.0", 10);

    private Label error = new Label();


    /**
     *  Constructor for the ParameterPanel object
     */
    public ParameterPanel() {
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(gbl);

        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(2, 2, 2, 2);

        gbc.gridx = 0;
        gbc.gridy = 0;
        add(new Label("defocus", Label.RIGHT), gbc);
        gbc.gridx = 2;
        gbc.gridy = 0;
        add(Tdelta, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        add(new Label("two-fold astigmatism", Label.RIGHT), gbc);
        gbc.gridx = 2;
        gbc.gridy = 1;
        add(TdeltaA, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        add(new Label("azimuth of two-fold astigmatism (deg.)", Label.RIGHT), gbc);
        gbc.gridx = 2;
        gbc.gridy = 2;
        add(TfiA, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        add(new Label("radius of the tableau", Label.RIGHT), gbc);
        gbc.gridx = 2;
        gbc.gridy = 3;
        add(TR0, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        add(new Label("axial coma", Label.RIGHT), gbc);
        gbc.gridx = 2;
        gbc.gridy = 4;
        add(TB, gbc);

        gbc.gridx = 0;
        gbc.gridy = 5;
        add(new Label("azimuth of axial coma (deg.)", Label.RIGHT), gbc);
        gbc.gridx = 2;
        gbc.gridy = 5;
        add(TfiB, gbc);

        gbc.gridx = 0;
        gbc.gridy = 6;
        add(new Label("three-fold astigmatism", Label.RIGHT), gbc);
        gbc.gridx = 2;
        gbc.gridy = 6;
        add(TA3, gbc);

        gbc.gridx = 0;
        gbc.gridy = 7;
        add(new Label("azimuth of three-fold astigmatism (deg.)", Label.RIGHT), gbc);
        gbc.gridx = 2;
        gbc.gridy = 7;
        add(TfiA3, gbc);

        //
        gbc.gridx = 1;
        gbc.gridy = 0;
        add(new Label("D", Label.RIGHT), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        add(new Label("A2", Label.RIGHT), gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        add(new Label("alfa2 ", Label.RIGHT), gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        add(new Label("tilt", Label.RIGHT), gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        add(new Label("B ", Label.RIGHT), gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        add(new Label("beta", Label.RIGHT), gbc);

        gbc.gridx = 1;
        gbc.gridy = 6;
        add(new Label("A3", Label.RIGHT), gbc);

        gbc.gridx = 1;
        gbc.gridy = 7;
        add(new Label("alfa3", Label.RIGHT), gbc);

        //
        gbc.gridx = 0;
        gbc.gridy = 8;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        error.setForeground(Color.red);
        add(error, gbc);
    }


    /**
     *  Sets the Error attribute of the ParameterPanel object
     *
     *@param  e  The new Error value
     */
    public void setError(String e) {
        error.setText(e);
    }


    /**
     *  Sets the Delta attribute of the ParameterPanel object
     *
     *@param  d  The new Delta value
     */
    public void setDelta(double d) {
        Tdelta.setText(Double.toString(d));
    }


    /**
     *  Sets the DeltaA attribute of the ParameterPanel object
     *
     *@param  d  The new DeltaA value
     */
    public void setDeltaA(double d) {
        TdeltaA.setText(Double.toString(d));
    }


    /**
     *  Sets the FiA attribute of the ParameterPanel object
     *
     *@param  d  The new FiA value
     */
    public void setFiA(double d) {
        TfiA.setText(Double.toString(d));
    }


    /**
     *  Sets the R0 attribute of the ParameterPanel object
     *
     *@param  d  The new R0 value
     */
    public void setR0(double d) {
        TR0.setText(Double.toString(d));
    }


    /**
     *  Sets the B attribute of the ParameterPanel object
     *
     *@param  d  The new B value
     */
    public void setB(double d) {
        TB.setText(Double.toString(d));
    }


    /**
     *  Sets the FiB attribute of the ParameterPanel object
     *
     *@param  d  The new FiB value
     */
    public void setFiB(double d) {
        TfiB.setText(Double.toString(d));
    }


    /**
     *  Sets the A3 attribute of the ParameterPanel object
     *
     *@param  d  The new A3 value
     */
    public void setA3(double d) {
        TA3.setText(Double.toString(d));
    }


    /**
     *  Sets the FiA3 attribute of the ParameterPanel object
     *
     *@param  d  The new FiA3 value
     */
    public void setFiA3(double d) {
        TfiA3.setText(Double.toString(d));
    }


    // Defokussierung (Stärke)
    /**
     *  Gets the Delta attribute of the ParameterPanel object
     *
     *@return                            The Delta value
     *@exception  NumberFormatException  Description of Exception
     */
    public double getDelta() throws NumberFormatException {
        return read(Tdelta.getText(), "defocus");
    }


    // Astigmatismus (Stärke)
    /**
     *  Gets the DeltaA attribute of the ParameterPanel object
     *
     *@return                            The DeltaA value
     *@exception  NumberFormatException  Description of Exception
     */
    public double getDeltaA() throws NumberFormatException {
        return read(TdeltaA.getText(), "two-fold astigmatism");
    }


    // Astigmatismus (Richtung)
    /**
     *  Gets the FiA attribute of the ParameterPanel object
     *
     *@return                            The FiA value
     *@exception  NumberFormatException  Description of Exception
     */
    public double getFiA() throws NumberFormatException {
        return readDeg(TfiA.getText(), "azimuth of two-fold astigmatism");
    }


    // Kippung (Stärke)
    /**
     *  Gets the R0 attribute of the ParameterPanel object
     *
     *@return                            The R0 value
     *@exception  NumberFormatException  Description of Exception
     */
    public double getR0() throws NumberFormatException {
        return read(TR0.getText(), "tilt");
    }


    // Koma (Stärke)
    /**
     *  Gets the B attribute of the ParameterPanel object
     *
     *@return                            The B value
     *@exception  NumberFormatException  Description of Exception
     */
    public double getB() throws NumberFormatException {
        return read(TB.getText(), "axial coma");
    }


    // Koma (Richtung)
    /**
     *  Gets the FiB attribute of the ParameterPanel object
     *
     *@return                            The FiB value
     *@exception  NumberFormatException  Description of Exception
     */
    public double getFiB() throws NumberFormatException {
        return readDeg(TfiB.getText(), "azimuth of axial coma");
    }


    // dreizähliger Astigmatismus (Stärke)
    /**
     *  Gets the A3 attribute of the ParameterPanel object
     *
     *@return                            The A3 value
     *@exception  NumberFormatException  Description of Exception
     */
    public double getA3() throws NumberFormatException {
        return read(TA3.getText(), "three-fold astigmatism");
    }


    // dreizähliger Astigmatismus (Richtung)
    /**
     *  Gets the FiA3 attribute of the ParameterPanel object
     *
     *@return                            The FiA3 value
     *@exception  NumberFormatException  Description of Exception
     */
    public double getFiA3() throws NumberFormatException {
        return readDeg(TfiA3.getText(), "azimuth of three-fold astigmatism");
    }


    /**
     *  Description of the Method
     *
     *@param  text                       Description of Parameter
     *@param  label                      Description of Parameter
     *@return                            Description of the Returned Value
     *@exception  NumberFormatException  Description of Exception
     */
    private double readDeg(String text, String label) throws NumberFormatException {
        return Math.PI * read(text, label) / 180;
    }


    /**
     *  Description of the Method
     *
     *@param  text                       Description of Parameter
     *@param  label                      Description of Parameter
     *@return                            Description of the Returned Value
     *@exception  NumberFormatException  Description of Exception
     */
    private double read(String text, String label) throws NumberFormatException {
        try {
            return new Double(text).doubleValue();
            // not available in java 1.1
            //return Double.parseDouble(text);
        } catch (NumberFormatException e) {
            error.setText("invalid input: " + label);
            throw e;
        }
    }
}

