home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 25 / IOPROG_25.ISO / SOFT / JavaS / javastar-eval.exe / data1.cab / Program_Files / examples / namedb / OpenlookButton.java < prev    next >
Encoding:
Java Source  |  1999-02-11  |  6.9 KB  |  247 lines

  1. /*
  2.  * @(#)OpenlookButton.java    1.2 0
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. import java.applet.*;
  9. import java.lang.*;
  10. import java.util.*;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13.  
  14. /**
  15.  * OpenlookButton - a class that produces a lightweight button.
  16.  *
  17.  * Lightweight components can have "transparent" areas, meaning that
  18.  * you can see the background of the container behind them.
  19.  *
  20.  */
  21. public class OpenlookButton extends Component {
  22.  
  23.   static int capWidth = 10;          // The width of the Button's endcap
  24.   String label;                      // The Button's text
  25.   protected boolean pressed = false; // true if the button is detented.
  26.   ActionListener actionListener;     // Post action events to listeners
  27.   
  28.   
  29.   /**
  30.    * Constructs an OpenlookButton with no label.
  31.    */
  32.   public OpenlookButton() {
  33.       this("");
  34.   }
  35.  
  36.   /**
  37.    * Constructs an OpenlookButton with the specified label.
  38.    * @param label the label of the button
  39.    */
  40.   public OpenlookButton(String label) {
  41.       this.label = label;
  42.       enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  43.   }
  44.  
  45.   /**
  46.    * gets the label
  47.    * @see setLabel
  48.    */
  49.   public String getLabel() {
  50.       return label;
  51.   }
  52.   
  53.   /**
  54.    * sets the label
  55.    * @see getLabel
  56.    */
  57.   public void setLabel(String label) {
  58.       this.label = label;
  59.       invalidate();
  60.       repaint();
  61.   }
  62.   
  63.   /**
  64.    * paints the button
  65.    */
  66.   public void paint(Graphics g) {
  67.       int width = getSize().width - 1;
  68.       int height = getSize().height - 1;
  69.  
  70.       Color interior;
  71.       Color highlight1;
  72.       Color highlight2;
  73.       
  74.       interior = getBackground();
  75.  
  76.       // ***** determine what colors to use
  77.       if(pressed) {
  78.       highlight1 = interior.darker();
  79.       highlight2 = interior.brighter();
  80.       } else {
  81.       highlight1 = interior.brighter();
  82.       highlight2 = interior.darker();
  83.       }
  84.  
  85.       // ***** paint the interior of the button
  86.       g.setColor(interior);
  87.       // left cap
  88.       g.fillArc(0, 0,                 // start
  89.         capWidth, height,            // size
  90.         90, 180);            // angle
  91.  
  92.       // right cap
  93.       g.fillArc(width - capWidth, 0,        // start
  94.         capWidth, height,           // size
  95.         270, 180);            // angle
  96.  
  97.       // inner rectangle
  98.       g.fillRect(capWidth/2, 0, width - capWidth, height);
  99.       
  100.  
  101.       // ***** highlight the perimeter of the button
  102.       // draw upper and lower highlight lines
  103.       g.setColor(highlight1);
  104.       g.drawLine(capWidth/2, 0, width - capWidth/2, 0);
  105.       g.setColor(highlight2);
  106.       g.drawLine(capWidth/2, height, width - capWidth/2, height);
  107.  
  108.       // upper arc left cap
  109.       g.setColor(highlight1);
  110.       g.drawArc(0, 0,                       // start
  111.                 capWidth, height,           // size
  112.                 90, 180-40                  // angle
  113.       );
  114.  
  115.       // lower arc left cap
  116.       g.setColor(highlight2);
  117.       g.drawArc(0, 0,                       // start
  118.                 capWidth, height,           // size
  119.                 270-40, 40                  // angle
  120.       );
  121.  
  122.       // upper arc right cap
  123.       g.setColor(highlight1);
  124.       g.drawArc(width - capWidth, 0,        // start
  125.                 capWidth, height,           // size
  126.                 90-40, 40                   // angle
  127.       );
  128.  
  129.       // lower arc right cap
  130.       g.setColor(highlight2);
  131.       g.drawArc(width - capWidth, 0,        // start
  132.                 capWidth, height,           // size
  133.                 270, 180-40                 // angle
  134.       );
  135.  
  136.       // ***** draw the label centered in the button
  137.       Font f = getFont();
  138.       g.setFont(f);
  139.       if(f != null) {
  140.       FontMetrics fm = getFontMetrics(getFont());
  141.       g.setColor(getForeground());
  142.       g.drawString(label,
  143.                width/2 - fm.stringWidth(label)/2,
  144.                height/2 + fm.getHeight()/2 - fm.getMaxDescent()
  145.       );
  146.       }
  147.   }
  148.   
  149.   /**
  150.    * The preferred size of the button. 
  151.    */
  152.   public Dimension getPreferredSize() {
  153.       Font f = getFont();
  154.       if(f != null) {
  155.       FontMetrics fm = getFontMetrics(getFont());
  156.       return new Dimension(fm.stringWidth(label) + capWidth*2,
  157.                    fm.getHeight() + 10);
  158.       } else {
  159.       return new Dimension(100, 50);
  160.       }
  161.   }
  162.   
  163.   /**
  164.    * The minimum size of the button. 
  165.    */
  166.   public Dimension getMinimumSize() {
  167.     //      return new Dimension(100, 50);
  168.     return getPreferredSize();
  169.   }
  170.  
  171.   /**
  172.    * Adds the specified action listener to receive action events
  173.    * from this button.
  174.    * @param listener the action listener
  175.    */
  176.    public void addActionListener(ActionListener listener) {
  177.        actionListener = AWTEventMulticaster.add(actionListener, listener);
  178.        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  179.    }
  180.  
  181.    /**
  182.     * Removes the specified action listener so it no longer receives
  183.     * action events from this button.
  184.     * @param listener the action listener
  185.     */
  186.    public void removeActionListener(ActionListener listener) {
  187.        actionListener = AWTEventMulticaster.remove(actionListener, listener);
  188.    }
  189.  
  190.  
  191.    /**
  192.     * Paints the button and sends an action event to all listeners.
  193.     */
  194.    public void processMouseEvent(MouseEvent e) {
  195.        Graphics g;
  196.        switch(e.getID()) {
  197.           case MouseEvent.MOUSE_PRESSED:
  198.             // render myself inverted....
  199.             pressed = true;
  200.     
  201.         // Repaint might flicker a bit. To avoid this, you can use
  202.         // double buffering (see the Gauge example).
  203.         repaint();
  204.             break;
  205.           case MouseEvent.MOUSE_RELEASED:
  206.             if(actionListener != null) {
  207.                actionListener.actionPerformed(new ActionEvent(
  208.                    this, ActionEvent.ACTION_PERFORMED, label));
  209.             }
  210.             // render myself normal again
  211.             if(pressed == true) {
  212.                 pressed = false;
  213.  
  214.             // Repaint might flicker a bit. To avoid this, you can use
  215.             // double buffering (see the Gauge example).
  216.         repaint();
  217.             }
  218.             break;
  219.           case MouseEvent.MOUSE_ENTERED:
  220.  
  221.             break;
  222.           case MouseEvent.MOUSE_EXITED:
  223.             if(pressed == true) {
  224.                 // Cancel! Don't send action event.
  225.                 pressed = false;
  226.  
  227.             // Repaint might flicker a bit. To avoid this, you can use
  228.             // double buffering (see the Gauge example).
  229.         repaint();
  230.  
  231.                 // Note: for a more complete button implementation,
  232.                 // you wouldn't want to cancel at this point, but
  233.                 // rather detect when the mouse re-entered, and
  234.                 // re-highlight the button. There are a few state
  235.                 // issues that that you need to handle, which we leave
  236.                 // this an an excercise for the reader (I always
  237.                 // wanted to say that!)
  238.             }
  239.             break;
  240.        }
  241.        super.processMouseEvent(e);
  242.    }
  243.  
  244.  
  245. }
  246.  
  247.