home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 9.7 KB | 301 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.
-
-
- @(#)PjListLayout.java 0.00 7-Feb-96
-
- A PjListLayout that manages the Label and Column pairlayout in a View(eg. PortView).
-
- Authors:
-
- jlee James Lee
-
- Version Ident:
-
- $Header$
-
- History:
-
- 7-Feb-96 jlee Initial creation.
- 14-Feb-96 jlee Added alignment(LEFT, CENTER and RIGHT) and button component.
- 15-Feb-96 jlee Added and used PjFinals class for all size info.
- 21-Feb-96 jlee Added handling of three components(label, button, and column) layout.
- 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;
-
-
- /**
- * PjListLayout is used to layout label and column pair in a panel.
- * <P>
- * This custom layout places the label and/or button(al least one of them)
- * at the top and column just below. The width of the label and column is fixed,
- * which is the same width as PjAdView's. But the height of
- * label plus column changes accordingly as frame changes.
- * <pre>
- * ____________________________________________
- * |Label(optional) | Button(optional)|
- * |-------------------------------------------|
- * | |
- * | |
- * | |
- * | Column |
- * | |
- * | |
- * | |
- * |___________________________________________|
- * </pre>
- *
- * @version 0.00, 2/7/96
- * @author James Lee
- */
- public class PjListLayout implements LayoutManager
- {
-
- // --- Class variables
-
- /**
- * The left alignment variable.
- */
- public static final int LEFT = 0;
-
- /**
- * The right alignment variable.
- */
- public static final int CENTER = 1;
-
- /**
- * The right alignment variable.
- */
- public static final int RIGHT = 2;
-
- // --- Instance variables
-
- private int hgap;
- private int vgap;
- private int align;
-
- //100 is a default value.
- private final Dimension listSize = new Dimension( PjFinals.nPjMinimumPageViewWidth, 100 );
- 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 PjListLayout()
- {
- this(LEFT, 0, 0);
- }
-
- /**
- * Constructs a new Flow Layout with the specified alignment and gap
- * values.
- * @param align the alignment value
- */
- public PjListLayout(int align)
- {
- this(align, 0, 0);
- }
-
- /**
- * Constructs a new Flow Layout with the specified alignment and gap
- * values.
- * @param align the alignment value
- * @param hgap the horizontal gap variable
- * @param vgap the vertical gap variable
- */
- public PjListLayout(int align, int hgap, int vgap)
- {
- this.align = align;
- this.hgap = hgap;
- this.vgap = vgap;
- }
-
-
- // --- 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(listSize);
- 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(listSize);
- 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 maxheight = rec.height - (insets.top + insets.bottom + vgap*2);
-
- int nmembers = target.countComponents();
- int width = maxwidth - listSize.width;
-
- int x = 0;
- int y = 0;
-
- int prefWidth = 0;
- int prefHeight = 0;
-
- for (int i = 0 ; i < nmembers ; i++)
- {
- Component m = target.getComponent(i);
- if ( m.isVisible() )
- {
- switch ( i )
- {
- case 0://Label
- prefWidth = m.preferredSize().width;
- prefHeight = m.preferredSize().height;
- y = insets.top + vgap;
- switch ( align )
- {
- case LEFT:
- x = width / 2;
- break;
- case CENTER:
- x = width / 2 + (listSize.width - prefWidth) / 2;
- break;
- case RIGHT:
- x = width / 2 + listSize.width - prefWidth;
- break;
- default:
- break;
- } //switch (align)
- m.reshape( x, y, prefWidth, prefHeight );
- break;
-
- case 1://Column or Button
- switch( nmembers )
- {
- case 2: //Column
- x = width / 2;
- y = insets.top + 2 * vgap + prefHeight + 2;
- m.reshape( x, y, listSize.width, maxheight - prefHeight - vgap );
- break;
- case 3: //Button
- prefWidth = m.preferredSize().width;
- prefHeight = m.preferredSize().height;
- x = width / 2 + listSize.width - prefWidth;
- y = insets.top + vgap;
- m.reshape( x, y, prefWidth, prefHeight );
- break;
- default:
- break;
- }//switch(nmembers)
- break;
-
- case 2://Column
- x = width / 2;
- y = insets.top + 2 * vgap + prefHeight + 2;
- m.reshape( x, y, listSize.width, maxheight - prefHeight - vgap );
- break;
-
- default:
- break;
-
- }//switch
- }//if
- }//for
-
- }//layoutContainer
-
- /**
- * Returns the String representation of this PjListLayout's values.
- */
- public String toString()
- {
- return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]" +
- "[width=" + listSize.width + ",height=" + listSize.height + "]";
- }
- }
-
-