home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / ButtonCell.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  8.1 KB  |  300 lines

  1. /*
  2.  * ButtonCell.java   1.0   12 Jan 1997
  3.  *
  4.  * Copyright (c) 1996 Krumel & Associates, Inc.  All Rights Reserved.
  5.  *
  6.  * This software is provided as is.  Krumel & Associates shall not be liable
  7.  * for any damages suffered by licensee as a result of using, modifying or
  8.  * distributing this software or its derivatives.
  9.  */
  10.  
  11. package symantec.itools.db.awt;
  12.  
  13. import java.awt.*;
  14. import java.awt.image.ImageObserver;
  15.  
  16. public class ButtonCell implements TableCell, ImageObserver {
  17.     Grid                view;
  18.     DataSource          dataSource;
  19.     Coordinate          coords;
  20.     boolean             pressed;
  21.     boolean             drawUp = true;
  22.     boolean             defaultFlag;
  23.     int                 type;
  24.  
  25.     static final int   PADSIDES = 5;
  26.  
  27.     //this one is friendly
  28.     ButtonCell() {}
  29.  
  30.     public ButtonCell(Grid v, DataSource ds) {
  31.         view = v;
  32.         dataSource = ds;
  33.     }
  34.  
  35.     //one relative
  36.     public ButtonCell(int r, int c) {
  37.         coords = new Coordinate(r-1, c-1);
  38.     }
  39.  
  40.     public int type() {
  41.         return type;
  42.     }
  43.  
  44.     public int type(int t) {
  45.         if (t > CORNER_CELL || t < 0) {
  46.             throw new IllegalArgumentException("Invalid cell type");
  47.         }
  48.  
  49.         return type = t;
  50.     }
  51.  
  52.     public TableCell cloneCell() {
  53.         ButtonCell bc = new ButtonCell(view, dataSource);
  54.  
  55.         if (coords != null) {
  56.             bc.coords = new Coordinate(coords);
  57.         }
  58.         bc.type = type;
  59.         bc.defaultFlag = defaultFlag;
  60.  
  61.         return bc;
  62.     }
  63.  
  64.     public void setGrid(Grid v, DataSource ds) {
  65.         view = v;
  66.         dataSource = ds;
  67.     }
  68.  
  69.     public void setDefaultFlag() { defaultFlag = true; }
  70.  
  71.     public void reset() {
  72.         pressed = false;
  73.         drawUp = true;
  74.     }
  75.  
  76.     public void setCoordinates(Coordinate p) {
  77.         coords = p;
  78.     }
  79.  
  80.     public Coordinate getCoordinates() {
  81.         return coords;
  82.     }
  83.  
  84.     public int row() { return coords.row; }
  85.  
  86.     public void setRow(int r) { coords.row = r; }
  87.  
  88.     public int col() { return coords.col; }
  89.  
  90.     public void setCol(int c) { coords.col = c; }
  91.  
  92.     boolean inside(int x, int y) {
  93.         Rectangle r = view.getCellBounds(this);
  94.  
  95.         if (x > 0 && y > 0 && x < r.width && y < r.height) {
  96.             return true;
  97.         }
  98.  
  99.         return false;
  100.     }
  101.  
  102.     public boolean mouseEvent(Event e) {
  103.         if (e.id == Event.MOUSE_DOWN) {
  104.             view.setCapture(this);
  105.             pressed = true;
  106.             drawUp = false;
  107.             view.generateEvent(e, view.BUTTON_DOWN_EVENT, this);
  108.             view.redrawCell(this);
  109.         } else if (e.id == Event.MOUSE_DRAG) {
  110.             if (inside(e.x, e.y) && drawUp) {
  111.                 drawUp = false;
  112.                 view.generateEvent(e, view.BUTTON_FLICKER_DOWN_EVENT, this);
  113.                 view.redrawCell(this);
  114.             } else if (!inside(e.x, e.y) && !drawUp) {
  115.                 drawUp = true;
  116.                 view.generateEvent(e, view.BUTTON_FLICKER_UP_EVENT, this);
  117.                 view.redrawCell(this);
  118.             }
  119.         } else if (e.id == Event.MOUSE_UP) {
  120.             if (pressed && !drawUp) {
  121.                 view.generateEvent(e, view.BUTTON_UP_EVENT, this);
  122.             }
  123.  
  124.             pressed = false;
  125.             drawUp = true;
  126.             view.redrawCell(this);
  127.             view.lostCapture();
  128.         }
  129.  
  130.  
  131.         return true;
  132.     }
  133.  
  134.     public Data getData() throws DataNotAvailable {
  135.         return dataSource.getData(coords);
  136.     }
  137.  
  138.     String chopString(String text, FontMetrics fm, Rectangle r) {
  139.         int w = r.width;
  140.         int i = 1;
  141.         String t;
  142.  
  143.         if (text.length() == 0) { return text; }
  144.  
  145.  
  146.         do {
  147.             t = text.substring(0, i);
  148.         } while(fm.stringWidth(t) < w - PADSIDES*2 && i++ < text.length());
  149.  
  150.         return text.substring(0, i-1);
  151.     }
  152.  
  153.     public void drawCell(Graphics g, CellHints hints) {
  154.         Data        data;
  155.  
  156.         try {
  157.             data = getData();
  158.         } catch (DataNotAvailable ex) {
  159.             data = new ImageStringData(dataSource, "");
  160.         }
  161.  
  162.         Rectangle   r = hints.bounds();
  163.         FontMetrics fm = g.getFontMetrics();
  164.         int         asc = fm.getAscent() + 1;
  165.         String      text = chopString(data.toString(), fm, r);
  166.         int         sw = fm.stringWidth(text);
  167.         int         imageOffset = 0;
  168.         Color       fg = g.getColor();
  169.         int         origX = r.x;
  170.  
  171.         drawButton(g, r);
  172.         Image im = data.toImage();
  173.         switch(hints.alignment()) {
  174.             case Grid.LEFT:
  175.                 if (im != null) {
  176.                     imageOffset = im.getWidth(this) + PADSIDES;
  177.                 }
  178.  
  179.                 //check to see if it will all fit within cell
  180.                 if (imageOffset+sw+2+PADSIDES <= r.width && im != null) {
  181.                     r.x += PADSIDES;
  182.                     r.y += 3;
  183.                     g.drawImage(im, r.x, r.y, this);
  184.                     r.y -= 3;
  185.                 } else {
  186.                     imageOffset = PADSIDES;
  187.                 }
  188.  
  189.                 r.x = origX + imageOffset + 2;
  190.                 break;
  191.             case Grid.CENTER:
  192.                 r.x = origX + (r.width-sw)/2;
  193.                 break;
  194.             case Grid.RIGHT:
  195.                 if (im != null) {
  196.                     imageOffset = im.getWidth(this) + PADSIDES;
  197.                 }
  198.                 //check to see if it will all fit within cell
  199.                 if (sw+imageOffset+2+PADSIDES <= r.width && im != null) {
  200.                     r.x = r.x + r.width - sw - PADSIDES - imageOffset - 2;
  201.                     r.y += 3;
  202.                     g.drawImage(im, r.x, r.y, this);
  203.                     r.y -= 3;
  204.                 }
  205.  
  206.                   r.x = origX+r.width-sw-PADSIDES;
  207.                 break;
  208.         }
  209.  
  210.         g.setColor(hints.foreground());
  211.         g.drawString(text, r.x, r.y+fm.getAscent()+2);
  212.     }
  213.  
  214.     void drawButton(Graphics g, Rectangle r) {
  215.         //all highlighted and ready for delete
  216.         Color bg = Color.lightGray;
  217.  
  218.         g.setColor(bg);
  219.         g.fillRect(r.x,
  220.                    r.y,
  221.                    r.width-1,
  222.                    r.height);
  223.  
  224.         //draw shade for button
  225.         g.setColor(Color.gray);
  226.         g.drawRect(r.x+1, r.y+1, r.width-3, r.height-3);
  227.         g.setColor(Color.black);
  228.         g.drawRect(r.x, r.y, r.width-1, r.height-1);
  229.  
  230.         //draw the hightlight
  231.         if (drawUp) {
  232.             //draw button raised
  233.             g.setColor(Color.white);
  234.             g.drawLine(r.x+1, r.y+1, r.x + r.width-2, r.y+1);
  235.             g.drawLine(r.x+1, r.y+1, r.x+1, r.y + r.height - 2);
  236.         } else {
  237.             //draw button recessed
  238.             g.setColor(Color.white);
  239.             g.drawLine(r.x+1, r.y + r.height - 2, r.x + r.width-2, r.y + r.height-2);
  240.             g.drawLine(r.x + r.width - 2, r.y+1, r.x  + r.width - 2, r.y + r.height - 3);
  241.         }
  242.     }
  243.  
  244.     public boolean isCellTypeEditable() { return false; }
  245.  
  246.     public boolean actionEvent(Event e) {
  247.         return true;
  248.     }
  249.  
  250.     public boolean keyEvent(Event e) {
  251.         return true;
  252.     }
  253.  
  254.     public boolean canLoseFocus() { return true; }
  255.  
  256.     public boolean focusEvent(Event e) {
  257.         if (e.id == Event.GOT_FOCUS) {
  258.             view.setCapture(this);
  259.             view.generateEvent(e, view.GOT_CELL_FOCUS, this);
  260.         } else {
  261.             view.lostCapture();
  262.             view.generateEvent(e, view.LOST_CELL_FOCUS, this);
  263.         }
  264.  
  265.         return true;
  266.    }
  267.  
  268.     public boolean loseFocusOnArrow() {
  269.         return false;
  270.     }
  271.  
  272.     public void activateCursor() {
  273.     }
  274.  
  275.     public void deactivateCursor() {
  276.     }
  277.  
  278.     public String stats() {
  279.         return "";
  280.     }
  281.  
  282.     /**
  283.      * Repaints the list when the cell's image has changed.
  284.      * @return true if image has changed; false otherwise.
  285.      */
  286.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
  287.         if ((flags & (ABORT|ERROR)) != 0) {
  288.             return false;
  289.         }
  290.  
  291.         if ((flags & ALLBITS) != 0) {
  292.             view.redrawAsync();
  293.             return false;
  294.         } else {
  295.             return true;
  296.         }
  297.     }
  298. }
  299.  
  300.