Source code: gui/ComputeButtonListener.java
1 /**
2 * ComputeButtonListener.java
3 *
4 * File creation date: 12.03.2001
5 * End of coding: 26.03.2001
6 * Last minor change: 26.03.2001
7 */
8
9 package gui;
10
11 import java.applet.*;
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.util.*;
15
16 import expression.*;
17 import exception.*;
18
19 /**
20 * This class is listener for "Compute" button
21 * @author Janno Liivak
22 * @version 1.5
23 */
24 public class ComputeButtonListener implements ActionListener
25 {
26 /** applet wher button "Compute" is located */
27 Applet logic;
28 /** location of user input */
29 TextArea inputArea;
30 /** location of program output */
31 TextArea outputArea;
32 /** location of possible tasks */
33 Choice taskChoice;
34 /** expression in a input field */
35 Expression ex;
36 /**
37 * vector that helps to print the syntax tree of expression and truth table
38 */
39 Vector vect;
40 /** label where program status is shown */
41 Label statusLabel;
42
43 /**
44 * Constructs the Compute button listener
45 * @param applet is place where button is located
46 * @param input is location of user input
47 * @param output is location of program output
48 * @param task is locaition of possible tasks for a program
49 * @param status is label with program status
50 */
51 public ComputeButtonListener(Applet applet, TextArea input, TextArea output,
52 Choice task, Label status)
53 {
54 logic = applet;
55 inputArea = input;
56 outputArea = output;
57 taskChoice = task;
58 statusLabel = status;
59 } // constructor
60
61 /**
62 * What to do when button Compute is pressed
63 * @param event button was pressed
64 */
65 public void actionPerformed(ActionEvent event)
66 {
67 String text = inputArea.getText();
68 String[][] table;
69 try
70 {
71 ex = new Expression(text);
72 // Different tasks
73 switch(taskChoice.getSelectedIndex())
74 {
75 //
76 case 0:
77 statusLabel.setText("Arvutan...");
78 outputArea.setText("Viga - Valige funktsioon!");
79 statusLabel.setText("Valmis");
80 break;
81 case 1:
82 statusLabel.setText("Arvutan...");
83 outputArea.setText("");
84 table = ex.getTruthTable();
85 vect = ex.getFullTruthTable();
86 drawFullTruthTable(table, vect);
87 statusLabel.setText("Valmis");
88 break;
89 case 2:
90 statusLabel.setText("Arvutan...");
91 outputArea.setText("");
92 table = ex.getTruthTable();
93 drawTruthTable(table);
94 statusLabel.setText("Valmis");
95 break;
96 case 3:
97 statusLabel.setText("Arvutan...");
98 outputArea.setText("");
99 table = ex.getTruthTable();
100 expressionClass(table);
101 statusLabel.setText("Valmis");
102 break;
103 case 4:
104 statusLabel.setText("Arvutan...");
105 outputArea.setText(wrapLine(ex.getCdnf()));
106 statusLabel.setText("Valmis");
107 break;
108 case 5:
109 statusLabel.setText("Arvutan...");
110 outputArea.setText(wrapLine(ex.getCcnf()));
111 statusLabel.setText("Valmis");
112 break;
113 case 6:
114 statusLabel.setText("Arvutan...");
115 vect = ex.getTree().draw();
116 outputArea.setText("");
117 for (int a = 0; a < vect.capacity(); a++)
118 {
119 outputArea.append((String)vect.elementAt(a) + "\n");
120 }
121 statusLabel.setText("Valmis");
122 break;
123 case 7:
124 statusLabel.setText("Arvutan...");
125 outputArea.setText(ex.getNormalNotation());
126 statusLabel.setText("Valmis");
127 break;
128 case 8:
129 statusLabel.setText("Arvutan...");
130 outputArea.setText(ex.getPolishNotation());
131 statusLabel.setText("Valmis");
132 break;
133 case 9:
134 statusLabel.setText("Arvutan...");
135 outputArea.setText(ex.getRpn());
136 statusLabel.setText("Valmis");
137 break;
138 case 10:
139 statusLabel.setText("Arvutan...");
140 ex.optimize();
141 switch (ex.getInitialNotation())
142 {
143 // input is polish notation (prefix)
144 case 1:
145 outputArea.setText(ex.getPolishNotation());
146 break;
147
148 // input is reverse polish notation (postfix)
149 case 2:
150 outputArea.setText(ex.getRpn());
151 break;
152 // input is normal (infix)
153 case 3:
154 outputArea.setText(ex.getNormalNotation());
155 break;
156 }
157 statusLabel.setText("Valmis");
158 break;
159 default:
160 }
161 }
162 catch (IncorrectExpressionException e)
163 {
164 outputArea.setText("Vigane sisend!!!");
165 statusLabel.setText("Valmis");
166 }
167 catch (VariableLengthException e)
168 {
169 outputArea.setText("Sobimatu muutuja pikkus!");
170 statusLabel.setText("Valmis");
171 }
172 catch (NoVariablesException e)
173 {
174 outputArea.setText("Avaldises ei ole muutujaid!");
175 statusLabel.setText("Valmis");
176 }
177 catch (Exception e)
178 {
179 outputArea.setText("Viga programmi täitmisel!");
180 statusLabel.setText("Valmis");
181 }
182 } // actionPerformed
183
184 /**
185 * This method displays the truth coulumn of the given expression.
186 * @param table is a two dimensional array of strings that contains information
187 * about truth coulumn of the given expression
188 */
189 public void drawTruthTable(String[][] table)
190 {
191 int i, j;
192 for (j = 0; j < table.length - 1; j++)
193 this.outputArea.append(table[j][0] + " ");
194 this.outputArea.append("\n");
195 for (j = 0; j < table.length; j++)
196 this.outputArea.append("--");
197 this.outputArea.append("\n");
198 for (i = 1; i < table[0].length; i++)
199 {
200 for (j = 0; j < table.length - 1; j++)
201 this.outputArea.append(table[j][i] + " ");
202 this.outputArea.append(" " + table[j][i]);
203 if (i < table[0].length - 1)
204 this.outputArea.append("\n");
205 }
206 } // drawTruthTable
207
208 /**
209 * This method displays the full truth table of the given expression
210 * @param table is a two dimensional array of strings that contains information
211 * about truth coulumn of the given expression
212 * @param vect is a Vector containing strings that form the part of the full
213 * truth table
214 */
215 public void drawFullTruthTable(String[][] table, Vector vect)
216 {
217 int i;
218 for (i = 0; i < vect.size(); i++)
219 {
220 this.outputArea.append((String)vect.elementAt(i));
221 if (i < vect.size() - 1)
222 this.outputArea.append("\n");
223 }
224 } // drawFullTruthTable
225
226 /**
227 * This method displays the class of the given expression
228 * @param table is a two dimensional array of strings that contains information
229 * about truth coulumn of the given expression
230 */
231 public void expressionClass(String[][] table)
232 {
233 int zeros = 0;
234 int ones = 0;
235 int i,j;
236 j = table.length - 1;
237 for (i = 1; i < table[0].length; i++)
238 {
239 if (table[j][i] == "0")
240 {
241 zeros++;
242 }
243 else if (table[j][i] == "1")
244 {
245 ones++;
246 }
247 }
248 if ((zeros == 0) && (ones > 0))
249 this.outputArea.setText("Loogiliselt tõene");
250 else if ((zeros > 0) && (ones == 0))
251 this.outputArea.setText("Loogiliselt väär");
252 else
253 this.outputArea.setText("Kontingentne");
254 } // expressionClass
255
256 /**
257 * This method wraps too long lines to a output area width
258 * @param text is a text that is being wrapped
259 * @return returns text vith added newline characters
260 */
261 public String wrapLine(String text)
262 {
263 StringBuffer strbuf = new StringBuffer(text);
264 if (strbuf.length() > 59)
265 {
266 for (int n = strbuf.length() / 59; n > 0; n--)
267 {
268 strbuf = strbuf.insert(n * 59, "\n");
269 } // for
270 text = new String(strbuf);
271 } // if
272 return text;
273 } // wrapLine
274
275 } // class