/* Copyright 2000 Charles G. Wright
* This software may be distributed under the terms of the
* GNU General Public License.
*
* $Id: InputField.java,v 1.1 2000-06-15 10:14:59-05 chuckles Exp chuckles $
*/
import java.math.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/** A suped up text field, containing a label, a text box
* increment and decrement buttons, and a unit label.
* Includes a UnitConverter, and handles conversions
* to/from metric automatically.*/
public class InputField extends Panel implements ActionListener, Observer {
private double increment;
private UnitConverter uc;
private java.awt.Label name;
private java.awt.Label label;
private TextField tf;
private double value;
private ActionListener parent;
private ActionEvent ae;
private Button bleft;
private Button bright;
String text_value;
/**
Create an InputField. Arguments are
*
- unitType - the type of unit being specified in the
* input, as defined in class UnitConverter.
*
- increment - the amount that the field will be incremented or
* decremented when these respective buttons are clicked.*/
public InputField(int unitType, double initialValue, double increment){
setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
// not used yet...
this.increment = increment;
uc = new UnitConverter(unitType);
uc.addObserver(this);
value = initialValue;
text_value = Double.toString(uc.getConvertedValue(initialValue));
name = new Label(uc.getName());
add(name);
bleft = new Button("<");
bleft.addActionListener(this);
add(bleft);
bright = new Button(">");
bright.addActionListener(this);
add(bright);
tf = new TextField(text_value, 6);
tf.addActionListener(this);
add(tf);
label = new Label(uc.getLabel());
add(label);
ae = new ActionEvent(this, 0, uc.getName());
}
public void setNameText(String text){
name.setText(text);
}
public void setLabelText(String text){
label.setText(text);
}
public double getValue(){
return(uc.getMetricValue(Double.valueOf(tf.getText()).doubleValue()));
}
/** Called as a result of the unit system being
* changed. This object is added as an Observer of the
* UnitConverter object contained herein. It will call
* this update() method when the unit system changes. */
public void update(Observable obs, Object arg){
System.out.println("updating units in InputField object");
setLabelText(uc.getLabel());
tf.setText(Double.toString(uc.getConvertedValue(value)));
}
// This method is called after this object is created,
// to register the object that provides the actionPerformed()
// method. I might have just passed "parent" in the
// constructor, but this gives the flexibility to
// put the event handler elsewhere, such as in a
// central event handler object.
// Also, this should handle multiple action listeners
// instead of just one, but...
public void addActionListener(ActionListener parent){
this.parent = parent;
}
/** Here is where we come when the enter key is pressed in the text field. */
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if (source == tf) {
try{
value = uc.getMetricValue(Double.valueOf(tf.getText()).doubleValue());
// If we are here, the value is a valid number. Save the
// String for next time, to restore in the case of a bad value.
text_value = tf.getText();
// Call action listener, to act on new value.
parent.actionPerformed(ae);
} catch (NumberFormatException nfe){
// Restore last good value.
// Should probably also flash error message...
tf.setText(text_value);
}
} else if (source == bleft){
value = uc.getConvertedValue(value - increment);
text_value = Double.toString(value);
tf.setText(text_value);
parent.actionPerformed(ae);
} else if (source == bright){
value = uc.getConvertedValue(value + increment);
text_value = Double.toString(value);
tf.setText(text_value);
parent.actionPerformed(ae);
}
}
}