home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 6.8 KB | 255 lines |
- /*---------------------------------------------------------------------------
-
- Written by the Personal Journal developers of Dow Jones & Company, Inc.
-
- Dow Jones makes no representations or warranties about
- the suitability of this software, either express or
- implied, including but not limited to the implied warranties
- of merchantability, fitness for a particular purpose,
- or non-infringement. Dow Jones will not be liable for
- any damages suffered by a user as a result of using,
- modifying or distributing this software or its derivatives.
-
-
- CheckboxTabBar.java
-
- A tab bar based on java.awt.Checkbox and java.util.CheckboxGroup
-
-
- Authors:
-
- rphall Rick Hall
-
-
- Version Ident:
-
- $Header$
-
-
- History:
-
- 10-dec-1995 rphall Initial Creation
-
-
- ---------------------------------------------------------------------------*/
-
- package pj.awt;
-
- import pj.awt.CheckboxTab;
- import pj.awt.Notebook;
- import pj.awt.StringTabSpec;
- import pj.awt.TabBar;
- import pj.awt.TabSpec;
-
- import collections.Assertable;
- import collections.ImplementationCheckable;
- import collections.ImplementationError;
- import java.awt.CheckboxGroup;
- import java.awt.Component;
- import java.awt.Event;
- import java.awt.GridLayout;
- import java.awt.Panel;
- import java.lang.String;
-
- /**
- * A tab bar implemented using CheckboxTab and java.util.CheckboxGroup.
- *
- * @see TabBar
- * @version 0.00 03-Jan-96
- * @author Rick Hall
- */
- public class CheckboxTabBar extends Panel
- implements Assertable, ImplementationCheckable, TabBar
- {
-
- // --- Public constructors
-
- /**
- * Construct a tab bar.
- */
- public CheckboxTabBar(Notebook parent)
- {
- assert(parent != null);
- nbParent = parent;
- cgTabGrp = new CheckboxGroup();
-
- GridLayout gl = new GridLayout(
- 0, // int rows, any number
- 1); // int cols, one
- setLayout(gl);
-
- } // CheckboxTabBar
-
- // --- Public operations
-
- /**
- * Append a tab and conditionally select it.
- *
- * @param tab The tab to append
- * @param selected True to make the new tab the selected tab,
- * false otherwise.
- * @exception ImplementationError if tab is not instanceof StringTabSpec.
- */
- public void appendTab(TabSpec tab, boolean selected)
- throws ImplementationError
- {
- assert(tab instanceof StringTabSpec);
- add(new CheckboxTab((StringTabSpec)tab,cgTabGrp,selected) );
- }
-
- /**
- * Add a tab but do not select it.
- * @param tab The tab to append
- * @exception ImplementationError if tab is not instanceof StringTabSpec.
- */
- public void appendTab(TabSpec tab)
- throws ImplementationError
- { appendTab(tab,false); }
-
- /**
- * Enable a tab.
- * Subclasses must implement this method.
- *
- * @param name The name of the tab to enable.
- */
- public void enableTab(String name)
- {
- CheckboxTab cbt = getTab(name);
- if (name != null) cbt.enable();
- }
-
- /**
- * Enable all tabs.
- */
- public void enableTabs()
- {
- for (int i=0; i<countComponents(); i++)
- ((CheckboxTab)getComponent(i)).enable();
- }
-
-
- /**
- * Disable a tab.
- * Subclasses must implement this method.
- *
- * @param name The name of the tab to disable.
- */
- public void disableTab(String name)
- {
- CheckboxTab cbt = getTab(name);
- if (name != null) cbt.disable();
- }
-
- /**
- * Disable all tabs.
- */
- public void disableTabs()
- {
- for (int i=0; i<countComponents(); i++)
- ((CheckboxTab)getComponent(i)).disable();
- }
-
- /**
- * @return A specification for the currently selected tab. If no
- * tab is selected, returns null. If non-null, the returned
- * specification is an instance of StringTabSpec.
- */
- public TabSpec currentTabSpec()
- {
- StringTabSpec sts = null;
- for (int i=0; i<countComponents(); i++)
- {
- CheckboxTab cbt = (CheckboxTab)getComponent(i);
- if ( cbt.getState() )
- {
- sts = new StringTabSpec( cbt.strTabName, cbt.strPageName,
- (String)cbt.getTabVisual() );
- break;
- } // if set
- } // for components
- return sts;
- } // currentTabSpec
-
- /**
- * Make a tab appear selected.
- *
- * @param name The name of the tab that should appear selected.
- */
- public void selectTab(String name)
- {
- CheckboxTab cbt = getTab(name);
- if (cbt != null) cbt.setState(true);
- }
-
- /**
- * Make a tab appear deselected.
- *
- * @param name The name of the tab that should appear deselected.
- */
- public void deselectTab(String name)
- {
- CheckboxTab cbt = getTab(name);
- if (name != null) cbt.setState(false);
- }
-
- /**
- * Raise an exception if predicate is false.
- * @see collections.Assertable
- */
- public void assert(boolean predicate) throws ImplementationError
- { ImplementationError.assert(this, predicate); }
-
- /**
- * Checks that all components are instances of CheckboxTab.
- * @exception ImplementationError if some component is not
- * an instance of CheckboxTab
- */
- public void checkImplementation() throws ImplementationError
- {
- for (int i=0; i<countComponents(); i++)
- assert(getComponent(i) instanceof CheckboxTab);
- }
-
- public boolean action(Event e, Object arg)
- {
- if (e.target instanceof CheckboxTab)
- {
- String str = ((CheckboxTab)e.target).getPageName();
- nbParent.page(str);
- }
- return true;
- }
-
- // --- Protected operations
-
- /**
- * Find a tab given its name.
- *
- * @param name The name of the tab.
- * @return null if no tab exists with the specified name.
- * @exception ImplementationError if some object other than
- * a CheckboxTab is found with the specified name.
- */
- protected CheckboxTab getTab(String name)
- throws ImplementationError
- {
- Component c = null;
- for (int i=0; i<countComponents(); i++)
- {
- c = getComponent(i);
- assert(c instanceof CheckboxTab);
- String label = ((CheckboxTab)c).getTabName();
- if (label == name) break;
- } // for
-
- return (CheckboxTab)c;
- } // getTab
-
- // --- Protected attributes
-
- /** The CheckboxGroup of a CheckboxTabBar */
- Notebook nbParent;
- CheckboxGroup cgTabGrp;
-
- } // TabBar
-