public abstract class PercentLayoutBase extends Object implements Serializable
The main class of the percent layout manager.
Percent layout provides a table-like layout, which is organized in columns
and rows, each of which defined by a CellConstraints
object. With these constraints objects it is possible to set a cell's
alignment and its minimum size. A cell can also be assigned weight factors
for its height and width. If there is more space available than needed by the
existing cells, the remaining space is divided and assigned to cells with a
weight factor greater than 0. So each cell can be given a certain percentage
of the remaining space, thus the name of this layout manager.
Sometimes certain columns or rows in the layout should have the same size,
even if they contain components with different preferred or minimum sizes. To
achieve this, CellGroup
objects can be added to this
layout manager. These objects define the indices of the columns and rows,
which belong to the same group. All cells in a group have the same initial
size. To ensure that the affected cells have always the same size, their
weight factors must also be equal.
This class is an abstract base class that implements the complete layouting algorithm. There will be concrete implementations for different layout types that are based on the central percent layout functionality. These classes will serve as adapters for specific layouts; they will create a layout description that can be handled by this base class.
The family of percent layout classes is independent on a concrete GUI
library. It can work together e.g. with Swing or SWT. To achieve this access
to the managed GUI components is encapsulated by the
PercentLayoutPlatformAdapter
interface. A platform specific implementation of this interface must be
passed to an instance of this class.
Note: The PercentLayout
class is not thread safe. It should be
accessed by a single thread (the GUI thread) only.
Modifier | Constructor and Description |
---|---|
protected |
PercentLayoutBase()
Creates a new, uninitialized instance of
PercentLayoutBase . |
protected |
PercentLayoutBase(Collection<? extends CellConstraints> colConstr,
Collection<? extends CellConstraints> rowConstr)
Creates a new instance of
PercentLayoutBase and initializes
it. |
protected |
PercentLayoutBase(int cols,
int rows)
Creates a new instance of
PercentLayoutBase and sets the
numbers of the rows and columns. |
protected |
PercentLayoutBase(String colConstr,
String rowConstr)
Creates a new instance of
PercentLayoutBase and initializes
it. |
Modifier and Type | Method and Description |
---|---|
void |
addColumnGroup(CellGroup grp)
Adds a
CellGroup object for columns to this layout manager. |
void |
addRowGroup(CellGroup grp)
Adds a
CellGroup object for rows to this layout manager. |
protected void |
alignComponent(Rectangle bounds,
int colIdx,
int rowIdx,
int[] sizes,
int startPos,
int idx,
Object container,
boolean vert)
Aligns the specified component.
|
protected void |
applyCellGroups(int[] sizes,
Collection<CellGroup> cellGroups)
Applies the defined cell groups to the so far calculated cell sizes.
|
protected void |
applyWeightFactors(int[] sizes,
int containerSize,
boolean vert)
Processes the cells with a weight factor larger than 0.
|
protected void |
applyWeightFactors(int[] sizes,
int containerSize,
CellConstraints[] constraints,
int totalWeight)
Processes the cells with a weight factor larger than 0.
|
int[] |
calcCellPositions(int[] sizes,
int startPos)
Calculates the start positions of all cells in a column or row.
|
protected int |
calcCellSize(PercentData pd,
int colIdx,
int rowIdx,
Object container,
boolean minimum,
boolean vert)
Determines the size of a single cell.
|
protected int[] |
calcCellSizes(CellConstraints[] constraints,
int count,
Object container,
boolean minimum,
boolean vert)
Calculates the minimum size of either the columns or the rows in the
layout.
|
protected int[] |
calcCellSizesWithGroups(CellConstraints[] constraints,
int count,
Collection<CellGroup> cellGroups,
Object container,
boolean minimum,
boolean vert)
Calculates the sizes of all columns or rows.
|
protected int |
calcComponentSize(PercentData pd,
Object comp,
Object container,
boolean minimum,
boolean vert)
Determines the size of a component based on the given constraints object.
|
protected int |
calcComponentSizes(CellConstraints constraints,
int index,
int count,
Object container,
boolean minimum,
boolean vert)
Calculates the size of either a column or a row in the layout.
|
Dimension |
calcMinimumLayoutSize(Object container)
Returns the minimum size of this layout.
|
Dimension |
calcPreferredLayoutSize(Object container)
Returns the preferred size of this layout.
|
int[] |
calcSizes(CellConstraints[] constraints,
int count,
Collection<CellGroup> cellGroups,
Object container,
int containerSize,
boolean vert)
Calculates the final cell sizes in one direction (horizontal or
vertical).
|
protected int |
calcTotalWeight(CellConstraints[] constraints)
Helper method for calculating the total weight factor.
|
protected PercentData |
checkConstraints(Object constraintsObj)
Checks the specified constraints object.
|
protected void |
clearCells()
Resets the states of all cells in the layout.
|
protected void |
clearCells(int columns,
int rows)
Resets the states of all cells in the layout.
|
protected CellConstraints |
constraintsFor(PercentData pd,
boolean vert)
Returns the constraints object for the specified percent data and the
given orientation.
|
protected PercentLayoutPlatformAdapter |
fetchPlatformAdapter()
Returns a reference to the associated platform adapter.
|
void |
flushCache()
Clears all cached values.
|
CellConstraints[] |
getAllColumnConstraints()
Returns an array with the current column constraints.
|
CellConstraints[] |
getAllRowConstraints()
Returns an array with the current row constraints.
|
CellConstraints |
getColumnConstraints(int idx)
Returns the column constraints object for the column with the given
index.
|
int |
getColumnCount()
Returns the number of columns in this layout.
|
Collection<CellGroup> |
getColumnGroups()
Returns an unmodifiable collection with the column groups defined for
this layout.
|
protected Object |
getComponent(int col,
int row)
Returns the component at the specified position of this layout.
|
CellConstraints.Builder |
getConstraintsBuilder()
Returns the builder instance for creating
CellConstraints
objects. |
protected CellConstraints[] |
getInternalAllColumnConstraints()
Returns the internal array of all cell constraints objects for the
layout's column.
|
protected CellConstraints[] |
getInternalAllRowConstraints()
Returns the internal array of all cell constraints objects for the
layout's rows.
|
protected int |
getMinimumComponentSize(int col,
int row,
boolean vert)
Returns the minimum size of the component at the specified column and row
position in the given orientation.
|
static int |
getOrientationValue(int v1,
int v2,
boolean vert)
Helper method for extracting a value from a 2D vector with the specified
orientation.
|
protected PercentData |
getPercentData(int col,
int row)
Returns the constraints object for the component at the specified
position of this layout.
|
PercentLayoutPlatformAdapter |
getPlatformAdapter()
Returns the platform adapter associated with this layout class.
|
protected int |
getPreferredComponentSize(int col,
int row,
boolean vert)
Returns the preferred size of the component at the specified column and
row position in the given orientation.
|
CellConstraints |
getRowConstraints(int idx)
Returns the row constraints object for the row with the given index.
|
int |
getRowCount()
Returns the number of rows in this layout.
|
Collection<CellGroup> |
getRowGroups()
Returns an unmodifiable collection with the row groups defined for this
layout.
|
protected UnitSizeHandler |
getSizeHandler()
Returns the currently used size handler implementation.
|
protected int |
getTotalWeightX()
Returns the total weight factor for columns.
|
protected int |
getTotalWeightY()
Returns the total weight factor for rows.
|
protected void |
handleMultiSpans(int[] sizes,
CellConstraints[] constraints,
List<net.sf.jguiraffe.gui.layout.PercentLayoutBase.CellData> components,
Object container,
boolean minimum,
boolean vert)
Checks the sizes of components that span multiple cells.
|
protected void |
initCell(Object component,
Object constraints)
Initializes the specified cell in the table-like layout.
|
protected abstract void |
initCells(PercentLayoutPlatformAdapter adapter)
Initializes the whole layout.
|
protected void |
initDimensions(int cols,
int rows)
Initializes the dimensions of the table that holds the layout.
|
protected void |
initFromCollections(Collection<? extends CellConstraints> colConstr,
Collection<? extends CellConstraints> rowConstr)
Initializes this instance from the given collections with
CellConstraints objects. |
boolean |
isCanShrink()
Returns a flag whether this layout can shrink below its preferred size.
|
void |
performLayout(Object container,
int[] colSizes,
int[] rowSizes,
int[] colPos,
int[] rowPos)
Layouts all components that are contained in the associated container.
|
void |
performLayout(Object container,
Rectangle insets,
Dimension size)
Layouts all components in the associated container calculating all
necessary intermediate sizes and positions.
|
boolean |
removeComponent(Object comp)
Removes the specified component from this layout.
|
void |
setCanShrink(boolean canShrink)
Sets a flag whether this layout can shrink below its preferred size.
|
void |
setColumnConstraints(int idx,
CellConstraints cc)
Sets the column constraints object for the column with the given index.
|
protected void |
setComponentBounds(int col,
int row,
Rectangle bounds)
Sets the bounds of the component at the specified column and row
position.
|
void |
setPlatformAdapter(PercentLayoutPlatformAdapter platformAdapter)
Sets the platform adapter for this layout manager.
|
void |
setRowConstraints(int idx,
CellConstraints cc)
Sets the row constraints object for the row with the given index.
|
protected PercentLayoutBase()
PercentLayoutBase
.
If this constructor is used, the concrete implementation of the
initCells(PercentLayoutPlatformAdapter)
method must perform all
initialization.protected PercentLayoutBase(int cols, int rows)
PercentLayoutBase
and sets the
numbers of the rows and columns. The constraints for the cells are set to
default values.cols
- the number of columnsrows
- the number of rowsprotected PercentLayoutBase(Collection<? extends CellConstraints> colConstr, Collection<? extends CellConstraints> rowConstr)
PercentLayoutBase
and initializes
it. The constraints for the columns and rows are specified in the passed
collections, which must contain instances of
CellConstraints
.colConstr
- a collection with column constraintsrowConstr
- a collection with row constraintsprotected PercentLayoutBase(String colConstr, String rowConstr)
PercentLayoutBase
and initializes
it. The column and row constraints are defined as strings. These strings
must contain valid specifications of cell constraints as defined in the
documentation of CellConstraints
. As separators
between two cell definitions the following characters can be used: "
,;".colConstr
- a string defining column constraintsrowConstr
- a string defining row constraintspublic PercentLayoutPlatformAdapter getPlatformAdapter()
public void setPlatformAdapter(PercentLayoutPlatformAdapter platformAdapter)
platformAdapter
- the platform adapter to usepublic boolean isCanShrink()
public void setCanShrink(boolean canShrink)
canShrink
- the shrink flagpublic int getColumnCount()
public int getRowCount()
public CellConstraints getColumnConstraints(int idx)
idx
- the index (0 based)public void setColumnConstraints(int idx, CellConstraints cc)
idx
- the index of the column (0 based)cc
- the constraints objectpublic CellConstraints[] getAllColumnConstraints()
public CellConstraints getRowConstraints(int idx)
idx
- the index (0 based)public void setRowConstraints(int idx, CellConstraints cc)
idx
- the index of the row (0 based)cc
- the constraints objectpublic CellConstraints[] getAllRowConstraints()
public Collection<CellGroup> getColumnGroups()
CellGroup
objects for columnspublic void addColumnGroup(CellGroup grp)
CellGroup
object for columns to this layout manager.
This causes the columns defined by this group object to have the same
width (as long as their weight factors are equal).grp
- the group objectpublic Collection<CellGroup> getRowGroups()
CellGroup
objects for rowspublic void addRowGroup(CellGroup grp)
CellGroup
object for rows to this layout manager.
This causes the rows defined by this group object to have the same height
(as long as their weight factors are equal).grp
- the group objectpublic void flushCache()
public boolean removeComponent(Object comp)
comp
- the component to removeprotected final PercentLayoutPlatformAdapter fetchPlatformAdapter()
IllegalStateException
exception is
thrown.IllegalStateException
- if no platform adapter is setpublic final CellConstraints.Builder getConstraintsBuilder()
CellConstraints
objects. Each instance of this class is associated with such a builder.
Sub classes or clients can use it for creating their constraints.CellConstraints
objectsprotected final void initDimensions(int cols, int rows)
cols
- the number of columnsrows
- the number of rowsprotected final CellConstraints[] getInternalAllColumnConstraints()
getAllColumnConstraints()
method returns a defensive copy of
this array. So for performance reasons this method should be used by
subclasses.protected final CellConstraints[] getInternalAllRowConstraints()
getAllRowConstraints()
method returns a defensive copy of this
array. So for performance reasons this method should be used by
subclasses.protected Object getComponent(int col, int row)
col
- the columnrow
- the rowprotected PercentData getPercentData(int col, int row)
col
- the columnrow
- the rowprotected PercentData checkConstraints(Object constraintsObj)
PercentData
and
that only valid values for column and row number and the spans are
accepted. If invalid values are detected, an IllegalStateException
exception is thrown.constraintsObj
- the constraints to checkIllegalStateException
- if invalid constraints are detectedprotected void initCell(Object component, Object constraints)
PercentData
and that its indices and dimensions are
valid.component
- the componentconstraints
- the constraints of this componentprotected void clearCells()
protected void clearCells(int columns, int rows)
columns
- the number of columnsrows
- the number of rowspublic int[] calcSizes(CellConstraints[] constraints, int count, Collection<CellGroup> cellGroups, Object container, int containerSize, boolean vert)
performLayout(Object, int[], int[], int[], int[])
.constraints
- an array with the constraints for the columns or rowscount
- the number of cells in the opposite dimension (i.e. if
columns are calculated, the number of rows and vice versa)cellGroups
- a collection with the cell groupscontainer
- the container this layout belongs tocontainerSize
- the total size available for the containervert
- a flag if this calculation is for the X or Y directionpublic void performLayout(Object container, int[] colSizes, int[] rowSizes, int[] colPos, int[] rowPos)
container
- the containercolSizes
- an array with the sizes of all columnsrowSizes
- an array with the sizes of all rowscolPos
- the start positions of all columnsrowPos
- the start positions of all rowspublic void performLayout(Object container, Rectangle insets, Dimension size)
performLayout()
method.container
- the containerinsets
- a rectangle with the container's insetssize
- the size of the containerpublic Dimension calcPreferredLayoutSize(Object container)
container
- the associated container objectpublic Dimension calcMinimumLayoutSize(Object container)
canShrink
flag: if this flag is false, it
returns the same result as calcPreferredLayoutSize(Object)
-
because the layout cannot shrink below its preferred size. Otherwise, a
size calculation is performed based on the component's minimum size
rather than their preferred sizes. Note that this mainly makes a
difference if cell constraints are used with the CellSize
preferred.container
- the associated container objectprotected final void initFromCollections(Collection<? extends CellConstraints> colConstr, Collection<? extends CellConstraints> rowConstr)
CellConstraints
objects.colConstr
- a collection with column constraints objectrowConstr
- a collection with row constraints objectprotected int[] calcCellSizes(CellConstraints[] constraints, int count, Object container, boolean minimum, boolean vert)
constraints
- an array with the constraints for the columns or rowscount
- the number of cells in the opposite dimension (i.e. if
columns are calculated, the number of rows and vice versa)container
- the container this layout belongs tominimum
- a flag whether the minimum size should be returnedvert
- a flag if this calculation is for the X or Y directionprotected int calcComponentSizes(CellConstraints constraints, int index, int count, Object container, boolean minimum, boolean vert)
minimum
parameter it is possible to force the method to
always return the minimum size. This is required if there is less space
available than is required for the preferred width.constraints
- the constraints object for the actual column or rowindex
- the index of the actual column or rowcount
- the number of cells in the opposite dimensioncontainer
- the container this layout belongs tominimum
- a flag whether the minimum size should be returnedvert
- a flag if this calculation is for the X or Y directionprotected void handleMultiSpans(int[] sizes, CellConstraints[] constraints, List<net.sf.jguiraffe.gui.layout.PercentLayoutBase.CellData> components, Object container, boolean minimum, boolean vert)
sizes
- an array with the so far calculated cell sizesconstraints
- an array with all cell constraintscomponents
- the list with the multi span componentscontainer
- the container objectminimum
- a flag whether the minimum size should be returnedvert
- the orientation flagprotected int calcCellSize(PercentData pd, int colIdx, int rowIdx, Object container, boolean minimum, boolean vert)
pd
- the constraints objectcolIdx
- the column indexrowIdx
- the row indexcontainer
- the container this layout belongs tominimum
- a flag whether the minimum size should be returnedvert
- a flag if this calculation is for the X or Y directionprotected int calcComponentSize(PercentData pd, Object comp, Object container, boolean minimum, boolean vert)
pd
- the constraints objectcomp
- the affected componentcontainer
- the container this layout belongs tominimum
- a flag whether the minimum size should be returnedvert
- a flag if this calculation is for the X or Y directionprotected void applyCellGroups(int[] sizes, Collection<CellGroup> cellGroups)
sizes
- an array with the (initial or minimum) cell sizescellGroups
- a collection with the defined cell groupsprotected int[] calcCellSizesWithGroups(CellConstraints[] constraints, int count, Collection<CellGroup> cellGroups, Object container, boolean minimum, boolean vert)
calcCellSizes()
and
applyCellGroups()
.constraints
- an array with the constraints for the columns or rowscount
- the number of cells in the opposite dimension (i.e. if
columns are calculated, the number of rows and vice versa)cellGroups
- a collection with the defined cell groupscontainer
- the container this layout belongs tominimum
- a flag whether the minimum size should be returnedvert
- a flag if this calculation is for the X or Y directionNullPointerException
- if a required parameter is missingprotected int calcTotalWeight(CellConstraints[] constraints)
constraints
- an array with cell constraintsprotected int getTotalWeightX()
protected int getTotalWeightY()
protected void applyWeightFactors(int[] sizes, int containerSize, CellConstraints[] constraints, int totalWeight)
sizes
- the cell sizes without weight factorscontainerSize
- the size of the container (without insets)constraints
- the cell constraintstotalWeight
- the total weight factorprotected void applyWeightFactors(int[] sizes, int containerSize, boolean vert)
sizes
- the cell sizes without weight factorscontainerSize
- the size of the container (without insets)vert
- a flag if this calculation is for the X or Y directionpublic int[] calcCellPositions(int[] sizes, int startPos)
sizes
- the cell sizes (must not be null)startPos
- the start positionNullPointerException
- if the array with sizes is nullprotected void alignComponent(Rectangle bounds, int colIdx, int rowIdx, int[] sizes, int startPos, int idx, Object container, boolean vert)
bounds
- stores the bounds of the componentcolIdx
- the column indexrowIdx
- the row indexsizes
- an array with the sizes of all cellsstartPos
- the start position of the actual cellidx
- the actual cell indexcontainer
- the container this layout belongs tovert
- a flag if this calculation is for the X or Y directionpublic static int getOrientationValue(int v1, int v2, boolean vert)
v1
- the x valuev2
- the y valuevert
- the orientation flag (true for the y value,
false for the x value)protected int getMinimumComponentSize(int col, int row, boolean vert)
col
- the column indexrow
- the row indexvert
- the orientation flagprotected int getPreferredComponentSize(int col, int row, boolean vert)
col
- the column indexrow
- the row indexvert
- the orientation flagprotected void setComponentBounds(int col, int row, Rectangle bounds)
col
- the column indexrow
- the row indexbounds
- the new bounds for this componentprotected UnitSizeHandler getSizeHandler()
protected CellConstraints constraintsFor(PercentData pd, boolean vert)
pd
- the percent data objectvert
- the orientation flagprotected abstract void initCells(PercentLayoutPlatformAdapter adapter)
adapter
- the currently used platform adapterCopyright © 2016 The JGUIraffe Team. All rights reserved.