home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 4.1 KB | 187 lines |
- import java.awt.*;
- import java.awt.event.*;
-
- /*
- * the panel that holds the cells
- */
- public class Sheet extends Panel implements MouseListener
- {
-
- /*
- * the number of rows and columns in the spreadsheet
- */
- int rows;
- int cols;
-
- /*
- * width and height of the panel
- */
- int width;
- int height;
-
- /*
- * the array of cells
- */
- Cell cells[][];
-
- /*
- * offsets into the panel where the cells are displayed
- */
- int xoffset;
- int yoffset;
-
- /*
- * the row and column selected by a mouse click
- */
- int selectedRow = 0;
- int selectedCol = 0;
-
- /*
- * the text entry field
- */
- TextField textField;
-
- /*
- * constructor
- * create all the cells and init them
- * @param r - number of rows
- * @param c - number of columns
- * @param w - width of the panel
- * @param h - height of the panel
- * @param t - instance of text entry field
- */
- public Sheet (int r, int c, int w, int h, TextField t) {
-
- int i, j;
-
- rows = r;
- cols = c;
- width = w;
- height = h;
- xoffset = 30;
- yoffset = 30;
- textField = t;
-
- cells = new Cell[rows][cols];
- for (i=0; i<rows; i+=1) {
- for (j=0; j<cols; j+=1) {
- cells[i][j] = new Cell (cells, rows, cols);
- }
- }
- addMouseListener(this);
-
- }
-
- /*
- * a mapping array for converting column indexes to characters
- */
- static String charMap[] = {
- "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
- "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
- "u", "v", "w", "x", "y", "z"
- };
-
- /*
- * paint each cell
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int i, j;
- int x, y;
- int w, h;
- double val;
- String s;
-
- w = width / cols;
- h = height / rows;
-
- x = 0;
- g.setColor (Color.black);
- for (i=0; i<rows; i+=1) {
- y = (i * height / rows) + yoffset;
- g.drawString (String.valueOf (i+1), x, y+h);
- }
- y = yoffset-2;
- for (j=0; j<cols; j+=1) {
- x = (j * width / cols) + xoffset+(w/2);
- g.drawString (charMap[j], x, y);
- }
- for (i=0; i<rows; i+=1) {
- for (j=0; j<cols; j+=1) {
- s = cells[i][j].evalToString ();
- x = (j * width / cols) + xoffset + 2;
- y = (i * height / rows) + yoffset - 2;
- if (i == selectedRow && j == selectedCol) {
- g.setColor (Color.yellow);
- g.fillRect (x, y, w-1, h-1);
- } else {
- g.setColor (Color.white);
- g.fillRect (x, y, w-1, h-1);
- }
- g.setColor (Color.black);
- g.drawString (s, x, y+h);
- }
- }
- }
-
- /*
- * called to recalculate the entire spreadsheet
- */
- void recalculate () {
-
- int i, j;
-
- for (i=0; i<rows; i+=1) {
- for (j=0; j<cols; j+=1) {
- cells[i][j].evaluate ();
- }
- }
- }
-
- /*
- * handle mouse down events
- * @param evt - event object
- * @param x - mouse x position
- * @param y - mouse y position
- */
-
- public void mouseClicked(MouseEvent e){}
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
- public void mouseReleased(MouseEvent e){}
- public void mousePressed(MouseEvent e)
- {
- int x = e.getX();
- int y = e.getY();
-
- int w = width / cols;
- int h = height / rows;
-
- int sr = (y-yoffset)/h;
- int sc = (x-xoffset)/w;
-
- if (sr < 0 || sr >= rows || sc < 0 || sc > cols)
- return;
-
-
- selectedRow = sr;
- selectedCol = sc;
- repaint ();
- textField.setText (cells[selectedRow][selectedCol].text);
- }
-
-
- /*
- * called to enter a text into a selected cell
- * @param s - the string to enter
- */
- void enter (String s) {
-
- cells[selectedRow][selectedCol].enter (s);
- recalculate ();
- repaint ();
- }
- }
-
-