home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / Source.bin / NumericSpinner.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  5.9 KB  |  196 lines

  1. package symantec.itools.awt.util.spinner;
  2.  
  3.  
  4. import java.beans.PropertyVetoException;
  5. import java.beans.PropertyChangeListener;
  6. import java.beans.VetoableChangeListener;
  7.  
  8.  
  9. //     02/15/97    RKM    Merged in change to set min and max as in the DESC file
  10. //    06/03/97    MSH    Incorporated Java 1.1 changes
  11. //    06/03/97    LAB Polished update to Java 1.1.
  12. //                    Changed the package to symantec.itools.awt.util.spinner.
  13. //    10/06/97    LAB Updated constructor to set min, max, tempMin, and tempMax directly
  14. //                    rather than call their setters so it would get introspected properly,
  15. //                    since the setters only actually set after addNotify has been called.
  16. //  02/05/98    DS  added changes for Spinner re-write
  17.  
  18. /**
  19.  * Creates a text box, containing a list of numbers, with up and down arrows.
  20.  * Use this component to allow your users to move through a set of fixed
  21.  * values or type a valid value in the box.
  22.  * <p>
  23.  * At run time only the selected value is displayed in the text box.
  24.  * <p>
  25.  * @see symantec.itools.awt.Spinner
  26.  * @version 1.1, June 2, 1997
  27.  * @author Symantec
  28.  */
  29. public class NumericSpinner
  30.     extends    Spinner
  31.     implements java.io.Serializable
  32. {
  33.     /**
  34.      * Constructs an empty NumericSpinner.
  35.      */
  36.     public NumericSpinner()
  37.     {
  38.         try
  39.         {
  40.             setIncrement(1);
  41.         }
  42.         catch( PropertyVetoException e ){}
  43.         min        = 0;
  44.         max        = 10;
  45.     }
  46.  
  47.     /**
  48.      * Sets the minimum value the spinner may have.
  49.      *
  50.      * Overriden here to set the size of the text area.
  51.      *
  52.      * @param i the new minimum value
  53.      * @see Spinner#getMin
  54.      * @see #setMax
  55.      * @exception PropertyVetoException
  56.      * if the specified property value is unacceptable
  57.      */
  58.     public void setMin(int i)  throws PropertyVetoException
  59.     {
  60.         super.setMin(i);
  61.  
  62.         if(added)
  63.         {
  64.             int oldValue = textWidth;
  65.  
  66.             textWidth = Math.max(Integer.toString(min).length(), Integer.toString(max).length());
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * Sets the maximum value the spinner may have.
  72.      *
  73.      * Overridden here to set the size of the text area.
  74.      *
  75.      * @param i the new maximum value
  76.      * @see Spinner#getMax
  77.      * @see #setMin
  78.      * @exception PropertyVetoException
  79.      * if the specified property value is unacceptable
  80.      */
  81.     public void setMax(int i) throws PropertyVetoException
  82.     {
  83.         super.setMax(i);
  84.  
  85.         if(added)
  86.         {
  87.             int oldValue = textWidth;
  88.  
  89.             textWidth = Math.max(Integer.toString(min).length(), Integer.toString(max).length());
  90.         }
  91.     }
  92.  
  93.     /**
  94.      * Sets the value to increment/decrement the Spinner by.
  95.      * @param int i the increment/decrement value
  96.      * @see #getIncrement
  97.      *
  98.      * @exception PropertyVetoException
  99.      * if the specified property value is unacceptable
  100.      */
  101.     public void setIncrement(int i) throws PropertyVetoException
  102.     {
  103.         Integer oldValue = new Integer( increment );
  104.         Integer newValue = new Integer( i );
  105.  
  106.         vetos.fireVetoableChange("increment", oldValue, newValue);
  107.         increment = i;
  108.         changes.firePropertyChange("increment", oldValue, newValue);
  109.     }
  110.  
  111.     /**
  112.      * Gets the increment/decrement value.
  113.      * @return the increment/decrement value
  114.      * @see #setIncrement
  115.      */
  116.     public int getIncrement()
  117.     {
  118.         return increment;
  119.     }
  120.  
  121.     /**
  122.      * Gets the current text from the Spinner.
  123.      * @return the text of the currently selected Spinner value
  124.      */
  125.     public String getCurrentText()
  126.     {
  127.         return Integer.toString(current);
  128.     }
  129.  
  130.     /**
  131.      * Tells this component that it has been added to a container.
  132.      * This is a standard Java AWT method which gets called by the AWT when
  133.      * this component is added to a container. Typically, it is used to
  134.      * create this component's peer.
  135.      * Here it's used to set maximum text width and note the text of the
  136.      * current selection.
  137.      *
  138.      * @see java.awt.Container#removeNotify
  139.      */
  140.     public void addNotify()
  141.     {
  142.         textWidth = Math.max(Integer.toString(min).length(), Integer.toString(max).length());
  143.         text = Integer.toString(current);
  144.         super.addNotify();
  145.         updateText(false);
  146.     }
  147.  
  148.     /**
  149.      * Adds a listener for all property change events.
  150.      * @param listener the listener to add
  151.      * @see #removePropertyChangeListener
  152.      */
  153.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  154.     {
  155.         super.addPropertyChangeListener(listener);
  156.         changes.addPropertyChangeListener(listener);
  157.     }
  158.  
  159.     /**
  160.      * Removes a listener for all property change events.
  161.      * @param listener the listener to remove
  162.      * @see #addPropertyChangeListener
  163.      */
  164.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  165.     {
  166.         super.removePropertyChangeListener(listener);
  167.         changes.removePropertyChangeListener(listener);
  168.     }
  169.  
  170.     /**
  171.      * Adds a listener for all vetoable property change events.
  172.      * @param listener the listener to add
  173.      * @see #removeVetoableChangeListener
  174.      */
  175.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  176.     {
  177.          super.addVetoableChangeListener(listener);
  178.         vetos.addVetoableChangeListener(listener);
  179.     }
  180.  
  181.     /**
  182.      * Removes a listener for all vetoable property change events.
  183.      * @param listener the listener to remove
  184.      * @see #addVetoableChangeListener
  185.      */
  186.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  187.     {
  188.         super.removeVetoableChangeListener(listener);
  189.         vetos.removeVetoableChangeListener(listener);
  190.     }
  191.  
  192.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  193.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  194.  
  195. }
  196.