home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 4.7 KB | 174 lines |
- /*
- * Copyright (c) 1996 by Jan Andersson, Torpa Konsult AB.
- *
- * Permission to use, copy, and distribute this software for
- * NON-COMMERCIAL purposes and without fee is hereby granted
- * provided that this copyright notice appears in all copies.
- *
- */
- import java.awt.*;
- import java.applet.Applet;
-
- /**
- * A class that implements a simple toolbar panel. Based on the
- * JanneButton class.
- *
- * @version 1.6 96/02/21
- * @author Jan Andersson, Torpa Konsult AB. (janne@torpa.se)
- */
- class JanneToolbar extends Panel {
- /**
- * layout manager
- */
- GridBagLayout gridBag;
- GridBagConstraints gridConstraints;
-
- /**
- * flag to tell if horizontal layout is used
- */
- boolean horizontal;
-
- /**
- * Insets. Specifies the external of each button.
- */
- Insets insets;
-
- /**
- * shadow and border widths used by the buttons
- */
- int shadow = 2;
- int border = 2;
-
- /**
- * flag to tell if string labels are dispayed in the buttons
- */
- boolean showLabel = true;
-
- /**
- * Creates a Toolbar panel with grid layout with the specified
- * rows, columns, horizontal gap, and vertical gap.
- * @param rows the rows; zero means 'any number.'
- * @param cols the columns; zero means 'any number.' Only one of 'rows'
- * and 'cols' can be zero, not both.
- * @param hgap the horizontal gap variable
- * @param vgap the vertical gap variable
- * @param showLabel true if the button labels should be displayed.
- */
- JanneToolbar(boolean horizontal, int gap, boolean showLabel) {
- this.horizontal = horizontal;
- this.showLabel = showLabel;
-
- gridBag = new GridBagLayout();
- setLayout(gridBag);
-
- gridConstraints = new GridBagConstraints();
- if (horizontal) {
- insets = new Insets(2, 2, 2, gap);
- //weightx = ??
- }
- else {
- gridConstraints.gridwidth = GridBagConstraints.REMAINDER;
- insets = new Insets(2, 2, gap, 2);
- }
- gridConstraints.insets = insets;
- gridConstraints.fill = GridBagConstraints.BOTH;
- }
-
- /**
- * Creates a horizontal toolbar panel.
- * Default gap (4) are used. Buttons are displayed without string label.
- */
- JanneToolbar() {
- this(true, 4, false);
- }
-
- /**
- * Create and add button. Created button is an instance of JanneButton.
- * @param image the button image
- * @param label the button label (null if not used)
- */
- public void addButton(Image image, String label) {
- // Add button to panel. Images are not resized.
- JanneButton button = new JanneButton(
- image, label, shadow, border, false, showLabel);
- addButton(button);
- }
-
- /**
- * Create and add button. Created button is an instance of JanneButton.
- * @param image the button image
- * @param label the button label (null if not used)
- * @extraGap extra gap
- */
- public void addButton(Image image, String label, int extraGap) {
- // Add button to panel. Images are not resized.
- JanneButton button = new JanneButton(
- image, label, shadow, border, false, showLabel);
- addButton(button, extraGap);
- }
-
- /**
- * Add button.
- * @param button sub-class of Component.
- */
- public void addButton(Component button) {
- gridBag.setConstraints(button, gridConstraints);
- add(button);
- }
-
- /**
- * Add button.
- * @param button sub-class of Component.
- * @extraGap extra gap
- */
- public void addButton(Component button, int extraGap) {
- // create new insets for extra gap
- Insets tmpInsets;
- if (horizontal)
- tmpInsets = new Insets(insets.top, insets.left + extraGap,
- insets.bottom, insets.right);
- else
- tmpInsets = new Insets(insets.top + extraGap, insets.left,
- insets.bottom, insets.right);
- gridConstraints.insets = tmpInsets;
-
- // Add button to panel.
- addButton(button);
-
- // reset insets
- gridConstraints.insets = insets;
- }
-
- /**
- * Do a (re-)layout on this panel.
- *
- * Called by JanneButton when size is updated. This is a hack to allow
- * the size of the panel to change (when images loaded) and to make sure
- * the toolbar is resized properly.
- */
- public synchronized void layout() {
- // x & y position of toolbar
- int x = 0;
- int y = 0;
-
- // get toolbar preferred size
- Dimension size = preferredSize();
-
- // center toolbar in parent (if parent known)
- Container parent = getParent();
- if (parent != null) {
- Dimension parentSize = parent.size();
- if (size.width < parentSize.width)
- x = parentSize.width/2 - size.width/2;
- if (size.height < parentSize.height)
- y = parentSize.height/2 - size.height/2;
- }
-
- // reshape toolbar to new size and re-layout
- reshape(x, y, size.width, size.height);
- super.layout();
- }
- }
-
-
-