FreeHEP API
Version current

org.freehep.swing.layout
Class ConstrainedGridLayout

java.lang.Object
  extended byorg.freehep.swing.layout.ConstrainedGridLayout
All Implemented Interfaces:
LayoutManager, LayoutManager2

public class ConstrainedGridLayout
extends Object
implements LayoutManager2

The ConstrainedGridLayout layout manager is based on the GraphPaperLayout layout manager written by Michael Martak. This layout manager divides the container into a set of equally sized cells arranged in a grid which has nw cells in the horizontal direction and nh cells in the vertical direction. Components can occupy an rectangular subset of these cells; the component's position and size are based on given rectangular constraints. This extends the functionality of the GraphPaperLayout manager by also constraining the overall aspect ratio of the container. It will also force a containing window to resize if the desired size of the container isn't a size which is allowed by the contraints. A byproduct of this is that the preferred sizes of components controlled by this layout manager will be reset to the largest allowed size which is smaller than the current size. Components can overlap making this layout manager a good choice for a JLayeredPane. To allow the standard moveToFront and moveToBack methods to work as expected, the constraints of a removed component are retained so that if it is later added again without contraints the layout manager will still arrange the components reasonably. (The standard moveToFront implementation, removes a component and re-adds it without taking into account the possible constraints.)

Version:
$Id: ConstrainedGridLayout.java,v 1.2 2001/01/10 13:27:35 loomis Exp $
Author:
Charles A. Loomis, Jr.
Source Code:
ConstrainedGridLayout.java

Field Summary
static String ASPECT_RATIO_ERROR
           
static String CONSTRAINED_GRID_LAYOUT_CONSTRAINT
           
static String CONSTRAINT_ERROR
           
static String GRID_SIZE_ERROR
           
static int MINIMUM_LAYOUT
           
static int PREFERRED_LAYOUT
           
 
Constructor Summary
ConstrainedGridLayout(Dimension gridSize, Dimension aspectRatio)
          Creates a ConstrainedGridLayout with the given grid size and aspect ratio.
 
Method Summary
 void addLayoutComponent(Component comp, Object constraints)
          Adds the specified component to the layout, using the specified constraint object.
 void addLayoutComponent(String name, Component comp)
          Adds the specified component with the specified name to the layout.
 boolean adjustSize(Container parent, int sizeIncrement)
          Change the size of the component by the given increment in the size.
 boolean adjustSize(Dimension trialSize)
          Adjust the given size of the parent (minus the insets) to an appropriate size.
 Dimension getAspectRatio()
          Return a new Dimension which is a copy of the current aspect ratio.
 Rectangle getConstrainedBounds(Container parent, Rectangle constrainedBounds, Rectangle inputBounds)
          Return a rectangle containing the constrained bounds for the given input bounds.
 Rectangle getConstraints(Component comp)
          Get the constraints being used for the given component.
 Rectangle getConstraints(Container parent, Rectangle constraints, Rectangle inputBounds)
          Return a rectangle containing the appropriate constraint rectangle for the given input bounds.
 Dimension getGridSize()
          Return a new Dimension which is a copy of the current grid size.
 float getLayoutAlignmentX(Container target)
          Returns the alignment along the x axis.
 float getLayoutAlignmentY(Container target)
          Returns the alignment along the y axis.
protected  Dimension getLayoutSize(Container parent, int selectionFlag)
          Calculate the largest minimum or preferred cell size.
 void invalidateLayout(Container target)
          Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
 void layoutContainer(Container parent)
          Lays out the container in the specified container.
 Dimension maximumLayoutSize(Container target)
          Returns the maximum size of this component.
 Dimension minimumLayoutSize(Container parent)
          Calculates the minimum size dimensions for the specified panel given the components in the specified parent container.
 Dimension preferredLayoutSize(Container parent)
          Calculates the preferred size dimensions for the specified panel given the components in the specified parent container.
 void removeLayoutComponent(Component comp)
          Removes the specified component from the layout.
 void setAspectRatio(Dimension aspectRatio)
          Set the current aspect ratio to the given Dimension.
 void setConstraints(Component comp, Rectangle constraints)
          Set (or reset) the constraints for the given component.
 void setGridSize(Dimension gridSize)
          Set the current grid size to the given Dimension.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GRID_SIZE_ERROR

public static final String GRID_SIZE_ERROR
See Also:
Constant Field Values

ASPECT_RATIO_ERROR

public static final String ASPECT_RATIO_ERROR
See Also:
Constant Field Values

CONSTRAINT_ERROR

public static final String CONSTRAINT_ERROR
See Also:
Constant Field Values

CONSTRAINED_GRID_LAYOUT_CONSTRAINT

public static final String CONSTRAINED_GRID_LAYOUT_CONSTRAINT
See Also:
Constant Field Values

MINIMUM_LAYOUT

public static final int MINIMUM_LAYOUT
See Also:
Constant Field Values

PREFERRED_LAYOUT

public static final int PREFERRED_LAYOUT
See Also:
Constant Field Values
Constructor Detail

ConstrainedGridLayout

public ConstrainedGridLayout(Dimension gridSize,
                             Dimension aspectRatio)
Creates a ConstrainedGridLayout with the given grid size and aspect ratio.

Parameters:
gridSize - size of grid in logical units (width x height)
aspectRatio - aspect ratio of container (width x height)
Method Detail

getGridSize

public Dimension getGridSize()
Return a new Dimension which is a copy of the current grid size.

Returns:
current grid size

setGridSize

public void setGridSize(Dimension gridSize)
Set the current grid size to the given Dimension. The grid size must have horizontal and vertical components which are positive.

Parameters:
gridSize - new grid size

getAspectRatio

public Dimension getAspectRatio()
Return a new Dimension which is a copy of the current aspect ratio.

Returns:
current aspect ratio

setAspectRatio

public void setAspectRatio(Dimension aspectRatio)
Set the current aspect ratio to the given Dimension. The aspect ratio must have horizontal and vertical components which are positive.

Parameters:
aspectRatio - new aspect ratio for the container

getConstraints

public Rectangle getConstraints(Component comp)
Get the constraints being used for the given component.

Parameters:
comp - the component to lookup
Returns:
Rectangle describing the position and size of the given component

setConstraints

public void setConstraints(Component comp,
                           Rectangle constraints)
Set (or reset) the constraints for the given component.

Parameters:
comp - the component to constrain
constraints - Rectangle describing the position and size of the component

addLayoutComponent

public void addLayoutComponent(String name,
                               Component comp)
Adds the specified component with the specified name to the layout. ConstrainedGridLayout will arrange this component normally if a constraint has been set previously or is set before the container is next validated. If not, then this component will be ignored by this layout manager. The component name is always ignored by this layout manager.

Specified by:
addLayoutComponent in interface LayoutManager
Parameters:
name - name of component (ignored)
comp - component to add to the layout

removeLayoutComponent

public void removeLayoutComponent(Component comp)
Removes the specified component from the layout. This method actually does nothing since the component list is obtained from the container when it is laid-out. The constraint is still stored in case the component is later added again to the container without the constraint being specified. (Happens when the standard implementation of moveToFront in a JLayeredPane is called.)

Specified by:
removeLayoutComponent in interface LayoutManager
Parameters:
comp - the component to be removed

preferredLayoutSize

public Dimension preferredLayoutSize(Container parent)
Calculates the preferred size dimensions for the specified panel given the components in the specified parent container.

Specified by:
preferredLayoutSize in interface LayoutManager
Parameters:
parent - the component to be laid out
See Also:
minimumLayoutSize(java.awt.Container)

minimumLayoutSize

public Dimension minimumLayoutSize(Container parent)
Calculates the minimum size dimensions for the specified panel given the components in the specified parent container.

Specified by:
minimumLayoutSize in interface LayoutManager
Parameters:
parent - the component to be laid out
See Also:
preferredLayoutSize(java.awt.Container)

getLayoutSize

protected Dimension getLayoutSize(Container parent,
                                  int selectionFlag)
Calculate the largest minimum or preferred cell size. For the minimum or preferred cell sizes: The components' minimum or preferred sizes are obtained and then divided by the number of rows and columns that the component spans. The largest cell size is returned.

Parameters:
parent - the container in which to do the layout.
selectionFlag - either MINIMUM_LAYOUT or PREFERRED_LAYOUT.
Returns:
the appropriate cell size

adjustSize

public boolean adjustSize(Dimension trialSize)
Adjust the given size of the parent (minus the insets) to an appropriate size. This being the largest size which fits inside of the original size and is a multiple of the aspect ratio and of the grid size. Note that the input Dimension is modified and returned rather than creating a new object.

Parameters:
trialSize - parent's size minus insets to adjust; trialSize is overwritten with the adjusted size of the component
Returns:
boolean flag indicating whether the size was actually changed

adjustSize

public boolean adjustSize(Container parent,
                          int sizeIncrement)
Change the size of the component by the given increment in the size. Positive increments enlarge the component; negative increments reduce the component.

Parameters:
parent - the component controlled by this layout manager
sizeIncrement - number of increments by which to increase the size
Returns:
flag indicating whether the size actually changed

layoutContainer

public void layoutContainer(Container parent)
Lays out the container in the specified container.

Specified by:
layoutContainer in interface LayoutManager
Parameters:
parent - the component which needs to be laid out

getConstrainedBounds

public Rectangle getConstrainedBounds(Container parent,
                                      Rectangle constrainedBounds,
                                      Rectangle inputBounds)
Return a rectangle containing the constrained bounds for the given input bounds. This is used to constrain the size of a component to this layout manager's grid.

Parameters:
parent - the Container which contains this component
constrainedBounds - the rectangle which will be overwritten with the result (if null, new Rectangle is created)
inputBounds - the input bounds to constrain to this layout manager's grid
Returns:
the constrained rectangle

getConstraints

public Rectangle getConstraints(Container parent,
                                Rectangle constraints,
                                Rectangle inputBounds)
Return a rectangle containing the appropriate constraint rectangle for the given input bounds. This constrains the input bounds to this layout manager's grid and returns the constraint rectangle in grid coordinates.

Parameters:
parent - the Container which contains this component
constraints - the rectangle which will be overwritten with the result (if null, new Rectangle is created)
inputBounds - the input bounds to constrain to this layout manager's grid
Returns:
the constraint rectangle

addLayoutComponent

public void addLayoutComponent(Component comp,
                               Object constraints)
Adds the specified component to the layout, using the specified constraint object.

Specified by:
addLayoutComponent in interface LayoutManager2
Parameters:
comp - the component to be added
constraints - where/how the component is added to the layout; this must be a Rectangle

maximumLayoutSize

public Dimension maximumLayoutSize(Container target)
Returns the maximum size of this component. This just returns the largest size possible.

Specified by:
maximumLayoutSize in interface LayoutManager2
See Also:
Component.getMinimumSize(), Component.getPreferredSize(), LayoutManager

getLayoutAlignmentX

public float getLayoutAlignmentX(Container target)
Returns the alignment along the x axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc. This just returns the centering alignment (0.5).

Specified by:
getLayoutAlignmentX in interface LayoutManager2

getLayoutAlignmentY

public float getLayoutAlignmentY(Container target)
Returns the alignment along the y axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc. This just returns the centering alignment.

Specified by:
getLayoutAlignmentY in interface LayoutManager2

invalidateLayout

public void invalidateLayout(Container target)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.

Specified by:
invalidateLayout in interface LayoutManager2

FreeHEP API
Version current

Copyright © 2000-2004 FreeHEP, All Rights Reserved.