home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / pjadviewlayout.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  7.7 KB  |  226 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.     @(#)PjAdViewLayout.java   0.00 5-Feb-96
  15.  
  16.         A PjAdViewLayout that manages the AdView layout.
  17.  
  18.     Authors:
  19.  
  20.         jlee        James Lee
  21.  
  22.     Version Ident:
  23.  
  24.         $Header$
  25.  
  26.     History:
  27.  
  28.          5-Feb-96    jlee   Initial creation.
  29.         15-Feb-96    jlee   Added and used PjFinals class for all size info.
  30.         12-Mar-96    jlee   Modified so that it can accomadate enclosing 3D box.
  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.  * PjAdViewLayout is used to layout Ad view controls in a panel.
  48.  * <P>
  49.  * This custom layout places the image to the left and the ad
  50.  * text to the right.  The url, if present, is below the text.
  51.  * <pre>
  52.  *      ____________________________________________
  53.  *      |                   |                       |
  54.  *      |  Image            |        Text           |
  55.  *      |                   |                       |
  56.  *      |___________________|_______________________|
  57.  *      |________________URL________________________|
  58.  *
  59.  * </pre>
  60.  * @version     0.00, 2/5/96
  61.  * @author      James Lee
  62.  */
  63. public class PjAdViewLayout implements LayoutManager {
  64.  
  65.     // --- Instance variables
  66.     private int hgap;
  67.     private int vgap;
  68.     private int padding = PjFinals.nPjVerticalSpacing;
  69.  
  70.     private final Dimension adImageSize = new Dimension( PjFinals.nPjAdImageWidth, PjFinals.nPjAdImageHeight );
  71.     private final Dimension adTextSize  = new Dimension( PjFinals.nPjAdTextBoxWidth, PjFinals.nPjAdTextBoxHeight );
  72.     private final Dimension adMoreSize  = new Dimension( PjFinals.nPjMinimumPageViewWidth, PjFinals.nPjButtonHeight );
  73.     private       Rectangle recLast     = new Rectangle(0, 0, 0, 0);
  74.  
  75.  
  76.     // --- Public constructors
  77.     /**
  78.      * Constructs a new Flow Layout with a centered alignment.
  79.      * Default value for hgap and vgap is 0.
  80.      */
  81.     public PjAdViewLayout()
  82.         {
  83.         this(0, 0);
  84.         }
  85.  
  86.     /**
  87.      * Constructs a new Flow Layout with the specified alignment and gap
  88.      * values.
  89.      * @param hgap the horizontal gap variable
  90.      * @param vgap the vertical gap variable
  91.      */
  92.     public PjAdViewLayout(int hgap, int vgap)
  93.         {
  94.         this.hgap = hgap;
  95.         this.vgap = vgap;
  96.         }
  97.  
  98.     // --- Public operations
  99.     /**
  100.      * Adds the specified component to the layout. Not used by this class.
  101.      * @param name the name of the component
  102.      * @param comp the the component to be added
  103.      */
  104.     public void addLayoutComponent(String name, Component comp)
  105.         {
  106.         }
  107.  
  108.     /**
  109.      * Removes the specified component from the layout. Not used by
  110.      * this class.
  111.      * @param comp the component to remove
  112.      */
  113.     public void removeLayoutComponent(Component comp)
  114.         {
  115.         }
  116.  
  117.     /**
  118.      * Returns the preferred dimensions for this layout given the components
  119.      * in the specified target container.
  120.      * @param target the component which needs to be laid out
  121.      * @see Container
  122.      * @see #minimumLayoutSize
  123.      */
  124.     public Dimension preferredLayoutSize(Container target)
  125.         {
  126.         int upperHeight = adTextSize.height > adImageSize.height ? adTextSize.height: adImageSize.height;
  127.  
  128.         Dimension dim = new Dimension(adMoreSize.width - 2, adMoreSize.height + upperHeight + padding);
  129.         int nmembers = target.countComponents();
  130.  
  131.         Insets insets = target.insets();
  132.         dim.width += insets.left + insets.right + hgap*2;
  133.         dim.height += insets.top + insets.bottom + vgap*2;
  134.  
  135.         return dim;
  136.         }
  137.  
  138.     /**
  139.      * Returns the minimum dimensions needed to layout the components
  140.      * contained in the specified target container.
  141.      * @param target the component which needs to be laid out
  142.      * @see #preferredLayoutSize
  143.      */
  144.     public Dimension minimumLayoutSize(Container target)
  145.         {
  146.         int upperHeight = adTextSize.height > adImageSize.height ? adTextSize.height: adImageSize.height;
  147.  
  148.         Dimension dim = new Dimension(adMoreSize.width - 2, adMoreSize.height + upperHeight + padding );
  149.  
  150.         Insets insets = target.insets();
  151.         dim.width += insets.left + insets.right + hgap*2;
  152.         dim.height += insets.top + insets.bottom + vgap*2;
  153.  
  154.         return dim;
  155.         }
  156.  
  157.     /**
  158.      * Lays out the container. This method will actually reshape the
  159.      * components in the target in order to satisfy the constraints of
  160.      * the PjAdViewLayout object.
  161.      * @param target the specified component being laid out.
  162.      * @see Container
  163.      */
  164.     public void layoutContainer(Container target)
  165.         {
  166.         Insets insets = target.insets();
  167.         Rectangle rec = target.bounds();
  168.  
  169.         if ( target.isValid() &&
  170.              rec.x == recLast.x && rec.y == recLast.y &&
  171.              rec.width == recLast.width && rec.height == recLast.height  )
  172.             return;
  173.         else
  174.             recLast = rec;
  175.  
  176.         int maxwidth = rec.width - (insets.left + insets.right + hgap*2);
  177.         int nmembers = target.countComponents();
  178.         int width = maxwidth - adMoreSize.width;
  179.  
  180.         int x = 0;
  181.         int y = 0;
  182.  
  183.         for (int i = 0 ; i < nmembers ; i++)
  184.             {
  185.             Component m = target.getComponent(i);
  186.  
  187.             //y = (rec.height - adImageSize.height - adMoreSize.height - padding) / 2;
  188.             y = 0;
  189.             if ( m.isVisible() )
  190.                 {
  191.                 switch ( i )
  192.                     {
  193.                     case 0://Ad Text
  194.                         x = width / 2 + adImageSize.width + padding;
  195.                         m.reshape( x, y, adTextSize.width, adTextSize.height );
  196.                         break;
  197.                     case 1://Ad More button
  198.                         x = width / 2;
  199.                         y += (adTextSize.height > adImageSize.height ? adTextSize.height: adImageSize.height);// + padding;
  200.                         m.reshape( x, y, adMoreSize.width - 2, adMoreSize.height );
  201.                         break;
  202.                     case 2://Ad GifCanvas
  203.                         x = width / 2;
  204.                         //y += (PjFinals.nPjAdTextBoxHeight - PjFinals.nPjAdImageHeight) / 2;
  205.                         y = 0;
  206.                         m.reshape( x, y, adImageSize.width, adImageSize.height );
  207.                         break;
  208.  
  209.                     default:
  210.                         break;
  211.                     }//switch
  212.                 }//if
  213.             }//for
  214.  
  215.         }
  216.  
  217.     /**
  218.      * Returns the String representation of this PjAdViewLayout's values.
  219.      */
  220.     public String toString()
  221.         {
  222.         return  getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]" +
  223.                 "[width=" + adMoreSize.width + ",height=" + (adMoreSize.height + adImageSize.height) + "]";
  224.         }
  225. }
  226.