home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / pjbuttonlayout.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  7.1 KB  |  237 lines

  1.  
  2. /*---------------------------------------------------------------------------
  3.  
  4.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  5.  
  6.     Dow Jones makes no representations or warranties about 
  7.     the suitability of this software, either express or 
  8.     implied, including but not limited to the implied warranties 
  9.     of merchantability, fitness for a particular purpose, 
  10.     or non-infringement.  Dow Jones will not be liable for 
  11.     any damages suffered by a user as a result of using, 
  12.     modifying or distributing this software or its derivatives.
  13.  
  14.  
  15.     @(#)PjButtonLayout.java   0.00 8-Feb-96
  16.  
  17.         A PjButtonLayout that manages the eaual size buttons layout.
  18.  
  19.     Authors:
  20.  
  21.         jlee        James Lee
  22.  
  23.     Version Ident:
  24.  
  25.         $Header$
  26.  
  27.     History:
  28.  
  29.          8-Feb-96    jlee   Initial creation.
  30.         15-Feb-96    jlee   Added and used PjFinals class for all size info.
  31.         25-Mar-96    jlee   Modified layoutCOntainer so that it dosn't repeat the same layout change.
  32. ---------------------------------------------------------------------------*/
  33.  
  34. package pj.awt;
  35.  
  36. import pj.awt.PjFinals;
  37.  
  38. import java.awt.Component;
  39. import java.awt.Container;
  40. import java.awt.Dimension;
  41. import java.awt.Graphics;
  42. import java.awt.Insets;
  43. import java.awt.LayoutManager;
  44. import java.awt.Rectangle;
  45.  
  46. /**
  47.  * PjButtonLayout is used to layout equal size buttons in a panel.
  48.  * <P>
  49.  * This custom layout places the equal size buttons horizontally,
  50.  * regardless of the length of caption of individual button.
  51.  * <pre>
  52.  *      ____________________________________________
  53.  *      |     Button1       |       Button2         |
  54.  *      ---------------------------------------------
  55.  * </pre>
  56.  * @version     0.00, 8/2/96
  57.  * @author      James Lee
  58.  */
  59. public class PjButtonLayout implements LayoutManager
  60.     {
  61.  
  62.     // Instance variables
  63.  
  64.     //widthFixed: wether or not the width of buttons are bound.
  65.     private boolean bWidthFixed = true;
  66.  
  67.     private int   hgap;
  68.     private int   vgap;
  69.  
  70.     private final Dimension dimButtonSize  = new Dimension( PjFinals.nPjMinimumPageViewWidth, PjFinals.nPjButtonHeight );
  71.     private       Rectangle recLast        = new Rectangle(0, 0, 0, 0);
  72.  
  73.  
  74.     // --- Public constructors
  75.     /**
  76.      * Constructs a new Flow Layout with a centered alignment.
  77.      * Default value for hgap and vgap is 0.
  78.      */
  79.     public PjButtonLayout()
  80.         {
  81.         this(0, 0);
  82.         }
  83.  
  84.     /**
  85.      * Constructs a new Flow Layout with the specified alignment and gap
  86.      * values.
  87.      * @param fixed the constraint flag for button width
  88.      */
  89.     public PjButtonLayout(boolean fixed)
  90.         {
  91.         this(0, 0);
  92.         this.bWidthFixed = fixed;
  93.         }
  94.  
  95.     /**
  96.      * Constructs a new Flow Layout with the specified alignment and gap
  97.      * values.
  98.      * @param hgap the horizontal gap variable
  99.      * @param vgap the vertical gap variable
  100.      */
  101.     public PjButtonLayout(int hgap, int vgap)
  102.         {
  103.         this.hgap = hgap;
  104.         this.vgap = vgap;
  105.         }
  106.  
  107.     /**
  108.      * Constructs a new Flow Layout with the specified alignment and gap
  109.      * values.
  110.      * @param hgap  the horizontal gap variable
  111.      * @param vgap  the vertical gap variable
  112.      * @param fixed the constraint flag for button width
  113.      */
  114.     public PjButtonLayout(int hgap, int vgap, boolean fixed)
  115.         {
  116.         this.hgap = hgap;
  117.         this.vgap = vgap;
  118.         this.bWidthFixed = fixed;
  119.         }
  120.  
  121.     // --- Public operations
  122.     /**
  123.      * Adds the specified component to the layout. Not used by this class.
  124.      * @param name the name of the component
  125.      * @param comp the the component to be added
  126.      */
  127.     public void addLayoutComponent(String name, Component comp)
  128.         {
  129.         }
  130.  
  131.     /**
  132.      * Removes the specified component from the layout. Not used by
  133.      * this class.
  134.      * @param comp the component to remove
  135.      */
  136.     public void removeLayoutComponent(Component comp)
  137.         {
  138.         }
  139.  
  140.     /**
  141.      * Returns the preferred dimensions for this layout given the components
  142.      * in the specified target container.
  143.      * @param target the component which needs to be laid out
  144.      * @see Container
  145.      * @see #minimumLayoutSize
  146.      */
  147.     public Dimension preferredLayoutSize(Container target)
  148.         {
  149.         Dimension dim = new Dimension(dimButtonSize.width + hgap, dimButtonSize.height);
  150.         int nmembers = target.countComponents();
  151.  
  152.         Insets insets = target.insets();
  153.         dim.width += insets.left + insets.right + hgap*2;
  154.         dim.height += insets.top + insets.bottom + vgap*2;
  155.  
  156.         return dim;
  157.         }
  158.  
  159.     /**
  160.      * Returns the minimum dimensions needed to layout the components
  161.      * contained in the specified target container.
  162.      * @param target the component which needs to be laid out
  163.      * @see #preferredLayoutSize
  164.      */
  165.     public Dimension minimumLayoutSize(Container target)
  166.         {
  167.         Dimension dim = new Dimension(dimButtonSize.width + hgap, dimButtonSize.height);
  168.  
  169.         Insets insets = target.insets();
  170.         dim.width += insets.left + insets.right + hgap*2;
  171.         dim.height += insets.top + insets.bottom + vgap*2;
  172.  
  173.         return dim;
  174.         }
  175.  
  176.  
  177.     /**
  178.      * Lays out the container. This method will actually reshape the
  179.      * components in the target in order to satisfy the constraints of
  180.      * the PjAdViewLayout object.
  181.      * @param target the specified component being laid out.
  182.      * @see Container
  183.      */
  184.     public void layoutContainer(Container target)
  185.         {
  186.         Insets insets = target.insets();
  187.         Rectangle rec = target.bounds();
  188.  
  189.         if ( target.isValid() &&
  190.             rec.x == recLast.x && rec.y == recLast.y &&
  191.             rec.width == recLast.width && rec.height == recLast.height  )
  192.  
  193.             return;
  194.         else
  195.             recLast = rec;
  196.  
  197.         int maxwidth = rec.width - (insets.left + insets.right + hgap*2);
  198.         int nmembers = target.countComponents();
  199.  
  200.         int width;
  201.         int btnWidth;
  202.  
  203.         if ( bWidthFixed )
  204.             {
  205.             width = maxwidth - dimButtonSize.width;
  206.             btnWidth = ( dimButtonSize.width - (nmembers - 1) * hgap ) / nmembers;
  207.             }
  208.         else
  209.             {
  210.             width = hgap*2;
  211.             btnWidth = ( maxwidth - (nmembers - 1) * hgap ) / nmembers;
  212.             }
  213.  
  214.         int x = width / 2;
  215.         int y = (rec.height - dimButtonSize.height - vgap*2) / 2;
  216.  
  217.         for (int i = 0 ; i < nmembers ; i++)
  218.             {
  219.             Component m = target.getComponent(i);
  220.  
  221.             if ( m.isVisible() )
  222.                 m.reshape( x, y, btnWidth, PjFinals.nPjButtonHeight );
  223.  
  224.             x += btnWidth + hgap;
  225.             }
  226.         }
  227.  
  228.     /**
  229.      * Returns the String representation of this PjButtonLayout's values.
  230.      */
  231.     public String toString()
  232.         {
  233.         return  getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]" +
  234.                 "[width=" + dimButtonSize.width + ",height=" + dimButtonSize.height + "]";
  235.         }
  236. }
  237.