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 / ToolBarSpacer.java < prev    next >
Encoding:
Java Source  |  1998-09-14  |  10.2 KB  |  345 lines

  1. package symantec.itools.awt.util;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.beans.PropertyVetoException;
  8. import java.beans.PropertyChangeEvent;
  9. import java.beans.VetoableChangeListener;
  10. import java.beans.PropertyChangeListener;
  11. import java.util.ResourceBundle;
  12.  
  13. //     01/29/97    TWB    Integrated changes from Macintosh
  14. //     07/24/97    LAB    Updated version to 1.1.  Added Space prperty.  Added support
  15. //                    for ToolBarPanel's Orientation property.  Removed unneeded
  16. //                    paint override.
  17. //     08/25/97    LAB    Now we get the parent of the parent in getPreferredSize which allows us to
  18. //                    properly get the ToolBarPanel that contains us (Addresses Mac Bug #7543).
  19.  
  20. /**
  21.  * ToolBarPanelSpacer component.
  22.  * This component is used to space items in a ToolBarPanel.
  23.  * @see symantec.itools.awt.util.ToolBarPanel
  24.  * @version 1.1, July 24, 1997
  25.  * @author Symantec
  26.  */
  27. public class ToolBarSpacer extends Canvas
  28. {
  29.     /**
  30.      * Create a ToolBarSpacer.  It's default space value is 10 pixels.
  31.      */
  32.     public ToolBarSpacer()
  33.     {
  34.         space = 10;
  35.     }
  36.  
  37.     /**
  38.      * Sets the size of the space the spacer will occupy in pixels.
  39.      * @param value the size of the space in pixels
  40.      * @exception PropertyVetoException
  41.      * if the specified property value is unacceptable
  42.      * @see #getSpace
  43.      */
  44.     public void setSpace(int value) throws PropertyVetoException
  45.     {
  46.         if (space != value)
  47.         {
  48.             Integer oldValue = new Integer(space);
  49.             Integer newValue = new Integer(value);
  50.  
  51.             vetos.fireVetoableChange("Space", oldValue, newValue);
  52.  
  53.             space = value;
  54.             changes.firePropertyChange("Space", oldValue, newValue);
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * Gets the size of the space the spacer will occupy in pixels.
  60.      * @return the size of the space in pixels
  61.      * @see #setSpace
  62.      */
  63.     public int getSpace()
  64.     {
  65.         return space;
  66.     }
  67.  
  68.     /**
  69.      * Returns the recommended dimensions to properly display this component.
  70.      * This is a standard Java AWT method which gets called to determine
  71.      * the recommended size of this component.
  72.      *
  73.      * @return The width is the Space value and the height is the tallest component
  74.      * the ToolBarPanel, if the parent ToolBarPanel's Orientation is HORIZONTAL.
  75.      * The height is the Space value and the width is the widest component
  76.      * the ToolBarPanel, if the parent ToolBarPanel's Orientation is VERTICAL.
  77.      *
  78.      * NOTE: If the parent of this class is not an instance of ToolBarPanel, this will
  79.      * return 10,10.
  80.      *
  81.      * @see #setSpace
  82.      * @see #getSpace
  83.      * @see #getMinimumSize
  84.      */
  85.     public synchronized Dimension getPreferredSize()
  86.     {
  87.         //We assume that we will be in a ToolBarPanel and since it has a java.awt.Panel
  88.         //in it, we have to get the parent of the parent to actually get the ToolBarPanel.
  89.         Component parent = getParent();
  90.         if(parent != null)
  91.             parent = parent.getParent();
  92.  
  93.         if (parent != null && parent instanceof ToolBarPanel)
  94.         {
  95.                     Dimension s = new Dimension(0, 0);
  96.  
  97.             Component[] list = ((ToolBarPanel)parent).getComponents();
  98.  
  99.             switch(((ToolBarPanel)parent).getOrientation())
  100.             {
  101.                 case ToolBarPanel.HORIZONTAL:
  102.                     s.width = space;
  103.                     for (int i = 0; i < list.length; ++i)
  104.                     {
  105.                         Component c = list[i];
  106.                         if (!(c instanceof ToolBarSpacer))
  107.                             s.height = Math.max(s.height, c.size().height);
  108.                     }
  109.                     break;
  110.                 case ToolBarPanel.VERTICAL:
  111.                     s.height = space;
  112.                     for (int i = 0; i < list.length; ++i)
  113.                     {
  114.                         Component c = list[i];
  115.                         if (!(c instanceof ToolBarSpacer))
  116.                             s.width = Math.max(s.width, c.size().width);
  117.                     }
  118.                     break;
  119.             }
  120.  
  121.             return s;
  122.         }
  123.         else
  124.             return new Dimension(10, 10);
  125.     }
  126.  
  127.     /**
  128.      * @deprecated
  129.      * @see #getPreferredSize
  130.      */
  131.     public synchronized Dimension preferredSize()
  132.     {
  133.         return getPreferredSize();
  134.     }
  135.  
  136.     /**
  137.      * Returns the minimum dimensions to properly display this component.
  138.      * This is a standard Java AWT method which gets called to determine
  139.      * the minimum size of this component.
  140.      *
  141.      * In this case the minimum size is the same as the preferred size.
  142.      *
  143.      * @see #getPreferredSize
  144.      */
  145.     public synchronized Dimension getMinimumSize()
  146.     {
  147.         return preferredSize();
  148.     }
  149.  
  150.     /**
  151.      * @deprecated
  152.      * @see #getMinimumSize
  153.      */
  154.     public synchronized Dimension minimumSize()
  155.     {
  156.         return getMinimumSize();
  157.     }
  158.  
  159.     /**
  160.      * Is the specified space size valid?
  161.      * @param spaceSize the value to test
  162.      * @return true if the parameter is greater or equal to zero.
  163.      * @see #setSpace
  164.      * @see #getSpace
  165.      */
  166.     public boolean isValidSpace(int spaceSize)
  167.     {
  168.         return (spaceSize >= 0);
  169.     }
  170.  
  171.     /**
  172.      * Tells this component that it has been added to a container.
  173.      * This is a standard Java AWT method which gets called by the AWT when
  174.      * this component is added to a container. Typically, it is used to
  175.      * create this component's peer.
  176.      *
  177.      * It has been overridden here to hook-up event listeners.
  178.      *
  179.      * @see #removeNotify
  180.      */
  181.     public synchronized void addNotify()
  182.     {
  183.         super.addNotify();
  184.         
  185.         try
  186.         {
  187.             errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  188.         }
  189.         catch(Throwable ex)
  190.         {
  191.             errors = new symantec.itools.resources.ErrorsBundle();
  192.         }
  193.  
  194.         //Hook up listeners
  195.         if (veto == null)
  196.         {
  197.             veto = new Veto();
  198.             addSpaceListener(veto);
  199.         }
  200.     }
  201.  
  202.     /**
  203.      * Tells this component that it is being removed from a container.
  204.      * This is a standard Java AWT method which gets called by the AWT when
  205.      * this component is removed from a container. Typically, it is used to
  206.      * destroy the peers of this component and all its subcomponents.
  207.      *
  208.      * It has been overridden here to unhook event listeners.
  209.      *
  210.      * @see #addNotify
  211.      */
  212.     public synchronized void removeNotify()
  213.     {
  214.         //Unhook listeners
  215.         if (veto != null)
  216.         {
  217.             removeSpaceListener(veto);
  218.             veto = null;
  219.         }
  220.  
  221.         super.removeNotify();
  222.     }
  223.  
  224.     /**
  225.      * Adds a listener for all event changes.
  226.      * @param listener the listener to add.
  227.      * @see #removePropertyChangeListener
  228.      */
  229.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  230.     {
  231.         //super.addPropertyChangeListener(listener);
  232.         changes.addPropertyChangeListener(listener);
  233.     }
  234.  
  235.     /**
  236.      * Removes a listener for all event changes.
  237.      * @param listener the listener to remove.
  238.      * @see #addPropertyChangeListener
  239.      */
  240.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  241.     {
  242.         //super.removePropertyChangeListener(listener);
  243.         changes.removePropertyChangeListener(listener);
  244.     }
  245.  
  246.     /**
  247.      * Adds a vetoable listener for all event changes.
  248.      * @param listener the listener to add.
  249.      * @see #removeVetoableChangeListener
  250.      */
  251.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  252.     {
  253.         //super.addVetoableChangeListener(listener);
  254.         vetos.addVetoableChangeListener(listener);
  255.     }
  256.  
  257.     /**
  258.      * Removes a vetoable listener for all event changes.
  259.      * @param listener the listener to remove.
  260.      * @see #addVetoableChangeListener
  261.      */
  262.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  263.     {
  264.         //super.removeVetoableChangeListener(listener);
  265.         vetos.removeVetoableChangeListener(listener);
  266.     }
  267.  
  268.     /**
  269.      * Adds a listener for Space changes.
  270.      * @param listener the listener to add.
  271.      * @see #removeSpaceListener
  272.      */
  273.     public synchronized void addSpaceListener(PropertyChangeListener listener)
  274.     {
  275.         changes.addPropertyChangeListener("Space", listener);
  276.     }
  277.  
  278.     /**
  279.      * Removes a listener for Space changes.
  280.      * @param listener the listener to remove.
  281.      * @see #addSpaceListener
  282.      */
  283.     public synchronized void removeSpaceListener(PropertyChangeListener listener)
  284.     {
  285.         changes.removePropertyChangeListener("Space", listener);
  286.     }
  287.  
  288.     /**
  289.      * Adds a vetoable listener for Space changes.
  290.      * @param listener the listener to add.
  291.      * @see #removeSpaceListener
  292.      */
  293.     public synchronized void addSpaceListener(VetoableChangeListener listener)
  294.     {
  295.         vetos.addVetoableChangeListener("Space", listener);
  296.     }
  297.  
  298.     /**
  299.      * Removes a vetoable listener for Space changes.
  300.      * @param listener the listener to remove.
  301.      * @see #addSpaceListener
  302.      */
  303.     public synchronized void removeSpaceListener(VetoableChangeListener listener)
  304.     {
  305.         vetos.removeVetoableChangeListener("Space", listener);
  306.     }
  307.  
  308.     /**
  309.      * This is the PropertyChangeEvent handling inner class for the constrained Space property.
  310.      * Handles vetoing Spaces that are not valid.
  311.      */
  312.     class Veto implements VetoableChangeListener
  313.     {
  314.         /**
  315.          * This method gets called when an attempt to change the constrained Space property is made.
  316.          * Ensures the given Space is valid.
  317.          *
  318.          * @param     e a <code>PropertyChangeEvent</code> object describing the
  319.          *             event source and the property that has changed.
  320.          * @exception PropertyVetoException if the recipient wishes the property
  321.          *              change to be rolled back.
  322.          */
  323.         public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException
  324.         {
  325.             int i = ((Integer)e.getNewValue()).intValue();
  326.             if (!isValidSpace(i))
  327.             {
  328.                 throw new PropertyVetoException(errors.getString("InvalidSpace") + i, e);
  329.             }
  330.         }
  331.     }
  332.  
  333.     /**
  334.      * The size of the space the spacer will occupy in pixels..
  335.      */
  336.     protected int space;
  337.  
  338.     transient protected ResourceBundle errors;
  339.  
  340.     private Veto veto = null;
  341.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  342.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  343. }
  344.  
  345.