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

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)PjListLayout.java   0.00 7-Feb-96
  15.  
  16.         A PjListLayout that manages the Label and Column pairlayout in a View(eg. PortView).
  17.  
  18.     Authors:
  19.  
  20.         jlee        James Lee
  21.  
  22.     Version Ident:
  23.  
  24.         $Header$
  25.  
  26.     History:
  27.  
  28.          7-Feb-96    jlee   Initial creation.
  29.         14-Feb-96    jlee   Added alignment(LEFT, CENTER and RIGHT) and button component.
  30.         15-Feb-96    jlee   Added and used PjFinals class for all size info.
  31.         21-Feb-96    jlee   Added handling of three components(label, button, and column) layout.
  32.         25-Mar-96    jlee   Modified layoutCOntainer so that it dosn't repeat the same layout change.
  33. ---------------------------------------------------------------------------*/
  34.  
  35. package pj.awt;
  36.  
  37. import pj.awt.PjFinals;
  38.  
  39.  
  40. import java.awt.Component;
  41. import java.awt.Container;
  42. import java.awt.Dimension;
  43. import java.awt.Graphics;
  44. import java.awt.Insets;
  45. import java.awt.LayoutManager;
  46. import java.awt.Rectangle;
  47.  
  48.  
  49. /**
  50.  * PjListLayout is used to layout label and column pair in a panel.
  51.  * <P>
  52.  * This custom layout  places the label and/or button(al least one of them)
  53.  * at the top and column just below. The width of the label and column is fixed,
  54.  * which is the same width as PjAdView's. But the height of
  55.  * label plus column changes accordingly as frame changes.
  56.  * <pre>
  57.  *      ____________________________________________
  58.  *      |Label(optional)    |       Button(optional)|
  59.  *      |-------------------------------------------|
  60.  *      |                                           |
  61.  *      |                                           |
  62.  *      |                                           |
  63.  *      |                 Column                    |
  64.  *      |                                           |
  65.  *      |                                           |
  66.  *      |                                           |
  67.  *      |___________________________________________|
  68.  * </pre>
  69.  *
  70.  * @version     0.00, 2/7/96
  71.  * @author      James Lee
  72.  */
  73. public class PjListLayout implements LayoutManager
  74.     {
  75.  
  76.     // --- Class variables
  77.  
  78.     /**
  79.      * The left alignment variable.
  80.      */
  81.     public static final int LEFT        = 0;
  82.  
  83.     /**
  84.      * The right alignment variable.
  85.      */
  86.     public static final int CENTER      = 1;
  87.  
  88.     /**
  89.      * The right alignment variable.
  90.      */
  91.     public static final int RIGHT       = 2;
  92.  
  93.     // --- Instance variables
  94.  
  95.     private int hgap;
  96.     private int vgap;
  97.     private int align;
  98.  
  99.     //100 is a default value.
  100.     private final Dimension listSize    = new Dimension( PjFinals.nPjMinimumPageViewWidth, 100 );
  101.     private Rectangle recLast           = new Rectangle(0, 0, 0, 0);
  102.  
  103.  
  104.     // --- Public constructors
  105.  
  106.     /**
  107.      * Constructs a new Flow Layout with a centered alignment.
  108.      * Default value for hgap and vgap is 0.
  109.      */
  110.     public PjListLayout()
  111.         {
  112.         this(LEFT, 0, 0);
  113.         }
  114.  
  115.     /**
  116.      * Constructs a new Flow Layout with the specified alignment and gap
  117.      * values.
  118.      * @param align the alignment value
  119.      */
  120.     public PjListLayout(int align)
  121.         {
  122.         this(align, 0, 0);
  123.         }
  124.  
  125.     /**
  126.      * Constructs a new Flow Layout with the specified alignment and gap
  127.      * values.
  128.      * @param align the alignment value
  129.      * @param hgap the horizontal gap variable
  130.      * @param vgap the vertical gap variable
  131.      */
  132.     public PjListLayout(int align, int hgap, int vgap)
  133.         {
  134.         this.align  = align;
  135.         this.hgap   = hgap;
  136.         this.vgap   = vgap;
  137.         }
  138.  
  139.  
  140.     // --- Public operations
  141.  
  142.     /**
  143.      * Adds the specified component to the layout. Not used by this class.
  144.      * @param name the name of the component
  145.      * @param comp the the component to be added
  146.      */
  147.     public void addLayoutComponent(String name, Component comp)
  148.         {
  149.         }
  150.  
  151.     /**
  152.      * Removes the specified component from the layout. Not used by
  153.      * this class.
  154.      * @param comp the component to remove
  155.      */
  156.     public void removeLayoutComponent(Component comp)
  157.         {
  158.         }
  159.  
  160.     /**
  161.      * Returns the preferred dimensions for this layout given the components
  162.      * in the specified target container.
  163.      * @param target the component which needs to be laid out
  164.      * @see Container
  165.      * @see #minimumLayoutSize
  166.      */
  167.     public Dimension preferredLayoutSize(Container target)
  168.         {
  169.         Dimension dim = new Dimension(listSize);
  170.         Insets insets = target.insets();
  171.  
  172.         dim.width += insets.left + insets.right + hgap*2;
  173.         dim.height += insets.top + insets.bottom + vgap*2;
  174.  
  175.         return dim;
  176.         }
  177.  
  178.     /**
  179.      * Returns the minimum dimensions needed to layout the components
  180.      * contained in the specified target container.
  181.      * @param target the component which needs to be laid out
  182.      * @see #preferredLayoutSize
  183.      */
  184.     public Dimension minimumLayoutSize(Container target)
  185.         {
  186.         Dimension dim = new Dimension(listSize);
  187.         Insets insets = target.insets();
  188.  
  189.         dim.width += insets.left + insets.right + hgap*2;
  190.         dim.height += insets.top + insets.bottom + vgap*2;
  191.  
  192.         return dim;
  193.         }
  194.  
  195.  
  196.     /**
  197.      * Lays out the container. This method will actually reshape the
  198.      * components in the target in order to satisfy the constraints of
  199.      * the PjAdViewLayout object.
  200.      * @param target the specified component being laid out.
  201.      * @see Container
  202.      */
  203.     public void layoutContainer(Container target)
  204.         {
  205.         Insets insets = target.insets();
  206.         Rectangle rec = target.bounds();
  207.  
  208.         if ( target.isValid() &&
  209.             rec.x == recLast.x && rec.y == recLast.y &&
  210.             rec.width == recLast.width && rec.height == recLast.height  )
  211.  
  212.             return;
  213.         else
  214.             recLast = rec;
  215.  
  216.         int maxwidth = rec.width - (insets.left + insets.right + hgap*2);
  217.         int maxheight = rec.height - (insets.top + insets.bottom + vgap*2);
  218.  
  219.         int nmembers = target.countComponents();
  220.         int width = maxwidth - listSize.width;
  221.  
  222.         int x = 0;
  223.         int y = 0;
  224.  
  225.         int prefWidth = 0;
  226.         int prefHeight = 0;
  227.  
  228.         for (int i = 0 ; i < nmembers ; i++)
  229.             {
  230.             Component m = target.getComponent(i);
  231.             if ( m.isVisible() )
  232.                 {
  233.                 switch ( i )
  234.                     {
  235.                     case 0://Label
  236.                         prefWidth = m.preferredSize().width;
  237.                         prefHeight = m.preferredSize().height;
  238.                         y = insets.top + vgap;
  239.                         switch ( align )
  240.                             {
  241.                             case LEFT:
  242.                                 x = width / 2;
  243.                                 break;
  244.                             case CENTER:
  245.                                 x = width / 2 + (listSize.width - prefWidth) / 2;
  246.                                 break;
  247.                             case RIGHT:
  248.                                 x = width / 2 + listSize.width - prefWidth;
  249.                                 break;
  250.                             default:
  251.                                 break;
  252.                             } //switch (align)
  253.                         m.reshape( x, y, prefWidth, prefHeight );
  254.                         break;
  255.  
  256.                     case 1://Column or Button
  257.                         switch( nmembers )
  258.                             {
  259.                             case 2: //Column
  260.                                 x = width / 2;
  261.                                 y = insets.top + 2 * vgap + prefHeight + 2;
  262.                                 m.reshape( x, y, listSize.width, maxheight - prefHeight - vgap );
  263.                                 break;
  264.                             case 3: //Button
  265.                                 prefWidth = m.preferredSize().width;
  266.                                 prefHeight = m.preferredSize().height;
  267.                                 x = width / 2 + listSize.width - prefWidth;
  268.                                 y = insets.top + vgap;
  269.                                 m.reshape( x, y, prefWidth, prefHeight );
  270.                                 break;
  271.                             default:
  272.                                 break;
  273.                             }//switch(nmembers)
  274.                         break;
  275.  
  276.                     case 2://Column
  277.                         x = width / 2;
  278.                         y = insets.top + 2 * vgap + prefHeight + 2;
  279.                         m.reshape( x, y, listSize.width, maxheight - prefHeight - vgap );
  280.                         break;
  281.  
  282.                     default:
  283.                         break;
  284.  
  285.                     }//switch
  286.                 }//if
  287.             }//for
  288.  
  289.         }//layoutContainer
  290.  
  291.     /**
  292.      * Returns the String representation of this PjListLayout's values.
  293.      */
  294.     public String toString()
  295.         {
  296.         return  getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]" +
  297.                 "[width=" + listSize.width + ",height=" + listSize.height + "]";
  298.         }
  299. }
  300.  
  301.