uic.layout
Class NaturalLayout

java.lang.Object
  extended byuic.layout.NaturalLayout
All Implemented Interfaces:
LayoutManager, LayoutManager2, Serializable

public class NaturalLayout
extends Object
implements LayoutManager2, Serializable

NaturalLayout is a layout manager that arranges components in rows and columns like a spreadsheet. NaturalLayout allows each row or column to be a different size. A row or column can be given an absolute size in pixels, or it can grow and shrink according to the widgets that are contained.

Using spreadsheet terminology, a cell is the intersection of a row and column. Cells have finite, non-negative sizes measured in pixels. The dimensions of a cell depend solely upon the dimensions of its row and column.

A component occupies a rectangular group of one or more cells. If the component occupies more than one cell, the component is resized to fit perfectly in the rectangular region of cells. If the component occupies a single cell, it can be aligned in four ways within that cell.

A single cell component can be stretched horizontally to fit the cell (full justification), or it can be placed in the center of the cell. The component could also be left justified or right justified. Similarly, the component can be full, center, top, or bottom justified in the vertical.

Since:
1.1
See Also:
Constraints, Serialized Form

Field Summary
static int ALIGN_BOTTOM
           
static int ALIGN_CENTER
           
static int ALIGN_FULL
           
static int ALIGN_LEFT
           
static int ALIGN_RIGHT
           
static int ALIGN_TOP
           
protected  int[] columnOffset
          Offsets of columns in pixels.
protected  int[] columnSize
          Widths of columns in pixels
protected  double[] columnSpec
          Widths of columns expressed in absolute and relative terms
protected static double[][] DEFAULT_SIZE
          Default row/column size
protected  boolean dirty
          Indicates whether or not the size of the cells are known for the last known size of the container.
static double FILL
           
protected  Vector list
          List of components and their sizes
protected  int margin
          Margin/spacing
protected  Dimension maximumSize
           
static double MINIMUM
           
protected  Dimension minimumSize
           
protected  int oldHeight
          Previous known height of the container
protected  int oldWidth
          Previous known width of the container
static double PREFERRED
           
protected  Dimension preferredSize
           
protected  int[] rowOffset
          Offsets of rows in pixels.
protected  int[] rowSize
          Heights of rows in pixels
protected  double[] rowSpec
          Heights of rows expressed in absolute and relative terms
static int SIZE_POLICY_EXPANDING
           
static int SIZE_POLICY_FIXED
           
static int SIZE_POLICY_IGNORED
           
static int SIZE_POLICY_MAXIMUM
           
static int SIZE_POLICY_MINIMUM
           
static int SIZE_POLICY_MINIMUM_EXPANDING
           
static int SIZE_POLICY_PREFERRED
           
protected  int spacing
          Margin/spacing
 
Constructor Summary
NaturalLayout()
          Constructs an instance of NaturalLayout.
NaturalLayout(double[][] size)
          Constructs an instance of NaturalLayout.
NaturalLayout(int columns, int rows)
          Constructs an instance of NaturalLayout with a specified row/column size.
 
Method Summary
 void addLayoutComponent(Component component, Object constraint)
          Adds the specified component with the specified name to the layout.
 void addLayoutComponent(String name, Component component)
          Adds the specified component with the specified name to the layout.
protected  void calculateSize(Container container)
          Calculates the sizes of the rows and columns based on the absolute and relative sizes specified in rowSpec and columnSpec and the size of the container.
 void deleteColumn(int i)
          Deletes a column in this layout.
 void deleteRow(int i)
          Deletes a row in this layout.
 void drawGrid(Container container, Graphics g)
          Draws a grid on the given container.
protected  double ensureCorrectSpecSize(double size)
          Checks if size of a row/col is correct.
 double getColumn(int i)
          Gets the width of a single column in this layout.
protected  Entry getEntryContaining(Component c)
          Return the entry that contains argument component.
 float getLayoutAlignmentX(Container parent)
          Returns the alignment along the x axis.
 float getLayoutAlignmentY(Container parent)
          Returns the alignment along the y axis.
 int getMargin()
           
 int getNumColumns()
          Gets the number of columns in this layout.
 int getNumRows()
          Gets the number of rows in this layout.
 double getRow(int i)
          Gets the sizes of a row in this layout.
 int getSpacing()
           
 void insertColumn(int i)
          Convinience function for the above one.
 void insertColumn(int i, double size)
          Inserts a column in this layout.
 void insertRow(int i)
          Convinience function for the above one.
 void insertRow(int i, double size)
          Inserts a row in this layout.
 void invalidateLayout(Container target)
          Invalidates the layout.
 void layoutContainer(Container container)
          To lay out the specified container using this layout.
protected  void layoutIsChanged()
          Remove all cached data.
protected static int max2(int a, int b)
           
 Dimension maximumLayoutSize(Container component)
          Returns the maximum dimensions for this layout given the components in the specified target container.
 Dimension minimumLayoutSize(Container container)
          Determines the minimum size of the container argument using this layout.
protected  void placeWidget(Sizes minimumSizes, Sizes preferredSizes, Sizes maximumSizes, Entry entry)
          Helper method for calculateSize
 Dimension preferredLayoutSize(Container container)
          Determines the preferred size of the container argument using this layout.
 void removeLayoutComponent(Component component)
          Removes the specified component from the layout.
 void setColumn(int i, double size)
          Adjusts the width of a single column in this layout.
 void setConstraints(Component component, Constraints constraint)
          Sets the constraints of a given component.
 void setMargin(int margin)
          Set margin.
 void setRow(int i, double size)
          Adjusts the height of a single row in this layout.
 void setSpacing(int spacing)
          Set spacing.
 String toString()
          Converts this NaturalLayout to a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ALIGN_LEFT

public static final int ALIGN_LEFT
See Also:
Constant Field Values

ALIGN_TOP

public static final int ALIGN_TOP
See Also:
Constant Field Values

ALIGN_CENTER

public static final int ALIGN_CENTER
See Also:
Constant Field Values

ALIGN_FULL

public static final int ALIGN_FULL
See Also:
Constant Field Values

ALIGN_BOTTOM

public static final int ALIGN_BOTTOM
See Also:
Constant Field Values

ALIGN_RIGHT

public static final int ALIGN_RIGHT
See Also:
Constant Field Values

FILL

public static final double FILL
See Also:
Constant Field Values

PREFERRED

public static final double PREFERRED
See Also:
Constant Field Values

MINIMUM

public static final double MINIMUM
See Also:
Constant Field Values

SIZE_POLICY_FIXED

public static final int SIZE_POLICY_FIXED
See Also:
Constant Field Values

SIZE_POLICY_MINIMUM

public static final int SIZE_POLICY_MINIMUM
See Also:
Constant Field Values

SIZE_POLICY_MAXIMUM

public static final int SIZE_POLICY_MAXIMUM
See Also:
Constant Field Values

SIZE_POLICY_PREFERRED

public static final int SIZE_POLICY_PREFERRED
See Also:
Constant Field Values

SIZE_POLICY_MINIMUM_EXPANDING

public static final int SIZE_POLICY_MINIMUM_EXPANDING
See Also:
Constant Field Values

SIZE_POLICY_EXPANDING

public static final int SIZE_POLICY_EXPANDING
See Also:
Constant Field Values

SIZE_POLICY_IGNORED

public static final int SIZE_POLICY_IGNORED
See Also:
Constant Field Values

DEFAULT_SIZE

protected static final double[][] DEFAULT_SIZE
Default row/column size


columnSpec

protected double[] columnSpec
Widths of columns expressed in absolute and relative terms


rowSpec

protected double[] rowSpec
Heights of rows expressed in absolute and relative terms


columnSize

protected int[] columnSize
Widths of columns in pixels


rowSize

protected int[] rowSize
Heights of rows in pixels


columnOffset

protected int[] columnOffset
Offsets of columns in pixels. Or; position from left border of container. The left border of column n is at columnOffset[n] and the right border is at columnOffset[n + 1] for all columns including the last one. columnOffset.length = columnSize.length + 1


rowOffset

protected int[] rowOffset
Offsets of rows in pixels. The left border of row n is at rowOffset[n] and the right border is at rowOffset[n + 1] for all rows including the last one. rowOffset.length = rowSize.length + 1


list

protected Vector list
List of components and their sizes


dirty

protected boolean dirty
Indicates whether or not the size of the cells are known for the last known size of the container. If dirty is true or the container has been resized, the cell sizes must be recalculated using calculateSize.


oldWidth

protected int oldWidth
Previous known width of the container


oldHeight

protected int oldHeight
Previous known height of the container


minimumSize

protected Dimension minimumSize

preferredSize

protected Dimension preferredSize

maximumSize

protected Dimension maximumSize

margin

protected int margin
Margin/spacing


spacing

protected int spacing
Margin/spacing

Constructor Detail

NaturalLayout

public NaturalLayout()
Constructs an instance of NaturalLayout. This NaturalLayout will have one row and one column.


NaturalLayout

public NaturalLayout(int columns,
                     int rows)
Constructs an instance of NaturalLayout with a specified row/column size.

Parameters:
columns - the number of columns in this layout.
rows - the number of rows in this layout.

NaturalLayout

public NaturalLayout(double[][] size)
Constructs an instance of NaturalLayout.

Parameters:
size - widths of columns and heights of rows in the format, {{col0, col1, col2, ..., colN}, {row0, row1, row2, ..., rowM}} If this parameter is invalid, the NaturalLayout will have exactly one row and one column.
Method Detail

setConstraints

public void setConstraints(Component component,
                           Constraints constraint)
Sets the constraints of a given component.

Parameters:
component - desired component. This parameter cannot be null.
constraint - new set of constraints. This parameter cannot be null.

setColumn

public void setColumn(int i,
                      double size)
Adjusts the width of a single column in this layout. After calling this method, the caller should request this layout manager to perform the layout. If this is not done, the changes in the layout will not be seen until the container is resized.

Parameters:
i - zero-based index of column to set. If this parameter is not valid, an ArrayOutOfBoundsException will be thrown.
size - width of the column. This parameter cannot be null.
See Also:
getColumn(int)

setRow

public void setRow(int i,
                   double size)
Adjusts the height of a single row in this layout. After calling this method, the caller should request this layout manager to perform the layout. If this is not done, the changes in the layout will not be seen until the container is resized.

Parameters:
i - zero-based index of row to set. If this parameter is not valid, an ArrayOutOfBoundsException will be thrown.
size - height of the row. This parameter cannot be null.

getColumn

public double getColumn(int i)
Gets the width of a single column in this layout.

Parameters:
i - zero-based index of row to get. If this parameter is not valid, an ArrayOutOfBoundsException will be thrown.
Returns:
width of the requested column

getRow

public double getRow(int i)
Gets the sizes of a row in this layout.

Parameters:
i - zero-based index of row to get. If this parameter is not valid, an ArrayOutOfBoundsException will be thrown.
Returns:
height of each of the requested row
See Also:
setRow(int, double)

getNumColumns

public int getNumColumns()
Gets the number of columns in this layout.

Returns:
the number of columns

getNumRows

public int getNumRows()
Gets the number of rows in this layout.

Returns:
the number of rows

insertColumn

public void insertColumn(int i,
                         double size)
Inserts a column in this layout. All components to the right of the insertion point are moved right one column. The container will need to be laid out after this method returns. See setColumn.

Parameters:
i - zero-based index at which to insert the column.
size - size of the column to be inserted
See Also:
setColumn(int, double), deleteColumn(int)

insertColumn

public void insertColumn(int i)
Convinience function for the above one. Insert colums as many columns as needed to allow the column i to exist. All new Columns will be set to FILL.

Parameters:
i - column to insert. When the layout has 3 columns and i = 4 then 2 columns will be inserted. Making sure that afterwards 5 columns exist (i=4 means 5 columns since we start counting at 0).

insertRow

public void insertRow(int i,
                      double size)
Inserts a row in this layout. All components below the insertion point are moved down one row. The container will need to be laid out after this method returns. See setRow.

Parameters:
i - zero-based index at which to insert the column.
size - size of the row to be inserted
See Also:
setRow(int, double), deleteRow(int)

insertRow

public void insertRow(int i)
Convinience function for the above one. Insert as many rows as needed to allow the row i to exist. All new rows will be set to FILL.

Parameters:
i - row to insert. When the layout has 3 rows and i = 4 then 2 rows will be inserted. Making sure that afterwards 5 rows exist (i=4 means 5 rows since we start counting at 0).

deleteColumn

public void deleteColumn(int i)
Deletes a column in this layout. All components to the right of the deletion point are moved left one column. The container will need to be laid out after this method returns. See setColumn.

Parameters:
i - zero-based index of column to delete
See Also:
setColumn(int, double), deleteColumn(int)

deleteRow

public void deleteRow(int i)
Deletes a row in this layout. All components below the deletion point are moved up one row. The container will need to be laid out after this method returns. See setRow. There must be at least two rows in order to delete a row.

Parameters:
i - zero-based index of column to delete
See Also:
setRow(int, double), deleteRow(int)

layoutIsChanged

protected void layoutIsChanged()
Remove all cached data.


ensureCorrectSpecSize

protected double ensureCorrectSpecSize(double size)
Checks if size of a row/col is correct. If not 0 is returned. 0 is a correct size


getEntryContaining

protected Entry getEntryContaining(Component c)
                            throws IllegalArgumentException
Return the entry that contains argument component.

Parameters:
c - that this layouter should contain.
Returns:
the Entry that contains this component.
Throws:
IllegalArgumentException

toString

public String toString()
Converts this NaturalLayout to a string.

Returns:
a string representing the columns and row sizes in the form "{{col0, col1, col2, ..., colN}, {row0, row1, row2, ..., rowM}}"

drawGrid

public void drawGrid(Container container,
                     Graphics g)
Draws a grid on the given container. This is useful for seeing where the rows and columns go. In the container's paint method, call this method.

Parameters:
container - container using this NaturalLayout
g - graphics content of container (can be offscreen)

calculateSize

protected void calculateSize(Container container)
Calculates the sizes of the rows and columns based on the absolute and relative sizes specified in rowSpec and columnSpec and the size of the container. The result is stored in rowSize and columnSize.

Parameters:
container - container using this NaturalLayout

placeWidget

protected void placeWidget(Sizes minimumSizes,
                           Sizes preferredSizes,
                           Sizes maximumSizes,
                           Entry entry)
Helper method for calculateSize


layoutContainer

public void layoutContainer(Container container)
To lay out the specified container using this layout. This method reshapes the components in the specified target container in order to satisfy the constraints of all components.

User code should not have to call this method directly.

Specified by:
layoutContainer in interface LayoutManager
Parameters:
container - container being served by this layout manager

preferredLayoutSize

public Dimension preferredLayoutSize(Container container)
Determines the preferred size of the container argument using this layout. The preferred size is the smallest size that, if used for the container's size, will ensure that all components are at least as large as their preferred size.

Specified by:
preferredLayoutSize in interface LayoutManager
Parameters:
container - container being served by this layout manager
Returns:
a dimension indicating the container's preferred size

minimumLayoutSize

public Dimension minimumLayoutSize(Container container)
Determines the minimum size of the container argument using this layout. The minimum size is the smallest size that, if used for the container's size, will ensure that all components are at least as large as their minimum size.

Specified by:
minimumLayoutSize in interface LayoutManager
Parameters:
container - container being served by this layout manager
Returns:
a dimension indicating the container's minimum size

addLayoutComponent

public void addLayoutComponent(String name,
                               Component component)
Adds the specified component with the specified name to the layout.

Specified by:
addLayoutComponent in interface LayoutManager
Parameters:
name - indicates entry's position and anchor
component - component to add

addLayoutComponent

public void addLayoutComponent(Component component,
                               Object constraint)
Adds the specified component with the specified name to the layout.

Specified by:
addLayoutComponent in interface LayoutManager2
Parameters:
component - component to add
constraint - indicates entry's position and alignment

removeLayoutComponent

public void removeLayoutComponent(Component component)
Removes the specified component from the layout.

Specified by:
removeLayoutComponent in interface LayoutManager
Parameters:
component - component being removed

maximumLayoutSize

public Dimension maximumLayoutSize(Container component)
Returns the maximum dimensions for this layout given the components in the specified target container.

Specified by:
maximumLayoutSize in interface LayoutManager2
Parameters:
component - the component which needs to be laid out

getLayoutAlignmentX

public float getLayoutAlignmentX(Container parent)
Returns the alignment along the x axis.

Specified by:
getLayoutAlignmentX in interface LayoutManager2
Returns:
unconditionally, 0.5

getLayoutAlignmentY

public float getLayoutAlignmentY(Container parent)
Returns the alignment along the y axis.

Specified by:
getLayoutAlignmentY in interface LayoutManager2
Returns:
unconditionally, 0.5

invalidateLayout

public void invalidateLayout(Container target)
Invalidates the layout.

Specified by:
invalidateLayout in interface LayoutManager2

setMargin

public void setMargin(int margin)
Set margin. The margin is the absolute space around the widget.
When you have a 10x10 pixels icon placed on a widget that has a margin of 6 pixels, the complete height of the widget will be 16 pixels, and the icon will be placed in the center.


setSpacing

public void setSpacing(int spacing)
Set spacing. The spacing is the absolute space between each child-widget, when spacing is 6 and an icon is placed at x=10, and has with of 10 then next widget will be placed at 10+10+6 is 26.
Notice that spacing is only between child-widgets; see margin for the area around the parent widget.


getSpacing

public int getSpacing()

getMargin

public int getMargin()

max2

protected static int max2(int a,
                          int b)


Copyright © 2002,2003 Thomas Zander Available under the Free Apache licence