Source code: org/hartmath/CalculatorPanel.java
1 /*
2 * CalculatorPanel.java
3 * Copyright (C) 2001, Klaus Hartlage
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 package org.hartmath;
20
21 import javax.swing.text.*;
22 import javax.swing.*;
23 import java.awt.event.*;
24 import java.awt.*;
25 import java.io.*;
26 import java.util.*;
27
28 import com.hartmath.*;
29 import com.hartmath.lib.C;
30 import com.hartmath.expression.HObject;
31 import com.hartmath.expression.HJavaObject;
32 import com.hartmath.expression.HString;
33 import com.hartmath.expression.HFunction;
34
35 /**
36 * Description of the Class
37 *
38 *@author khartlage
39 *@created 2. Oktober 2001
40 */
41 public class CalculatorPanel extends JPanel {
42
43 private HartMathPanel parent;
44 private CalcButton bin;
45 private CalcButton oct;
46 private CalcButton hex;
47 private CalcButton numeric_eval;
48 private CalcButton symbolic_eval;
49 private CalcButton traceExecution;
50 private CalcButton convert_tobase;
51 private CalcButton clear_input;
52 private JCheckBox prettyPrintMode;
53 private JCheckBox convertResult;
54 private JCheckBox copyToClipboard;
55 private JComboBox outputFormat;
56
57 private static String outputLabels[] = {
58 "<Output formats>",
59 "LaTeX",
60 "Java double",
61 "Java BigInteger",
62 "FullForm",
63 "MathML Presentation",
64 "MathML Content",
65 "XML tags",
66 "BeanShell script"
67 };
68 private static HFunction converters[] = {
69 null,
70 C.ToTeX.f(),
71 C.ToJDouble.f(),
72 C.ToJBigInteger.f(),
73 C.FullForm.f(),
74 C.MML.f(),
75 C.MMLContent.f(),
76 C.ToXML.f(),
77 C.ToJava.f(),
78 };
79
80
81 /**
82 * Constructor for the CalculatorPanel object
83 *
84 *@param parent Description of Parameter
85 */
86 public CalculatorPanel(HartMathPanel parent) {
87 this.parent = parent;
88 CalcButton n0;
89 CalcButton n1;
90 CalcButton n2;
91 CalcButton n3;
92 CalcButton n4;
93 CalcButton n5;
94 CalcButton n6;
95 CalcButton n7;
96 CalcButton n8;
97 CalcButton n9;
98 CalcButton a;
99 CalcButton b;
100 CalcButton c;
101 CalcButton d;
102 CalcButton e;
103 CalcButton f;
104 GridBagLayout gbl = new GridBagLayout();
105 // 12 x 14
106
107 setLayout(gbl);
108 setFont(new Font("Courier", Font.PLAIN, 11));
109
110 GridBagConstraints gbc = new GridBagConstraints();
111
112 // gbc.fill = GridBagConstraints.NONE;
113 gbc.anchor = GridBagConstraints.CENTER;
114 gbc.fill = GridBagConstraints.BOTH;
115 gbc.weightx = 300;
116 // 20;
117 gbc.weighty = 100;
118 // 100;
119
120 outputFormat = new JComboBox(outputLabels);
121
122 ItemListener itemListener0 =
123 new ItemListener() {
124 public void itemStateChanged(ItemEvent itemEvent) {
125 int state = itemEvent.getStateChange();
126 if (state == ItemEvent.SELECTED) {
127 HartMathPanel.setPrettyPrinterMode(true);
128 HartMathPanel.setFont("Serif");
129 }
130 else {
131 HartMathPanel.setPrettyPrinterMode(false);
132 HartMathPanel.setFont("Courier");
133 }
134 }
135
136 };
137 prettyPrintMode = new JCheckBox("Pretty Printer Mode", true);
138 prettyPrintMode.addItemListener(itemListener0);
139 /*
140 * ItemListener itemListener1 =
141 * new ItemListener() {
142 * public void itemStateChanged(ItemEvent itemEvent) {
143 * int state = itemEvent.getStateChange();
144 * if (state == ItemEvent.SELECTED) {
145 * convertResult.setText("Convert Results");
146 * }
147 * else {
148 * convertResult.setText("Convert Input");
149 * }
150 * }
151 * };
152 */
153 convertResult = new JCheckBox("Convert Results", true);
154 // convertResult.addItemListener(itemListener1);
155
156 ItemListener itemListener2 =
157 new ItemListener() {
158 public void itemStateChanged(ItemEvent itemEvent) {
159 int state = itemEvent.getStateChange();
160 HartMathPanel.setClipBoard(state == ItemEvent.SELECTED);
161 }
162
163 };
164 copyToClipboard = new JCheckBox("Copy Result to Clipboard", false);
165 copyToClipboard.addItemListener(itemListener2);
166
167 add(new CalcButton("("), gbl, gbc, 0, 1, 1, 1);
168 add(new CalcButton("{"), gbl, gbc, 0, 2, 1, 1);
169 add(new CalcButton("["), gbl, gbc, 0, 3, 1, 1);
170 add(new CalcButton("x^2"), gbl, gbc, 0, 4, 1, 1);
171 add(new CalcButton("Poly( )"), gbl, gbc, 0, 5, 1, 1);
172
173 add(new CalcButton(")"), gbl, gbc, 1, 1, 1, 1);
174 add(new CalcButton("}"), gbl, gbc, 1, 2, 1, 1);
175 add(new CalcButton("]"), gbl, gbc, 1, 3, 1, 1);
176 add(new CalcButton("E"), gbl, gbc, 1, 4, 1, 1);
177 add(new CalcButton("Pi"), gbl, gbc, 1, 5, 1, 1);
178
179 add(n7 = new CalcButton("7"), gbl, gbc, 2, 1, 1, 1);
180 add(n4 = new CalcButton("4"), gbl, gbc, 2, 2, 1, 1);
181 add(n1 = new CalcButton("1"), gbl, gbc, 2, 3, 1, 1);
182 add(n0 = new CalcButton("0"), gbl, gbc, 2, 4, 1, 1);
183 add(a = new CalcButton("a"), gbl, gbc, 2, 5, 1, 1);
184
185 add(n8 = new CalcButton("8"), gbl, gbc, 3, 1, 1, 1);
186 add(n5 = new CalcButton("5"), gbl, gbc, 3, 2, 1, 1);
187 add(n2 = new CalcButton("2"), gbl, gbc, 3, 3, 1, 1);
188 add(new CalcButton("."), gbl, gbc, 3, 4, 1, 1);
189 add(b = new CalcButton("b"), gbl, gbc, 3, 5, 1, 1);
190
191 add(n9 = new CalcButton("9"), gbl, gbc, 4, 1, 1, 1);
192 add(n6 = new CalcButton("6"), gbl, gbc, 4, 2, 1, 1);
193 add(n3 = new CalcButton("3"), gbl, gbc, 4, 3, 1, 1);
194 add(new CalcButton(","), gbl, gbc, 4, 4, 1, 1);
195 add(c = new CalcButton("c"), gbl, gbc, 4, 5, 1, 1);
196
197 add(new CalcButton("/"), gbl, gbc, 5, 1, 1, 1);
198 add(new CalcButton("*"), gbl, gbc, 5, 2, 1, 1);
199 add(new CalcButton("-"), gbl, gbc, 5, 3, 1, 1);
200 add(new CalcButton("+"), gbl, gbc, 5, 4, 1, 1);
201 add(d = new CalcButton("d"), gbl, gbc, 5, 5, 1, 1);
202
203 add(new CalcButton("%"), gbl, gbc, 6, 1, 1, 1);
204 add(new CalcButton("^"), gbl, gbc, 6, 2, 1, 1);
205 add(new CalcButton(":="), gbl, gbc, 6, 3, 1, 1);
206 add(new CalcButton("="), gbl, gbc, 6, 4, 1, 1);
207 add(e = new CalcButton("e"), gbl, gbc, 6, 5, 1, 1);
208
209 add(new CalcButton("&&"), gbl, gbc, 7, 1, 1, 1);
210 add(new CalcButton("||"), gbl, gbc, 7, 2, 1, 1);
211 add(new CalcButton("=="), gbl, gbc, 7, 3, 1, 1);
212 add(new CalcButton("!="), gbl, gbc, 7, 4, 1, 1);
213 add(f = new CalcButton("f"), gbl, gbc, 7, 5, 1, 1);
214
215 add(hex = new CalcButton("Hex"), gbl, gbc, 8, 1, 1, 1);
216 add(oct = new CalcButton("Oct"), gbl, gbc, 9, 1, 1, 1);
217 add(bin = new CalcButton("Bin"), gbl, gbc, 10, 1, 1, 1);
218 add(clear_input = new CalcButton("Clear"), gbl, gbc, 11, 1, 1, 1);
219
220 add(convert_tobase = new CalcButton("Convert to Base"),
221 gbl, gbc, 8, 2, 2, 1);
222
223 add(traceExecution = new CalcButton("Trace Evaluation"),
224 gbl, gbc, 8, 3, 2, 1);
225
226 add(numeric_eval = new CalcButton("Numeric Evaluation"),
227 gbl, gbc, 8, 4, 2, 1);
228
229 add(symbolic_eval = new CalcButton("Symbolic Evaluation"),
230 gbl, gbc, 8, 5, 2, 1);
231
232 add(outputFormat, gbl, gbc, 10, 2, 2, 1);
233
234 add(convertResult, gbl, gbc, 10, 3, 2, 1);
235 add(copyToClipboard, gbl, gbc, 10, 4, 2, 1);
236 add(prettyPrintMode, gbl, gbc, 10, 5, 2, 1);
237
238 clear_input.setForeground(Color.darkGray);
239 traceExecution.setForeground(Color.darkGray);
240 numeric_eval.setForeground(Color.red);
241 symbolic_eval.setForeground(Color.red);
242
243 n0.setForeground(Color.blue);
244 n1.setForeground(Color.blue);
245 n2.setForeground(Color.blue);
246 n3.setForeground(Color.blue);
247 n4.setForeground(Color.blue);
248 n5.setForeground(Color.blue);
249 n6.setForeground(Color.blue);
250 n7.setForeground(Color.blue);
251 n8.setForeground(Color.blue);
252 n9.setForeground(Color.blue);
253
254 a.setForeground(Color.gray);
255 b.setForeground(Color.gray);
256 c.setForeground(Color.gray);
257 d.setForeground(Color.gray);
258 e.setForeground(Color.gray);
259 f.setForeground(Color.gray);
260 }
261
262
263 /**
264 * Description of the Method
265 *
266 *@param c Description of Parameter
267 *@param gbl Description of Parameter
268 *@param gbc Description of Parameter
269 *@param x Description of Parameter
270 *@param y Description of Parameter
271 *@param w Description of Parameter
272 *@param h Description of Parameter
273 */
274 public void add(JComponent c, GridBagLayout gbl, GridBagConstraints gbc,
275 int x, int y, int w, int h) {
276 gbc.gridx = x;
277 gbc.gridy = y;
278 gbc.gridwidth = w;
279 gbc.gridheight = h;
280
281 gbl.setConstraints(c, gbc);
282 add(c);
283 }
284
285
286 /**
287 * Gets the converter function if enabled; otherwise returns null
288 *
289 *@return The converter function or null
290 */
291 HFunction getConverter() {
292 int index = outputFormat.getSelectedIndex();
293 if (convertResult.isSelected()) {
294 return converters[index];
295 }
296 return null;
297 }
298
299
300 /**
301 * Description of the Class
302 *
303 *@author khartlage
304 *@created 31. Juli 2001
305 */
306 public class CalcButton extends JButton {
307 ActionHandler actionHandler = new ActionHandler();
308
309
310 /**
311 * Constructor for the CalcButton object
312 *
313 *@param label Description of Parameter
314 */
315 public CalcButton(String label) {
316 super(label);
317 setMargin(new Insets(2, 2, 2, 2));
318 addActionListener(actionHandler);
319 setRequestFocusEnabled(false);
320 }
321 }
322
323
324 /**
325 * Description of the Class
326 *
327 *@author khartlage
328 *@created 2. Oktober 2001
329 */
330 class ActionHandler implements ActionListener {
331 /**
332 * Description of the Method
333 *
334 *@param evt Description of Parameter
335 */
336 public void actionPerformed(ActionEvent evt) {
337
338 if (evt.getSource() == symbolic_eval) {
339 HFunction function = getConverter();
340 if (function != null) {
341 parent.evaluateButton(function,true);
342 }
343 else {
344 parent.evaluateButton(null, false);
345 }
346 return;
347 }
348
349 if (evt.getSource() == numeric_eval) {
350 parent.evaluateButton(C.N.f(), false);
351 return;
352 }
353
354 if (evt.getSource() == traceExecution) {
355 parent.evaluateButton(C.Trace.f(), false);
356 return;
357 }
358 if (evt.getSource() == convert_tobase) {
359 parent.insertInput("ToBase( , )");
360 return;
361 }
362 if (evt.getSource() == clear_input) {
363 parent.clearInput();
364 return;
365 }
366
367 if (evt.getSource() == hex) {
368 parent.insertInput("0x");
369 return;
370 }
371 if (evt.getSource() == oct) {
372 parent.insertInput("0o");
373 return;
374 }
375 if (evt.getSource() == bin) {
376 parent.insertInput("0b");
377 return;
378 }
379 if (evt.getSource() instanceof CalcButton) {
380 parent.insertInput(((CalcButton)
381 evt.getSource()).
382 getText());
383 return;
384 }
385
386 }
387 }
388
389 }