/**
 *Title:      Idea Simulator
 *Copyright:  Copyright (c) 2000 - Idea Group
 *Company:    IDEA(Interactive Distance Education Aid) - DEES - UFMG
 *Date:       May 2000
 *File:       DecimalField.java
 */
package idea.utils;

import javax.swing.*;
import javax.swing.text.*; 

import java.awt.Toolkit;
import java.text.*;

/**
 * A class representing a field for decimal numbers.
 * For example:
 * <pre>
 *    DecimalField field = new DecimalField(10.25, 8, f);
 *    panel.add(field);
 * </pre>
 *
 * @see    javax.swing.JTextField
 */
public class DecimalField extends JTextField {
    
    /**
     * The number formater
     */
    private NumberFormat format;
    
    /**
     * Constructor method.
     *
     * @param    value    The field value.
     * @param    columns  The number os columns to show.
     * @param    f        The number formater. 
     */
    public DecimalField(double value, int columns, NumberFormat f) {
        super(columns);
        format = f;
        setValue(value);
    }
    
    /**
     * Returns the field value - A double.
     *
     * @return   The field value.
     */
    public double getValue() {
        double retVal = 0.0;
        
        try {
            retVal = format.parse(getText()).doubleValue();
        } catch (ParseException e) {
            // This should never happen because insertString allows
            // only properly formatted data to get in the field.
            Toolkit.getDefaultToolkit().beep();
            System.err.println("getValue: could not parse: " + getText());
        }
        return retVal;
    } //End of getValue
    
    /**
     * Sets the field value - A double.
     *
     * @param    value  The desired value to set.
     */
    public void setValue(double value) {
        setText(format.format(value));
    } //End of setValue        
    
} //End of Class

//==== End of File
