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 / ToolBarPanel.java < prev    next >
Encoding:
Java Source  |  1998-09-14  |  13.5 KB  |  433 lines

  1. package symantec.itools.awt.util;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Rectangle;
  8. import java.awt.FlowLayout;
  9. import java.awt.LayoutManager;
  10. import symantec.itools.awt.BorderPanel;
  11. import symantec.itools.awt.BevelStyle;
  12. import java.beans.PropertyVetoException;
  13. import java.beans.PropertyChangeEvent;
  14. import java.beans.VetoableChangeListener;
  15. import java.beans.PropertyChangeListener;
  16. import java.util.ResourceBundle;
  17.  
  18. // 01/29/97    TWB    Integrated changes from Windows
  19. // 05/22/97 RKM    Removed call to super to call getComponents in preferredSize
  20. //                Java 1.1 compiler does not like this
  21. // 06/03/97 TGL    Converted to 1.1, added bound/constrained
  22. // 07/23/97 LAB    Updated preferredSize to getPreferredSize and eliminated minimumSize since
  23. //                BorderPanel already overrides it to call getPreferredSize.  Added Orientaion
  24. //                property to allow the toolbar's getPreferredSize to return a Dimenstion that
  25. //                is best suited for a vertical orientation.  Added a new constructor with
  26. //                parameters.  Constrained the Orientation property to known Orientations.
  27. //                Updated version to 1.1.  Added initialization to the default constructor
  28. //                (it was missing initialization before).
  29. // 08/13/97 LAB    Added a check in setOrientation() to see if the panel contains any components,
  30. //                if not then the call does not result in a reshape.  Addresses Mac Bug #7253.
  31. // 10/01/97 LAB    Made getPreferredSize() independent from the base class to make sure the
  32. //                size is always calculated correctly, regardless of changes to the base class.
  33. //                This was affecting the HORIZONTAL orientation (Addresses Mac Bug #6987).
  34. //                Changed names of strings in PropertyChangeEvent handling to follow Bean Spec
  35. //                naming conventions.
  36.  
  37. /**
  38.  * ToolBarPanel component.
  39.  * This component creates a panel to which you can add buttons to create a toolbar
  40.  * in a window. Toolbars commonly contain buttons, but a ToolBarPanel can hold
  41.  * other types of components like static text, check boxes, even images.
  42.  *
  43.  * Tool bar components are separated with a ToolBarSpacer component.
  44.  *
  45.  * @see symantec.itools.awt.util.ToolBarSpacer
  46.  *
  47.  * @version 1.1, July 23, 1997
  48.  * @author Symantec
  49.  */
  50. public class ToolBarPanel extends BorderPanel
  51. {
  52.     /**
  53.      * The constant to use to have a horizontally oriented toolbar.
  54.      * @see #setOrientation
  55.      * @see #getOrientation
  56.      */
  57.     public static final int HORIZONTAL    = 0;
  58.  
  59.     /**
  60.      * The constant to use to have a vertically oriented toolbar.
  61.      * @see #setOrientation
  62.      * @see #getOrientation
  63.      */
  64.     public static final int VERTICAL        = 1;
  65.  
  66.     /**
  67.      * Create ToolBarPanel.  Default ToolBarPanel uses a BEVEL_RAISED style and is
  68.      * HORIZONTAL.
  69.      */
  70.     public ToolBarPanel()
  71.     {
  72.         padleft     = 0;
  73.         padright    = 0;
  74.         padtop      = 0;
  75.         padbottom   = 0;
  76.         ixPad       = 4;
  77.         iyPadTop    = 4;
  78.         iyPadBottom = 3;
  79.  
  80.         super.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
  81.         try
  82.         {
  83.             setBevelStyle(BevelStyle.BEVEL_RAISED);
  84.             setOrientation(HORIZONTAL);
  85.         }
  86.         catch(PropertyVetoException veto) {};
  87.     }
  88.  
  89.     /**
  90.      * Create ToolBarPanel.
  91.      * @param bevelStyle either BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE.
  92.      * If the value passed is not valid, it will default to BEVEL_RAISED.
  93.      * @param orientationType either HORIZONTAL or VERTICAL.  If the value passed is not
  94.      * valid, it will default to HORIZONTAL.
  95.      * @see BevelStyle#BEVEL_RAISED
  96.      * @see BevelStyle#BEVEL_LOWERED
  97.      * @see BevelStyle#BEVEL_LINE
  98.      * @see BevelStyle#BEVEL_NONE
  99.      * @see #HORIZONTAL
  100.      * @see #VERTICAL
  101.      */
  102.     public ToolBarPanel(int bevelStyle, int orientationType)
  103.     {
  104.         this();
  105.  
  106.         try
  107.         {
  108.             setBevelStyle(bevelStyle);
  109.         }
  110.         catch (PropertyVetoException exc) {}
  111.         try
  112.         {
  113.             setOrientation(orientationType);
  114.         }
  115.         catch (PropertyVetoException exc) {}
  116.     }
  117.  
  118.     /**
  119.      * Sets the orientation of the toolbar.  Modifies the results from getPreferredSize.
  120.      * If the orientation is VERTICAL, getPreferredSize will return the Dimension
  121.      * best suited for this toolbar layed out vertically.  If the orientaion
  122.      * is HORIZONTAL getPreferredSize will return the Dimension best suited for this
  123.      * toolbar layed out horizontally.
  124.      * @param orientationType the orientation to adhere to.  Either HORIZONTAL or VERTICAL
  125.      * @exception PropertyVetoException
  126.      * if the specified property value is unacceptable
  127.      * @see #getOrientation
  128.      * @see #HORIZONTAL
  129.      * @see #VERTICAL
  130.      */
  131.     public void setOrientation(int orientationType) throws PropertyVetoException
  132.     {
  133.         if (orientation != orientationType)
  134.         {
  135.             Integer oldValue = new Integer(orientation);
  136.             Integer newValue = new Integer(orientationType);
  137.  
  138.             vetos.fireVetoableChange("orientation", oldValue, newValue);
  139.  
  140.             orientation = orientationType;
  141.  
  142.             FlowLayout temp = (FlowLayout)panel.getLayout();
  143.  
  144.             if (orientation == HORIZONTAL)
  145.             {
  146.                 temp.setAlignment(FlowLayout.LEFT);
  147.             }
  148.             else if (orientation == VERTICAL)
  149.             {
  150.                 temp.setAlignment(FlowLayout.CENTER);
  151.             }
  152.             panel.setLayout(temp);
  153.  
  154.             if(getComponentCount() > 0)
  155.             {
  156.                 setSize(getPreferredSize());
  157.                 validate();
  158.             }
  159.  
  160.             changes.firePropertyChange("orientation", oldValue, newValue);
  161.         }
  162.     }
  163.  
  164.     /**
  165.      * Gets the orientation of the toolbar.
  166.      * @return VERTICAL: getPreferredSize will return the Dimension
  167.      * best suited for this toolbar layed out vertically. HORIZONTAL: getPreferredSize
  168.      * will return the Dimension best suited for this toolbar layed out horizontally.
  169.      * @see #setOrientation
  170.      */
  171.     public int getOrientation()
  172.     {
  173.         return orientation;
  174.     }
  175.  
  176.     /**
  177.      * Takes no action.
  178.      * This is a standard Java AWT method which gets called to specify
  179.      * which layout manager should be used to layout the components in
  180.      * standard containers.
  181.      *
  182.      * Since layout managers CANNOT BE USED with this container the standard
  183.      * setLayout has been OVERRIDDEN for this container and does nothing.
  184.      *
  185.      * @param lm the layout manager to use to layout this container's components
  186.      * (IGNORED)
  187.      * @see java.awt.Container#getLayout
  188.      **/
  189.     public void setLayout(LayoutManager lm)
  190.     {
  191.     }
  192.  
  193.     /**
  194.      * Returns the recommended dimensions to properly display this component.
  195.      * This is a standard Java AWT method which gets called to determine
  196.      * the recommended size of this component.
  197.      *
  198.      * @see symantec.itools.awt.BorderPanel#getMinimumSize
  199.      */
  200.     public synchronized Dimension getPreferredSize()
  201.     {
  202.         Dimension s = new Dimension(0, 0);
  203.         Component[] list = getComponents();
  204.  
  205.         switch(orientation)
  206.         {
  207.             case HORIZONTAL:
  208.                 for (int i = 0; i < list.length; ++i)
  209.                 {
  210.                     Dimension cs = list[i].getSize();
  211.                     s.width += cs.width;
  212.                     s.height = Math.max(s.height, cs.height);
  213.                 }
  214.                 break;
  215.             case VERTICAL:
  216.                 for (int i = 0; i < list.length; ++i)
  217.                 {
  218.                     Dimension cs = list[i].getSize();
  219.                     s.height += cs.height;
  220.                     s.width = Math.max(s.width, cs.width);
  221.                 }
  222.                 break;
  223.         }
  224.         s.width        += (padleft + padright + ixPad * 2) + 1;
  225.         s.height    += (getLabelTopMargin() + padbottom + iyPadTop + iyPadBottom) + 1;
  226.  
  227.         if (s.width == 0)
  228.             s.width = 50;
  229.  
  230.         if (s.height == 0)
  231.             s.height = 50;
  232.  
  233.         return s;
  234.     }
  235.  
  236.     /**
  237.      * Is the specified orientation type valid?
  238.      * @param orientationType the type to test
  239.      * @return if true then the parameter was equal to either HORIZONTAL or VERTICAL.
  240.      * @see #setOrientation
  241.      * @see #getOrientation
  242.      * @see #HORIZONTAL
  243.      * @see #VERTICAL
  244.      */
  245.     public boolean isValidOrientation(int orientationType)
  246.     {
  247.         switch(orientationType)
  248.         {
  249.             case HORIZONTAL:
  250.             case VERTICAL:
  251.                 return true;
  252.             default:
  253.                 return false;
  254.         }
  255.     }
  256.  
  257.     /**
  258.      * Tells this component that it has been added to a container.
  259.      * This is a standard Java AWT method which gets called by the AWT when
  260.      * this component is added to a container. Typically, it is used to
  261.      * create this component's peer.
  262.      *
  263.      * It has been overridden here to hook-up event listeners.
  264.      *
  265.      * @see #removeNotify
  266.      */
  267.     public synchronized void addNotify()
  268.     {
  269.         super.addNotify();
  270.         
  271.         try
  272.         {
  273.             errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  274.         }
  275.         catch(Throwable ex)
  276.         {
  277.             errors = new symantec.itools.resources.ErrorsBundle();
  278.         }
  279.  
  280.         //Hook up listeners
  281.         if (veto == null)
  282.         {
  283.             veto = new Veto();
  284.             addOrientationListener(veto);
  285.         }
  286.     }
  287.  
  288.     /**
  289.      * Tells this component that it is being removed from a container.
  290.      * This is a standard Java AWT method which gets called by the AWT when
  291.      * this component is removed from a container. Typically, it is used to
  292.      * destroy the peers of this component and all its subcomponents.
  293.      *
  294.      * It has been overridden here to unhook event listeners.
  295.      *
  296.      * @see #addNotify
  297.      */
  298.     public synchronized void removeNotify()
  299.     {
  300.         //Unhook listeners
  301.         if (veto != null)
  302.         {
  303.             removeOrientationListener(veto);
  304.             veto = null;
  305.         }
  306.  
  307.         super.removeNotify();
  308.     }
  309.  
  310.     /**
  311.      * Adds a listener for all event changes.
  312.      * @param listener the listener to add.
  313.      * @see #removePropertyChangeListener
  314.      */
  315.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  316.     {
  317.         super.addPropertyChangeListener(listener);
  318.         changes.addPropertyChangeListener(listener);
  319.     }
  320.  
  321.     /**
  322.      * Removes a listener for all event changes.
  323.      * @param listener the listener to remove
  324.      * @see #addPropertyChangeListener
  325.      */
  326.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  327.     {
  328.         super.removePropertyChangeListener(listener);
  329.         changes.removePropertyChangeListener(listener);
  330.     }
  331.  
  332.     /**
  333.      * Adds a vetoable listener for all event changes.
  334.      * @param listener the listener to add
  335.      * @see #removeVetoableChangeListener
  336.      */
  337.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  338.     {
  339.         super.addVetoableChangeListener(listener);
  340.         vetos.addVetoableChangeListener(listener);
  341.     }
  342.  
  343.     /**
  344.      * Removes a vetoable listener for all event changes.
  345.      * @param listener the listener to remove
  346.      * @see #addVetoableChangeListener
  347.      */
  348.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  349.     {
  350.         super.removeVetoableChangeListener(listener);
  351.         vetos.removeVetoableChangeListener(listener);
  352.     }
  353.  
  354.     /**
  355.      * Adds a listener for Orienation changes.
  356.      * @param listener the listener to add
  357.      * @see #removeOrientationListener
  358.      */
  359.     public synchronized void addOrientationListener(PropertyChangeListener listener)
  360.     {
  361.         changes.addPropertyChangeListener("orientation", listener);
  362.     }
  363.  
  364.     /**
  365.      * Removes a listener for Orienation changes.
  366.      * @param listener the listener to remove
  367.      * @see #addOrientationListener
  368.      */
  369.     public synchronized void removeOrientationListener(PropertyChangeListener listener)
  370.     {
  371.         changes.removePropertyChangeListener("orientation", listener);
  372.     }
  373.  
  374.     /**
  375.      * Adds a vetoable listener for Orienation changes.
  376.      * @param listener the listener to add
  377.      * @see #removeOrientationListener
  378.      */
  379.     public synchronized void addOrientationListener(VetoableChangeListener listener)
  380.     {
  381.         vetos.addVetoableChangeListener("orientation", listener);
  382.     }
  383.  
  384.     /**
  385.      * Removes a vetoable listener for Orienation changes.
  386.      * @param listener the listener to remove
  387.      * @see #addOrientationListener
  388.      */
  389.     public synchronized void removeOrientationListener(VetoableChangeListener listener)
  390.     {
  391.         vetos.removeVetoableChangeListener("orientation", listener);
  392.     }
  393.  
  394.     /**
  395.      * This is the PropertyChangeEvent handling inner class for the constrained Orientation property.
  396.      * Handles vetoing Orientations that are not valid.
  397.      */
  398.     class Veto implements VetoableChangeListener
  399.     {
  400.         /**
  401.          * This method gets called when an attempt to change the constrained Orientation property is made.
  402.          * Ensures the given Orientation is valid.
  403.          *
  404.          * @param     e a <code>PropertyChangeEvent</code> object describing the
  405.          *             event source and the property that has changed
  406.          * @exception PropertyVetoException if the recipient wishes the property
  407.          *              change to be rolled back
  408.          */
  409.         public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException
  410.         {
  411.             int i = ((Integer)e.getNewValue()).intValue();
  412.             if (!isValidOrientation(i))
  413.             {
  414.                 throw new PropertyVetoException(errors.getString("InvalidOrientation") + i, e);
  415.             }
  416.         }
  417.     }
  418.  
  419.     /**
  420.      * The orientation for the toolbar.  Either HORIZONTAL or VERTICAL.
  421.      * @see #HORIZONTAL
  422.      * @see #VERTICAL
  423.      */
  424.     protected int orientation;
  425.  
  426.     transient protected ResourceBundle errors;
  427.  
  428.     private Veto veto = null;
  429.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  430.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  431. }
  432.  
  433.