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