home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 7.1 KB | 237 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.
-
-
- @(#)PjButtonLayout.java 0.00 8-Feb-96
-
- A PjButtonLayout that manages the eaual size buttons layout.
-
- Authors:
-
- jlee James Lee
-
- Version Ident:
-
- $Header$
-
- History:
-
- 8-Feb-96 jlee Initial creation.
- 15-Feb-96 jlee Added and used PjFinals class for all size info.
- 25-Mar-96 jlee Modified layoutCOntainer so that it dosn't repeat the same layout change.
- ---------------------------------------------------------------------------*/
-
- package pj.awt;
-
- import pj.awt.PjFinals;
-
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.LayoutManager;
- import java.awt.Rectangle;
-
- /**
- * PjButtonLayout is used to layout equal size buttons in a panel.
- * <P>
- * This custom layout places the equal size buttons horizontally,
- * regardless of the length of caption of individual button.
- * <pre>
- * ____________________________________________
- * | Button1 | Button2 |
- * ---------------------------------------------
- * </pre>
- * @version 0.00, 8/2/96
- * @author James Lee
- */
- public class PjButtonLayout implements LayoutManager
- {
-
- // Instance variables
-
- //widthFixed: wether or not the width of buttons are bound.
- private boolean bWidthFixed = true;
-
- private int hgap;
- private int vgap;
-
- private final Dimension dimButtonSize = new Dimension( PjFinals.nPjMinimumPageViewWidth, PjFinals.nPjButtonHeight );
- private Rectangle recLast = new Rectangle(0, 0, 0, 0);
-
-
- // --- Public constructors
- /**
- * Constructs a new Flow Layout with a centered alignment.
- * Default value for hgap and vgap is 0.
- */
- public PjButtonLayout()
- {
- this(0, 0);
- }
-
- /**
- * Constructs a new Flow Layout with the specified alignment and gap
- * values.
- * @param fixed the constraint flag for button width
- */
- public PjButtonLayout(boolean fixed)
- {
- this(0, 0);
- this.bWidthFixed = fixed;
- }
-
- /**
- * Constructs a new Flow Layout with the specified alignment and gap
- * values.
- * @param hgap the horizontal gap variable
- * @param vgap the vertical gap variable
- */
- public PjButtonLayout(int hgap, int vgap)
- {
- this.hgap = hgap;
- this.vgap = vgap;
- }
-
- /**
- * Constructs a new Flow Layout with the specified alignment and gap
- * values.
- * @param hgap the horizontal gap variable
- * @param vgap the vertical gap variable
- * @param fixed the constraint flag for button width
- */
- public PjButtonLayout(int hgap, int vgap, boolean fixed)
- {
- this.hgap = hgap;
- this.vgap = vgap;
- this.bWidthFixed = fixed;
- }
-
- // --- Public operations
- /**
- * Adds the specified component to the layout. Not used by this class.
- * @param name the name of the component
- * @param comp the the component to be added
- */
- public void addLayoutComponent(String name, Component comp)
- {
- }
-
- /**
- * Removes the specified component from the layout. Not used by
- * this class.
- * @param comp the component to remove
- */
- public void removeLayoutComponent(Component comp)
- {
- }
-
- /**
- * Returns the preferred dimensions for this layout given the components
- * in the specified target container.
- * @param target the component which needs to be laid out
- * @see Container
- * @see #minimumLayoutSize
- */
- public Dimension preferredLayoutSize(Container target)
- {
- Dimension dim = new Dimension(dimButtonSize.width + hgap, dimButtonSize.height);
- int nmembers = target.countComponents();
-
- Insets insets = target.insets();
- dim.width += insets.left + insets.right + hgap*2;
- dim.height += insets.top + insets.bottom + vgap*2;
-
- return dim;
- }
-
- /**
- * Returns the minimum dimensions needed to layout the components
- * contained in the specified target container.
- * @param target the component which needs to be laid out
- * @see #preferredLayoutSize
- */
- public Dimension minimumLayoutSize(Container target)
- {
- Dimension dim = new Dimension(dimButtonSize.width + hgap, dimButtonSize.height);
-
- Insets insets = target.insets();
- dim.width += insets.left + insets.right + hgap*2;
- dim.height += insets.top + insets.bottom + vgap*2;
-
- return dim;
- }
-
-
- /**
- * Lays out the container. This method will actually reshape the
- * components in the target in order to satisfy the constraints of
- * the PjAdViewLayout object.
- * @param target the specified component being laid out.
- * @see Container
- */
- public void layoutContainer(Container target)
- {
- Insets insets = target.insets();
- Rectangle rec = target.bounds();
-
- if ( target.isValid() &&
- rec.x == recLast.x && rec.y == recLast.y &&
- rec.width == recLast.width && rec.height == recLast.height )
-
- return;
- else
- recLast = rec;
-
- int maxwidth = rec.width - (insets.left + insets.right + hgap*2);
- int nmembers = target.countComponents();
-
- int width;
- int btnWidth;
-
- if ( bWidthFixed )
- {
- width = maxwidth - dimButtonSize.width;
- btnWidth = ( dimButtonSize.width - (nmembers - 1) * hgap ) / nmembers;
- }
- else
- {
- width = hgap*2;
- btnWidth = ( maxwidth - (nmembers - 1) * hgap ) / nmembers;
- }
-
- int x = width / 2;
- int y = (rec.height - dimButtonSize.height - vgap*2) / 2;
-
- for (int i = 0 ; i < nmembers ; i++)
- {
- Component m = target.getComponent(i);
-
- if ( m.isVisible() )
- m.reshape( x, y, btnWidth, PjFinals.nPjButtonHeight );
-
- x += btnWidth + hgap;
- }
- }
-
- /**
- * Returns the String representation of this PjButtonLayout's values.
- */
- public String toString()
- {
- return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]" +
- "[width=" + dimButtonSize.width + ",height=" + dimButtonSize.height + "]";
- }
- }
-