KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jepexamples > Evaluator


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9
10 /**
11  * Evaluator - JEP Example Applet
12  * Copyright (c) 2004 Nathan Funk
13  *
14  * @author Nathan Funk
15
16 HTML code for running the applet:
17 <applet code="org/nfunk/jepexamples/Evaluator.class" width=400 height=200>
18 </applet>
19
20 */

21 package org.nfunk.jepexamples;
22
23 import java.applet.Applet JavaDoc;
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import org.nfunk.jep.JEP;
28
29
30 /**
31  * This applet is an simple example for how JEP can be used to evaluate
32  * expressions. It also displays the different options, and the effects of
33  * their settings.
34  */

35 public class Evaluator extends Applet JavaDoc {
36
37     /** Parser */
38     private JEP myParser;
39     
40     /** Current xValue */
41     private double xValue;
42
43     /* GUI components */
44     private TextField exprField, xField;
45     private TextArea errorTextArea;
46     private Label resultLabel;
47     private Checkbox implicitCheckbox, undeclaredCheckbox;
48
49     
50     /**
51      * This method is called if the applet is run as an standalone
52      * program. It creates a frame for the applet and adds the applet
53      * to that frame.
54      */

55     public static void main(String JavaDoc 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     /**
75      * The initialization function of the applet. It adds all the
76      * components such as text fields and also creates the JEP object
77      */

78     public void init() {
79         // initialize value for x
80
xValue = 10;
81
82         // add the interface components
83
addGUIComponents();
84         
85         // Set up the parser (more initialization in parseExpression())
86
myParser = new JEP();
87         myParser.initFunTab(); // clear the contents of the function table
88
myParser.addStandardFunctions();
89         myParser.setTraverse(true);
90
91         
92         // simulate changed options to initialize output
93
optionsChanged();
94     }
95
96     /**
97      * Creates and adds the necessary GUI components.
98      */

99     private void addGUIComponents() {
100         setBackground(Color.white);
101         
102         GridBagLayout gridbag = new GridBagLayout();
103         GridBagConstraints c = new GridBagConstraints();
104         setLayout(gridbag);
105
106         // Expression
107
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         // x
119
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         // Result
131
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         // Options
144
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         // Errors
169
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         // Set up listeners
187
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     /**
221      * Parses the current expression in the exprField. This method also
222      * re-initializes the contents of the symbol and function tables. This
223      * is neccessary because the "allow undeclared variables" option adds
224      * variables from expressions to the symbol table.
225      */

226     private void parseExpression() {
227         myParser.initSymTab(); // clear the contents of the symbol table
228
myParser.addStandardConstants();
229         myParser.addComplex(); // among other things adds i to the symbol table
230
myParser.addVariable("x", xValue);
231         myParser.parseExpression(exprField.getText());
232     }
233
234     /**
235      * Whenever the expression is changed, this method is called.
236      * The expression is parsed, and the updateResult() method
237      * invoked.
238      */

239     private void exprFieldTextValueChanged() {
240         parseExpression();
241         updateResult();
242     }
243     
244     /**
245      * Every time the value in the x field is changed, this method is
246      * called. It takes the value from the field as a double, and
247      * sets the value of x in the parser.
248      */

249     private void xFieldTextValueChanged() {
250         
251         try {
252             xValue = Double.valueOf(xField.getText()).doubleValue();
253         } catch (NumberFormatException JavaDoc 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     /**
264      * Every time one of the options is changed, this method is called. The
265      * parser settings are adjusted to the GUI settings, the expression is
266      * parsed again, and the results updated.
267      */

268     private void optionsChanged() {
269         myParser.setImplicitMul(implicitCheckbox.getState());
270         myParser.setAllowUndeclared(undeclaredCheckbox.getState());
271         parseExpression();
272         updateResult();
273     }
274     
275     /**
276      * This method uses JEP's getValueAsObject() method to obtain the current
277      * value of the expression entered.
278      */

279     private void updateResult() {
280         Object JavaDoc result;
281         String JavaDoc errorInfo;
282         
283         // Get the value
284
result = myParser.getValueAsObject();
285         
286         // Is the result ok?
287
if (result!=null) {
288             resultLabel.setText(result.toString());
289         } else {
290             resultLabel.setText("");
291         }
292         
293         // Get the error information
294
if ((errorInfo = myParser.getErrorInfo()) != null) {
295             errorTextArea.setText(errorInfo);
296         } else {
297             errorTextArea.setText("");
298         }
299     }
300 }
301
Popular Tags