package com.dwave.gui; import java.awt.event.FocusListener; import java.awt.event.FocusEvent; import java.util.StringTokenizer; import javax.swing.JTextField; import javax.swing.BorderFactory; import javax.swing.text.Document; import com.dwave.util.ParseFunction; import com.dwave.util.ParseException; public class DMathTextField extends DTextField { public static final int INTEGER = 0, DOUBLE = 1, FLOAT = 2; private int objectType = 1; /** * Constructs a new TextField. A default model is created, the initial * string is null, and the number of columns is set to 0. */ public DMathTextField() { this(null, null, 0); } /** * Constructs a new TextField initialized with the specified text. * A default model is created and the number of columns is 0. * * @param text the text to be displayed, or null */ public DMathTextField(String text) { this(null, text, 0); } /** * Constructs a new empty TextField with the specified number of columns. * A default model is created and the initial string is set to null. * * @param columns the number of columns to use to calculate * the preferred width. If columns is set to zero, the * preferred width will be whatever naturally results from * the component implementation. */ public DMathTextField(int columns) { this(null, null, columns); } /** * Constructs a new TextField initialized with the specified text * and columns. A default model is created. * * @param text the text to be displayed, or null * @param columns the number of columns to use to calculate * the preferred width. If columns is set to zero, the * preferred width will be whatever naturally results from * the component implementation. */ public DMathTextField(String text, int columns) { this(null, text, columns); } /** * Constructs a new DMathTextField that uses the given text storage * model and the given number of columns. This is the constructor * through which the other constructors feed. If the document is null, * a default model is created. * * @param doc the text storage to use. If this is null, a default * will be provided by calling the createDefaultModel method. * @param text the initial string to display, or null * @param columns the number of columns to use to calculate * the preferred width >= 0. If columns is set to zero, the * preferred width will be whatever naturally results from * the component implementation. * @exception IllegalArgumentException if columns < 0 */ public DMathTextField(Document doc, String text, int columns) { super(doc, text, columns); this.addFocusListener(this); this.setBorder(BorderFactory.createLoweredBevelBorder()); } public double getTextEvaluatedNumerically() throws ParseException { ParseFunction parser = new ParseFunction(this.getText()); if(parser.parse()) return parser.getResult(); else throw new ParseException("Unable to parse expression: "+this.getText()); } public void setTextToEvaluation() throws ParseException { ParseFunction parser = new ParseFunction(this.getText()); if(parser.parse()) { switch(objectType) { case(DOUBLE): this.setText(new Double(parser.getResult()).toString()); break; case(INTEGER): this.setText(new Integer((int)parser.getResult()).toString()); break; case(FLOAT): this.setText(new Float((float)parser.getResult()).toString()); break; } this.setCaretPosition(0); } else throw new ParseException("Unable to parse expression: "+this.getText()); } /** * Invoked when a component loses the keyboard focus. */ public void focusLost(FocusEvent fe) { try { if(this.getText() != null) this.setTextToEvaluation(); else this.setText("0.0"); } catch(Exception e) { this.setText("0.0"); } } public boolean setNumericType(int type) { if(type >= 0 && type <= 3) { objectType = type; return true; } return false; } public int getNumericType() { return objectType; } }