home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch06 / Sheet.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  4.1 KB  |  187 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. /*
  5.  * the panel that holds the cells
  6.  */
  7. public class Sheet extends Panel implements MouseListener
  8. {
  9.  
  10. /*
  11.  * the number of rows and columns in the spreadsheet
  12.  */
  13. int rows;
  14. int cols;
  15.  
  16. /*
  17.  * width and height of the panel
  18.  */
  19. int width;
  20. int height;
  21.  
  22. /*
  23.  * the array of cells
  24.  */
  25. Cell cells[][];
  26.  
  27. /*
  28.  * offsets into the panel where the cells are displayed
  29.  */
  30. int xoffset;
  31. int yoffset;
  32.  
  33. /*
  34.  * the row and column selected by a mouse click
  35.  */
  36. int selectedRow = 0;
  37. int selectedCol = 0;
  38.  
  39. /*
  40.  * the text entry field
  41.  */
  42. TextField textField;
  43.  
  44. /*
  45.  * constructor
  46.  * create all the cells and init them
  47.  * @param r - number of rows
  48.  * @param c - number of columns
  49.  * @param w - width of the panel
  50.  * @param h - height of the panel
  51.  * @param t - instance of text entry field
  52.  */
  53. public Sheet (int r, int c, int w, int h, TextField t) {
  54.  
  55.        int i, j;
  56.  
  57.        rows = r;
  58.        cols = c;
  59.        width = w;
  60.        height = h;
  61.        xoffset = 30;
  62.        yoffset = 30;
  63.        textField = t;
  64.  
  65.        cells = new Cell[rows][cols]; 
  66.        for (i=0; i<rows; i+=1) {
  67.               for (j=0; j<cols; j+=1) {
  68.                      cells[i][j] = new Cell (cells, rows, cols);
  69.               }
  70.        }
  71.     addMouseListener(this); 
  72.  
  73. }
  74.  
  75. /*
  76.  * a mapping array for converting column indexes to characters
  77.  */
  78. static String charMap[] = {
  79.        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
  80.        "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
  81.        "u", "v", "w", "x", "y", "z"
  82. };
  83.  
  84. /*
  85.  * paint each cell
  86.  * @param g - destination graphics object
  87.  */
  88. public void paint (Graphics g) {
  89.  
  90.        int i, j;
  91.        int x, y;
  92.        int w, h;
  93.        double val;
  94.        String s;
  95.  
  96.        w = width / cols;
  97.        h = height / rows;
  98.  
  99.        x = 0;
  100.        g.setColor (Color.black);
  101.        for (i=0; i<rows; i+=1) {
  102.               y = (i * height / rows) + yoffset;
  103.               g.drawString (String.valueOf (i+1), x, y+h);
  104.        }
  105.        y = yoffset-2;
  106.        for (j=0; j<cols; j+=1) {
  107.               x = (j * width / cols) + xoffset+(w/2);
  108.               g.drawString (charMap[j], x, y);
  109.        }
  110.        for (i=0; i<rows; i+=1) {
  111.               for (j=0; j<cols; j+=1) {
  112.                      s = cells[i][j].evalToString ();
  113.                      x = (j * width / cols) + xoffset + 2;
  114.                      y = (i * height / rows) + yoffset - 2;
  115.                      if (i == selectedRow && j == selectedCol) {
  116.                             g.setColor (Color.yellow);
  117.                             g.fillRect (x, y, w-1, h-1);
  118.                      } else {
  119.                             g.setColor (Color.white);
  120.                             g.fillRect (x, y, w-1, h-1);
  121.                      }
  122.                      g.setColor (Color.black);
  123.                      g.drawString (s, x, y+h); 
  124.               }
  125.        }
  126. }
  127.  
  128. /*
  129.  * called to recalculate the entire spreadsheet
  130.  */
  131. void recalculate () {
  132.  
  133.        int i, j;
  134.  
  135.        for (i=0; i<rows; i+=1) {
  136.               for (j=0; j<cols; j+=1) {
  137.                      cells[i][j].evaluate ();
  138.               }
  139.        }
  140. }
  141.  
  142. /*
  143.  * handle mouse down events
  144.  * @param evt - event object
  145.  * @param x - mouse x position
  146.  * @param y - mouse y position
  147.  */
  148.  
  149.     public void mouseClicked(MouseEvent e){}
  150.     public void mouseEntered(MouseEvent e){}
  151.     public void mouseExited(MouseEvent e){}
  152.     public void mouseReleased(MouseEvent e){}
  153.     public void mousePressed(MouseEvent e)
  154.     {
  155.         int x = e.getX();
  156.         int y = e.getY();
  157.  
  158.            int w = width / cols;
  159.            int h = height / rows;
  160.  
  161.            int sr = (y-yoffset)/h;
  162.            int sc = (x-xoffset)/w;
  163.  
  164.            if (sr < 0 || sr >= rows || sc < 0 || sc > cols)
  165.            return;
  166.  
  167.  
  168.            selectedRow = sr;
  169.            selectedCol = sc; 
  170.            repaint ();
  171.            textField.setText (cells[selectedRow][selectedCol].text);
  172.     }
  173.  
  174.  
  175. /*
  176.  * called to enter a text into a selected cell
  177.  * @param s - the string to enter
  178.  */
  179. void enter (String s) {
  180.  
  181.        cells[selectedRow][selectedCol].enter (s);
  182.        recalculate ();
  183.        repaint ();
  184. }
  185. }
  186.  
  187.