7027690: /applets/SpreadSheet demo needs to be improved
authormrkam
Wed, 30 Mar 2011 08:54:08 -0700
changeset 8978 046c16ff00cb
parent 8977 139a072009cb
child 8979 30050c8aea19
7027690: /applets/SpreadSheet demo needs to be improved Reviewed-by: alexp
jdk/src/share/demo/applets/SpreadSheet/SpreadSheet.java
--- a/jdk/src/share/demo/applets/SpreadSheet/SpreadSheet.java	Wed Mar 30 08:52:37 2011 -0700
+++ b/jdk/src/share/demo/applets/SpreadSheet/SpreadSheet.java	Wed Mar 30 08:54:08 2011 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,39 +29,38 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- */
 
 import java.applet.Applet;
 import java.awt.*;
 import java.awt.event.*;
 import java.io.*;
-import java.lang.*;
 import java.net.*;
 
-public class SpreadSheet
-    extends Applet
-    implements MouseListener, KeyListener {
-    String              title;
-    Font                titleFont;
-    Color               cellColor;
-    Color               inputColor;
-    int                 cellWidth = 100;
-    int                 cellHeight = 15;
-    int                 titleHeight = 15;
-    int                 rowLabelWidth = 15;
-    Font                inputFont;
-    boolean             isStopped = false;
-    boolean             fullUpdate = true;
-    int                 rows;
-    int                 columns;
-    int                 currentKey = -1;
-    int                 selectedRow = -1;
-    int                 selectedColumn = -1;
-    SpreadSheetInput    inputArea;
-    Cell                cells[][];
-    Cell                current = null;
+
+@SuppressWarnings("serial")
+public class SpreadSheet extends Applet implements MouseListener, KeyListener {
 
+    String title;
+    Font titleFont;
+    Color cellColor;
+    Color inputColor;
+    int cellWidth = 100;
+    int cellHeight = 15;
+    int titleHeight = 15;
+    int rowLabelWidth = 15;
+    Font inputFont;
+    boolean isStopped = false;
+    boolean fullUpdate = true;
+    int rows;
+    int columns;
+    int currentKey = -1;
+    int selectedRow = -1;
+    int selectedColumn = -1;
+    SpreadSheetInput inputArea;
+    Cell cells[][];
+    Cell current = null;
+
+    @Override
     public synchronized void init() {
         String rs;
 
@@ -87,17 +86,17 @@
         }
         cells = new Cell[rows][columns];
         char l[] = new char[1];
-        for (int i=0; i < rows; i++) {
-            for (int j=0; j < columns; j++) {
+        for (int i = 0; i < rows; i++) {
+            for (int j = 0; j < columns; j++) {
 
                 cells[i][j] = new Cell(this,
-                                       Color.lightGray,
-                                       Color.black,
-                                       cellColor,
-                                       cellWidth - 2,
-                                       cellHeight - 2);
-                l[0] = (char)((int)'a' + j);
-                rs = getParameter("" + new String(l) + (i+1));
+                        Color.lightGray,
+                        Color.black,
+                        cellColor,
+                        cellWidth - 2,
+                        cellHeight - 2);
+                l[0] = (char) ((int) 'a' + j);
+                rs = getParameter("" + new String(l) + (i + 1));
                 if (rs != null) {
                     cells[i][j].setUnparsedValue(rs);
                 }
@@ -106,9 +105,9 @@
 
         Dimension d = getSize();
         inputArea = new SpreadSheetInput(null, this, d.width - 2, cellHeight - 1,
-                                         inputColor, Color.white);
+                inputColor, Color.white);
         resize(columns * cellWidth + rowLabelWidth,
-               (rows + 3) * cellHeight + titleHeight);
+                (rows + 3) * cellHeight + titleHeight);
         addMouseListener(this);
         addKeyListener(this);
     }
@@ -121,19 +120,22 @@
         repaint();
     }
 
+    @Override
     public void stop() {
         isStopped = true;
     }
 
+    @Override
     public void start() {
         isStopped = false;
     }
 
+    @Override
     public void destroy() {
-        for (int i=0; i < rows; i++) {
-            for (int j=0; j < columns; j++) {
+        for (int i = 0; i < rows; i++) {
+            for (int j = 0; j < columns; j++) {
                 if (cells[i][j].type == Cell.URL) {
-                    cells[i][j].updaterThread.stop();
+                    cells[i][j].updaterThread.run = false;
                 }
             }
         }
@@ -147,16 +149,17 @@
         repaint();
     }
 
+    @Override
     public void update(Graphics g) {
-        if (! fullUpdate) {
+        if (!fullUpdate) {
             int cx, cy;
 
             g.setFont(titleFont);
-            for (int i=0; i < rows; i++) {
-                for (int j=0; j < columns; j++) {
+            for (int i = 0; i < rows; i++) {
+                for (int j = 0; j < columns; j++) {
                     if (cells[i][j].needRedisplay) {
                         cx = (j * cellWidth) + 2 + rowLabelWidth;
-                        cy = ((i+1) * cellHeight) + 2 + titleHeight;
+                        cy = ((i + 1) * cellHeight) + 2 + titleHeight;
                         cells[i][j].paint(g, cx, cy);
                     }
                 }
@@ -168,13 +171,14 @@
     }
 
     public void recalculate() {
-        int     i,j;
+        int i, j;
 
         //System.out.println("SpreadSheet.recalculate");
-        for (i=0; i < rows; i++) {
-            for (j=0; j < columns; j++) {
+        for (i = 0; i < rows; i++) {
+            for (j = 0; j < columns; j++) {
                 if (cells[i][j] != null && cells[i][j].type == Cell.FORMULA) {
-                    cells[i][j].setRawValue(evaluateFormula(cells[i][j].parseRoot));
+                    cells[i][j].setRawValue(evaluateFormula(
+                            cells[i][j].parseRoot));
                     cells[i][j].needRedisplay = true;
                 }
             }
@@ -182,8 +186,8 @@
         repaint();
     }
 
-    public float evaluateFormula(Node n) {
-        float   val = 0.0f;
+    float evaluateFormula(Node n) {
+        float val = 0.0f;
 
         //System.out.println("evaluateFormula:");
         //n.print(3);
@@ -192,43 +196,40 @@
             return val;
         }
         switch (n.type) {
-          case Node.OP:
-            val = evaluateFormula(n.left);
-            switch (n.op) {
-              case '+':
-                val += evaluateFormula(n.right);
-                break;
-              case '*':
-                val *= evaluateFormula(n.right);
-                break;
-              case '-':
-                val -= evaluateFormula(n.right);
+            case Node.OP:
+                val = evaluateFormula(n.left);
+                switch (n.op) {
+                    case '+':
+                        val += evaluateFormula(n.right);
+                        break;
+                    case '*':
+                        val *= evaluateFormula(n.right);
+                        break;
+                    case '-':
+                        val -= evaluateFormula(n.right);
+                        break;
+                    case '/':
+                        val /= evaluateFormula(n.right);
+                        break;
+                }
                 break;
-              case '/':
-                val /= evaluateFormula(n.right);
-                break;
-            }
-            break;
-          case Node.VALUE:
-            //System.out.println("=>" + n.value);
-            return n.value;
-          case Node.CELL:
-            if (n == null) {
-                //System.out.println("NULL at 192");
-            } else {
+            case Node.VALUE:
+                //System.out.println("=>" + n.value);
+                return n.value;
+            case Node.CELL:
                 if (cells[n.row][n.column] == null) {
                     //System.out.println("NULL at 193");
                 } else {
                     //System.out.println("=>" + cells[n.row][n.column].value);
                     return cells[n.row][n.column].value;
                 }
-            }
         }
 
         //System.out.println("=>" + val);
         return val;
     }
 
+    @Override
     public synchronized void paint(Graphics g) {
         int i, j;
         int cx, cy;
@@ -240,40 +241,40 @@
         g.setFont(titleFont);
         i = g.getFontMetrics().stringWidth(title);
         g.drawString((title == null) ? "Spreadsheet" : title,
-                     (d.width - i) / 2, 12);
+                (d.width - i) / 2, 12);
         g.setColor(inputColor);
         g.fillRect(0, cellHeight, d.width, cellHeight);
         g.setFont(titleFont);
-        for (i=0; i < rows+1; i++) {
-            cy = (i+2) * cellHeight;
+        for (i = 0; i < rows + 1; i++) {
+            cy = (i + 2) * cellHeight;
             g.setColor(getBackground());
             g.draw3DRect(0, cy, d.width, 2, true);
             if (i < rows) {
                 g.setColor(Color.red);
-                g.drawString("" + (i+1), 2, cy + 12);
+                g.drawString("" + (i + 1), 2, cy + 12);
             }
         }
 
         g.setColor(Color.red);
-        cy = (rows+3) * cellHeight + (cellHeight / 2);
-        for (i=0; i < columns; i++) {
+        cy = (rows + 3) * cellHeight + (cellHeight / 2);
+        for (i = 0; i < columns; i++) {
             cx = i * cellWidth;
             g.setColor(getBackground());
             g.draw3DRect(cx + rowLabelWidth,
-                          2 * cellHeight, 1, d.height, true);
+                    2 * cellHeight, 1, d.height, true);
             if (i < columns) {
                 g.setColor(Color.red);
-                l[0] = (char)((int)'A' + i);
+                l[0] = (char) ((int) 'A' + i);
                 g.drawString(new String(l),
-                             cx + rowLabelWidth + (cellWidth / 2),
-                             cy);
+                        cx + rowLabelWidth + (cellWidth / 2),
+                        cy);
             }
         }
 
-        for (i=0; i < rows; i++) {
-            for (j=0; j < columns; j++) {
+        for (i = 0; i < rows; i++) {
+            for (j = 0; j < columns; j++) {
                 cx = (j * cellWidth) + 2 + rowLabelWidth;
-                cy = ((i+1) * cellHeight) + 2 + titleHeight;
+                cy = ((i + 1) * cellHeight) + 2 + titleHeight;
                 if (cells[i][j] != null) {
                     cells[i][j].paint(g, cx, cy);
                 }
@@ -282,141 +283,152 @@
 
         g.setColor(getBackground());
         g.draw3DRect(0, titleHeight,
-                      d.width,
-                      d.height - titleHeight,
-                      false);
+                d.width,
+                d.height - titleHeight,
+                false);
         inputArea.paint(g, 1, titleHeight + 1);
     }
 
-      //1.1 event handling
-
-  public void mouseClicked(MouseEvent e)
-  {}
-
-  public void mousePressed(MouseEvent e)
-  {
-    int x = e.getX();
-    int y = e.getY();
-    Cell cell;
-    if (y < (titleHeight + cellHeight)) {
-      selectedRow = -1;
-      if (y <= titleHeight && current != null) {
-        current.deselect();
-        current = null;
-      }
-      e.consume();
+    //1.1 event handling
+    @Override
+    public void mouseClicked(MouseEvent e) {
     }
-    if (x < rowLabelWidth) {
-      selectedRow = -1;
-      if (current != null) {
-        current.deselect();
+
+    @Override
+    public void mousePressed(MouseEvent e) {
+        int x = e.getX();
+        int y = e.getY();
+        Cell cell;
+        if (y < (titleHeight + cellHeight)) {
+            selectedRow = -1;
+            if (y <= titleHeight && current != null) {
+                current.deselect();
+                current = null;
+            }
+            e.consume();
+        }
+        if (x < rowLabelWidth) {
+            selectedRow = -1;
+            if (current != null) {
+                current.deselect();
                 current = null;
-      }
-      e.consume();
+            }
+            e.consume();
 
+        }
+        selectedRow = ((y - cellHeight - titleHeight) / cellHeight);
+        selectedColumn = (x - rowLabelWidth) / cellWidth;
+        if (selectedRow > rows
+                || selectedColumn >= columns) {
+            selectedRow = -1;
+            if (current != null) {
+                current.deselect();
+                current = null;
+            }
+        } else {
+            if (selectedRow >= rows) {
+                selectedRow = -1;
+                if (current != null) {
+                    current.deselect();
+                    current = null;
+                }
+                e.consume();
+            }
+            if (selectedRow != -1) {
+                cell = cells[selectedRow][selectedColumn];
+                inputArea.setText(cell.getPrintString());
+                if (current != null) {
+                    current.deselect();
+                }
+                current = cell;
+                current.select();
+                requestFocus();
+                fullUpdate = true;
+                repaint();
+            }
+            e.consume();
+        }
     }
-    selectedRow = ((y - cellHeight - titleHeight) / cellHeight);
-    selectedColumn = (x - rowLabelWidth) / cellWidth;
-    if (selectedRow > rows ||
-        selectedColumn >= columns) {
-      selectedRow = -1;
-      if (current != null) {
-        current.deselect();
-        current = null;
-      }
-    } else {
-      if (selectedRow >= rows) {
-        selectedRow = -1;
-        if (current != null) {
-          current.deselect();
-          current = null;
-        }
+
+    @Override
+    public void mouseReleased(MouseEvent e) {
+    }
+
+    @Override
+    public void mouseEntered(MouseEvent e) {
+    }
+
+    @Override
+    public void mouseExited(MouseEvent e) {
+    }
+
+    @Override
+    public void keyPressed(KeyEvent e) {
+    }
+
+    @Override
+    public void keyTyped(KeyEvent e) {
+        fullUpdate = true;
+        inputArea.processKey(e);
         e.consume();
-      }
-      if (selectedRow != -1) {
-        cell = cells[selectedRow][selectedColumn];
-        inputArea.setText(new String(cell.getPrintString()));
-        if (current != null) {
-          current.deselect();
-        }
-        current = cell;
-        current.select();
-        requestFocus();
-        fullUpdate = true;
-        repaint();
-      }
-      e.consume();
+    }
+
+    @Override
+    public void keyReleased(KeyEvent e) {
     }
-  }
 
-  public void mouseReleased(MouseEvent e)
-  {}
-
-  public void mouseEntered(MouseEvent e)
-  {}
-
-  public void mouseExited(MouseEvent e)
-  {}
+    @Override
+    public String getAppletInfo() {
+        return "Title: SpreadSheet \nAuthor: Sami Shaio \nA simple spread sheet.";
+    }
 
-  public void keyPressed(KeyEvent e)
-  {
-  }
-
-  public void keyTyped(KeyEvent e) {
-    fullUpdate=true;
-    inputArea.processKey(e);
-    e.consume();
-  }
-
-  public void keyReleased(KeyEvent e)
-  {}
-
-  public String getAppletInfo() {
-    return "Title: SpreadSheet \nAuthor: Sami Shaio \nA simple spread sheet.";
-  }
-
-  public String[][] getParameterInfo() {
-    String[][] info = {
-      {"title", "string", "The title of the spread sheet.  Default is 'Spreadsheet'"},
-      {"rows", "int", "The number of rows.  Default is 9."},
-      {"columns", "int", "The number of columns.  Default is 5."}
-    };
-    return info;
-  }
+    @Override
+    public String[][] getParameterInfo() {
+        String[][] info = {
+            { "title", "string",
+                "The title of the spread sheet.  Default is 'Spreadsheet'" },
+            { "rows", "int", "The number of rows.  Default is 9." },
+            { "columns", "int", "The number of columns.  Default is 5." }
+        };
+        return info;
+    }
+}
 
 
-}
+class CellUpdater extends Thread {
 
-class CellUpdater extends Thread {
-    Cell        target;
+    Cell target;
     InputStream dataStream = null;
     StreamTokenizer tokenStream;
+    public volatile boolean run = true;
 
     public CellUpdater(Cell c) {
         super("cell updater");
         target = c;
     }
 
+    @Override
     public void run() {
         try {
             dataStream = new URL(target.app.getDocumentBase(),
-                                 target.getValueString()).openStream();
-            tokenStream = new StreamTokenizer(new BufferedReader(new InputStreamReader(dataStream)));
+                    target.getValueString()).openStream();
+            tokenStream = new StreamTokenizer(new BufferedReader(
+                    new InputStreamReader(dataStream)));
             tokenStream.eolIsSignificant(false);
 
-            while (true) {
+            while (run) {
                 switch (tokenStream.nextToken()) {
-                case StreamTokenizer.TT_EOF:
-                    dataStream.close();
-                    return;
-                default:
-                    break;
-                case StreamTokenizer.TT_NUMBER:
-                    target.setTransientValue((float)tokenStream.nval);
-                    if (! target.app.isStopped && ! target.paused) {
-                        target.app.repaint();
-                    }
-                    break;
+                    case StreamTokenizer.TT_EOF:
+                        dataStream.close();
+                        return;
+                    default:
+                        break;
+                    case StreamTokenizer.TT_NUMBER:
+                        target.setTransientValue((float) tokenStream.nval);
+                        if (!target.app.isStopped && !target.paused) {
+                            target.app.repaint();
+                        }
+                        break;
                 }
                 try {
                     Thread.sleep(2000);
@@ -430,35 +442,36 @@
     }
 }
 
+
 class Cell {
+
     public static final int VALUE = 0;
     public static final int LABEL = 1;
-    public static final int URL   = 2;
+    public static final int URL = 2;
     public static final int FORMULA = 3;
-
-    Node        parseRoot;
-    boolean     needRedisplay;
+    Node parseRoot;
+    boolean needRedisplay;
     boolean selected = false;
     boolean transientValue = false;
-    public int  type = Cell.VALUE;
-    String      valueString = "";
-    String      printString = "v";
-    float       value;
-    Color       bgColor;
-    Color       fgColor;
-    Color       highlightColor;
-    int         width;
-    int         height;
+    public int type = Cell.VALUE;
+    String valueString = "";
+    String printString = "v";
+    float value;
+    Color bgColor;
+    Color fgColor;
+    Color highlightColor;
+    int width;
+    int height;
     SpreadSheet app;
     CellUpdater updaterThread;
-    boolean     paused = false;
+    boolean paused = false;
 
     public Cell(SpreadSheet app,
-                Color bgColor,
-                Color fgColor,
-                Color highlightColor,
-                int width,
-                int height) {
+            Color bgColor,
+            Color fgColor,
+            Color highlightColor,
+            int width,
+            int height) {
         this.app = app;
         this.bgColor = bgColor;
         this.fgColor = fgColor;
@@ -472,6 +485,7 @@
         valueString = Float.toString(f);
         value = f;
     }
+
     public void setValue(float f) {
         setRawValue(f);
         printString = "v" + valueString;
@@ -490,18 +504,18 @@
 
     public void setUnparsedValue(String s) {
         switch (s.charAt(0)) {
-          case 'v':
-            setValue(Cell.VALUE, s.substring(1));
-            break;
-          case 'f':
-            setValue(Cell.FORMULA, s.substring(1));
-            break;
-          case 'l':
-            setValue(Cell.LABEL, s.substring(1));
-            break;
-          case 'u':
-            setValue(Cell.URL, s.substring(1));
-            break;
+            case 'v':
+                setValue(Cell.VALUE, s.substring(1));
+                break;
+            case 'f':
+                setValue(Cell.FORMULA, s.substring(1));
+                break;
+            case 'l':
+                setValue(Cell.LABEL, s.substring(1));
+                break;
+            case 'u':
+                setValue(Cell.URL, s.substring(1));
+                break;
         }
     }
 
@@ -519,8 +533,6 @@
     public String parseFormula(String formula, Node node) {
         String subformula;
         String restFormula;
-        float value;
-        int length = formula.length();
         Node left;
         Node right;
         char op;
@@ -534,52 +546,53 @@
             //System.out.println("Parse succeeded");
             return null;
         }
-        if (subformula == formula) {
+        if (subformula.equals(formula)) {
             //System.out.println("Parse failed");
             return formula;
         }
 
         // parse an operator and then another value
         switch (op = subformula.charAt(0)) {
-          case 0:
-            //System.out.println("Parse succeeded");
-            return null;
-          case ')':
-            //System.out.println("Returning subformula=" + subformula);
-            return subformula;
-          case '+':
-          case '*':
-          case '-':
-          case '/':
-            restFormula = subformula.substring(1);
-            subformula = parseValue(restFormula, right=new Node());
-            //System.out.println("subformula(2) = " + subformula);
-            if (subformula != restFormula) {
+            case 0:
                 //System.out.println("Parse succeeded");
-                left = new Node(node);
-                node.left = left;
-                node.right = right;
-                node.op = op;
-                node.type = Node.OP;
-                //node.print(3);
+                return null;
+            case ')':
+                //System.out.println("Returning subformula=" + subformula);
                 return subformula;
-            } else {
-                //System.out.println("Parse failed");
+            case '+':
+            case '*':
+            case '-':
+            case '/':
+                restFormula = subformula.substring(1);
+                subformula = parseValue(restFormula, right = new Node());
+                //System.out.println("subformula(2) = " + subformula);
+                if (subformula == null ? restFormula != null : !subformula.
+                        equals(restFormula)) {
+                    //System.out.println("Parse succeeded");
+                    left = new Node(node);
+                    node.left = left;
+                    node.right = right;
+                    node.op = op;
+                    node.type = Node.OP;
+                    //node.print(3);
+                    return subformula;
+                } else {
+                    //System.out.println("Parse failed");
+                    return formula;
+                }
+            default:
+                //System.out.println("Parse failed (bad operator): " + subformula);
                 return formula;
-            }
-          default:
-            //System.out.println("Parse failed (bad operator): " + subformula);
-            return formula;
         }
     }
 
     public String parseValue(String formula, Node node) {
-        char    c = formula.charAt(0);
-        String  subformula;
-        String  restFormula;
-        float   value;
-        int     row;
-        int     column;
+        char c = formula.charAt(0);
+        String subformula;
+        String restFormula;
+        float _value;
+        int row;
+        int column;
 
         //System.out.println("parseValue: " + formula);
         restFormula = formula;
@@ -588,11 +601,11 @@
             restFormula = formula.substring(1);
             subformula = parseFormula(restFormula, node);
             //System.out.println("rest=(" + subformula + ")");
-            if (subformula == null ||
-                subformula.length() == restFormula.length()) {
+            if (subformula == null
+                    || subformula.length() == restFormula.length()) {
                 //System.out.println("Failed");
                 return formula;
-            } else if (! (subformula.charAt(0) == ')')) {
+            } else if (!(subformula.charAt(0) == ')')) {
                 //System.out.println("Failed (missing parentheses)");
                 return formula;
             }
@@ -601,31 +614,31 @@
             int i;
 
             //System.out.println("formula=" + formula);
-            for (i=0; i < formula.length(); i++) {
+            for (i = 0; i < formula.length(); i++) {
                 c = formula.charAt(i);
                 if ((c < '0' || c > '9') && c != '.') {
                     break;
                 }
             }
             try {
-                value = Float.valueOf(formula.substring(0, i)).floatValue();
+                _value = Float.valueOf(formula.substring(0, i)).floatValue();
             } catch (NumberFormatException e) {
                 //System.out.println("Failed (number format error)");
                 return formula;
             }
             node.type = Node.VALUE;
-            node.value = value;
+            node.value = _value;
             //node.print(3);
             restFormula = formula.substring(i);
             //System.out.println("value= " + value + " i=" + i +
-                //                     " rest = " + restFormula);
+            //                     " rest = " + restFormula);
             return restFormula;
         } else if (c >= 'A' && c <= 'Z') {
             int i;
 
             column = c - 'A';
             restFormula = formula.substring(1);
-            for (i=0; i < restFormula.length(); i++) {
+            for (i = 0; i < restFormula.length(); i++) {
                 c = restFormula.charAt(i);
                 if (c < '0' || c > '9') {
                     break;
@@ -650,33 +663,32 @@
         return restFormula;
     }
 
-
     public void setValue(int type, String s) {
         paused = false;
         if (this.type == Cell.URL) {
-            updaterThread.stop();
+            updaterThread.run = false;
             updaterThread = null;
         }
 
-        valueString = new String(s);
+        valueString = s;
         this.type = type;
         needRedisplay = true;
         switch (type) {
-          case Cell.VALUE:
-            setValue(Float.valueOf(s).floatValue());
-            break;
-          case Cell.LABEL:
-            printString = "l" + valueString;
-            break;
-          case Cell.URL:
-            printString = "u" + valueString;
-            updaterThread = new CellUpdater(this);
-            updaterThread.start();
-            break;
-          case Cell.FORMULA:
-            parseFormula(valueString, parseRoot = new Node());
-            printString = "f" + valueString;
-            break;
+            case Cell.VALUE:
+                setValue(Float.valueOf(s).floatValue());
+                break;
+            case Cell.LABEL:
+                printString = "l" + valueString;
+                break;
+            case Cell.URL:
+                printString = "u" + valueString;
+                updaterThread = new CellUpdater(this);
+                updaterThread.start();
+                break;
+            case Cell.FORMULA:
+                parseFormula(valueString, parseRoot = new Node());
+                printString = "f" + valueString;
+                break;
         }
         app.recalculate();
     }
@@ -693,12 +705,14 @@
         selected = true;
         paused = true;
     }
+
     public void deselect() {
         selected = false;
         paused = false;
         needRedisplay = true;
         app.repaint();
     }
+
     public void paint(Graphics g, int x, int y) {
         if (selected) {
             g.setColor(highlightColor);
@@ -708,23 +722,23 @@
         g.fillRect(x, y, width - 1, height);
         if (valueString != null) {
             switch (type) {
-              case Cell.VALUE:
-              case Cell.LABEL:
-                g.setColor(fgColor);
-                break;
-              case Cell.FORMULA:
-                g.setColor(Color.red);
-                break;
-              case Cell.URL:
-                g.setColor(Color.blue);
-                break;
+                case Cell.VALUE:
+                case Cell.LABEL:
+                    g.setColor(fgColor);
+                    break;
+                case Cell.FORMULA:
+                    g.setColor(Color.red);
+                    break;
+                case Cell.URL:
+                    g.setColor(Color.blue);
+                    break;
             }
-            if (transientValue){
+            if (transientValue) {
                 g.drawString("" + value, x, y + (height / 2) + 5);
             } else {
                 if (valueString.length() > 14) {
                     g.drawString(valueString.substring(0, 14),
-                                 x, y + (height / 2) + 5);
+                            x, y + (height / 2) + 5);
                 } else {
                     g.drawString(valueString, x, y + (height / 2) + 5);
                 }
@@ -734,18 +748,19 @@
     }
 }
 
+
 class Node {
+
     public static final int OP = 0;
     public static final int VALUE = 1;
     public static final int CELL = 2;
-
-    int         type;
-    Node        left;
-    Node        right;
-    int         row;
-    int         column;
-    float       value;
-    char        op;
+    int type;
+    Node left;
+    Node right;
+    int row;
+    int column;
+    float value;
+    char op;
 
     public Node() {
         left = null;
@@ -756,6 +771,7 @@
         op = 0;
         type = Node.VALUE;
     }
+
     public Node(Node n) {
         left = n.left;
         right = n.right;
@@ -765,47 +781,51 @@
         op = n.op;
         type = n.type;
     }
+
     public void indent(int ind) {
         for (int i = 0; i < ind; i++) {
             System.out.print(" ");
         }
     }
+
     public void print(int indentLevel) {
         char l[] = new char[1];
         indent(indentLevel);
         System.out.println("NODE type=" + type);
         indent(indentLevel);
         switch (type) {
-          case Node.VALUE:
-            System.out.println(" value=" + value);
-            break;
-          case Node.CELL:
-            l[0] = (char)((int)'A' + column);
-            System.out.println(" cell=" + new String(l) + (row+1));
-            break;
-          case Node.OP:
-            System.out.println(" op=" + op);
-            left.print(indentLevel + 3);
-            right.print(indentLevel + 3);
-            break;
+            case Node.VALUE:
+                System.out.println(" value=" + value);
+                break;
+            case Node.CELL:
+                l[0] = (char) ((int) 'A' + column);
+                System.out.println(" cell=" + new String(l) + (row + 1));
+                break;
+            case Node.OP:
+                System.out.println(" op=" + op);
+                left.print(indentLevel + 3);
+                right.print(indentLevel + 3);
+                break;
         }
     }
 }
 
+
 class InputField {
-    int         maxchars = 50;
-    int         cursorPos = 0;
-    Applet      app;
-    String      sval;
-    char        buffer[];
-    int         nChars;
-    int         width;
-    int         height;
-    Color       bgColor;
-    Color       fgColor;
+
+    int maxchars = 50;
+    int cursorPos = 0;
+    Applet app;
+    String sval;
+    char buffer[];
+    int nChars;
+    int width;
+    int height;
+    Color bgColor;
+    Color fgColor;
 
     public InputField(String initValue, Applet app, int width, int height,
-                      Color bgColor, Color fgColor) {
+            Color bgColor, Color fgColor) {
         this.width = width;
         this.height = height;
         this.bgColor = bgColor;
@@ -823,7 +843,7 @@
     public void setText(String val) {
         int i;
 
-        for (i=0; i < maxchars; i++) {
+        for (i = 0; i < maxchars; i++) {
             buffer[i] = 0;
         }
         if (val == null) {
@@ -876,47 +896,52 @@
     }
 }
 
+
 class SpreadSheetInput
-    extends InputField {
+        extends InputField {
 
-  public SpreadSheetInput(String initValue,
-                          SpreadSheet app,
-                          int width,
-                          int height,
-                          Color bgColor,
-                          Color fgColor) {
-    super(initValue, app, width, height, bgColor, fgColor);
-  }
+    public SpreadSheetInput(String initValue,
+            SpreadSheet app,
+            int width,
+            int height,
+            Color bgColor,
+            Color fgColor) {
+        super(initValue, app, width, height, bgColor, fgColor);
+    }
 
+    @Override
     public void selected() {
         float f;
-        sval = ("".equals(sval)) ? "v":sval;
+        sval = ("".equals(sval)) ? "v" : sval;
         switch (sval.charAt(0)) {
-          case 'v':
-              String s= sval.substring(1);
-            try {
-                int i;
-                for (i = 0; i < s.length(); i++) {
-                    char c = s.charAt(i);
-                    if (c < '0' || c > '9')
-                        break;
+            case 'v':
+                String s = sval.substring(1);
+                try {
+                    int i;
+                    for (i = 0; i < s.length(); i++) {
+                        char c = s.charAt(i);
+                        if (c < '0' || c > '9') {
+                            break;
+                        }
+                    }
+                    s = s.substring(0, i);
+                    f = Float.valueOf(s).floatValue();
+                    ((SpreadSheet) app).setCurrentValue(f);
+                } catch (NumberFormatException e) {
+                    System.out.println("Not a float: '" + s + "'");
                 }
-                s = s.substring(0, i);
-                f = Float.valueOf(s).floatValue();
-                ((SpreadSheet)app).setCurrentValue(f);
-            } catch (NumberFormatException e) {
-                System.out.println("Not a float: '" + s + "'");
-            }
-            break;
-          case 'l':
-            ((SpreadSheet)app).setCurrentValue(Cell.LABEL, sval.substring(1));
-            break;
-          case 'u':
-            ((SpreadSheet)app).setCurrentValue(Cell.URL, sval.substring(1));
-            break;
-          case 'f':
-            ((SpreadSheet)app).setCurrentValue(Cell.FORMULA, sval.substring(1));
-            break;
+                break;
+            case 'l':
+                ((SpreadSheet) app).setCurrentValue(Cell.LABEL,
+                        sval.substring(1));
+                break;
+            case 'u':
+                ((SpreadSheet) app).setCurrentValue(Cell.URL, sval.substring(1));
+                break;
+            case 'f':
+                ((SpreadSheet) app).setCurrentValue(Cell.FORMULA,
+                        sval.substring(1));
+                break;
         }
     }
 }