pagelayout
Class PageLayout

java.lang.Object
  extended by pagelayout.PageLayout
All Implemented Interfaces:
java.awt.LayoutManager, java.awt.LayoutManager2

public class PageLayout
extends java.lang.Object
implements java.awt.LayoutManager, java.awt.LayoutManager2

PageLayout is a layout manager which can be used by placing the components in rows, columns, or grids of cells with specified horizontal and vertical alignment (akin to text on a page of a document, and hence the appellation). At its simplest, a cell, which is just a rectangle, may contain a single component or a gap. However, a cell may recursively contain other cells which are themselves rows, columns or grids of cells containing components or gaps. As a result, components may be arranged in relatively complex layouts with comparatively little programming effort.

In principle, it is possible to duplicate the functionality of PageLayout by using objects of the class javax.swing.JPanel together with one of the many other existing layout managers, but only at the cost of substantially increased complexity. Conversely, the functionality of the various layout managers that are a part of the standard Java distribution can be very simply duplicated by using just a single layout manager, the PageLayout.

Here is an example of the use of PageLayout for managing the layout of the dialog box shown below.





                // Main frame and its container
                JFrame frame=new JFrame();
                Container container=frame.getContentPane();

                // Create the components
                JLabel findWhat=new JLabel("Find What:");
                JTextField textInput=new JTextField();
                JButton find=new JButton("Find");
                JCheckBox matchCase=new JCheckBox("Match Case");
                JCheckBox wrapAround= new JCheckBox("Wrap Around");
                JButton cancel=new JButton("Cancel");
                JCheckBox wholeWords=new JCheckBox("Whole Words");
                JCheckBox searchBackwards=new JCheckBox("Search Backwards");

                // First column
                Column col1=new Column(findWhat);

                // Second column
                Column col2=new Column(
                        new Row(textInput),
                        new Row(matchCase,wrapAround),
                        new Row(wholeWords,searchBackwards)
                        );

                // Third column
                Column col3=new Column(find,cancel);

                // Create page from columns
                Row row=new Row(col1,col2,col3);
                // Add size constraints
                row.linkHeight(find,new Component[]{textInput},
                        new double[]{1});
                row.linkWidth(cancel,new Component[]{find},
                        new double[]{1});
                row.linkWidth(wholeWords,new Component[]{matchCase},
                        new double[]{1});

                // Construct the layout
                PageLayout pageLayout=row.createLayout(container);

                frame.pack();
                frame.setSize(frame.getPreferredSize());
                frame.setResizable(false);
                frame.show();

        

Note that the constructor for PageLout is not called directly, but by calling the createLayout method of the class Cell. Finally the methods linkHeight and linkHeight are used to impose some width and height constraints.

See Also:
Cell, Column, Row, CellGrid

Nested Class Summary
static class PageLayout.ContainerHeightLink
           
static class PageLayout.ContainerSizeLink
           
static class PageLayout.ContainerWidthLink
           
 
Constructor Summary
protected PageLayout(java.awt.Container parent, Cell topLevelCell)
          The constructor for the Layout Manager.
 
Method Summary
 void addLayoutComponent(java.awt.Component component, java.lang.Object constraint)
          This method of the LayoutManager2 interface is not implemented.
 void addLayoutComponent(java.lang.String name, java.awt.Component component)
          This method of the LayoutManager interface is not implemented.
static PageLayout.ContainerSizeLink createLink(java.awt.Component[] components, double[][] fraction, boolean isX)
           
 java.awt.Insets getInset()
           
 float getLayoutAlignmentX(java.awt.Container target)
          Returns the 0.f, which specifies that the component would like to be aligned along the x-axis at the origin.
 float getLayoutAlignmentY(java.awt.Container target)
          Returns the 0.f, which specifies that the component would like to be aligned along the y-axis at the origin.
 Cell getTopLevelCell()
          Returns the top level cell, which is the cell whose createLayout method was used to create this layout manager.
 void invalidateLayout(java.awt.Container target)
          Invalidates the layout.
 void layoutContainer(java.awt.Container parent)
          This method of the LayoutManager interface is called by the container whenever it needs to layout the components within it.
 void linkToContainerHeight(java.awt.Component[] components, double[][] affineConstants)
          This method can be used to impose constraints on the heights of the components within the container as linear functions of the height of the container itself.
 void linkToContainerWidth(java.awt.Component[] components, double[][] affineConstants)
          This method can be used to impose constraints on the widths of the components within the container as linear functions of the width of the container itself.
 java.awt.Dimension maximumLayoutSize(java.awt.Container parent)
          Returns the maximum layout size of the container.
 java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
          Returns the minimum layout size of the container.
 java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
          Returns the preferred layout size of the container.
 void removeLayoutComponent(java.awt.Component component)
          This method of the LayoutManager is not implemented.
 void setContainerGaps(int horizontalGap, int verticalGap)
          Sets the so called container gaps so that the components are laid out, if possible, in an area that leaves out empty rectangular areas of specified size on the edges of the container.
 void setDimensions(java.awt.Container parent)
           
 void setLinks(java.util.Vector<PageLayout.ContainerSizeLink> v)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PageLayout

protected PageLayout(java.awt.Container parent,
                     Cell topLevelCell)
The constructor for the Layout Manager. Note that the createLayout method of the top level cell should be called to instantiate an object of this class.

Parameters:
parent - the container whose layout is to be managed by the PageLayout object.
topLevelCell - the top-level cell which may be any of the concrete sub-classes of the abstract class Cell.
Method Detail

setDimensions

public void setDimensions(java.awt.Container parent)

setContainerGaps

public void setContainerGaps(int horizontalGap,
                             int verticalGap)
Sets the so called container gaps so that the components are laid out, if possible, in an area that leaves out empty rectangular areas of specified size on the edges of the container.

Parameters:
horizontalGap - the desired width of the empty strips inside the vertical edges of the container.
verticalGap - the desired height of the empty strips inside the horizontal edges of the container.

getTopLevelCell

public Cell getTopLevelCell()
Returns the top level cell, which is the cell whose createLayout method was used to create this layout manager.

Returns:
The top level cell as defined above.

addLayoutComponent

public void addLayoutComponent(java.lang.String name,
                               java.awt.Component component)
This method of the LayoutManager interface is not implemented. To replace an existing component, use the replaceCell method of the class Cell, which can also be used to add a new component to an existing Row or Column object.

Specified by:
addLayoutComponent in interface java.awt.LayoutManager

addLayoutComponent

public void addLayoutComponent(java.awt.Component component,
                               java.lang.Object constraint)
This method of the LayoutManager2 interface is not implemented. To replace an existing component, use the replaceCell method of the class Cell, which can also be used to add a new component to an existing Row or Column object.

Specified by:
addLayoutComponent in interface java.awt.LayoutManager2

layoutContainer

public void layoutContainer(java.awt.Container parent)
This method of the LayoutManager interface is called by the container whenever it needs to layout the components within it. The PageLayout calls the layout method of the top level cell to actually perform the layout.

Specified by:
layoutContainer in interface java.awt.LayoutManager
Parameters:
parent - the container within which the components need to be laid out.

minimumLayoutSize

public java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
Returns the minimum layout size of the container.

Specified by:
minimumLayoutSize in interface java.awt.LayoutManager
Parameters:
parent - the container whose minimum size is required.
Returns:
The minimum layout size.

maximumLayoutSize

public java.awt.Dimension maximumLayoutSize(java.awt.Container parent)
Returns the maximum layout size of the container.

Specified by:
maximumLayoutSize in interface java.awt.LayoutManager2
Parameters:
parent - the container whose maximum size is required.
Returns:
The maximum layout size.

preferredLayoutSize

public java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
Returns the preferred layout size of the container.

Specified by:
preferredLayoutSize in interface java.awt.LayoutManager
Parameters:
parent - the container whose preferred size is required.
Returns:
The preferred layout size.

removeLayoutComponent

public void removeLayoutComponent(java.awt.Component component)
This method of the LayoutManager is not implemented. To remove a component, use the removeComponent method of the top level cell, which can be retrieved by using the getTopLevelCell method of this class.

Specified by:
removeLayoutComponent in interface java.awt.LayoutManager

getLayoutAlignmentX

public float getLayoutAlignmentX(java.awt.Container target)
Returns the 0.f, which specifies that the component would like to be aligned along the x-axis at the origin.

Specified by:
getLayoutAlignmentX in interface java.awt.LayoutManager2

getLayoutAlignmentY

public float getLayoutAlignmentY(java.awt.Container target)
Returns the 0.f, which specifies that the component would like to be aligned along the y-axis at the origin.

Specified by:
getLayoutAlignmentY in interface java.awt.LayoutManager2

invalidateLayout

public void invalidateLayout(java.awt.Container target)
Invalidates the layout.

Specified by:
invalidateLayout in interface java.awt.LayoutManager2

linkToContainerWidth

public void linkToContainerWidth(java.awt.Component[] components,
                                 double[][] affineConstants)
This method can be used to impose constraints on the widths of the components within the container as linear functions of the width of the container itself. In particular, in terms of the current width W of the container, the the width of the component components[i] is reset to be W*affineConstants[i][0]+affineConstants[i][1] everytime the container is resized. Thus, for example, with affineConstants[i][0]=0.5 and affineConstants[i][1]=-10, the width of the component is restricted to be ten pixels less than half the width of the container.

Parameters:
components - the array of components whose widths are to be linked with the width of the container.
affineConstants - the array of constraint parameters as defined above.

linkToContainerHeight

public void linkToContainerHeight(java.awt.Component[] components,
                                  double[][] affineConstants)
This method can be used to impose constraints on the heights of the components within the container as linear functions of the height of the container itself. In particular, in terms of the current height H of the container, the the height of the component components[i] is reset to be H*affineConstants[i][0]+affineConstants[i][1] everytime the container is resized. Thus, for example, with affineConstants[i][0]=0.5 and affineConstants[i][1]=-10, the height of the component is restricted to be ten pixels less than half the height of the container.

Parameters:
components - the array of components whose heights are to be linked with the height of the container.
affineConstants - the array of constraint parameters as defined above.

setLinks

public void setLinks(java.util.Vector<PageLayout.ContainerSizeLink> v)

createLink

public static PageLayout.ContainerSizeLink createLink(java.awt.Component[] components,
                                                      double[][] fraction,
                                                      boolean isX)

getInset

public java.awt.Insets getInset()