home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / WDESAMPL.BIN / SpreadSheet.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-12-06  |  5.9 KB  |  286 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8.  
  9. public class SpreadSheet extends Applet {
  10.    String title;
  11.    Font titleFont;
  12.    Color cellColor;
  13.    Color inputColor;
  14.    int cellWidth = 100;
  15.    int cellHeight = 15;
  16.    int titleHeight = 15;
  17.    int rowLabelWidth = 15;
  18.    Font inputFont;
  19.    boolean isStopped = false;
  20.    boolean fullUpdate = true;
  21.    int rows;
  22.    int columns;
  23.    int currentKey = -1;
  24.    int selectedRow = -1;
  25.    int selectedColumn = -1;
  26.    SpreadSheetInput inputArea;
  27.    Cell[][] cells;
  28.    Cell current;
  29.  
  30.    public synchronized void init() {
  31.       this.cellColor = Color.white;
  32.       this.inputColor = new Color(100, 100, 225);
  33.       this.inputFont = new Font("Courier", 0, 10);
  34.       this.titleFont = new Font("Courier", 1, 12);
  35.       this.title = ((Applet)this).getParameter("title");
  36.       if (this.title == null) {
  37.          this.title = "Spreadsheet";
  38.       }
  39.  
  40.       String var1 = ((Applet)this).getParameter("rows");
  41.       if (var1 == null) {
  42.          this.rows = 9;
  43.       } else {
  44.          this.rows = Integer.parseInt(var1);
  45.       }
  46.  
  47.       var1 = ((Applet)this).getParameter("columns");
  48.       if (var1 == null) {
  49.          this.columns = 5;
  50.       } else {
  51.          this.columns = Integer.parseInt(var1);
  52.       }
  53.  
  54.       this.cells = new Cell[this.rows][this.columns];
  55.       char[] var2 = new char[1];
  56.  
  57.       for(int var3 = 0; var3 < this.rows; ++var3) {
  58.          for(int var4 = 0; var4 < this.columns; ++var4) {
  59.             this.cells[var3][var4] = new Cell(this, Color.lightGray, Color.black, this.cellColor, this.cellWidth - 2, this.cellHeight - 2);
  60.             var2[0] = (char)(97 + var4);
  61.             var1 = ((Applet)this).getParameter("" + new String(var2) + (var3 + 1));
  62.             if (var1 != null) {
  63.                this.cells[var3][var4].setUnparsedValue(var1);
  64.             }
  65.          }
  66.       }
  67.  
  68.       Dimension var7 = ((Component)this).size();
  69.       this.inputArea = new SpreadSheetInput((String)null, this, var7.width - 2, this.cellHeight - 1, this.inputColor, Color.white);
  70.       ((Applet)this).resize(this.columns * this.cellWidth + this.rowLabelWidth, (this.rows + 3) * this.cellHeight + this.titleHeight);
  71.    }
  72.  
  73.    public void setCurrentValue(float var1) {
  74.       if (this.selectedRow != -1 && this.selectedColumn != -1) {
  75.          this.cells[this.selectedRow][this.selectedColumn].setValue(var1);
  76.          ((Component)this).repaint();
  77.       }
  78.    }
  79.  
  80.    public void stop() {
  81.       this.isStopped = true;
  82.    }
  83.  
  84.    public void start() {
  85.       this.isStopped = false;
  86.    }
  87.  
  88.    public void destroy() {
  89.       for(int var1 = 0; var1 < this.rows; ++var1) {
  90.          for(int var2 = 0; var2 < this.columns; ++var2) {
  91.             if (this.cells[var1][var2].type == 2) {
  92.                this.cells[var1][var2].updaterThread.stop();
  93.             }
  94.          }
  95.       }
  96.  
  97.    }
  98.  
  99.    public void setCurrentValue(int var1, String var2) {
  100.       if (this.selectedRow != -1 && this.selectedColumn != -1) {
  101.          this.cells[this.selectedRow][this.selectedColumn].setValue(var1, var2);
  102.          ((Component)this).repaint();
  103.       }
  104.    }
  105.  
  106.    public void update(Graphics var1) {
  107.       if (this.fullUpdate) {
  108.          this.paint(var1);
  109.          this.fullUpdate = false;
  110.       } else {
  111.          var1.setFont(this.titleFont);
  112.  
  113.          for(int var4 = 0; var4 < this.rows; ++var4) {
  114.             for(int var5 = 0; var5 < this.columns; ++var5) {
  115.                if (this.cells[var4][var5].needRedisplay) {
  116.                   int var2 = var5 * this.cellWidth + 2 + this.rowLabelWidth;
  117.                   int var3 = (var4 + 1) * this.cellHeight + 2 + this.titleHeight;
  118.                   this.cells[var4][var5].paint(var1, var2, var3);
  119.                }
  120.             }
  121.          }
  122.  
  123.       }
  124.    }
  125.  
  126.    public void recalculate() {
  127.       for(int var1 = 0; var1 < this.rows; ++var1) {
  128.          for(int var2 = 0; var2 < this.columns; ++var2) {
  129.             if (this.cells[var1][var2] != null && this.cells[var1][var2].type == 3) {
  130.                this.cells[var1][var2].setRawValue(this.evaluateFormula(this.cells[var1][var2].parseRoot));
  131.                this.cells[var1][var2].needRedisplay = true;
  132.             }
  133.          }
  134.       }
  135.  
  136.       ((Component)this).repaint();
  137.    }
  138.  
  139.    public float evaluateFormula(Node var1) {
  140.       float var2 = 0.0F;
  141.       if (var1 == null) {
  142.          return var2;
  143.       } else {
  144.          switch (var1.type) {
  145.             case 0:
  146.                var2 = this.evaluateFormula(var1.left);
  147.                switch (var1.op) {
  148.                   case '*':
  149.                      var2 *= this.evaluateFormula(var1.right);
  150.                      return var2;
  151.                   case '+':
  152.                      var2 += this.evaluateFormula(var1.right);
  153.                      return var2;
  154.                   case ',':
  155.                   case '.':
  156.                   default:
  157.                      return var2;
  158.                   case '-':
  159.                      var2 -= this.evaluateFormula(var1.right);
  160.                      return var2;
  161.                   case '/':
  162.                      var2 /= this.evaluateFormula(var1.right);
  163.                      return var2;
  164.                }
  165.             case 1:
  166.                return var1.value;
  167.             case 2:
  168.                if (var1 != null && this.cells[var1.row][var1.column] != null) {
  169.                   return this.cells[var1.row][var1.column].value;
  170.                }
  171.          }
  172.  
  173.          return var2;
  174.       }
  175.    }
  176.  
  177.    public synchronized void paint(Graphics var1) {
  178.       char[] var6 = new char[1];
  179.       Dimension var7 = ((Component)this).size();
  180.       var1.setFont(this.titleFont);
  181.       int var2 = var1.getFontMetrics().stringWidth(this.title);
  182.       var1.drawString(this.title == null ? "Spreadsheet" : this.title, (var7.width - var2) / 2, 12);
  183.       var1.setColor(this.inputColor);
  184.       var1.fillRect(0, this.cellHeight, var7.width, this.cellHeight);
  185.       var1.setFont(this.titleFont);
  186.  
  187.       for(int var8 = 0; var8 < this.rows + 1; ++var8) {
  188.          int var5 = (var8 + 2) * this.cellHeight;
  189.          var1.setColor(((Component)this).getBackground());
  190.          var1.draw3DRect(0, var5, var7.width, 2, true);
  191.          if (var8 < this.rows) {
  192.             var1.setColor(Color.red);
  193.             var1.drawString("" + (var8 + 1), 2, var5 + 12);
  194.          }
  195.       }
  196.  
  197.       var1.setColor(Color.red);
  198.       int var12 = (this.rows + 3) * this.cellHeight + this.cellHeight / 2;
  199.  
  200.       for(int var9 = 0; var9 < this.columns; ++var9) {
  201.          int var4 = var9 * this.cellWidth;
  202.          var1.setColor(((Component)this).getBackground());
  203.          var1.draw3DRect(var4 + this.rowLabelWidth, 2 * this.cellHeight, 1, var7.height, true);
  204.          if (var9 < this.columns) {
  205.             var1.setColor(Color.red);
  206.             var6[0] = (char)(65 + var9);
  207.             var1.drawString(new String(var6), var4 + this.rowLabelWidth + this.cellWidth / 2, var12);
  208.          }
  209.       }
  210.  
  211.       for(int var10 = 0; var10 < this.rows; ++var10) {
  212.          for(int var3 = 0; var3 < this.columns; ++var3) {
  213.             int var11 = var3 * this.cellWidth + 2 + this.rowLabelWidth;
  214.             var12 = (var10 + 1) * this.cellHeight + 2 + this.titleHeight;
  215.             if (this.cells[var10][var3] != null) {
  216.                this.cells[var10][var3].paint(var1, var11, var12);
  217.             }
  218.          }
  219.       }
  220.  
  221.       var1.setColor(((Component)this).getBackground());
  222.       var1.draw3DRect(0, this.titleHeight, var7.width, var7.height - this.titleHeight, false);
  223.       this.inputArea.paint(var1, 1, this.titleHeight + 1);
  224.    }
  225.  
  226.    public boolean mouseDown(Event var1, int var2, int var3) {
  227.       if (var3 < this.titleHeight + this.cellHeight) {
  228.          this.selectedRow = -1;
  229.          if (var3 <= this.titleHeight && this.current != null) {
  230.             this.current.deselect();
  231.             this.current = null;
  232.          }
  233.  
  234.          return true;
  235.       } else if (var2 < this.rowLabelWidth) {
  236.          this.selectedRow = -1;
  237.          if (this.current != null) {
  238.             this.current.deselect();
  239.             this.current = null;
  240.          }
  241.  
  242.          return true;
  243.       } else {
  244.          this.selectedRow = (var3 - this.cellHeight - this.titleHeight) / this.cellHeight;
  245.          this.selectedColumn = (var2 - this.rowLabelWidth) / this.cellWidth;
  246.          if (this.selectedRow <= this.rows && this.selectedColumn < this.columns) {
  247.             if (this.selectedRow >= this.rows) {
  248.                this.selectedRow = -1;
  249.                if (this.current != null) {
  250.                   this.current.deselect();
  251.                   this.current = null;
  252.                }
  253.  
  254.                return true;
  255.             }
  256.  
  257.             Cell var4 = this.cells[this.selectedRow][this.selectedColumn];
  258.             this.inputArea.setText(new String(var4.getPrintString()));
  259.             if (this.current != null) {
  260.                this.current.deselect();
  261.             }
  262.  
  263.             this.current = var4;
  264.             this.current.select();
  265.             ((Component)this).requestFocus();
  266.             this.fullUpdate = true;
  267.             ((Component)this).repaint();
  268.          } else {
  269.             this.selectedRow = -1;
  270.             if (this.current != null) {
  271.                this.current.deselect();
  272.                this.current = null;
  273.             }
  274.          }
  275.  
  276.          return true;
  277.       }
  278.    }
  279.  
  280.    public boolean keyDown(Event var1, int var2) {
  281.       this.fullUpdate = true;
  282.       this.inputArea.keyDown(var2);
  283.       return true;
  284.    }
  285. }
  286.