⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spreadsheet.java

📁 实用的spreadsheet功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		int width,		int height) {	this.app = app;	this.bgColor = bgColor;	this.fgColor = fgColor;	this.highlightColor = highlightColor;	this.width = width;	this.height = height;	needRedisplay = true;    }		    public void setRawValue(float f) {	valueString = Float.toString(f);	value = f;    }    public void setValue(float f) {	setRawValue(f);	printString = "v" + valueString;	type = Cell.VALUE;	paused = false;	app.recalculate();	needRedisplay = true;    }    public void setTransientValue(float f) {	transientValue = true;	value = f;	needRedisplay = true;	app.recalculate();    }    public void setUnparsedValue(String s) {	switch (s.charAt(0)) {	  case 'v':	    setValue(Cell.VALUE, s.substring(1));	    break;	  case 'f':	    setValue(Cell.FORMULA, s.substring(1));	    break;	  case 'l':	    setValue(Cell.LABEL, s.substring(1));	    break;	  case 'u':	    setValue(Cell.URL, s.substring(1));	    break;	}    }    /**     * Parse a spreadsheet formula. The syntax is defined as:     *     * formula -> value     * formula -> value op value     * value -> '(' formula ')'     * value -> cell     * value -> <number>     * op -> '+' | '*' | '/' | '-'     * cell -> <letter><number>     */    public String parseFormula(String formula, Node node) {	String subformula;	String restFormula;	float value;	int length = formula.length();	Node left;	Node right;	char op;	if (formula == null) {	    return null;	}	subformula = parseValue(formula, node);	//System.out.println("subformula = " + subformula);	if (subformula == null || subformula.length() == 0) {	    //System.out.println("Parse succeeded");	    return null;	}	if (subformula == formula) {	    //System.out.println("Parse failed");	    return formula;	}	// parse an operator and then another value	switch (op = subformula.charAt(0)) {	  case 0:	    //System.out.println("Parse succeeded");	    return null;	  case ')':	    //System.out.println("Returning subformula=" + subformula);	    return subformula;	  case '+':	  case '*':	  case '-':	  case '/':	    restFormula = subformula.substring(1);	    subformula = parseValue(restFormula, right=new Node());	    //System.out.println("subformula(2) = " + subformula);	    if (subformula != restFormula) {		//System.out.println("Parse succeeded");		left = new Node(node);		node.left = left;		node.right = right;		node.op = op;		node.type = Node.OP;		//node.print(3);		return subformula;	    } else {		//System.out.println("Parse failed");		return formula;	    }	  default:	    //System.out.println("Parse failed (bad operator): " + subformula);	    return formula;	}    }    public String parseValue(String formula, Node node) {	char	c = formula.charAt(0);	String	subformula;	String	restFormula;	float	value;	int	row;	int	column;	//System.out.println("parseValue: " + formula);	restFormula = formula;	if (c == '(') {	    //System.out.println("parseValue(" + formula + ")");	    restFormula = formula.substring(1);	    subformula = parseFormula(restFormula, node);	    //System.out.println("rest=(" + subformula + ")");	    if (subformula == null ||		subformula.length() == restFormula.length()) {		//System.out.println("Failed");		return formula;	    } else if (! (subformula.charAt(0) == ')')) {	        //System.out.println("Failed (missing parentheses)");		return formula;	    }	    restFormula = subformula;	} else if (c >= '0' && c <= '9') {	    int i;	    //System.out.println("formula=" + formula);	    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();	    } catch (NumberFormatException e) {		//System.out.println("Failed (number format error)");		return formula;	    }	    node.type = Node.VALUE;	    node.value = value;	    //node.print(3);	    restFormula = formula.substring(i);	    //System.out.println("value= " + value + " i=" + i +		//		       " rest = " + restFormula);	    return restFormula;	} else if (c >= 'A' && c <= 'Z') {	    int i;	    column = c - 'A';	    restFormula = formula.substring(1);	    for (i=0; i < restFormula.length(); i++) {		c = restFormula.charAt(i);		if (c < '0' || c > '9') {		    break;		}	    }	    row = Float.valueOf(restFormula.substring(0, i)).intValue();	    //System.out.println("row = " + row + " column = " + column);	    node.row = row - 1;	    node.column = column;	    node.type = Node.CELL;	    //node.print(3);	    if (i == restFormula.length()) {		restFormula = null;	    } else {		restFormula = restFormula.substring(i);		if (restFormula.charAt(0) == 0) {		    return null;		}	    }	    	}	return restFormula;    }    public void setValue(int type, String s) {	paused = false;	if (this.type == Cell.URL) {	    updaterThread.stop();	    updaterThread = null;	}	valueString = new String(s);	this.type = type;	needRedisplay = true;	switch (type) {	  case Cell.VALUE:	    setValue(Float.valueOf(s).floatValue());	    break;	  case Cell.LABEL:	    printString = "l" + valueString;	    break;	  case Cell.URL:	    printString = "u" + valueString;	    updaterThread = new CellUpdater(this);	    updaterThread.start();	    break;	  case Cell.FORMULA:	    parseFormula(valueString, parseRoot = new Node());	    printString = "f" + valueString;	    break;	}	app.recalculate();    }    public String getValueString() {	return valueString;    }    public String getPrintString() {	return printString;    }    public void select() {	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);	} else {	    g.setColor(bgColor);	}	g.fillRect(x, y, width - 1, height);	if (valueString != null) {	    switch (type) {	      case Cell.VALUE:	      case Cell.LABEL:		g.setColor(fgColor);		break;	      case Cell.FORMULA:		g.setColor(Color.red);		break;	      case Cell.URL:		g.setColor(Color.blue);		break;	    }	    if (transientValue){		g.drawString("" + value, x, y + (height / 2) + 5);	    } else {		if (valueString.length() > 14) {		    g.drawString(valueString.substring(0, 14),				 x, y + (height / 2) + 5);		} else {		    g.drawString(valueString, x, y + (height / 2) + 5);		}	    }	}	needRedisplay = false;    }}class Node {    public static final int OP = 0;    public static final	int VALUE = 1;    public static final int CELL = 2;    int		type;    Node 	left;    Node 	right;    int  	row;    int  	column;    float	value;    char	op;    public Node() {	left = null;	right = null;	value = 0;	row = -1;	column = -1;	op = 0;	type = Node.VALUE;    }    public Node(Node n) {	left = n.left;	right = n.right;	value = n.value;	row = n.row;	column = n.column;	op = n.op;	type = n.type;    }    public void indent(int ind) {	for (int i = 0; i < ind; i++) {	    System.out.print(" ");	}    }    public void print(int indentLevel) {	char l[] = new char[1];	indent(indentLevel);	System.out.println("NODE type=" + type);	indent(indentLevel);	switch (type) {	  case Node.VALUE:	    System.out.println(" value=" + value);	    break;	  case Node.CELL:	    l[0] = (char)((int)'A' + column);	    System.out.println(" cell=" + new String(l) + (row+1));	    break;	  case Node.OP:	    System.out.println(" op=" + op);	    left.print(indentLevel + 3);	    right.print(indentLevel + 3);	    break;	}    }}class InputField {    int		maxchars = 50;    int		cursorPos = 0;    Applet	app;    String	sval;    char	buffer[];    int		nChars;    int		width;    int		height;    Color	bgColor;    Color	fgColor;    public InputField(String initValue, Applet app, int width, int height,		      Color bgColor, Color fgColor) {	this.width = width;	this.height = height;	this.bgColor = bgColor;	this.fgColor = fgColor;	this.app = app;	buffer = new char[maxchars];	nChars = 0;	if (initValue != null) {	    initValue.getChars(0, initValue.length(), this.buffer, 0);	    nChars = initValue.length();	}	sval = initValue;    }    public void setText(String val) {	int i;	for (i=0; i < maxchars; i++) {	    buffer[i] = 0;	}	if (val == null) {	    sval = "";	} else {	    sval = val;	}	nChars = sval.length();	sval.getChars(0, sval.length(), buffer, 0);    }    public String getValue() {	return sval;    }    public void paint(Graphics g, int x, int y) {	g.setColor(bgColor);	g.fillRect(x, y, width, height);	if (sval != null) {	    g.setColor(fgColor);	    g.drawString(sval, x, y + (height / 2) + 3);	}    }    public void processKey(KeyEvent e) {        char ch = e.getKeyChar();        switch (ch) {            case '\b': // delete                if (nChars > 0) {                    nChars--;                    sval = new String(buffer, 0, nChars);                }                break;            case '\n': // return	        selected();	        break;            default:	        if (nChars < maxchars && ch >= '0') {	            buffer[nChars++] = ch;	            sval = new String(buffer, 0, nChars);	        }        }        app.repaint();        }      public void keyReleased(KeyEvent e) {    }            public void selected() {    }}class SpreadSheetInput     extends InputField {      public SpreadSheetInput(String initValue,			  SpreadSheet app,			  int width,			  int height,			  Color bgColor,			  Color fgColor) {    super(initValue, app, width, height, bgColor, fgColor);  }          public void selected() {	float f;	switch (sval.charAt(0)) {	  case 'v':	      String s= sval.substring(1);	    try {		int i;		for (i = 0; i < s.length(); i++) {		    char c = s.charAt(i);		    if (c < '0' || c > '9')			break;		}		s = s.substring(0, i);		f = Float.valueOf(s).floatValue();		((SpreadSheet)app).setCurrentValue(f);	    } catch (NumberFormatException e) {		System.out.println("Not a float: '" + s + "'");	    }	    break;	  case 'l':	    ((SpreadSheet)app).setCurrentValue(Cell.LABEL, sval.substring(1));	    break;	  case 'u':	    ((SpreadSheet)app).setCurrentValue(Cell.URL, sval.substring(1));	    break;	  case 'f':	    ((SpreadSheet)app).setCurrentValue(Cell.FORMULA, sval.substring(1));	    break;	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -