ch.sahits.game.graphic.layout
Class MaximalGridLayout

java.lang.Object
  extended by ch.sahits.game.graphic.layout.MaximalGridLayout
All Implemented Interfaces:
LayoutManager, LayoutManager2

public class MaximalGridLayout
extends Object
implements LayoutManager2

Grid layout that sizes the cells according the the biggest sized component in the same row/column. If the component does not fill the whole size of the cell it is by default positioned in the center.

Author:
Andi Hotz, (c) Sahits GmbH, 2011 Created on May 27, 2011

Field Summary
private  Hashtable<Component,MaximalGridLayoutConstraints> compTable
           
private  int gap
           
private  int[] maxHight
           
private  int[] maxWidth
           
private  int nbCols
           
private  int nbRows
           
private  ECellPosition position
           
 
Constructor Summary
MaximalGridLayout()
          Default constructor initializing a grid of size 1x1.
MaximalGridLayout(int cols, int rows)
          Initialize the layout with the number of columns and rows
MaximalGridLayout(int cols, int rows, int gapWidth, ECellPosition positioning)
          Initialize the layout with the number of columns and rows and the gaping between them as well as the positioning of the whole if there is any space.
 
Method Summary
 void addLayoutComponent(Component comp, Object constraints)
          Adding a component with a MaximalGridLayoutConstraints
 void addLayoutComponent(String name, Component comp)
          Has no effect, since this layout manager does not use a per-component string.
private  int addUpColumnWidth(Container parent)
          Adding up the column width of a container.
private  int addUpRowHeight(Container parent)
          Adding up the row height of a container.
private  boolean checkHeight(Container parent)
          check if the size of the children add up to more than the parents size
private  boolean checkWidth(Container parent)
          check if the size of the children add up to more than the parents size
(package private)  int computeColumnIndex(int i)
          Compute the column based on the index of the component
(package private)  int computeHeight(int row, Container parent)
          Compute the height of the highest component in the specified row.
private  double computeHeightScale(Container parent)
          Compute the scale factor that is needed on the components to resize them to the heigth of the container
(package private)  int computeRowIndex(int i)
          Compute the row based on the index of the component
(package private)  int computeWidth(int column, Container parent)
          Compute the width of the widest component in the specified column.
private  double computeWidthScale(Container parent)
          Compute the scale factor that is needed on the components to resize them to the width of the container
(package private)  int computeXPadding(int columnWidth, int width, MaximalGridLayoutConstraints constraints)
          Compute the horizontal padding within a cell based upon the alignment of the component
(package private)  int computeYPadding(int rowHeight, int height, MaximalGridLayoutConstraints constraints)
          Compute the vertical padding within a cell based upon the alignment of the component
(package private)  int getColumnStart(int index, Container parent)
          Compute the x position where the column with the given index starts
 MaximalGridLayoutConstraints getConstraints(Component comp)
          Gets the constraints for the specified component.
 float getLayoutAlignmentX(Container target)
          If there is a constraint defined by the target, the position is used for the alignment
 float getLayoutAlignmentY(Container target)
          If there is a constraint defined by the target, the position is used for the alignment
(package private)  int getRowStart(int index, Container parent)
          Compute the y position where the row with the given index starts
 void invalidateLayout(Container target)
           
 void layoutContainer(Container parent)
           
 Dimension maximumLayoutSize(Container target)
           
 Dimension minimumLayoutSize(Container parent)
           
 Dimension preferredLayoutSize(Container parent)
          Computing the prefered size by adding the sizes of the columns and rows together with the insets
 void removeLayoutComponent(Component comp)
          Remove the component from the stored data.
private  void resetSizeCache()
           
 void setConstraints(Component comp, MaximalGridLayoutConstraints constraints)
          Sets the constraints for the specified component in this layout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nbCols

private final int nbCols

nbRows

private final int nbRows

maxWidth

private final int[] maxWidth

maxHight

private final int[] maxHight

gap

private final int gap

position

private final ECellPosition position

compTable

private final Hashtable<Component,MaximalGridLayoutConstraints> compTable
Constructor Detail

MaximalGridLayout

public MaximalGridLayout(int cols,
                         int rows)
Initialize the layout with the number of columns and rows

Parameters:
cols -
rows -

MaximalGridLayout

public MaximalGridLayout(int cols,
                         int rows,
                         int gapWidth,
                         ECellPosition positioning)
Initialize the layout with the number of columns and rows and the gaping between them as well as the positioning of the whole if there is any space.

Parameters:
cols -
rows -
gapWidth -
positioning -

MaximalGridLayout

public MaximalGridLayout()
Default constructor initializing a grid of size 1x1.

Method Detail

resetSizeCache

private void resetSizeCache()

addLayoutComponent

public void addLayoutComponent(String name,
                               Component comp)
Has no effect, since this layout manager does not use a per-component string.

Specified by:
addLayoutComponent in interface LayoutManager

removeLayoutComponent

public void removeLayoutComponent(Component comp)
Remove the component from the stored data.

Specified by:
removeLayoutComponent in interface LayoutManager

computeWidth

final int computeWidth(int column,
                       Container parent)
Compute the width of the widest component in the specified column.

Parameters:
column - number of the column (starting with index 1)
parent - parent container
Returns:

computeHeight

final int computeHeight(int row,
                        Container parent)
Compute the height of the highest component in the specified row.

Parameters:
row - number of the row (starting with index 1)
parent - parent container
Returns:

preferredLayoutSize

public Dimension preferredLayoutSize(Container parent)
Computing the prefered size by adding the sizes of the columns and rows together with the insets

Specified by:
preferredLayoutSize in interface LayoutManager

layoutContainer

public void layoutContainer(Container parent)
Specified by:
layoutContainer in interface LayoutManager

computeHeightScale

private double computeHeightScale(Container parent)
Compute the scale factor that is needed on the components to resize them to the heigth of the container

Parameters:
parent - container
Returns:
scale factor

computeWidthScale

private double computeWidthScale(Container parent)
Compute the scale factor that is needed on the components to resize them to the width of the container

Parameters:
parent - container
Returns:
scale factor

checkHeight

private boolean checkHeight(Container parent)
check if the size of the children add up to more than the parents size

Parameters:
parent - container
Returns:
true if the height of the parent container is larger or equal than the sum of all the rows plus the parents insets.

addUpRowHeight

private int addUpRowHeight(Container parent)
Adding up the row height of a container. Between each row the gap height is added

Parameters:
parent - container
Returns:
height of all child components plus the parents insets

checkWidth

private boolean checkWidth(Container parent)
check if the size of the children add up to more than the parents size

Parameters:
parent - container
Returns:
true if the width of the parent container is larger or equal than the sum of all the columns plus the parents insets.

addUpColumnWidth

private int addUpColumnWidth(Container parent)
Adding up the column width of a container. Between each column the gap width is added

Parameters:
parent - container
Returns:
width of all child components plus the parents insets

computeYPadding

final int computeYPadding(int rowHeight,
                          int height,
                          MaximalGridLayoutConstraints constraints)
Compute the vertical padding within a cell based upon the alignment of the component

Parameters:
rowHeight - height of the cell
height - of the component
constraints - Objects
Returns:
padding from the top

computeXPadding

final int computeXPadding(int columnWidth,
                          int width,
                          MaximalGridLayoutConstraints constraints)
Compute the horizontal padding within a cell based upon the alignment of the component

Parameters:
columnWidth - width of the cell
width - of the component
constraints - Object
Returns:
padding from the left

getRowStart

final int getRowStart(int index,
                      Container parent)
Compute the y position where the row with the given index starts

Parameters:
index - of the row
parent - container
Returns:

getColumnStart

final int getColumnStart(int index,
                         Container parent)
Compute the x position where the column with the given index starts

Parameters:
index - of the column
parent - container
Returns:

computeRowIndex

final int computeRowIndex(int i)
Compute the row based on the index of the component

Parameters:
i - index of the component
Returns:

computeColumnIndex

final int computeColumnIndex(int i)
Compute the column based on the index of the component

Parameters:
i - index of the component
Returns:

minimumLayoutSize

public Dimension minimumLayoutSize(Container parent)
Specified by:
minimumLayoutSize in interface LayoutManager

setConstraints

public void setConstraints(Component comp,
                           MaximalGridLayoutConstraints constraints)
Sets the constraints for the specified component in this layout.

Parameters:
comp - the component to be modified
constraints - the constraints to be applied

getConstraints

public MaximalGridLayoutConstraints getConstraints(Component comp)
Gets the constraints for the specified component. A copy of the actual GridBagConstraints object is returned.

Parameters:
comp - the component to be queried
Returns:
the constraint for the specified component in this grid bag layout; a copy of the actual constraint object is returned

addLayoutComponent

public void addLayoutComponent(Component comp,
                               Object constraints)
Adding a component with a MaximalGridLayoutConstraints

Specified by:
addLayoutComponent in interface LayoutManager2

maximumLayoutSize

public Dimension maximumLayoutSize(Container target)
Specified by:
maximumLayoutSize in interface LayoutManager2

getLayoutAlignmentX

public float getLayoutAlignmentX(Container target)
If there is a constraint defined by the target, the position is used for the alignment

Specified by:
getLayoutAlignmentX in interface LayoutManager2

getLayoutAlignmentY

public float getLayoutAlignmentY(Container target)
If there is a constraint defined by the target, the position is used for the alignment

Specified by:
getLayoutAlignmentY in interface LayoutManager2

invalidateLayout

public void invalidateLayout(Container target)
Specified by:
invalidateLayout in interface LayoutManager2


Copyright © 2011 Sahits GmbH. All Rights Reserved.