home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / desvs7nu / src / jannetoolbar.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.7 KB  |  174 lines

  1. /*
  2.  * Copyright (c) 1996 by Jan Andersson, Torpa Konsult AB.
  3.  *
  4.  * Permission to use, copy, and distribute this software for
  5.  * NON-COMMERCIAL purposes and without fee is hereby granted
  6.  * provided that this copyright notice appears in all copies.
  7.  *
  8.  */
  9. import java.awt.*;
  10. import java.applet.Applet;
  11.  
  12. /**
  13.  * A class that implements a simple toolbar panel. Based on the
  14.  * JanneButton class.
  15.  *
  16.  * @version     1.6 96/02/21
  17.  * @author      Jan Andersson, Torpa Konsult AB. (janne@torpa.se)
  18.  */
  19. class JanneToolbar extends Panel {
  20.    /**
  21.     * layout manager
  22.     */
  23.    GridBagLayout gridBag;
  24.    GridBagConstraints gridConstraints;
  25.  
  26.    /**
  27.     * flag to tell if horizontal layout is used
  28.     */
  29.    boolean horizontal;
  30.  
  31.    /**
  32.     * Insets. Specifies the external of each button.
  33.     */
  34.    Insets insets;
  35.  
  36.    /**
  37.     * shadow and border widths used by the buttons
  38.     */
  39.    int shadow = 2;
  40.    int border = 2;
  41.  
  42.    /**
  43.     * flag to tell if string labels are dispayed in the buttons
  44.     */
  45.    boolean showLabel = true;
  46.  
  47.    /**
  48.     * Creates a Toolbar panel with grid layout with the specified
  49.     * rows, columns, horizontal gap, and vertical gap.
  50.     * @param rows the rows; zero means 'any number.'
  51.     * @param cols the columns; zero means 'any number.'  Only one of 'rows'
  52.     * and 'cols' can be zero, not both.
  53.     * @param hgap the horizontal gap variable
  54.     * @param vgap the vertical gap variable
  55.     * @param showLabel true if the button labels should be displayed.
  56.     */
  57.    JanneToolbar(boolean horizontal, int gap, boolean showLabel) {
  58.       this.horizontal = horizontal;
  59.       this.showLabel = showLabel;
  60.  
  61.       gridBag = new GridBagLayout();
  62.       setLayout(gridBag);
  63.  
  64.       gridConstraints = new GridBagConstraints();
  65.       if (horizontal) {
  66.      insets = new Insets(2, 2, 2, gap);
  67.      //weightx = ??
  68.       }
  69.       else {
  70.      gridConstraints.gridwidth = GridBagConstraints.REMAINDER;
  71.      insets = new Insets(2, 2, gap, 2);
  72.       }
  73.       gridConstraints.insets = insets;
  74.       gridConstraints.fill = GridBagConstraints.BOTH;
  75.    }
  76.  
  77.    /**
  78.     * Creates a horizontal toolbar panel.
  79.     * Default gap (4) are used. Buttons are displayed without string label.
  80.     */
  81.    JanneToolbar() {
  82.       this(true, 4, false);
  83.    }
  84.  
  85.    /**
  86.     * Create and add button. Created button is an instance of JanneButton.
  87.     * @param image the button image
  88.     * @param label the button label (null if not used)
  89.     */
  90.    public void addButton(Image image, String label) {
  91.       // Add button to panel. Images are not resized.
  92.       JanneButton button = new JanneButton(
  93.      image, label, shadow, border, false, showLabel);
  94.       addButton(button);
  95.    }
  96.  
  97.    /**
  98.     * Create and add button. Created button is an instance of JanneButton.
  99.     * @param image the button image
  100.     * @param label the button label (null if not used)
  101.     * @extraGap extra gap
  102.     */
  103.    public void addButton(Image image, String label, int extraGap) {
  104.      // Add button to panel. Images are not resized.
  105.       JanneButton button = new JanneButton(
  106.      image, label, shadow, border, false, showLabel);
  107.       addButton(button, extraGap);
  108.    }
  109.  
  110.    /**
  111.     * Add button. 
  112.     * @param button sub-class of Component.
  113.     */
  114.    public void addButton(Component button) {
  115.       gridBag.setConstraints(button, gridConstraints);
  116.       add(button);
  117.    }
  118.    
  119.    /**
  120.     * Add button. 
  121.     * @param button sub-class of Component.
  122.     * @extraGap extra gap
  123.     */
  124.    public void addButton(Component button, int extraGap) {
  125.       // create new insets for extra gap
  126.       Insets tmpInsets;
  127.       if (horizontal)
  128.      tmpInsets = new Insets(insets.top, insets.left + extraGap,
  129.                 insets.bottom, insets.right);
  130.       else
  131.      tmpInsets = new Insets(insets.top + extraGap, insets.left,
  132.                 insets.bottom, insets.right);
  133.       gridConstraints.insets = tmpInsets;
  134.  
  135.       // Add button to panel.
  136.       addButton(button);
  137.  
  138.       // reset insets
  139.       gridConstraints.insets = insets;
  140.    }
  141.    
  142.    /** 
  143.     * Do a (re-)layout on this panel.
  144.     *
  145.     * Called by JanneButton when size is updated. This is a hack to allow
  146.     * the size of the panel to change (when images loaded) and to make sure
  147.     * the toolbar is resized properly.
  148.     */
  149.    public synchronized void layout() {
  150.       // x & y position of toolbar
  151.       int x = 0;
  152.       int y = 0;
  153.  
  154.       // get toolbar preferred size
  155.       Dimension size = preferredSize();
  156.  
  157.       // center toolbar in parent (if parent known)
  158.       Container parent = getParent();
  159.       if (parent != null) {
  160.      Dimension parentSize = parent.size();
  161.      if (size.width < parentSize.width)
  162.         x = parentSize.width/2 - size.width/2;
  163.      if (size.height < parentSize.height)
  164.         y = parentSize.height/2 - size.height/2;
  165.       }
  166.       
  167.       // reshape toolbar to new size and re-layout 
  168.       reshape(x, y, size.width, size.height);
  169.       super.layout();
  170.    }
  171. }
  172.  
  173.  
  174.