import java.lang.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import java.text.*; /** * Component: JMeter * * @version 2.0 22 Nov 1999 * @author Raymond Li * * Description: * Displays numbers in a textbox with correct decimal format. Target user cannot * modify the component. Behaves exactly like a JTextField, except the setText() * method has been disabled. * * Constructors: * public JMeter() * public JMeter( Color bg, Color fg ) * public JMeter( int leftAccuracy, int rightAccuracy, Color bg, Color fg ) * * Each one allows the developer more and more control over the charateristics * of the JMeter object. * * Usage: * The setValue( n ) function has been over-loaded to accept multiple data * types. int, short, double, and long are all accepted and displayed * according to the attributes given in left and right accuracy. * * Details: * The component will automatically redraw itself when the left and right * accuracies are mutated. If the number currently in the JMeter field has too * many digits to the left of the decimal place after the left accuracy * attribute has been modified, the number will not be displayed. However, this * is not true for the digits to the right. They will merely be rounded to the * correct decimal place. */ public class JMeter extends JTextField { private int fLeftAccuracy; // number of digit accuracy to left of decimal private int fRightAccuracy; // number of digit accuracy to right of decimal /** * Constants used throughout the class to promote flexibility if need be. */ private static final int DEFAULT_COLUMNS = 5; private static final int DEFAULT_LEFT_ACCURACY = 1; private static final int DEFAULT_RIGHT_ACCURACY = 0; private static final int DEFAULT_COLUMN_OFFSET = 2; private static final Color DEFAULT_BG = Color.black; private static final Color DEFAULT_FG = Color.green; private static final int LEADING = 0; private static final int TRAILING = 1; /** * Default constructor (parameter-less). */ public JMeter() { super(); this.setBackground( DEFAULT_BG ); this.setForeground( DEFAULT_FG ); this.fLeftAccuracy = DEFAULT_LEFT_ACCURACY; this.fRightAccuracy = DEFAULT_RIGHT_ACCURACY; Init(); } /** * Constructor with user-defined background and foreground. * * @param bg initial background color * @param fg initial foreground color, color of text */ public JMeter( Color bg, Color fg ) { super(); this.setBackground( bg ); this.setForeground( fg ); this.fLeftAccuracy = DEFAULT_LEFT_ACCURACY; this.fRightAccuracy = DEFAULT_RIGHT_ACCURACY; Init(); } /** * Constructor with user-defined left and right of decimal accuracy. * * @param leftAccuracy accuracy of digits to the left of decimal * @param rightAccuracy accuracy of digits to the right of decimal */ public JMeter( int leftAccuracy, int rightAccuracy ) { super(); this.setBackground( DEFAULT_BG ); this.setForeground( DEFAULT_FG ); this.fLeftAccuracy = leftAccuracy; this.fRightAccuracy = rightAccuracy; Init(); } /** * Constructor with user-defined accuracy and colors. * * @param leftAccuracy accuracy of digits to the left of decimal * @param rightAccuracy accuracy of digits to the right of decimal * @param bg initial background color * @param fg initial foreground color, color of text */ public JMeter( int leftAccuracy, int rightAccuracy, Color bg, Color fg ) { super(); this.setBackground( bg ); this.setForeground( fg ); this.fLeftAccuracy = leftAccuracy; this.fRightAccuracy = rightAccuracy; Init(); } /** * Initializations common to all constructors. */ protected void Init() { this.setHorizontalAlignment( JTextField.CENTER ); resetMeter(); } /** * Accessor for number left accuracy. * * @return the maximum number of digits to left of decimal */ public int getLeftAccuracy() { return this.fLeftAccuracy; } /** * Accessor for right accuracy. * * @return the maximum number of digits to right of decimal */ public int getRightAccuracy() { return this.fRightAccuracy; } /** * Mutator for left accuracy. */ public void setLeftAccuracy( int n ) { // Make sure there is at least one left of the decimal digit. if ( n >= 1 ) { this.fLeftAccuracy = n; redrawMeter(); } } /** * Mutator for left accuracy. */ public void setRightAccuracy( int n ) { /** * The digits to the right of the decimal should not exceed 10 or be * less than 0. */ if ( n >= 0 && n <= 10 ) { this.fRightAccuracy = n; redrawMeter(); } } /** * Disabled for safety purposes. */ public void setText( String s ) { } /** * Set the value of the meter using data of type short. */ public void setValue( short num ) { this.setValue( (double) num ); } /** * Set the value of the meter using data of type int. */ public void setValue( int num ) { this.setValue( (double) num ); } /** * Set the value of the meter using data of type long. */ public void setValue( long num ) { this.setValue( (double) num ); } /** * Set the value of the meter using data of type double. */ public void setValue( double num ) { String strTotalAccuracy; String strRightAccuracy = Integer.toString( this.fRightAccuracy ); String formatString; String ret; strTotalAccuracy = Integer.toString( this.fLeftAccuracy + this.fRightAccuracy + 1 ); formatString = "%0" + strTotalAccuracy + "." + strRightAccuracy + "f"; Format fmt = new Format( formatString ); ret = fmt.format( num ); if ( this.fRightAccuracy == 0 ) ret = ret.substring( 0, ret.length() - 1 ); super.setText( ret ); } /** * Meter is zero'ed. */ public void resetMeter () { /** * Creating default string to place into Meter. */ this.setColumns( this.fLeftAccuracy + this.fRightAccuracy + DEFAULT_COLUMN_OFFSET ); this.validate(); this.setValue( 0 ); } /** * Redraws the meter. When the user changes the accuracy of the number being displayed, * the TextField (view) as well as the text inside must be re-calculated. * * This is basically the same code as the setValue() method, except due to changes in * the left and/or right accuracy, the textfield needs to be resized. */ public void redrawMeter() { /* String strTotalAccuracy; String strRightAccuracy = Integer.toString( this.fRightAccuracy ); String formatString; strTotalAccuracy = Integer.toString( this.fLeftAccuracy + this.fRightAccuracy + 1 ); formatString = "%0" + strTotalAccuracy + "." + strRightAccuracy + "f"; Format fmt = new Format( formatString ); super.setText( fmt.format( Double.parseDouble( this.getText() ) ) ); */ this.setValue( Double.parseDouble( this.getText() ) ); this.setColumns( this.fLeftAccuracy + this.fRightAccuracy + DEFAULT_COLUMN_OFFSET ); this.validate(); } /** * Disallows special key input. */ protected void processComponentKeyEvent(KeyEvent event) { event.consume(); } /** * Disallows normal user input. */ protected void processInputMethodEvent( InputMethodEvent event ) { event.consume(); } }