home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / Voter.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  3.3 KB  |  133 lines

  1. package sunw.demo.misc;
  2.  
  3.  
  4. import java.awt.*;
  5. import java.beans.*;
  6.  
  7.  
  8. /** 
  9.  * A simple Java Bean that handles constrained property PropertyChange 
  10.  * events by unconditionally vetoing or accepting all proposed changes.  
  11.  * It can be  used with the JellyBean, which fires vetoable PropertyChange 
  12.  * events,  to demonstrate  constratined properties.  It's easy to 
  13.  * experiment with connecting constratined properties to Voter objects 
  14.  * with the BeanBox.  To do so programatically one would write:
  15.  * 
  16.  * <pre>
  17.  * import sunw.demo.misc.JellyBean;
  18.  * import sunw.demo.misc.Voter;
  19.  * import java.beans.*;
  20.  *   
  21.  * public class DemoVoter
  22.  * {
  23.  *   JellyBean bean = new JellyBean(); 
  24.  *   Voter voter = new Voter();
  25.  *   VetoableChangeAdapter adapter = new VetoableChangeAdapter();
  26.  *   
  27.  *   DemoVoter()
  28.  *   {
  29.  *     bean.addVetoableChangeListener(adapter);
  30.  *     
  31.  *     try {
  32.  *       bean.setPriceInCents(123); 
  33.  *     }
  34.  *     catch (PropertyVetoException e) {
  35.  *       System.err.println("Vetoed: " + e);
  36.  *     }
  37.  *   }
  38.  *   
  39.  *   class VetoableChangeAdapter implements VetoableChangeListener 
  40.  *   {
  41.  *     public void vetoableChange(PropertyChangeEvent e)
  42.  *       throws PropertyVetoException
  43.  *     {
  44.  *       voter.vetoableChange(e);
  45.  *     }
  46.  *   }
  47.  *   
  48.  *   public static void main(String[] argv)
  49.  *   {
  50.  *     new DemoVoter();
  51.  *   }
  52.  * }
  53.  * </pre>
  54.  * In the example above the nested adapter calls Voter.vetoable change
  55.  * each time a constrained bean property is set.  In this case the 
  56.  * constrained property is "priceInCents".
  57.  * 
  58.  * @see sunw.demo.misc.JellyBean
  59.  */
  60.  
  61. public class Voter extends Component {
  62.     private boolean vetoAll = true;    
  63.     private String text = "No";
  64.     private transient int baseline; 
  65.  
  66.     /** 
  67.      * Construct a Voter that, by default, vetos all proposed PropertyChange events.
  68.      */
  69.     public Voter() {
  70.         setFont(new Font("Helvetica", Font.BOLD, 36));
  71.         setBackground(Color.black);
  72.         setForeground(Color.red);
  73.     }
  74.  
  75.     /**
  76.      * If true, veto all proposed changes, otherwise accept them.
  77.      * @see #getVetoAll
  78.      * @see #vetoableChange
  79.      */
  80.     public void setVetoAll(boolean x) {
  81.         vetoAll = x;
  82.     if (vetoAll) {
  83.         text = "No";
  84.     } else {
  85.         text = "Yes";
  86.     }
  87.     repaint();
  88.     }
  89.  
  90.  
  91.     /**
  92.      * If true, veto all proposed changes, otherwise accept them.
  93.      * @see #setVetoAll
  94.      */
  95.     public boolean getVetoAll() {
  96.         return vetoAll;
  97.     }
  98.  
  99.     /**
  100.      * The PropertyChangeEvent handler method.    By default this method throws 
  101.      * a PropertyVetoException, which vetos the proposed change defined
  102.      * by the PropertyChangeEvent.
  103.      * 
  104.      * @exception PropertyVetoException if the vetoAll is true
  105.      * @see #setVetoAll
  106.      */
  107.     public void vetoableChange(PropertyChangeEvent x)
  108.                 throws PropertyVetoException {
  109.         if (vetoAll) {
  110.             throw new PropertyVetoException("NO!", x);
  111.         }
  112.     }
  113.  
  114.  
  115.     public Dimension getPreferredSize() {
  116.     FontMetrics fm = getFontMetrics(getFont());
  117.     baseline = fm.getMaxAscent() + 2;
  118.     int height = baseline + fm.getMaxDescent() + 2;
  119.     int width = fm.stringWidth("Yes") + 8;
  120.     return new Dimension(width,height);
  121.     }
  122.  
  123.     public void paint(Graphics g)  {
  124.     g.setColor(getBackground());
  125.     Dimension size = getSize();
  126.     g.fillRect(0, 0, size.width, size.height);
  127.     g.setColor(getForeground());
  128.     g.setFont(getFont());
  129.     g.drawString(text, 4, baseline);
  130.     }
  131.  
  132. }
  133.