1 9 10 21 package org.nfunk.jepexamples; 22 23 import java.applet.Applet ; 24 import java.awt.*; 25 import java.awt.event.*; 26 27 import org.nfunk.jep.JEP; 28 29 30 35 public class Evaluator extends Applet { 36 37 38 private JEP myParser; 39 40 41 private double xValue; 42 43 44 private TextField exprField, xField; 45 private TextArea errorTextArea; 46 private Label resultLabel; 47 private Checkbox implicitCheckbox, undeclaredCheckbox; 48 49 50 55 public static void main(String args[]) { 56 Evaluator a = new Evaluator(); 57 a.init(); 58 a.start(); 59 60 Frame f = new Frame("Evaluator"); 61 f.add("Center", a); 62 f.setSize(400,200); 63 f.addWindowListener( 64 new WindowAdapter() { 65 public void windowClosing(WindowEvent e) { 66 System.exit(0); 67 } 68 } 69 ); 70 71 f.show(); 72 } 73 74 78 public void init() { 79 xValue = 10; 81 82 addGUIComponents(); 84 85 myParser = new JEP(); 87 myParser.initFunTab(); myParser.addStandardFunctions(); 89 myParser.setTraverse(true); 90 91 92 optionsChanged(); 94 } 95 96 99 private void addGUIComponents() { 100 setBackground(Color.white); 101 102 GridBagLayout gridbag = new GridBagLayout(); 103 GridBagConstraints c = new GridBagConstraints(); 104 setLayout(gridbag); 105 106 c.fill = GridBagConstraints.HORIZONTAL; 108 c.weightx = 0.0; 109 Label exprFieldp = new Label("Expression: ", Label.RIGHT); 110 gridbag.setConstraints(exprFieldp,c); 111 add(exprFieldp); 112 113 c.weightx = 0.8; 114 exprField = new TextField(27); 115 gridbag.setConstraints(exprField,c); 116 add(exprField); 117 118 c.weightx = 0.0; 120 Label xFieldp = new Label("x: ", Label.RIGHT); 121 gridbag.setConstraints(xFieldp,c); 122 add(xFieldp); 123 124 c.weightx = 0.2; 125 c.gridwidth = GridBagConstraints.REMAINDER; 126 xField = new TextField("" + xValue,4); 127 gridbag.setConstraints(xField,c); 128 add(xField); 129 130 c.weightx = 0.0; 132 c.gridwidth = 1; 133 Label resultLabelText = new Label("Result: ", Label.RIGHT); 134 gridbag.setConstraints(resultLabelText,c); 135 add(resultLabelText); 136 137 c.weightx = 1.0; 138 c.gridwidth = GridBagConstraints.REMAINDER; 139 resultLabel = new Label("", Label.LEFT); 140 gridbag.setConstraints(resultLabel,c); 141 add(resultLabel); 142 143 c.weightx = 0.0; 145 c.gridwidth = 1; 146 Label optionsLabelText = new Label("Options: ", Label.RIGHT); 147 gridbag.setConstraints(optionsLabelText,c); 148 add(optionsLabelText); 149 150 c.weightx = 1.0; 151 c.gridwidth = GridBagConstraints.REMAINDER; 152 implicitCheckbox = new Checkbox("Implicit multiplication", true); 153 gridbag.setConstraints(implicitCheckbox,c); 154 add(implicitCheckbox); 155 156 c.weightx = 0.0; 157 c.gridwidth = 1; 158 Label spaceLabelText = new Label(" ", Label.RIGHT); 159 gridbag.setConstraints(spaceLabelText,c); 160 add(spaceLabelText); 161 162 c.weightx = 1.0; 163 c.gridwidth = GridBagConstraints.REMAINDER; 164 undeclaredCheckbox = new Checkbox("Allow undeclared identifiers"); 165 gridbag.setConstraints(undeclaredCheckbox,c); 166 add(undeclaredCheckbox); 167 168 c.weightx = 0.0; 170 c.gridwidth = 1; 171 c.anchor = GridBagConstraints.NORTH; 172 Label errorLabel = new Label("Errors: ", Label.RIGHT); 173 gridbag.setConstraints(errorLabel,c); 174 add(errorLabel); 175 176 c.fill = GridBagConstraints.BOTH; 177 c.weightx = 1.0; 178 c.weighty = 1.0; 179 c.gridwidth = GridBagConstraints.REMAINDER; 180 errorTextArea = new TextArea(""); 181 errorTextArea.setEditable(false); 182 errorTextArea.setBackground(Color.white); 183 gridbag.setConstraints(errorTextArea,c); 184 add(errorTextArea); 185 186 exprField.addTextListener( 188 new TextListener() { 189 public void textValueChanged(TextEvent evt) { 190 exprFieldTextValueChanged(); 191 } 192 } 193 ); 194 195 xField.addTextListener( 196 new TextListener() { 197 public void textValueChanged(TextEvent evt) { 198 xFieldTextValueChanged(); 199 } 200 } 201 ); 202 203 implicitCheckbox.addItemListener( 204 new ItemListener() { 205 public void itemStateChanged(ItemEvent evt) { 206 optionsChanged(); 207 } 208 } 209 ); 210 211 undeclaredCheckbox.addItemListener( 212 new ItemListener() { 213 public void itemStateChanged(ItemEvent evt) { 214 optionsChanged(); 215 } 216 } 217 ); 218 } 219 220 226 private void parseExpression() { 227 myParser.initSymTab(); myParser.addStandardConstants(); 229 myParser.addComplex(); myParser.addVariable("x", xValue); 231 myParser.parseExpression(exprField.getText()); 232 } 233 234 239 private void exprFieldTextValueChanged() { 240 parseExpression(); 241 updateResult(); 242 } 243 244 249 private void xFieldTextValueChanged() { 250 251 try { 252 xValue = Double.valueOf(xField.getText()).doubleValue(); 253 } catch (NumberFormatException e) { 254 System.out.println("Invalid format in xField"); 255 xValue = 0; 256 } 257 258 myParser.addVariable("x", xValue); 259 260 updateResult(); 261 } 262 263 268 private void optionsChanged() { 269 myParser.setImplicitMul(implicitCheckbox.getState()); 270 myParser.setAllowUndeclared(undeclaredCheckbox.getState()); 271 parseExpression(); 272 updateResult(); 273 } 274 275 279 private void updateResult() { 280 Object result; 281 String errorInfo; 282 283 result = myParser.getValueAsObject(); 285 286 if (result!=null) { 288 resultLabel.setText(result.toString()); 289 } else { 290 resultLabel.setText(""); 291 } 292 293 if ((errorInfo = myParser.getErrorInfo()) != null) { 295 errorTextArea.setText(errorInfo); 296 } else { 297 errorTextArea.setText(""); 298 } 299 } 300 } 301 | Popular Tags |