home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / pjhdlviewlayout.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  10.3 KB  |  312 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.     @(#)PjHdlViewLayout.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.         12-Feb-96    jlee   Initial creation.
  29.         12-Mar-96    jlee   Used a custom made TitleList class in place of List class.
  30.         25-Mar-96    jlee   Modified layoutCOntainer so that it dosn't repeat the same layout change.
  31.         29-Mar-96    jlee   Changed the handling of ButtonHeight in TitleList
  32. ---------------------------------------------------------------------------*/
  33.  
  34. package pj.awt;
  35.  
  36.  
  37. import pj.awt.PjFinals;
  38. import pj.awt.TitleList;
  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.  * PjHdlViewLayout is used to layout Headline View.
  50.  * <P>
  51.  * This custom layout  places two button( "Business & Finance", "World-Wid" )
  52.  * at the top and two corresponding columns just below. The width of buttons and
  53.  * columns should be the same, but the height can be changed dynamicaly.
  54.  * Below all of these, another column(Personal News) is placed with constraints
  55.  * as its height can not be taller than a certain size.
  56.  * <pre>
  57.  *      ____________________________________
  58.  *      |   Button       |    Button       |
  59.  *      |----------------|-----------------|
  60.  *      |                |                 |
  61.  *      |                |                 |
  62.  *      |                |                 |
  63.  *      |     Column     |     Column      |
  64.  *      |                |                 |
  65.  *      |                |                 |
  66.  *      |________________|_________________|
  67.   *     |                                  |
  68.  *      |                                  |
  69.  *      |             Column               |
  70.  *      |                                  |
  71.  *      |__________________________________|
  72.  *
  73.  * </pre>
  74.  *
  75.  * @version     0.00, 2/12/96
  76.  * @author      James Lee
  77.  */
  78. public class PjHdlViewLayout implements LayoutManager
  79.     {
  80.  
  81.     // ---Instance variables
  82.  
  83.     private int hgap;
  84.     private int vgap;
  85.     private Rectangle recLast = new Rectangle(0, 0, 0, 0);
  86.  
  87.  
  88.     // --- Public constructors
  89.     /**
  90.      * Constructs a new Flow Layout with a centered alignment.
  91.      * Default value for hgap and vgap is 0.
  92.      */
  93.     public PjHdlViewLayout()
  94.         {
  95.         this(0, 0);
  96.         }
  97.  
  98.     /**
  99.      * Constructs a new Flow Layout with the specified alignment and gap
  100.      * values.
  101.      * @param hgap the horizontal gap variable
  102.      * @param vgap the vertical gap variable
  103.      */
  104.     public PjHdlViewLayout(int hgap, int vgap)
  105.         {
  106.         this.hgap = hgap;
  107.         this.vgap = vgap;
  108.         }
  109.  
  110.     /// --- Public operations
  111.     /**
  112.      * Adds the specified component to the layout. Not used by this class.
  113.      * @param name the name of the component
  114.      * @param comp the the component to be added
  115.      */
  116.     public void addLayoutComponent(String name, Component comp)
  117.         {
  118.         }
  119.  
  120.     /**
  121.      * Removes the specified component from the layout. Not used by
  122.      * this class.
  123.      * @param comp the component to remove
  124.      */
  125.     public void removeLayoutComponent(Component comp)
  126.         {
  127.         }
  128.  
  129.     /**
  130.      * Returns the preferred dimensions for this layout given the components
  131.      * in the specified target container.
  132.      * @param target the component which needs to be laid out
  133.      * @see Container
  134.      * @see #minimumLayoutSize
  135.      */
  136.     public Dimension preferredLayoutSize(Container target)
  137.         {
  138.         Dimension dim = new Dimension(0, 0);
  139.         int nmembers = target.countComponents();
  140.  
  141.         for (int i = 0 ; i < nmembers ; i++)
  142.             {
  143.             Component m = target.getComponent(i);
  144.             if (m.isVisible())
  145.                 {
  146.                 Dimension d = m.preferredSize();
  147.  
  148.                 dim.height = Math.max(dim.height, d.height);
  149.                 if (i > 0)
  150.                     {
  151.                     dim.width += hgap;
  152.                     }
  153.                 dim.width += d.width;
  154.                 }
  155.             } // for
  156.  
  157.         Insets insets = target.insets();
  158.         dim.width    += insets.left + insets.right;
  159.         dim.height   += insets.top + insets.bottom + vgap*2;
  160.  
  161.         return dim;
  162.         } //preferredLayoutSize
  163.  
  164.     /**
  165.      * Returns the minimum dimensions needed to layout the components
  166.      * contained in the specified target container.
  167.      * @param target the component which needs to be laid out
  168.      * @see #preferredLayoutSize
  169.      */
  170.     public Dimension minimumLayoutSize(Container target)
  171.         {
  172.         Dimension dim = new Dimension(0, 0);
  173.         int nmembers = target.countComponents();
  174.  
  175.         for (int i = 0 ; i < nmembers ; i++)
  176.             {
  177.             Component m = target.getComponent(i);
  178.             if (m.isVisible())
  179.                 {
  180.                 Dimension d = m.minimumSize();
  181.                 dim.height = Math.max(dim.height, d.height);
  182.                 if (i > 0)
  183.                     {
  184.                     dim.width += hgap;
  185.                     }
  186.                 dim.width += d.width;
  187.                 }
  188.             }//for
  189.         Insets insets = target.insets();
  190.         dim.width += insets.left + insets.right;
  191.         dim.height += insets.top + insets.bottom + vgap*2;
  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 synchronized 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.             return;
  212.         else
  213.             recLast = rec;
  214.  
  215.         int maxwidth = rec.width - (insets.left + insets.right + hgap);
  216.         int maxheight = rec.height - (insets.top + insets.bottom + vgap*2);
  217.  
  218.         int nmembers = target.countComponents();
  219.         int upwidth = maxwidth / 2;
  220.         int upheight;
  221.  
  222.         int downwidth = maxwidth;
  223.         int downheight = maxheight * 8 / 30;
  224.  
  225.         int nLabelHeight = ((Container)target.getComponent(2)).getComponent(0).minimumSize().height + 2;
  226.         // Fix this long chain if possible
  227.         int nTitleListItem = (((Container)
  228.                                 ((Container)
  229.                                     ((Container)
  230.                                         target.getComponent(2)).
  231.                                             getComponent(1)).
  232.                                                 getComponent(0)).
  233.                                                     countComponents() - 1
  234.                              ) / 2;
  235.  
  236.         TitleList tlPersonalNews =  (TitleList)
  237.                                        ((Container)
  238.                                            ((Container)
  239.                                                target.getComponent(2)).
  240.                                                    getComponent(1)).
  241.                                                        getComponent(0);
  242.         Dimension dimTextFieldMim = null;
  243.  
  244.         if ( tlPersonalNews != null  )
  245.             dimTextFieldMim = tlPersonalNews.getTextFieldMinimumSize();
  246.         else
  247.             System.out.println("Error-PjHdlViewLayout-layoutContainer:TitleList is null!");
  248.  
  249.         int nBtnHeight = PjFinals.nTitleListButtonHeight;
  250.  
  251.         if ( dimTextFieldMim.height > PjFinals.nTitleListButtonHeight )
  252.             {
  253.             nBtnHeight = dimTextFieldMim.height;
  254.             tlPersonalNews.setButtonHeight( nBtnHeight );
  255.             }
  256.         else
  257.             tlPersonalNews.setButtonHeight( nBtnHeight );
  258.  
  259.  
  260.         if ( downheight > nTitleListItem * (nBtnHeight + 3) - 3  )
  261.             downheight = (nTitleListItem) * (nBtnHeight + 3) - 3  + nLabelHeight;
  262.         else
  263.             downheight = (downheight / (nBtnHeight + 3) + 1)
  264.                          * (nBtnHeight + 3) - 3 + nLabelHeight;
  265.  
  266.         upheight = maxheight - downheight;
  267.  
  268.         int x = insets.left;
  269.         int y = insets.top + vgap;
  270.  
  271.         for (int i = 0 ; i < nmembers ; i++)
  272.             {
  273.             Component m = target.getComponent(i);
  274.  
  275.             if ( m.isVisible() )
  276.                 {
  277.                 switch ( i )
  278.                     {
  279.                     case 0://Left Panel
  280.                         m.reshape( x, y, upwidth, upheight );
  281.                         m.validate();
  282.                         break;
  283.                     case 1://Right Panel
  284.                         x += upwidth + hgap;
  285.                         m.reshape( x, y, upwidth, upheight );
  286.                         m.validate();
  287.                         break;
  288.                     case 2://Bottom Panel
  289.                         x = insets.left;
  290.                         y += upheight + vgap;
  291.                         m.reshape( x, y, downwidth, downheight );
  292.                         m.validate();
  293.                         break;
  294.                     default:
  295.                         break;
  296.                     }//switch
  297.                 }//if
  298.             }//for
  299.  
  300.         }//layoutContainer
  301.  
  302.     /**
  303.      * Returns the String representation of this PjListLayout's values.
  304.      */
  305.     public String toString()
  306.         {
  307.         return  getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
  308.         }
  309. }
  310.  
  311.  
  312.