home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / titlelist.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  13.8 KB  |  453 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.     @(#)TitleList.java  0.00 09-Mar-96
  15.  
  16.         A list that displays personal news title list.
  17.  
  18.  
  19.     Authors:
  20.  
  21.         jlee            James Lee
  22.  
  23.  
  24.     Version Ident:
  25.  
  26.         $Header: /PjJavaClient/src/pj/awt/TitleList.java 5     3/22/96 11:13p Jlee $
  27.  
  28.  
  29.     History:
  30.  
  31.         9-Mar-96 jlee     Initial Creation
  32.        12-Mar-96 jlee     Made the TextField as un-editable and white background.
  33.        19-Mar-96 jlee     Fixed scrolling problem.
  34.        25-Mar-96 jlee     Modified layoutCOntainer so that it dosn't repeat the same layout change.
  35.        26-Mar-96 jlee     Made the scrollbar's routine more solid.
  36.        29-Mar-96 jlee     Changed ScrollBar update algorithm, and added getButtonHeight() and getButtonHeight()
  37. ---------------------------------------------------------------------------*/
  38.  
  39. package pj.awt;
  40.  
  41. import pj.awt.PjFinals;
  42.  
  43. import java.awt.Button;
  44. import java.awt.Color;
  45. import java.awt.Component;
  46. import java.awt.Container;
  47. import java.awt.Dimension;
  48. import java.awt.Event;
  49. import java.awt.Graphics;
  50. import java.awt.Font;
  51. import java.awt.Insets;
  52. import java.awt.LayoutManager;
  53. import java.awt.Panel;
  54. import java.awt.Rectangle;
  55. import java.awt.Scrollbar;
  56. import java.awt.TextField;
  57.  
  58.  
  59. import java.lang.String;
  60.  
  61. /**
  62.  * A custom list that displays personal news titles.
  63.  *
  64.  * @see TabBar
  65.  * @version 0.00 9-Mar-96
  66.  * @author James Lee
  67.  */
  68. public class TitleList extends Panel
  69.     {
  70.     // --- Instance variables
  71.     private Scrollbar   m_sbTitleList               = null;
  72.     private int         m_nSbTitleListPageIncrement  = 0;
  73.     private int         m_nSbTitleListCurValue       = 0;
  74.     private int         m_nNumberOfTitles           = 0;
  75.     private Font        m_fntButton                 = null;
  76.     private boolean     m_bEndOfAdding              = false;
  77.     private Dimension   dimTextFieldMinimumSize     = new Dimension( 10, 10 );
  78.     private TextField   tfTitleList                 = null;
  79.     private int         nTitleListButtonHeight      = 0;
  80.  
  81.     // --- Public constructors
  82.  
  83.     public TitleList()
  84.         {
  85.         setLayout( new TitleListLayout( 3, 3 ) );
  86.  
  87.         m_sbTitleList = new Scrollbar( Scrollbar.VERTICAL );
  88.         m_sbTitleList.setBackground( Color.lightGray );
  89.         m_sbTitleList.resize( PjFinals.nScrollbarWidth, 10 );
  90.         m_sbTitleList.hide();
  91.  
  92.         add( m_sbTitleList );
  93.         m_nNumberOfTitles = 0;
  94.         m_fntButton = PjFinals.fntPNButton;
  95.         System.out.println("Debug-TitleList:constructed");
  96.  
  97.         } // TitleList
  98.  
  99.     // --- Public operations
  100.  
  101.     public boolean isListFinishedAdding()
  102.         {
  103.         return m_bEndOfAdding;
  104.         }
  105.  
  106.     public void addItem( String str, boolean bEndOfAdding )
  107.         {
  108.         if ( !bEndOfAdding )
  109.             {
  110.             Button  b = new Button( String.valueOf( ++m_nNumberOfTitles ) );
  111.             b.hide();
  112.             b.setFont( m_fntButton );
  113.  
  114.             tfTitleList = new TextField( str );
  115.             tfTitleList.hide();
  116.             tfTitleList.setEditable( false );
  117.             tfTitleList.setBackground( Color.white );
  118.  
  119.             add( b );
  120.             add( tfTitleList );
  121.             }
  122.         else
  123.             {
  124.             m_bEndOfAdding = bEndOfAdding;
  125.             getParent().getParent().getParent().invalidate();
  126.             getParent().getParent().getParent().layout();
  127.  
  128.             if ( tfTitleList != null )
  129.                 dimTextFieldMinimumSize = tfTitleList.minimumSize();
  130.             }
  131.         }
  132.  
  133.     public Dimension getTextFieldMinimumSize()
  134.         {
  135.         return dimTextFieldMinimumSize;
  136.         }
  137.  
  138.     public void setButtonHeight( int nButtonHeight )
  139.         {
  140.         nTitleListButtonHeight = nButtonHeight;
  141.         }
  142.  
  143.     public int getButtonHeight()
  144.         {
  145.         return nTitleListButtonHeight;
  146.         }
  147.  
  148.     public int getPageIncrement()
  149.         {
  150.         return m_nSbTitleListPageIncrement;
  151.         }
  152.  
  153.     public void setPageIncrement( int inc )
  154.         {
  155.         m_nSbTitleListPageIncrement = inc;
  156.         }
  157.  
  158.     public void setScbValue( int v )
  159.         {
  160.         m_nSbTitleListCurValue = v;
  161.         m_sbTitleList.setValue( v );
  162.         }
  163.  
  164.     public int getScbValue()
  165.         {
  166.         return m_nSbTitleListCurValue;
  167.         }
  168.  
  169.     public void setScbActualValue( int v )
  170.         {
  171.         m_sbTitleList.setValue( v );
  172.         }
  173.  
  174.     public boolean handleEvent(Event evt)
  175.         {
  176.         int v, max;
  177.  
  178.         switch (evt.id)
  179.             {
  180.             case Event.SCROLL_LINE_UP:
  181.                 v = m_nSbTitleListCurValue - 1;
  182.                 if ( v < 0 )
  183.                     v = 0;
  184.                 setScbValue( v );
  185.                 break;
  186.             case Event.SCROLL_LINE_DOWN:
  187.                 max = m_sbTitleList.getMaximum() - m_sbTitleList.getVisible();
  188.                 v = m_nSbTitleListCurValue + 1;
  189.                 if ( v > max )
  190.                     v = max;
  191.                 setScbValue( v );
  192.                 break;
  193.             case Event.SCROLL_PAGE_UP:
  194.                 v = m_nSbTitleListCurValue - m_nSbTitleListPageIncrement;
  195.                 if ( v < 0 )
  196.                     v = 0;
  197.                 setScbValue( v );
  198.                 break;
  199.             case Event.SCROLL_PAGE_DOWN:
  200.                 max = m_sbTitleList.getMaximum() - m_sbTitleList.getVisible();
  201.                 v = m_nSbTitleListCurValue + m_nSbTitleListPageIncrement;
  202.                 if ( v > max )
  203.                     v = max;
  204.                 setScbValue( v );
  205.                 break;
  206.             case Event.SCROLL_ABSOLUTE:
  207.                 max = m_sbTitleList.getMaximum() - m_sbTitleList.getVisible();
  208.                 v = m_sbTitleList.getValue();
  209.                 if ( v > max )
  210.                     v = max;
  211.                 setScbValue( v );
  212.                 break;
  213.             default:
  214.                 return false;
  215.             }// switch
  216.  
  217.         invalidate();
  218.         layout();
  219.  
  220.         return true;
  221.         }// handleEvent
  222.  
  223.  
  224.     public synchronized void clear()
  225.         {
  226.         m_nNumberOfTitles = 0;
  227.         removeAll();
  228.         add( m_sbTitleList );
  229.         }
  230.     }//TitleList
  231.  
  232. /*---------------------------------------------------------------------------
  233.  
  234.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  235.  
  236.     Dow Jones makes no representations or warranties about 
  237.     the suitability of this software, either express or 
  238.     implied, including but not limited to the implied warranties 
  239.     of merchantability, fitness for a particular purpose, 
  240.     or non-infringement.  Dow Jones will not be liable for 
  241.     any damages suffered by a user as a result of using, 
  242.     modifying or distributing this software or its derivatives.
  243.  
  244.  
  245.     @(#)TitleListLayout.java   0.00 5-Feb-96
  246.  
  247.         A TitleListLayout that manages Tabs layout.
  248.  
  249.     Authors:
  250.  
  251.         jlee        James Lee
  252.  
  253.     Version Ident:
  254.  
  255.         $Header: /PjJavaClient/src/pj/awt/TitleList.java 5     3/22/96 11:13p Jlee $
  256.  
  257.     History:
  258.  
  259.          2-Mar-96    jlee   Initial creation.
  260.  
  261. ---------------------------------------------------------------------------*/
  262.  
  263. /**
  264.  * TitleListLayout is used to layout Tabs in a panel.
  265.  * @version     0.00, 3/2/96
  266.  * @author      James Lee
  267.  */
  268. class TitleListLayout implements LayoutManager {
  269.  
  270.     // --- Instance variables
  271.     private int hgap;
  272.     private int vgap;
  273.  
  274.     private int nWidth;
  275.     private int nHeight;
  276.  
  277.     private int         m_nSbLastVisibleSize = 0;
  278.     private Rectangle   recLast = new Rectangle(0, 0, 0, 0);
  279.  
  280.  
  281.     // --- Public constructors
  282.     /**
  283.      * Constructs a new TitleListLayout with a centered alignment.
  284.      * Default value for hgap and vgap is 0.
  285.      */
  286.     public TitleListLayout()
  287.         {
  288.         this(0, 0);
  289.         }
  290.  
  291.     /**
  292.      * Constructs a new TitleListLayout with the specified alignment and gap
  293.      * values.
  294.      * @param hgap the horizontal gap variable
  295.      * @param vgap the vertical gap variable
  296.      */
  297.     public TitleListLayout(int hgap, int vgap)
  298.         {
  299.         this.hgap = hgap;
  300.         this.vgap = vgap;
  301.         }
  302.     // --- Public operations
  303.  
  304.     /**
  305.      * Adds the specified component to the layout. Not used by this class.
  306.      * @param name the name of the component
  307.      * @param comp the the component to be added
  308.      */
  309.     public void addLayoutComponent(String name, Component comp)
  310.         {
  311.         }
  312.  
  313.     /**
  314.      * Removes the specified component from the layout. Not used by
  315.      * this class.
  316.      * @param comp the component to remove
  317.      */
  318.     public void removeLayoutComponent(Component comp)
  319.         {
  320.         }
  321.  
  322.     /**
  323.      * Returns the preferred dimensions for this layout given the components
  324.      * in the specified target container.
  325.      * @param target the component which needs to be laid out
  326.      * @see Container
  327.      * @see #minimumLayoutSize
  328.      */
  329.     public Dimension preferredLayoutSize(Container target)
  330.         {
  331.         Dimension dim = new Dimension(200, (PjFinals.nTitleListButtonHeight + vgap) * 4 - vgap);
  332.         return dim;
  333.         }
  334.  
  335.     /**
  336.      * Returns the minimum dimensions needed to layout the components
  337.      * contained in the specified target container.
  338.      * @param target the component which needs to be laid out
  339.      * @see #preferredLayoutSize
  340.      */
  341.     public Dimension minimumLayoutSize(Container target)
  342.         {
  343.         Dimension dim = new Dimension(200, (PjFinals.nTitleListButtonHeight + vgap) * 4 - vgap);
  344.         return dim;
  345.         }
  346.  
  347.  
  348.     /**
  349.      * Lays out the container. This method will actually reshape the
  350.      * components in the target in order to satisfy the constraints of
  351.      * the TitleListLayout object.
  352.      * @param target the specified component being laid out.
  353.      * @see Container
  354.      */
  355.     public void layoutContainer(Container target)
  356.         {
  357.         Component   m;
  358.         int         nSbMax = 0;
  359.         int         nSbValue = 0;
  360.         int         nSbVisibleSize = 0;
  361.         int         nButonHeight = ((TitleList)target).getButtonHeight();
  362.         Insets insets = target.insets();
  363.         Rectangle rec = target.bounds();
  364.  
  365.         if ( target.isValid() &&
  366.              rec.x == recLast.x && rec.y == recLast.y &&
  367.              rec.width == recLast.width && rec.height == recLast.height  )
  368.             return;
  369.         else
  370.             recLast = rec;
  371.  
  372.         nWidth = rec.width;
  373.         nHeight = rec.height;
  374.  
  375.         int nmembers = target.countComponents();
  376.  
  377.         if ( nmembers > 0 && ((TitleList)target).isListFinishedAdding() )
  378.             {
  379.             m = target.getComponent(0);//Scrollbar
  380.             m.show();
  381.  
  382.             if ( rec.height > 0 )
  383.                 m.reshape( rec.width - PjFinals.nScrollbarWidth, 0, PjFinals.nScrollbarWidth, rec.height );
  384.             else
  385.                 System.out.println("Debug-TitleList-layoutContainer: Scrollbar's height is less than 1");
  386.  
  387.             nSbVisibleSize = (rec.height + vgap) / ( nButonHeight + vgap);
  388.             nSbMax = (nmembers - 1) / 2;
  389.  
  390.             if ( nSbVisibleSize != m_nSbLastVisibleSize )
  391.                 {
  392.                 m_nSbLastVisibleSize = nSbVisibleSize;
  393.                 nSbValue = 0;
  394.                 if ( m instanceof Scrollbar )
  395.                     {
  396.                     ((Scrollbar)m).setValues( nSbValue, nSbVisibleSize, 0, nSbMax );
  397.                     ((TitleList)target).setScbValue( nSbValue );
  398.                     ((TitleList)target).setScbActualValue( nSbValue );
  399.                     ((TitleList)target).setPageIncrement( nSbVisibleSize );
  400.                     }
  401.                 }
  402.             else
  403.                 nSbValue = ((TitleList)target).getScbValue();//((Scrollbar)m).getValue();
  404.  
  405.             for ( int j = 0, i = 1; i < nmembers ; i++)
  406.                 {
  407.                 m = target.getComponent(i);
  408.  
  409.                 if ( i < nSbValue * 2 + 1 )
  410.                     {
  411.                     m.hide();
  412.                     continue;
  413.                     }
  414.  
  415.                 if ( j * (nButonHeight + vgap) > rec.height - nButonHeight )
  416.                     {
  417.                     m.hide();
  418.                     continue;
  419.                     }
  420.  
  421.                 m.show();
  422.                 switch (i % 2)
  423.                     {
  424.                     case 0://TextField
  425.                         m.reshape( PjFinals.nTitleListButtonWidth + hgap, j * (nButonHeight + vgap),
  426.                                    rec.width - PjFinals.nTitleListButtonWidth - hgap - PjFinals.nScrollbarWidth - 2,
  427.                                    nButonHeight );
  428.                         j++;
  429.                         break;
  430.  
  431.                     case 1://Button
  432.                         m.reshape( 0, j * (nButonHeight + vgap), PjFinals.nTitleListButtonWidth, nButonHeight );
  433.                         break;
  434.  
  435.                     default:
  436.                         break;
  437.                     }
  438.                 }//for
  439.             }//if
  440.         }
  441.  
  442.     /**
  443.      * Returns the String representation of this TitleListLayout's values.
  444.      */
  445.     public String toString()
  446.         {
  447.         return  getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]" +
  448.                 "[width=" + nWidth + ",height=" + nHeight + "]";
  449.         }
  450.  
  451.     }//TitleListLayout
  452.  
  453.