FileDocCategorySizeDatePackage
SpreadSheet.javaAPI DocSun JDK 1.4.2 Example22034Thu May 12 00:35:30 BST 2005None

SpreadSheet

public class SpreadSheet extends Applet implements MouseListener, KeyListener

Fields Summary
String
title
Font
titleFont
Color
cellColor
Color
inputColor
int
cellWidth
int
cellHeight
int
titleHeight
int
rowLabelWidth
Font
inputFont
boolean
isStopped
boolean
fullUpdate
int
rows
int
columns
int
currentKey
int
selectedRow
int
selectedColumn
SpreadSheetInput
inputArea
Cell[]
cells
Cell
current
Constructors Summary
Methods Summary
public voiddestroy()

	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();
		}
	    }
	}
    
public floatevaluateFormula(Node n)

	float	val = 0.0f;

	//System.out.println("evaluateFormula:");
	//n.print(3);
	if (n == null) {
	    //System.out.println("Null node");
	    return val;
	}
	switch (n.type) {
	  case Node.OP:
	    val = evaluateFormula(n.left);
	    switch (n.op) {
	      case '+":
		val += evaluateFormula(n.right);
		break;
	      case '*":
		val *= evaluateFormula(n.right);
		break;
	      case '-":
		val -= evaluateFormula(n.right);
		break;
	      case '/":
		val /= evaluateFormula(n.right);
		break;
	    }
	    break;
	  case Node.VALUE:
	    //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 {
		    //System.out.println("=>" + cells[n.row][n.column].value);
		    return cells[n.row][n.column].value;
		}
	    }
	}

	//System.out.println("=>" + val);
	return val;
    
public java.lang.StringgetAppletInfo()

    return "Title: SpreadSheet \nAuthor: Sami Shaio \nA simple spread sheet.";
  
public java.lang.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."}
    };
    return info;
  
public synchronized voidinit()


        
	String rs;
	
	cellColor = Color.white;
	inputColor = new Color(100, 100, 225);
	inputFont = new Font("Monospaced", Font.PLAIN, 10);
	titleFont = new Font("Monospaced", Font.BOLD, 12);
	title = getParameter("title");
	if (title == null) {
	    title = "Spreadsheet";
	}
	rs = getParameter("rows");
	if (rs == null) {
	    rows = 9;
	} else {
	    rows = Integer.parseInt(rs);
	}
	rs = getParameter("columns");
	if (rs == null) {
	    columns = 5;
	} else {
	    columns = Integer.parseInt(rs);
	}
	cells = new Cell[rows][columns];
	char l[] = new char[1];
	for (int i=0; i < rows; i++) {
	    for (int j=0; j < columns; j++) {

		cells[i][j] = new Cell(this,
				       Color.lightGray,
				       Color.black,
				       cellColor,
				       cellWidth - 2,
				       cellHeight - 2);
		l[0] = (char)((int)'a" + j);
		rs = getParameter("" + new String(l) + (i+1));
		if (rs != null) {
		    cells[i][j].setUnparsedValue(rs);
		}
	    }
	}

	Dimension d = getSize();
	inputArea = new SpreadSheetInput(null, this, d.width - 2, cellHeight - 1,
					 inputColor, Color.white);
	resize(columns * cellWidth + rowLabelWidth,
	       (rows + 3) * cellHeight + titleHeight);
	addMouseListener(this);
	addKeyListener(this);
    
public voidkeyPressed(java.awt.event.KeyEvent e)

  
public voidkeyReleased(java.awt.event.KeyEvent e)

public voidkeyTyped(java.awt.event.KeyEvent e)

    fullUpdate=true;
    inputArea.processKey(e);
    e.consume();
  
public voidmouseClicked(java.awt.event.MouseEvent e)

public voidmouseEntered(java.awt.event.MouseEvent e)

public voidmouseExited(java.awt.event.MouseEvent e)

public voidmousePressed(java.awt.event.MouseEvent e)

    int x = e.getX();
    int y = e.getY();
    Cell cell;
    if (y < (titleHeight + cellHeight)) {
      selectedRow = -1;
      if (y <= titleHeight && current != null) {
	current.deselect();
	current = null;
      }
      e.consume();
    }
    if (x < rowLabelWidth) {
      selectedRow = -1;
      if (current != null) {
	current.deselect();
		current = null;
      }
      e.consume();
      
    }
    selectedRow = ((y - cellHeight - titleHeight) / cellHeight);
    selectedColumn = (x - rowLabelWidth) / cellWidth;
    if (selectedRow > rows ||
	selectedColumn >= columns) {
      selectedRow = -1;
      if (current != null) {
	current.deselect();
	current = null;
      }
    } else {
      if (selectedRow >= rows) {
	selectedRow = -1;
	if (current != null) {
	  current.deselect();
	  current = null;
	}
	e.consume();
      }
      if (selectedRow != -1) {      
        cell = cells[selectedRow][selectedColumn];
        inputArea.setText(new String(cell.getPrintString()));
        if (current != null) {
          current.deselect();
        }
        current = cell;
        current.select();
        requestFocus();
        fullUpdate = true;
        repaint();
      }
      e.consume();
    }
  
public voidmouseReleased(java.awt.event.MouseEvent e)

public synchronized voidpaint(java.awt.Graphics g)

	int i, j;
	int cx, cy;
	char l[] = new char[1];


	Dimension d = getSize();

	g.setFont(titleFont);
	i = g.getFontMetrics().stringWidth(title);
	g.drawString((title == null) ? "Spreadsheet" : title,
		     (d.width - i) / 2, 12);
	g.setColor(inputColor);
	g.fillRect(0, cellHeight, d.width, cellHeight);
	g.setFont(titleFont);
	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.setColor(Color.red);
	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);
		g.drawString(new String(l),
			     cx + rowLabelWidth + (cellWidth / 2),
			     cy);
	    }
	}

	for (i=0; i < rows; i++) {
	    for (j=0; j < columns; j++) {
		cx = (j * cellWidth) + 2 + rowLabelWidth;
		cy = ((i+1) * cellHeight) + 2 + titleHeight;
		if (cells[i][j] != null) {
		    cells[i][j].paint(g, cx, cy);
		}
	    }
	}

	g.setColor(getBackground());
	g.draw3DRect(0, titleHeight,
		      d.width,
		      d.height - titleHeight,
		      false);
	inputArea.paint(g, 1, titleHeight + 1);
    
public voidrecalculate()

	int	i,j;

	//System.out.println("SpreadSheet.recalculate");
	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].needRedisplay = true;
		}
	    }
	}
	repaint();
    
public voidsetCurrentValue(float val)

	if (selectedRow == -1 || selectedColumn == -1) {
	    return;
	}
	cells[selectedRow][selectedColumn].setValue(val);
	repaint();
    
public voidsetCurrentValue(int type, java.lang.String val)

	if (selectedRow == -1 || selectedColumn == -1) {
	    return;
	}
	cells[selectedRow][selectedColumn].setValue(type, val);
	repaint();
    
public voidstart()

	isStopped = false;
    
public voidstop()

	isStopped = true;
    
public voidupdate(java.awt.Graphics g)

	if (! fullUpdate) {
	    int cx, cy;

	    g.setFont(titleFont);
	    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;
			cells[i][j].paint(g, cx, cy);
		    }
		}
	    }
	} else {
	    paint(g);
	    fullUpdate = false;
	}