jdk/src/demo/share/applets/SpreadSheet/SpreadSheet.java
author prr
Thu, 18 Dec 2014 10:45:45 -0800
changeset 29908 83e2c403fefd
parent 25859 3317bb8137f4
permissions -rw-r--r--
8067050: Better font consistency checking Reviewed-by: bae, srl, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8978
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8978
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8978
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8978
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8978
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8978
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8978
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8978
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8978
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.applet.Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    48
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    49
@SuppressWarnings("serial")
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    50
public class SpreadSheet extends Applet implements MouseListener, KeyListener {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
    String title;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    53
    Font titleFont;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    54
    Color cellColor;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    55
    Color inputColor;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    56
    int cellWidth = 100;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    57
    int cellHeight = 15;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    58
    int titleHeight = 15;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    59
    int rowLabelWidth = 15;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    60
    Font inputFont;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    61
    boolean isStopped = false;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    62
    boolean fullUpdate = true;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    63
    int rows;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    64
    int columns;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    65
    int currentKey = -1;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    66
    int selectedRow = -1;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    67
    int selectedColumn = -1;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    68
    SpreadSheetInput inputArea;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    69
    Cell cells[][];
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    70
    Cell current = null;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    71
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    72
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public synchronized void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        String rs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        cellColor = Color.white;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        inputColor = new Color(100, 100, 225);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        inputFont = new Font("Monospaced", Font.PLAIN, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        titleFont = new Font("Monospaced", Font.BOLD, 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        title = getParameter("title");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (title == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            title = "Spreadsheet";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        rs = getParameter("rows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (rs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            rows = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            rows = Integer.parseInt(rs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        rs = getParameter("columns");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (rs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            columns = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            columns = Integer.parseInt(rs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        cells = new Cell[rows][columns];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        char l[] = new char[1];
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    98
        for (int i = 0; i < rows; i++) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
    99
            for (int j = 0; j < columns; j++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                cells[i][j] = new Cell(this,
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   102
                        Color.lightGray,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   103
                        Color.black,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   104
                        cellColor,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   105
                        cellWidth - 2,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   106
                        cellHeight - 2);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   107
                l[0] = (char) ((int) 'a' + j);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   108
                rs = getParameter("" + new String(l) + (i + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (rs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    cells[i][j].setUnparsedValue(rs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        Dimension d = getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        inputArea = new SpreadSheetInput(null, this, d.width - 2, cellHeight - 1,
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   117
                inputColor, Color.white);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        resize(columns * cellWidth + rowLabelWidth,
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   119
                (rows + 3) * cellHeight + titleHeight);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        addMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        addKeyListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void setCurrentValue(float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (selectedRow == -1 || selectedColumn == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        cells[selectedRow][selectedColumn].setValue(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   132
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        isStopped = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   137
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        isStopped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   142
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void destroy() {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   144
        for (int i = 0; i < rows; i++) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   145
            for (int j = 0; j < columns; j++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (cells[i][j].type == Cell.URL) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   147
                    cells[i][j].updaterThread.run = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void setCurrentValue(int type, String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (selectedRow == -1 || selectedColumn == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        cells[selectedRow][selectedColumn].setValue(type, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   161
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void update(Graphics g) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   163
        if (!fullUpdate) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            int cx, cy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            g.setFont(titleFont);
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   167
            for (int i = 0; i < rows; i++) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   168
                for (int j = 0; j < columns; j++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    if (cells[i][j].needRedisplay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        cx = (j * cellWidth) + 2 + rowLabelWidth;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   171
                        cy = ((i + 1) * cellHeight) + 2 + titleHeight;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        cells[i][j].paint(g, cx, cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            fullUpdate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void recalculate() {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   183
        int i, j;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        //System.out.println("SpreadSheet.recalculate");
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   186
        for (i = 0; i < rows; i++) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   187
            for (j = 0; j < columns; j++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (cells[i][j] != null && cells[i][j].type == Cell.FORMULA) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   189
                    cells[i][j].setRawValue(evaluateFormula(
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   190
                            cells[i][j].parseRoot));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    cells[i][j].needRedisplay = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   198
    float evaluateFormula(Node n) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   199
        float val = 0.0f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        //System.out.println("evaluateFormula:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        //n.print(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (n == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            //System.out.println("Null node");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        switch (n.type) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   208
            case Node.OP:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   209
                val = evaluateFormula(n.left);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   210
                switch (n.op) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   211
                    case '+':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   212
                        val += evaluateFormula(n.right);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   213
                        break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   214
                    case '*':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   215
                        val *= evaluateFormula(n.right);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   216
                        break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   217
                    case '-':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   218
                        val -= evaluateFormula(n.right);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   219
                        break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   220
                    case '/':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   221
                        val /= evaluateFormula(n.right);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   222
                        break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   223
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                break;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   225
            case Node.VALUE:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   226
                //System.out.println("=>" + n.value);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   227
                return n.value;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   228
            case Node.CELL:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (cells[n.row][n.column] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    //System.out.println("NULL at 193");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    //System.out.println("=>" + cells[n.row][n.column].value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    return cells[n.row][n.column].value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        //System.out.println("=>" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   241
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public synchronized void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int cx, cy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        char l[] = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Dimension d = getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        g.setFont(titleFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        i = g.getFontMetrics().stringWidth(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        g.drawString((title == null) ? "Spreadsheet" : title,
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   253
                (d.width - i) / 2, 12);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        g.setColor(inputColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        g.fillRect(0, cellHeight, d.width, cellHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        g.setFont(titleFont);
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   257
        for (i = 0; i < rows + 1; i++) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   258
            cy = (i + 2) * cellHeight;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            g.setColor(getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            g.draw3DRect(0, cy, d.width, 2, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (i < rows) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                g.setColor(Color.red);
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   263
                g.drawString("" + (i + 1), 2, cy + 12);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        g.setColor(Color.red);
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   268
        cy = (rows + 3) * cellHeight + (cellHeight / 2);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   269
        for (i = 0; i < columns; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            cx = i * cellWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            g.setColor(getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            g.draw3DRect(cx + rowLabelWidth,
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   273
                    2 * cellHeight, 1, d.height, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (i < columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                g.setColor(Color.red);
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   276
                l[0] = (char) ((int) 'A' + i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                g.drawString(new String(l),
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   278
                        cx + rowLabelWidth + (cellWidth / 2),
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   279
                        cy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   283
        for (i = 0; i < rows; i++) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   284
            for (j = 0; j < columns; j++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                cx = (j * cellWidth) + 2 + rowLabelWidth;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   286
                cy = ((i + 1) * cellHeight) + 2 + titleHeight;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                if (cells[i][j] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    cells[i][j].paint(g, cx, cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        g.setColor(getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        g.draw3DRect(0, titleHeight,
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   295
                d.width,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   296
                d.height - titleHeight,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   297
                false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        inputArea.paint(g, 1, titleHeight + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   301
    //1.1 event handling
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   302
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   303
    public void mouseClicked(MouseEvent e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   305
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   306
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   307
    public void mousePressed(MouseEvent e) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   308
        int x = e.getX();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   309
        int y = e.getY();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   310
        Cell cell;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   311
        if (y < (titleHeight + cellHeight)) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   312
            selectedRow = -1;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   313
            if (y <= titleHeight && current != null) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   314
                current.deselect();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   315
                current = null;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   316
            }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   317
            e.consume();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   318
        }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   319
        if (x < rowLabelWidth) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   320
            selectedRow = -1;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   321
            if (current != null) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   322
                current.deselect();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                current = null;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   324
            }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   325
            e.consume();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   327
        }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   328
        selectedRow = ((y - cellHeight - titleHeight) / cellHeight);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   329
        selectedColumn = (x - rowLabelWidth) / cellWidth;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   330
        if (selectedRow > rows
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   331
                || selectedColumn >= columns) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   332
            selectedRow = -1;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   333
            if (current != null) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   334
                current.deselect();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   335
                current = null;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   336
            }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   337
        } else {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   338
            if (selectedRow >= rows) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   339
                selectedRow = -1;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   340
                if (current != null) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   341
                    current.deselect();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   342
                    current = null;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   343
                }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   344
                e.consume();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   345
            }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   346
            if (selectedRow != -1) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   347
                cell = cells[selectedRow][selectedColumn];
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   348
                inputArea.setText(cell.getPrintString());
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   349
                if (current != null) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   350
                    current.deselect();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   351
                }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   352
                current = cell;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   353
                current.select();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   354
                requestFocus();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   355
                fullUpdate = true;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   356
                repaint();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   357
            }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   358
            e.consume();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   359
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   361
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   362
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   363
    public void mouseReleased(MouseEvent e) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   364
    }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   365
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   366
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   367
    public void mouseEntered(MouseEvent e) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   368
    }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   369
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   370
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   371
    public void mouseExited(MouseEvent e) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   372
    }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   373
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   374
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   375
    public void keyPressed(KeyEvent e) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   376
    }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   377
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   378
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   379
    public void keyTyped(KeyEvent e) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   380
        fullUpdate = true;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   381
        inputArea.processKey(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        e.consume();
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   383
    }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   384
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   385
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   386
    public void keyReleased(KeyEvent e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   389
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   390
    public String getAppletInfo() {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   391
        return "Title: SpreadSheet \nAuthor: Sami Shaio \nA simple spread sheet.";
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   392
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   394
    @Override
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   395
    public String[][] getParameterInfo() {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   396
        String[][] info = {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   397
            { "title", "string",
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   398
                "The title of the spread sheet.  Default is 'Spreadsheet'" },
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   399
            { "rows", "int", "The number of rows.  Default is 9." },
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   400
            { "columns", "int", "The number of columns.  Default is 5." }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   401
        };
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   402
        return info;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   403
    }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   404
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   407
class CellUpdater extends Thread {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   409
    Cell target;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    InputStream dataStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    StreamTokenizer tokenStream;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   412
    public volatile boolean run = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    public CellUpdater(Cell c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        super("cell updater");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        target = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   419
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            dataStream = new URL(target.app.getDocumentBase(),
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   423
                    target.getValueString()).openStream();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   424
            tokenStream = new StreamTokenizer(new BufferedReader(
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   425
                    new InputStreamReader(dataStream)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            tokenStream.eolIsSignificant(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   428
            while (run) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                switch (tokenStream.nextToken()) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   430
                    case StreamTokenizer.TT_EOF:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   431
                        dataStream.close();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   432
                        return;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   433
                    default:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   434
                        break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   435
                    case StreamTokenizer.TT_NUMBER:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   436
                        target.setTransientValue((float) tokenStream.nval);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   437
                        if (!target.app.isStopped && !target.paused) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   438
                            target.app.repaint();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   439
                        }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   440
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    Thread.sleep(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   454
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
class Cell {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   456
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public static final int VALUE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public static final int LABEL = 1;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   459
    public static final int URL = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public static final int FORMULA = 3;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   461
    Node parseRoot;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   462
    boolean needRedisplay;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    boolean selected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    boolean transientValue = false;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   465
    public int type = Cell.VALUE;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   466
    String valueString = "";
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   467
    String printString = "v";
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   468
    float value;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   469
    Color bgColor;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   470
    Color fgColor;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   471
    Color highlightColor;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   472
    int width;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   473
    int height;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    SpreadSheet app;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    CellUpdater updaterThread;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   476
    boolean paused = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public Cell(SpreadSheet app,
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   479
            Color bgColor,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   480
            Color fgColor,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   481
            Color highlightColor,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   482
            int width,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   483
            int height) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        this.app = app;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        this.bgColor = bgColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        this.fgColor = fgColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        this.highlightColor = highlightColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        needRedisplay = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public void setRawValue(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        valueString = Float.toString(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        value = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   497
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void setValue(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        setRawValue(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        printString = "v" + valueString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        type = Cell.VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        paused = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        app.recalculate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        needRedisplay = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    public void setTransientValue(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        transientValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        value = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        needRedisplay = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        app.recalculate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public void setUnparsedValue(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        switch (s.charAt(0)) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   516
            case 'v':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   517
                setValue(Cell.VALUE, s.substring(1));
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   518
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   519
            case 'f':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   520
                setValue(Cell.FORMULA, s.substring(1));
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   521
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   522
            case 'l':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   523
                setValue(Cell.LABEL, s.substring(1));
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   524
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   525
            case 'u':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   526
                setValue(Cell.URL, s.substring(1));
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   527
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Parse a spreadsheet formula. The syntax is defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * formula -> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * formula -> value op value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * value -> '(' formula ')'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * value -> cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * value -> <number>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * op -> '+' | '*' | '/' | '-'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * cell -> <letter><number>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public String parseFormula(String formula, Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        String subformula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        String restFormula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        Node left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        Node right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        char op;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (formula == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        subformula = parseValue(formula, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        //System.out.println("subformula = " + subformula);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (subformula == null || subformula.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            //System.out.println("Parse succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   558
        if (subformula.equals(formula)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            //System.out.println("Parse failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            return formula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // parse an operator and then another value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        switch (op = subformula.charAt(0)) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   565
            case 0:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                //System.out.println("Parse succeeded");
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   567
                return null;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   568
            case ')':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   569
                //System.out.println("Returning subformula=" + subformula);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                return subformula;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   571
            case '+':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   572
            case '*':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   573
            case '-':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   574
            case '/':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   575
                restFormula = subformula.substring(1);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   576
                subformula = parseValue(restFormula, right = new Node());
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   577
                //System.out.println("subformula(2) = " + subformula);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   578
                if (subformula == null ? restFormula != null : !subformula.
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   579
                        equals(restFormula)) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   580
                    //System.out.println("Parse succeeded");
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   581
                    left = new Node(node);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   582
                    node.left = left;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   583
                    node.right = right;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   584
                    node.op = op;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   585
                    node.type = Node.OP;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   586
                    //node.print(3);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   587
                    return subformula;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   588
                } else {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   589
                    //System.out.println("Parse failed");
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   590
                    return formula;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   591
                }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   592
            default:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   593
                //System.out.println("Parse failed (bad operator): " + subformula);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                return formula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    public String parseValue(String formula, Node node) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   599
        char c = formula.charAt(0);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   600
        String subformula;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   601
        String restFormula;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   602
        float _value;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   603
        int row;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   604
        int column;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        //System.out.println("parseValue: " + formula);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        restFormula = formula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (c == '(') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            //System.out.println("parseValue(" + formula + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            restFormula = formula.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            subformula = parseFormula(restFormula, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            //System.out.println("rest=(" + subformula + ")");
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   613
            if (subformula == null
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   614
                    || subformula.length() == restFormula.length()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                //System.out.println("Failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                return formula;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   617
            } else if (!(subformula.charAt(0) == ')')) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                //System.out.println("Failed (missing parentheses)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                return formula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            restFormula = subformula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        } else if (c >= '0' && c <= '9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            //System.out.println("formula=" + formula);
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   626
            for (i = 0; i < formula.length(); i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                c = formula.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                if ((c < '0' || c > '9') && c != '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            try {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   633
                _value = Float.valueOf(formula.substring(0, i)).floatValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                //System.out.println("Failed (number format error)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                return formula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            node.type = Node.VALUE;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   639
            node.value = _value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            //node.print(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            restFormula = formula.substring(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            //System.out.println("value= " + value + " i=" + i +
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   643
            //                     " rest = " + restFormula);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return restFormula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        } else if (c >= 'A' && c <= 'Z') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            column = c - 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            restFormula = formula.substring(1);
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   650
            for (i = 0; i < restFormula.length(); i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                c = restFormula.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                if (c < '0' || c > '9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            row = Float.valueOf(restFormula.substring(0, i)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            //System.out.println("row = " + row + " column = " + column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            node.row = row - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            node.column = column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            node.type = Node.CELL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            //node.print(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            if (i == restFormula.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                restFormula = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                restFormula = restFormula.substring(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                if (restFormula.charAt(0) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        return restFormula;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public void setValue(int type, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        paused = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        if (this.type == Cell.URL) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   678
            updaterThread.run = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            updaterThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   682
        valueString = s;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        needRedisplay = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        switch (type) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   686
            case Cell.VALUE:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   687
                setValue(Float.valueOf(s).floatValue());
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   688
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   689
            case Cell.LABEL:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   690
                printString = "l" + valueString;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   691
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   692
            case Cell.URL:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   693
                printString = "u" + valueString;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   694
                updaterThread = new CellUpdater(this);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   695
                updaterThread.start();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   696
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   697
            case Cell.FORMULA:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   698
                parseFormula(valueString, parseRoot = new Node());
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   699
                printString = "f" + valueString;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   700
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        app.recalculate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public String getValueString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        return valueString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public String getPrintString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        return printString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    public void select() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        selected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        paused = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   717
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    public void deselect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        selected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        paused = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        needRedisplay = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        app.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   724
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    public void paint(Graphics g, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (selected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            g.setColor(highlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            g.setColor(bgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        g.fillRect(x, y, width - 1, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if (valueString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            switch (type) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   734
                case Cell.VALUE:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   735
                case Cell.LABEL:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   736
                    g.setColor(fgColor);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   737
                    break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   738
                case Cell.FORMULA:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   739
                    g.setColor(Color.red);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   740
                    break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   741
                case Cell.URL:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   742
                    g.setColor(Color.blue);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   743
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   745
            if (transientValue) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                g.drawString("" + value, x, y + (height / 2) + 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                if (valueString.length() > 14) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    g.drawString(valueString.substring(0, 14),
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   750
                            x, y + (height / 2) + 5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    g.drawString(valueString, x, y + (height / 2) + 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        needRedisplay = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   760
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
class Node {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   762
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public static final int OP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    public static final int VALUE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    public static final int CELL = 2;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   766
    int type;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   767
    Node left;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   768
    Node right;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   769
    int row;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   770
    int column;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   771
    float value;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   772
    char op;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    public Node() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        left = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        right = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        row = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        column = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        op = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        type = Node.VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   783
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    public Node(Node n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        left = n.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        right = n.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        value = n.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        row = n.row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        column = n.column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        op = n.op;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        type = n.type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   793
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    public void indent(int ind) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        for (int i = 0; i < ind; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            System.out.print(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   799
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    public void print(int indentLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        char l[] = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        indent(indentLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        System.out.println("NODE type=" + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        indent(indentLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        switch (type) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   806
            case Node.VALUE:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   807
                System.out.println(" value=" + value);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   808
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   809
            case Node.CELL:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   810
                l[0] = (char) ((int) 'A' + column);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   811
                System.out.println(" cell=" + new String(l) + (row + 1));
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   812
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   813
            case Node.OP:
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   814
                System.out.println(" op=" + op);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   815
                left.print(indentLevel + 3);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   816
                right.print(indentLevel + 3);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   817
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   822
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
class InputField {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   824
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   825
    int maxchars = 50;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   826
    int cursorPos = 0;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   827
    Applet app;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   828
    String sval;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   829
    char buffer[];
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   830
    int nChars;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   831
    int width;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   832
    int height;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   833
    Color bgColor;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   834
    Color fgColor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    public InputField(String initValue, Applet app, int width, int height,
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   837
            Color bgColor, Color fgColor) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        this.bgColor = bgColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        this.fgColor = fgColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        this.app = app;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        buffer = new char[maxchars];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        nChars = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        if (initValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            initValue.getChars(0, initValue.length(), this.buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            nChars = initValue.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        sval = initValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    public void setText(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   855
        for (i = 0; i < maxchars; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            buffer[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            sval = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            sval = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        nChars = sval.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        sval.getChars(0, sval.length(), buffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    public String getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        return sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    public void paint(Graphics g, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        g.setColor(bgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        g.fillRect(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if (sval != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            g.setColor(fgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            g.drawString(sval, x, y + (height / 2) + 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    public void processKey(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        char ch = e.getKeyChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            case '\b': // delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                if (nChars > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                    nChars--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                    sval = new String(buffer, 0, nChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            case '\n': // return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                selected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                if (nChars < maxchars && ch >= '0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    buffer[nChars++] = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    sval = new String(buffer, 0, nChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        app.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    public void keyReleased(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    public void selected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   908
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
class SpreadSheetInput
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   910
        extends InputField {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   912
    public SpreadSheetInput(String initValue,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   913
            SpreadSheet app,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   914
            int width,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   915
            int height,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   916
            Color bgColor,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   917
            Color fgColor) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   918
        super(initValue, app, width, height, bgColor, fgColor);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   919
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   921
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    public void selected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        float f;
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   924
        sval = ("".equals(sval)) ? "v" : sval;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        switch (sval.charAt(0)) {
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   926
            case 'v':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   927
                String s = sval.substring(1);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   928
                try {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   929
                    int i;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   930
                    for (i = 0; i < s.length(); i++) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   931
                        char c = s.charAt(i);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   932
                        if (c < '0' || c > '9') {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   933
                            break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   934
                        }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   935
                    }
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   936
                    s = s.substring(0, i);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   937
                    f = Float.valueOf(s).floatValue();
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   938
                    ((SpreadSheet) app).setCurrentValue(f);
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   939
                } catch (NumberFormatException e) {
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   940
                    System.out.println("Not a float: '" + s + "'");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                }
8978
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   942
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   943
            case 'l':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   944
                ((SpreadSheet) app).setCurrentValue(Cell.LABEL,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   945
                        sval.substring(1));
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   946
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   947
            case 'u':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   948
                ((SpreadSheet) app).setCurrentValue(Cell.URL, sval.substring(1));
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   949
                break;
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   950
            case 'f':
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   951
                ((SpreadSheet) app).setCurrentValue(Cell.FORMULA,
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   952
                        sval.substring(1));
046c16ff00cb 7027690: /applets/SpreadSheet demo needs to be improved
mrkam
parents: 5506
diff changeset
   953
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
}