package nl.bluering; import java.awt.*; import java.awt.event.*; /** * this class implements a calculator using Expressions. It is an applet, * but can also run as application.. */ public class Calc2 extends java.applet.Applet implements ActionListener { TextArea memview; // Shows variables Label warnview; // Shows warnings TextField infield; // Input field TextArea output; // Shows output Panel keypad; // Shows the keypad Choice intest; ExpWriter memviewwriter; volatile boolean lock=false; public void setlock(boolean b) {lock=b; } /** * the object that does the calculations, and remembers the variables. */ RootLib engine; public Calc2() { System.out.println("Welcome in WhiteSpace. Errors appear here:\n"); } /** * sets up the graphical user interface and does all other initialisations. */ public void init() { engine=new RootLib(this); memview = new TextArea("",10,40); memview.setEditable(false); warnview = new Label(" "); warnview.setForeground(Color.red); infield =new TextField(""); output = new TextArea("Welcome in WhiteSpace!\nVersion 1.1\n",10,40); output.setEditable(false); keypad = new Panel(); createlayout(); setLayout(new BorderLayout()); add(keypad,"Center"); memviewwriter=new ExpWriter(memview,ExpWriter.PROLOG); infield.requestFocus(); } /** * this methods allows you to start the applet as an application. It * creates a window that shows the applet. */ public static void main(String[] ps) { Frame f=new Frame("calculator"); f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}}); Calc2 d=new Calc2(); f.setSize(400,400); f.add(d); d.init(); f.pack(); f.show(); d.infield.addActionListener(d); d.infield.requestFocus(); } /** * this (deprecated) method is used by browsers with an old * java environment. It is needed to enable pressing enter in the * input textfield. */ public boolean action(Event evt, Object o) { if(o instanceof String) actionPerformed(new ActionEvent(o,0,"OK")); return true; } /** * this method processes all events. If a key on the keypad is clicked, it * appends the character to the input. If OK is clicked or enter is pressed * it calculates the answer. */ public void actionPerformed(ActionEvent e) { warn(" "); String command=e.getActionCommand(); if(command.equals("OK")||e.getSource()==infield) {docalculate(); } else if (command.equals("Backspace")) {String s = infield.getText(); int l=s.length()-1; infield.setText(s.substring(0,l>0?l:0)); } else if (command.equals("Clear")) {infield.setText(""); } else if (command.length()<2) {infield.setText(infield.getText()+command); } infield.requestFocus(); infield.setCaretPosition(infield.getText().length()); } /** * do the calculation. */ void docalculate() { if(lock) return; Expression inexp=getinput(); if (inexp==null) { warn("I do not understand input"); lock=false; return; } Variable top; setlock(true); engine.toprun(inexp); } /** *The engine must call this function if it is finished, * */ public void endquery() { memview.setText(""); engine.uservar.dump(memviewwriter); engine.userdefdump(memviewwriter); setlock(false); } /** * Get the input from infield and parse it. */ Expression getinput() { try { String inputstring=(String)infield.getText(); CoolFileHandle incfh=new CoolFileHandle(inputstring); Expression input=new ExpParser(incfh).get(); Expression gettherest=new Expression("allout",1).add(""); String unparsedinput=incfh.get(gettherest); if(!unparsedinput.equals("")) warn("warning, could not parse \""+unparsedinput+"\""); return input; }catch(Exception e) {e.printStackTrace(); } return null; } /** * print the string in the output window. */ public void println(String s) {output.append(s+"\n"); output.setCaretPosition(output.getText().length()-1); } /** * print the string in the warning field. */ void warn(String s) {warnview.setText(s); } /** *create the very complicated gridbag layout. */ void createlayout() {GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); keypad.setFont(new Font("Helvetica", Font.PLAIN, 14)); keypad.setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx=1.0; c.weighty=0.0; c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(memview, c); keypad.add(memview); c.gridx=0;c.gridy=1; gridbag.setConstraints(output, c); keypad.add(output); c.gridx=0;c.gridy=2; gridbag.setConstraints(warnview, c); keypad.add(warnview); c.gridx=0;c.gridy=3; gridbag.setConstraints(infield, c); keypad.add(infield); //Row 1 c.gridheight = 1; c.gridwidth=1; c.gridy = 4; c.gridx=0;makebutton("7", gridbag, c); c.gridx=1;makebutton("8", gridbag, c); c.gridx=2;makebutton("9", gridbag, c); c.gridx=3;makebutton("+", gridbag, c); c.gridx=4;makebutton("(", gridbag, c); c.gridx=5;makebutton(")", gridbag, c); c.gridx=6;c.gridwidth=2; makebutton("Backspace", gridbag, c); //Row 2 c.gridheight = 1; c.gridwidth=1; c.gridy = 5; c.gridx=0;makebutton("4", gridbag, c); c.gridx=1;makebutton("5", gridbag, c); c.gridx=2;makebutton("6", gridbag, c); c.gridx=3;makebutton("-", gridbag, c); c.gridx=4;makebutton("^", gridbag, c); c.gridx=5;makebutton("%", gridbag, c); c.gridx=6;makebutton(";", gridbag, c); c.gridx=7;makebutton("Clear", gridbag, c); //Row 3 c.gridwidth = 1; c.gridheight = 1; c.gridy=6; c.gridx=0;makebutton("1", gridbag, c); c.gridx=1;makebutton("2", gridbag, c); c.gridx=2;makebutton("3", gridbag, c); c.gridx=3;makebutton("*", gridbag, c); c.gridx=4;makebutton("&", gridbag, c); c.gridx=5;makebutton("|", gridbag, c); c.gridx=6;makebutton("!", gridbag, c); c.gridheight = 2;c.gridx=7; makebutton("OK", gridbag, c); //Row 4 c.gridwidth = 1; c.gridheight = 1; c.gridy=7;c.gridwidth=2; c.gridx=0;makebutton("0", gridbag, c); c.gridwidth=1; c.gridx=2;makebutton(".", gridbag, c); c.gridx=3;makebutton("/", gridbag, c); c.gridx=4;makebutton("<", gridbag, c); c.gridx=5;makebutton(">", gridbag, c); c.gridx=6;makebutton("=", gridbag, c); } /** * add a new button. This method is used for making the keypad. */ void makebutton(String name, GridBagLayout gridbag,GridBagConstraints c) {Button button = new Button(name); gridbag.setConstraints(button, c); keypad.add(button); button.addActionListener(this); } }