home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / CardLayout.java < prev    next >
Text File  |  1996-05-03  |  8KB  |  278 lines

  1. /*
  2.  * @(#)CardLayout.java    1.11 95/12/14 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt;
  21.  
  22. import java.util.Hashtable;
  23. import java.util.Enumeration;
  24.  
  25. /**
  26.  * A layout manager for a container that contains several
  27.  * 'cards'. Only one card is visible at a time,
  28.  * allowing you to flip through the cards.
  29.  *
  30.  * @version     1.11 14 Dec 1995
  31.  * @author     Arthur van Hoff
  32.  */
  33.  
  34. public class CardLayout implements LayoutManager {
  35.     Hashtable tab = new Hashtable();
  36.     int hgap;
  37.     int vgap;
  38.  
  39.     /**
  40.      * Creates a new card layout.
  41.      */
  42.     public CardLayout() {
  43.     this(0, 0);
  44.     }
  45.  
  46.     /**
  47.      * Creates a card layout with the specified gaps.
  48.      * @param hgap the horizontal gap
  49.      * @param vgap the vertical gap
  50.      */
  51.     public CardLayout(int hgap, int vgap) {
  52.     this.hgap = hgap;
  53.     this.vgap = vgap;
  54.     }
  55.  
  56.     /**
  57.      * Adds the specified component with the specified name to the layout.
  58.      * @param name the name of the component
  59.      * @param comp the component to be added
  60.      */
  61.     public void addLayoutComponent(String name, Component comp) {
  62.     if (tab.size() > 0) {
  63.         comp.hide();
  64.     }
  65.     tab.put(name, comp);
  66.     }
  67.  
  68.     /**
  69.      * Removes the specified component from the layout.
  70.      * @param comp the component to be removed
  71.      */
  72.     public void removeLayoutComponent(Component comp) {
  73.     for (Enumeration e = tab.keys() ; e.hasMoreElements() ; ) {
  74.         String key = (String)e.nextElement();
  75.         if (tab.get(key) == comp) {
  76.         tab.remove(key);
  77.         return;
  78.         }
  79.     }
  80.     }
  81.  
  82.     /** 
  83.      * Calculates the preferred size for the specified panel.
  84.      * @param parent the name of the parent container
  85.      * @return the dimensions of this panel. 
  86.      * @see #minimumLayoutSize
  87.      */
  88.     public Dimension preferredLayoutSize(Container parent) {
  89.     Insets insets = parent.insets();
  90.     int ncomponents = parent.countComponents();
  91.     int w = 0;
  92.     int h = 0;
  93.  
  94.     for (int i = 0 ; i < ncomponents ; i++) {
  95.         Component comp = parent.getComponent(i);
  96.         Dimension d = comp.preferredSize();
  97.         if (d.width > w) {
  98.         w = d.width;
  99.         }
  100.         if (d.height > h) {
  101.         h = d.height;
  102.         }
  103.     }
  104.     return new Dimension(insets.left + insets.right + w + hgap*2, 
  105.                  insets.top + insets.bottom + h + vgap*2);
  106.     }
  107.  
  108.     /** 
  109.      * Calculates the minimum size for the specified panel.
  110.      * @param parent the name of the parent container
  111.      * @return the dimensions of this panel. 
  112.      * @see #preferredLayoutSize
  113.      */
  114.     public Dimension minimumLayoutSize(Container parent) {
  115.     Insets insets = parent.insets();
  116.     int ncomponents = parent.countComponents();
  117.     int w = 0;
  118.     int h = 0;
  119.  
  120.     for (int i = 0 ; i < ncomponents ; i++) {
  121.         Component comp = parent.getComponent(i);
  122.         Dimension d = comp.minimumSize();
  123.         if (d.width > w) {
  124.         w = d.width;
  125.         }
  126.         if (d.height > h) {
  127.         h = d.height;
  128.         }
  129.     }
  130.     return new Dimension(insets.left + insets.right + w + hgap*2, 
  131.                  insets.top + insets.bottom + h + vgap*2);
  132.     }
  133.  
  134.     /** 
  135.      * Performs a layout in the specified panel.
  136.      * @param parent the name of the parent container 
  137.      */
  138.     public void layoutContainer(Container parent) {
  139.     synchronized (parent) {
  140.         Insets insets = parent.insets();
  141.         int ncomponents = parent.countComponents();
  142.         for (int i = 0 ; i < ncomponents ; i++) {
  143.         Component comp = parent.getComponent(i);
  144.         if (comp.visible) {
  145.             comp.reshape(hgap + insets.left, vgap + insets.top, 
  146.                  parent.width - (hgap*2 + insets.left + insets.right), 
  147.                  parent.height - (vgap*2 + insets.top + insets.bottom));
  148.         }
  149.         }
  150.     }
  151.     }
  152.  
  153.     /**
  154.      * Make sure that the Container really has a CardLayout installed.
  155.      * Otherwise havoc can ensue!
  156.      */
  157.     void checkLayout(Container parent) {
  158.     if (parent.getLayout() != this) {
  159.         throw new IllegalArgumentException("wrong parent for CardLayout");
  160.     }
  161.     }
  162.  
  163.     /**
  164.      * Flip to the first card.
  165.      * @param parent the name of the parent container
  166.      */
  167.     public void first(Container parent) {
  168.     synchronized (parent) {
  169.         checkLayout(parent);
  170.         int ncomponents = parent.countComponents();
  171.         for (int i = 0 ; i < ncomponents ; i++) {
  172.         Component comp = parent.getComponent(i);
  173.         if (comp.visible) {
  174.             comp.hide();
  175.             comp = parent.getComponent(0);
  176.             comp.show();
  177.             parent.validate();
  178.             return;
  179.         }
  180.         }
  181.     }
  182.     }
  183.  
  184.     /**
  185.      * Flips to the next card of the specified container.
  186.      * @param parent the name of the container
  187.      */
  188.     public void next(Container parent) {
  189.     synchronized (parent) {
  190.         checkLayout(parent);
  191.         int ncomponents = parent.countComponents();
  192.         for (int i = 0 ; i < ncomponents ; i++) {
  193.         Component comp = parent.getComponent(i);
  194.         if (comp.visible) {
  195.             comp.hide();
  196.             comp = parent.getComponent((i + 1 < ncomponents) ? i+1 : 0);
  197.             comp.show();
  198.             parent.validate();
  199.             return;
  200.         }
  201.         }
  202.     }
  203.     }
  204.  
  205.     /**
  206.      * Flips to the previous card of the specified container.
  207.      * @param parent the name of the parent container
  208.      */
  209.     public void previous(Container parent) {
  210.     synchronized (parent) {
  211.         checkLayout(parent);
  212.         int ncomponents = parent.countComponents();
  213.         for (int i = 0 ; i < ncomponents ; i++) {
  214.         Component comp = parent.getComponent(i);
  215.         if (comp.visible) {
  216.             comp.hide();
  217.             comp = parent.getComponent((i > 0) ? i-1 : ncomponents-1);
  218.             comp.show();
  219.             parent.validate();
  220.             return;
  221.         }
  222.         }
  223.     }
  224.     }
  225.  
  226.     /**
  227.      * Flips to the last card of the specified container.
  228.      * @param parent the name of the parent container
  229.      */
  230.     public void last(Container parent) {
  231.     synchronized (parent) {
  232.         checkLayout(parent);
  233.         int ncomponents = parent.countComponents();
  234.         for (int i = 0 ; i < ncomponents ; i++) {
  235.         Component comp = parent.getComponent(i);
  236.         if (comp.visible) {
  237.             comp.hide();
  238.             comp = parent.getComponent(ncomponents - 1);
  239.             comp.show();
  240.             parent.validate();
  241.             return;
  242.         }
  243.         }
  244.     }
  245.     }
  246.  
  247.     /**
  248.      * Flips to the specified component name in the specified container.
  249.      * @param parent the name of the parent container
  250.      * @param name the component name
  251.      */
  252.     public void show(Container parent, String name) {
  253.     synchronized (parent) {
  254.         checkLayout(parent);
  255.         Component next = (Component)tab.get(name);
  256.         if ((next != null) && !next.visible){
  257.         int ncomponents = parent.countComponents();
  258.         for (int i = 0 ; i < ncomponents ; i++) {
  259.             Component comp = parent.getComponent(i);
  260.             if (comp.visible) {
  261.             comp.hide();
  262.             break;
  263.             }
  264.         }
  265.         next.show();
  266.         parent.validate();
  267.         }
  268.     }
  269.     }
  270.     
  271.     /**
  272.      * Returns the String representation of this CardLayout's values.
  273.      */
  274.     public String toString() {
  275.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
  276.     }
  277. }
  278.