home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / checkboxtabbar.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  6.8 KB  |  255 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.     CheckboxTabBar.java
  15.  
  16.         A tab bar based on java.awt.Checkbox and java.util.CheckboxGroup
  17.  
  18.  
  19.     Authors:
  20.  
  21.         rphall          Rick Hall
  22.  
  23.  
  24.     Version Ident:
  25.  
  26.         $Header$
  27.  
  28.  
  29.     History:
  30.  
  31.         10-dec-1995 rphall     Initial Creation
  32.  
  33.  
  34. ---------------------------------------------------------------------------*/
  35.  
  36. package pj.awt;
  37.  
  38. import pj.awt.CheckboxTab;
  39. import pj.awt.Notebook;
  40. import pj.awt.StringTabSpec;
  41. import pj.awt.TabBar;
  42. import pj.awt.TabSpec;
  43.  
  44. import collections.Assertable;
  45. import collections.ImplementationCheckable;
  46. import collections.ImplementationError;
  47. import java.awt.CheckboxGroup;
  48. import java.awt.Component;
  49. import java.awt.Event;
  50. import java.awt.GridLayout;
  51. import java.awt.Panel;
  52. import java.lang.String;
  53.  
  54. /**
  55.  * A tab bar implemented using CheckboxTab and java.util.CheckboxGroup.
  56.  *
  57.  * @see TabBar
  58.  * @version 0.00 03-Jan-96
  59.  * @author Rick Hall
  60. */
  61. public class CheckboxTabBar extends Panel
  62.     implements Assertable, ImplementationCheckable, TabBar
  63.     {
  64.  
  65.     // --- Public constructors
  66.  
  67.     /**
  68.      * Construct a tab bar.
  69.     */
  70.     public CheckboxTabBar(Notebook parent)
  71.         {
  72.         assert(parent != null);
  73.         nbParent = parent;
  74.         cgTabGrp = new CheckboxGroup();
  75.  
  76.         GridLayout gl = new GridLayout(
  77.                 0,  // int rows, any number
  78.                 1); // int cols, one
  79.         setLayout(gl);
  80.  
  81.         } // CheckboxTabBar
  82.  
  83.     // --- Public operations
  84.  
  85.     /**
  86.      * Append a tab and conditionally select it.
  87.      *
  88.      * @param tab The tab to append
  89.      * @param selected True to make the new tab the selected tab,
  90.      * false otherwise.
  91.      * @exception ImplementationError if tab is not instanceof StringTabSpec.
  92.     */
  93.     public void appendTab(TabSpec tab, boolean selected)
  94.         throws ImplementationError
  95.         {
  96.         assert(tab instanceof StringTabSpec);
  97.         add(new CheckboxTab((StringTabSpec)tab,cgTabGrp,selected) );
  98.         }
  99.  
  100.     /**
  101.      * Add a tab but do not select it.
  102.      * @param tab The tab to append
  103.      * @exception ImplementationError if tab is not instanceof StringTabSpec.
  104.     */
  105.     public void appendTab(TabSpec tab)
  106.         throws ImplementationError
  107.         { appendTab(tab,false); }
  108.  
  109.     /**
  110.      * Enable a tab.
  111.      * Subclasses must implement this method.
  112.      *
  113.      * @param name  The name of the tab to enable.
  114.     */
  115.     public void enableTab(String name)
  116.         {
  117.         CheckboxTab cbt = getTab(name);
  118.         if (name != null) cbt.enable();
  119.         }
  120.  
  121.     /**
  122.      * Enable all tabs.
  123.     */
  124.     public void enableTabs()
  125.         {
  126.         for (int i=0; i<countComponents(); i++)
  127.             ((CheckboxTab)getComponent(i)).enable();
  128.         }
  129.  
  130.  
  131.     /**
  132.      * Disable a tab.
  133.      * Subclasses must implement this method.
  134.      *
  135.      * @param name  The name of the tab to disable.
  136.     */
  137.     public void disableTab(String name)
  138.         {
  139.         CheckboxTab cbt = getTab(name);
  140.         if (name != null) cbt.disable();
  141.         }
  142.  
  143.     /**
  144.      * Disable all tabs.
  145.     */
  146.     public void disableTabs()
  147.         {
  148.         for (int i=0; i<countComponents(); i++)
  149.             ((CheckboxTab)getComponent(i)).disable();
  150.         }
  151.  
  152.     /**
  153.      * @return A specification for the currently selected tab.  If no
  154.      * tab is selected, returns null.  If non-null, the returned
  155.      * specification is an instance of StringTabSpec.
  156.     */
  157.     public TabSpec currentTabSpec()
  158.         {
  159.         StringTabSpec sts = null;
  160.         for (int i=0; i<countComponents(); i++)
  161.             {
  162.             CheckboxTab cbt = (CheckboxTab)getComponent(i);
  163.             if ( cbt.getState() )
  164.                 {
  165.                 sts = new StringTabSpec( cbt.strTabName, cbt.strPageName,
  166.                         (String)cbt.getTabVisual() );
  167.                 break;
  168.                 } // if set
  169.             } // for components
  170.         return sts;
  171.         } // currentTabSpec
  172.  
  173.     /**
  174.      * Make a tab appear selected.
  175.      *
  176.      * @param name The name of the tab that should appear selected.
  177.     */
  178.     public void selectTab(String name)
  179.         {
  180.         CheckboxTab cbt = getTab(name);
  181.         if (cbt != null) cbt.setState(true);
  182.         }
  183.  
  184.     /**
  185.      * Make a tab appear deselected.
  186.      *
  187.      * @param name The name of the tab that should appear deselected.
  188.     */
  189.     public void deselectTab(String name)
  190.         {
  191.         CheckboxTab cbt = getTab(name);
  192.         if (name != null) cbt.setState(false);
  193.         }
  194.  
  195.     /**
  196.      * Raise an exception if predicate is false.
  197.      * @see collections.Assertable
  198.     */
  199.     public void assert(boolean predicate) throws ImplementationError
  200.         { ImplementationError.assert(this, predicate); }
  201.  
  202.     /**
  203.      * Checks that all components are instances of CheckboxTab.
  204.      * @exception ImplementationError if some component is not
  205.      * an instance of CheckboxTab
  206.     */
  207.     public void checkImplementation() throws ImplementationError
  208.         {
  209.         for (int i=0; i<countComponents(); i++)
  210.             assert(getComponent(i) instanceof CheckboxTab);
  211.         }
  212.  
  213.     public boolean action(Event e, Object arg)
  214.         {
  215.         if (e.target instanceof CheckboxTab)
  216.             {
  217.             String str = ((CheckboxTab)e.target).getPageName();
  218.             nbParent.page(str);
  219.             }
  220.         return true;
  221.         }
  222.  
  223.     // --- Protected operations
  224.  
  225.     /**
  226.      * Find a tab given its name.
  227.      *
  228.      * @param name The name of the tab.
  229.      * @return null if no tab exists with the specified name.
  230.      * @exception ImplementationError if some object other than
  231.      * a CheckboxTab is found with the specified name.
  232.     */
  233.     protected CheckboxTab getTab(String name)
  234.         throws ImplementationError
  235.         {
  236.         Component c = null;
  237.         for (int i=0; i<countComponents(); i++)
  238.             {
  239.             c = getComponent(i);
  240.             assert(c instanceof CheckboxTab);
  241.             String label = ((CheckboxTab)c).getTabName();
  242.             if (label == name) break;
  243.             } // for
  244.  
  245.         return (CheckboxTab)c;
  246.         } // getTab
  247.  
  248.     // --- Protected attributes
  249.  
  250.     /** The CheckboxGroup of a CheckboxTabBar */
  251.     Notebook nbParent;
  252.     CheckboxGroup cgTabGrp;
  253.  
  254.     } // TabBar
  255.