// --------------------------------------------------------------------------- // NAME : DoubleBox.java // VERSION : 1.0 // AUTHOR : Craig Wilcox // --------------------------------------------------------------------------- // ----------------------- // Import java class libs. // ----------------------- import java.io.*; // for Serializable import java.awt.*; // for TextField import java.awt.event.*; // for key events import java.beans.*; // for PropertyChangeSupport // ------------------ // Define main class. // ------------------ public class DoubleBox extends TextField implements Serializable { // DoubleBox's variables private double m_low; private double m_high; private double m_prev; private PropertyChangeSupport m_pcs; public DoubleBox() { super(); enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK); m_pcs = new PropertyChangeSupport(this); m_prev = 0.0; m_low = -Double.MAX_VALUE; m_high = Double.MAX_VALUE; } /** * Creates the DoubleBox text field * @param init an initial value, * @param size the size of the text field */ public DoubleBox(double init, int size) { super(""+init, size); enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK); m_pcs = new PropertyChangeSupport(this); m_prev = init; m_low = -Double.MAX_VALUE; m_high = Double.MAX_VALUE; } /** * Creates the DoubleBox text field * @param init an initial value, * @param min the minimum double accepted * @param max the maximum double accepted * @param size the size of the text field */ public DoubleBox(double init, double min, double max, int size) { super("" + init, size); enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK); m_pcs = new PropertyChangeSupport(this); m_prev = init; m_low = min; m_high = max; } public void addPropertyChangeListener(PropertyChangeListener listener) { m_pcs.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { m_pcs.removePropertyChangeListener(listener); } /** * Get the double data value * @return the double data value * @remark if invalid, returns the valid prefix (or 0 if none) */ public double getValue() { double value; try { value = Double.valueOf(getText().trim()).doubleValue(); if (value < m_low || value > m_high) throw new NumberFormatException(); } catch (NumberFormatException e) { requestFocus(); this.setText("" + m_prev); return m_prev; } return (double)value; } /** * Set the value of this field and update its corresponding string * @param newVal the newValue for this field, * @return the double data value * @remark if invalid, returns the valid prefix (or 0 if none) */ void setValue(double newVal) { double oldVal = m_prev; this.setText("" + newVal); newVal = getValue(); if (newVal != oldVal) { m_pcs.firePropertyChange("value", new Double (oldVal), new Double(newVal)); m_prev = newVal; } } // Discard non-numeric, non-backspace characters. public void processKeyEvent(KeyEvent event) { char c = event.getKeyChar(); // These events get default processing. if ((c >= '0' && c <= '9') || c == KeyEvent.VK_DELETE || c == KeyEvent.VK_BACK_SPACE || c == '.' || c == '-' || c == KeyEvent.VK_LEFT || c == KeyEvent.VK_RIGHT) { return; } // This event gets default processing, but the setValue() // accessor is also called. if ((c == KeyEvent.VK_ENTER) || (c == KeyEvent.VK_TAB)){ double d = getValue(); setValue(d); return; } // All other characters we discard if (c == KeyEvent.VK_SEMICOLON) setValue(2.7); event.consume(); } }