📄 zcalculator.java
字号:
/*
* Copyright 2002 EZCell , Inc. All rights reserved.
* Version 1.0.
* Author W.John
*/
package ezcell;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
public class ZCalculator extends Thread {
static int NOT_CHECKED_EXPRESSION = -1;
private ZDefaultSheet sheet;
private Hashtable items;
private boolean itemChanged;
private int cookie;
private Vector parsingCells;
ZCalculator(ZDefaultSheet sheet) {
this.sheet = sheet;
items = new Hashtable();
parsingCells = new Vector();
start();
}
/**
*
* @param cell
*
* @return
*/
public String getText(ZCell cell) throws Exception {
_Value value = (_Value) items.get(cell);
if (value.cookie == NOT_CHECKED_EXPRESSION) {
throw new Exception("invalid expression!");
} else {
return String.valueOf(value.value);
}
}
/**
*
* @param r
* @param c
*
* @return
*/
public double getValue(int r, int c) {
ZCell cell = sheet.getCell(r, c);
return getValue(cell);
}
/**
*
* @param cell
*
* @return
*/
public double getValue(ZCell cell) {
// is recurse?
if (parsingCells.contains(cell)) {
return 0.0f;
}
if(cell.isChild() )
return 0.0f;
parsingCells.add(cell);
double value = 0.0f;
if (items.containsKey(cell)) {
_Value vo = (_Value) items.get(cell);
// if the value just be refreshed ,needn't calculate again
if (vo.cookie == cookie) {
parsingCells.remove(cell);
return vo.value;
}
try {
if (cell.getText().charAt(0) == '?') {
// launches a parser
ZFormulaParser parser = new ZFormulaParser(this);
value = parser.eval(cell.getText().substring(1));
// System.out.println("cell:" + cell.getRow() + "" + cell.getCol() + ":" + value);
// signs it ,avoid repeatedly parsing
vo.cookie = cookie;
if (vo.value != value) {
vo.value = value;
// gives a change to its canvas for refreshing
sheet.firePropertyChanged(new ZRect(cell.getCol(), cell.getRow(), cell.getCol(), cell.getRow()), true);
// System.out.println(new ZRect(cell.getCol(), cell.getRow(), cell.getCol(), cell.getRow()));
}
} else {
// invalid expression, no longer need to be computed!
items.remove(cell);
}
} catch (Exception ex) {
// invalid expression, no longer need to be computed!
items.remove(cell);
}
} else {
try {
value = Double.valueOf(cell.getText()).doubleValue();
} catch (NumberFormatException ex) {
// if get here , cell's text must not be composed with digits
}
}
parsingCells.remove(cell);
return value;
}
/**
* Checks if cell items has been changed since last calculation. If true,
* have the thread wait.
*
* This wait thread will be activated by set,remove method.
*/
synchronized public void checkItemChanged() {
try {
// i would like have a rest, if everything has been done
if (!itemChanged) {
wait();
}
itemChanged = false;
} catch (InterruptedException ex) {
}
}
/**
* Removes the cell from items which being computed. The method does nothing
* if the cell is not in the items.
* @param cell the cell that need to be removed.
*/
synchronized public void remove(ZCell cell) {
if (!items.containsKey(cell)) {
items.remove(cell);
}
itemChanged = true;
this.notifyAll();
}
/**
*/
public void run() {
while (!interrupted()) {
checkItemChanged();
cookie++;
// avoid the same value of cookie as not checked expression
if (cookie == NOT_CHECKED_EXPRESSION) {
cookie++;
}
parsingCells.clear();
Enumeration cells = items.keys();
while (cells.hasMoreElements()) {
if (itemChanged) {
break;
}
ZCell cell = (ZCell) cells.nextElement();
getValue(cell);
}
// System.out.println(cookie);
}
}
/**
*
* @param cell
*/
synchronized public void set(ZCell cell) {
try {
if (cell.getText().charAt(0) == '?') {
if (!items.containsKey(cell)) {
items.put(cell, new _Value());
}
} else if (items.containsKey(cell)) {
items.remove(cell);
}
} catch (Exception ex) {
if (items.containsKey(cell)) {
items.remove(cell);
}
}
itemChanged = true;
this.notifyAll();
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class _Value {
double value;
int cookie;
_Value(double value, int cookie) {
this.value = value;
this.cookie = cookie;
}
_Value() {
this.cookie = NOT_CHECKED_EXPRESSION;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -