提交 18059efb 编写于 作者: M mrkam

7027690: /applets/SpreadSheet demo needs to be improved

Reviewed-by: alexp
上级 a7dd4655
/*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
......@@ -29,19 +29,17 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.net.*;
public class SpreadSheet
extends Applet
implements MouseListener, KeyListener {
@SuppressWarnings("serial")
public class SpreadSheet extends Applet implements MouseListener, KeyListener {
String title;
Font titleFont;
Color cellColor;
......@@ -62,6 +60,7 @@ public class SpreadSheet
Cell cells[][];
Cell current = null;
@Override
public synchronized void init() {
String rs;
......@@ -87,8 +86,8 @@ public class SpreadSheet
}
cells = new Cell[rows][columns];
char l[] = new char[1];
for (int i=0; i < rows; i++) {
for (int j=0; j < columns; j++) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cells[i][j] = new Cell(this,
Color.lightGray,
......@@ -96,8 +95,8 @@ public class SpreadSheet
cellColor,
cellWidth - 2,
cellHeight - 2);
l[0] = (char)((int)'a' + j);
rs = getParameter("" + new String(l) + (i+1));
l[0] = (char) ((int) 'a' + j);
rs = getParameter("" + new String(l) + (i + 1));
if (rs != null) {
cells[i][j].setUnparsedValue(rs);
}
......@@ -121,19 +120,22 @@ public class SpreadSheet
repaint();
}
@Override
public void stop() {
isStopped = true;
}
@Override
public void start() {
isStopped = false;
}
@Override
public void destroy() {
for (int i=0; i < rows; i++) {
for (int j=0; j < columns; j++) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (cells[i][j].type == Cell.URL) {
cells[i][j].updaterThread.stop();
cells[i][j].updaterThread.run = false;
}
}
}
......@@ -147,16 +149,17 @@ public class SpreadSheet
repaint();
}
@Override
public void update(Graphics g) {
if (! fullUpdate) {
if (!fullUpdate) {
int cx, cy;
g.setFont(titleFont);
for (int i=0; i < rows; i++) {
for (int j=0; j < columns; j++) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (cells[i][j].needRedisplay) {
cx = (j * cellWidth) + 2 + rowLabelWidth;
cy = ((i+1) * cellHeight) + 2 + titleHeight;
cy = ((i + 1) * cellHeight) + 2 + titleHeight;
cells[i][j].paint(g, cx, cy);
}
}
......@@ -168,13 +171,14 @@ public class SpreadSheet
}
public void recalculate() {
int i,j;
int i, j;
//System.out.println("SpreadSheet.recalculate");
for (i=0; i < rows; i++) {
for (j=0; j < columns; j++) {
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
if (cells[i][j] != null && cells[i][j].type == Cell.FORMULA) {
cells[i][j].setRawValue(evaluateFormula(cells[i][j].parseRoot));
cells[i][j].setRawValue(evaluateFormula(
cells[i][j].parseRoot));
cells[i][j].needRedisplay = true;
}
}
......@@ -182,7 +186,7 @@ public class SpreadSheet
repaint();
}
public float evaluateFormula(Node n) {
float evaluateFormula(Node n) {
float val = 0.0f;
//System.out.println("evaluateFormula:");
......@@ -213,9 +217,6 @@ public class SpreadSheet
//System.out.println("=>" + n.value);
return n.value;
case Node.CELL:
if (n == null) {
//System.out.println("NULL at 192");
} else {
if (cells[n.row][n.column] == null) {
//System.out.println("NULL at 193");
} else {
......@@ -223,12 +224,12 @@ public class SpreadSheet
return cells[n.row][n.column].value;
}
}
}
//System.out.println("=>" + val);
return val;
}
@Override
public synchronized void paint(Graphics g) {
int i, j;
int cx, cy;
......@@ -244,36 +245,36 @@ public class SpreadSheet
g.setColor(inputColor);
g.fillRect(0, cellHeight, d.width, cellHeight);
g.setFont(titleFont);
for (i=0; i < rows+1; i++) {
cy = (i+2) * cellHeight;
for (i = 0; i < rows + 1; i++) {
cy = (i + 2) * cellHeight;
g.setColor(getBackground());
g.draw3DRect(0, cy, d.width, 2, true);
if (i < rows) {
g.setColor(Color.red);
g.drawString("" + (i+1), 2, cy + 12);
g.drawString("" + (i + 1), 2, cy + 12);
}
}
g.setColor(Color.red);
cy = (rows+3) * cellHeight + (cellHeight / 2);
for (i=0; i < columns; i++) {
cy = (rows + 3) * cellHeight + (cellHeight / 2);
for (i = 0; i < columns; i++) {
cx = i * cellWidth;
g.setColor(getBackground());
g.draw3DRect(cx + rowLabelWidth,
2 * cellHeight, 1, d.height, true);
if (i < columns) {
g.setColor(Color.red);
l[0] = (char)((int)'A' + i);
l[0] = (char) ((int) 'A' + i);
g.drawString(new String(l),
cx + rowLabelWidth + (cellWidth / 2),
cy);
}
}
for (i=0; i < rows; i++) {
for (j=0; j < columns; j++) {
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
cx = (j * cellWidth) + 2 + rowLabelWidth;
cy = ((i+1) * cellHeight) + 2 + titleHeight;
cy = ((i + 1) * cellHeight) + 2 + titleHeight;
if (cells[i][j] != null) {
cells[i][j].paint(g, cx, cy);
}
......@@ -289,12 +290,12 @@ public class SpreadSheet
}
//1.1 event handling
@Override
public void mouseClicked(MouseEvent e) {
}
public void mouseClicked(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
Cell cell;
......@@ -317,8 +318,8 @@ public class SpreadSheet
}
selectedRow = ((y - cellHeight - titleHeight) / cellHeight);
selectedColumn = (x - rowLabelWidth) / cellWidth;
if (selectedRow > rows ||
selectedColumn >= columns) {
if (selectedRow > rows
|| selectedColumn >= columns) {
selectedRow = -1;
if (current != null) {
current.deselect();
......@@ -335,7 +336,7 @@ public class SpreadSheet
}
if (selectedRow != -1) {
cell = cells[selectedRow][selectedColumn];
inputArea.setText(new String(cell.getPrintString()));
inputArea.setText(cell.getPrintString());
if (current != null) {
current.deselect();
}
......@@ -349,62 +350,73 @@ public class SpreadSheet
}
}
public void mouseReleased(MouseEvent e)
{}
@Override
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e)
{}
@Override
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e)
{}
@Override
public void mouseExited(MouseEvent e) {
}
public void keyPressed(KeyEvent e)
{
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
fullUpdate=true;
fullUpdate = true;
inputArea.processKey(e);
e.consume();
}
public void keyReleased(KeyEvent e)
{}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public String getAppletInfo() {
return "Title: SpreadSheet \nAuthor: Sami Shaio \nA simple spread sheet.";
}
@Override
public String[][] getParameterInfo() {
String[][] info = {
{"title", "string", "The title of the spread sheet. Default is 'Spreadsheet'"},
{"rows", "int", "The number of rows. Default is 9."},
{"columns", "int", "The number of columns. Default is 5."}
{ "title", "string",
"The title of the spread sheet. Default is 'Spreadsheet'" },
{ "rows", "int", "The number of rows. Default is 9." },
{ "columns", "int", "The number of columns. Default is 5." }
};
return info;
}
}
class CellUpdater extends Thread {
Cell target;
InputStream dataStream = null;
StreamTokenizer tokenStream;
public volatile boolean run = true;
public CellUpdater(Cell c) {
super("cell updater");
target = c;
}
@Override
public void run() {
try {
dataStream = new URL(target.app.getDocumentBase(),
target.getValueString()).openStream();
tokenStream = new StreamTokenizer(new BufferedReader(new InputStreamReader(dataStream)));
tokenStream = new StreamTokenizer(new BufferedReader(
new InputStreamReader(dataStream)));
tokenStream.eolIsSignificant(false);
while (true) {
while (run) {
switch (tokenStream.nextToken()) {
case StreamTokenizer.TT_EOF:
dataStream.close();
......@@ -412,8 +424,8 @@ class CellUpdater extends Thread {
default:
break;
case StreamTokenizer.TT_NUMBER:
target.setTransientValue((float)tokenStream.nval);
if (! target.app.isStopped && ! target.paused) {
target.setTransientValue((float) tokenStream.nval);
if (!target.app.isStopped && !target.paused) {
target.app.repaint();
}
break;
......@@ -430,12 +442,13 @@ class CellUpdater extends Thread {
}
}
class Cell {
public static final int VALUE = 0;
public static final int LABEL = 1;
public static final int URL = 2;
public static final int FORMULA = 3;
Node parseRoot;
boolean needRedisplay;
boolean selected = false;
......@@ -472,6 +485,7 @@ class Cell {
valueString = Float.toString(f);
value = f;
}
public void setValue(float f) {
setRawValue(f);
printString = "v" + valueString;
......@@ -519,8 +533,6 @@ class Cell {
public String parseFormula(String formula, Node node) {
String subformula;
String restFormula;
float value;
int length = formula.length();
Node left;
Node right;
char op;
......@@ -534,7 +546,7 @@ class Cell {
//System.out.println("Parse succeeded");
return null;
}
if (subformula == formula) {
if (subformula.equals(formula)) {
//System.out.println("Parse failed");
return formula;
}
......@@ -552,9 +564,10 @@ class Cell {
case '-':
case '/':
restFormula = subformula.substring(1);
subformula = parseValue(restFormula, right=new Node());
subformula = parseValue(restFormula, right = new Node());
//System.out.println("subformula(2) = " + subformula);
if (subformula != restFormula) {
if (subformula == null ? restFormula != null : !subformula.
equals(restFormula)) {
//System.out.println("Parse succeeded");
left = new Node(node);
node.left = left;
......@@ -577,7 +590,7 @@ class Cell {
char c = formula.charAt(0);
String subformula;
String restFormula;
float value;
float _value;
int row;
int column;
......@@ -588,11 +601,11 @@ class Cell {
restFormula = formula.substring(1);
subformula = parseFormula(restFormula, node);
//System.out.println("rest=(" + subformula + ")");
if (subformula == null ||
subformula.length() == restFormula.length()) {
if (subformula == null
|| subformula.length() == restFormula.length()) {
//System.out.println("Failed");
return formula;
} else if (! (subformula.charAt(0) == ')')) {
} else if (!(subformula.charAt(0) == ')')) {
//System.out.println("Failed (missing parentheses)");
return formula;
}
......@@ -601,20 +614,20 @@ class Cell {
int i;
//System.out.println("formula=" + formula);
for (i=0; i < formula.length(); i++) {
for (i = 0; i < formula.length(); i++) {
c = formula.charAt(i);
if ((c < '0' || c > '9') && c != '.') {
break;
}
}
try {
value = Float.valueOf(formula.substring(0, i)).floatValue();
_value = Float.valueOf(formula.substring(0, i)).floatValue();
} catch (NumberFormatException e) {
//System.out.println("Failed (number format error)");
return formula;
}
node.type = Node.VALUE;
node.value = value;
node.value = _value;
//node.print(3);
restFormula = formula.substring(i);
//System.out.println("value= " + value + " i=" + i +
......@@ -625,7 +638,7 @@ class Cell {
column = c - 'A';
restFormula = formula.substring(1);
for (i=0; i < restFormula.length(); i++) {
for (i = 0; i < restFormula.length(); i++) {
c = restFormula.charAt(i);
if (c < '0' || c > '9') {
break;
......@@ -650,15 +663,14 @@ class Cell {
return restFormula;
}
public void setValue(int type, String s) {
paused = false;
if (this.type == Cell.URL) {
updaterThread.stop();
updaterThread.run = false;
updaterThread = null;
}
valueString = new String(s);
valueString = s;
this.type = type;
needRedisplay = true;
switch (type) {
......@@ -693,12 +705,14 @@ class Cell {
selected = true;
paused = true;
}
public void deselect() {
selected = false;
paused = false;
needRedisplay = true;
app.repaint();
}
public void paint(Graphics g, int x, int y) {
if (selected) {
g.setColor(highlightColor);
......@@ -719,7 +733,7 @@ class Cell {
g.setColor(Color.blue);
break;
}
if (transientValue){
if (transientValue) {
g.drawString("" + value, x, y + (height / 2) + 5);
} else {
if (valueString.length() > 14) {
......@@ -734,11 +748,12 @@ class Cell {
}
}
class Node {
public static final int OP = 0;
public static final int VALUE = 1;
public static final int CELL = 2;
int type;
Node left;
Node right;
......@@ -756,6 +771,7 @@ class Node {
op = 0;
type = Node.VALUE;
}
public Node(Node n) {
left = n.left;
right = n.right;
......@@ -765,11 +781,13 @@ class Node {
op = n.op;
type = n.type;
}
public void indent(int ind) {
for (int i = 0; i < ind; i++) {
System.out.print(" ");
}
}
public void print(int indentLevel) {
char l[] = new char[1];
indent(indentLevel);
......@@ -780,8 +798,8 @@ class Node {
System.out.println(" value=" + value);
break;
case Node.CELL:
l[0] = (char)((int)'A' + column);
System.out.println(" cell=" + new String(l) + (row+1));
l[0] = (char) ((int) 'A' + column);
System.out.println(" cell=" + new String(l) + (row + 1));
break;
case Node.OP:
System.out.println(" op=" + op);
......@@ -792,7 +810,9 @@ class Node {
}
}
class InputField {
int maxchars = 50;
int cursorPos = 0;
Applet app;
......@@ -823,7 +843,7 @@ class InputField {
public void setText(String val) {
int i;
for (i=0; i < maxchars; i++) {
for (i = 0; i < maxchars; i++) {
buffer[i] = 0;
}
if (val == null) {
......@@ -876,6 +896,7 @@ class InputField {
}
}
class SpreadSheetInput
extends InputField {
......@@ -888,34 +909,38 @@ class SpreadSheetInput
super(initValue, app, width, height, bgColor, fgColor);
}
@Override
public void selected() {
float f;
sval = ("".equals(sval)) ? "v":sval;
sval = ("".equals(sval)) ? "v" : sval;
switch (sval.charAt(0)) {
case 'v':
String s= sval.substring(1);
String s = sval.substring(1);
try {
int i;
for (i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c < '0' || c > '9')
if (c < '0' || c > '9') {
break;
}
}
s = s.substring(0, i);
f = Float.valueOf(s).floatValue();
((SpreadSheet)app).setCurrentValue(f);
((SpreadSheet) app).setCurrentValue(f);
} catch (NumberFormatException e) {
System.out.println("Not a float: '" + s + "'");
}
break;
case 'l':
((SpreadSheet)app).setCurrentValue(Cell.LABEL, sval.substring(1));
((SpreadSheet) app).setCurrentValue(Cell.LABEL,
sval.substring(1));
break;
case 'u':
((SpreadSheet)app).setCurrentValue(Cell.URL, sval.substring(1));
((SpreadSheet) app).setCurrentValue(Cell.URL, sval.substring(1));
break;
case 'f':
((SpreadSheet)app).setCurrentValue(Cell.FORMULA, sval.substring(1));
((SpreadSheet) app).setCurrentValue(Cell.FORMULA,
sval.substring(1));
break;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册