Class CellConstraints

  • All Implemented Interfaces:
    Serializable

    public final class CellConstraints
    extends Object
    implements Serializable

    A class for describing column and row constraints for the PercentLayout layout manager.

    Objects of this class define properties for the single columns and rows that comprise the layout maintained by PercentLayout. Supported properties are:

    • The alignment of the represented cell. This can be one of the constants defined by the CellAlignment enumeration class.
    • An initial size. This property defines how the column's or row's size depends on the sizes of the contained components. The possible values are defined by the enumeration class CellSize.
    • A minimum size for this cell. This is a numeric value specifying the minimum width for columns and minimum height for rows. To determine the size of a column or row the layout first calculates the initial size. If this is less than the minimum size, the minimum size is used. It is possible to use different units for specifying this property.
    • A weight factor. This factor determines how this cell should behave if additional space becomes available when the container's size changes (e.g. if the user resizes the window). If this factor is 0, the cell won't change its size; it will always keep its initial size. For other values than 0 the remaining space is given to the affected cells with regard to their factors. It is recommended to use percentage values for these factors, which sum up to 100 percent (hence the name of the PercentLayout layout manager). Then it is easy to understand that each cell with a weight factor greater than 0 is granted as much percent of the remaining space.

    Instances of this class are not created directly, but the inner Builder class is used for this purpose (an application of the builder pattern). A typical invocation sequence could look as follows:

     CellConstraints.Builder ccb = new CellConstraints.Builder();
     CellConstraints cc = ccb.withCellSize(CellSize.Preferred).withMinimumSize(
             new NumberWithUnit(20, Unit.PIXEL)).withWeight(25).withCellAlignment(
             CellAlignment.FULL).create();
     

    For instances of this class a string representation is defined. Strings conforming to the format explained below can also be passed to the builder for creating instances:

     CONSTRAINT   ::= [ALIGN"/"]SIZE["/"WEIGHT]
     SIZE         ::= INITSIZE | MINSIZE | BOTHSIZES
     BOTHSIZES    ::= INITSIZE "(" MINSIZE ")"
     INITSIZE     ::= "PREFERRED" | "MINIMUM" | "NONE"
     MINSIZE      ::= <Positive number> [UNIT]
     UNIT         ::= "px" | "cm" | "in" | "dlu"
     ALIGN        ::= "START" | "CENTER" | "END" | "FULL"
     WEIGHT       ::= <Positive number>
     
    Here are some examples:
    • PREFERRED: only the cell size is set, the other properties are set to default values.
    • CENTER/MINIMUM(20px)/33: A CellConstraints object is created with the CellSize MINIMUM and the alignment CENTER. The minimum size is set to 20 pixels, and the weight factor is set to 33.
    • (20): Here only the minimum size of the cell is set. The CellSize is automatically set to NONE.

    Instances of this class are immutable and thus can be shared between multiple threads. The Builder class makes use of this feature and caches the instances that have been created. So if another constraint with already used properties is requested, a shared instance can be returned.

    Version:
    $Id: CellConstraints.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    See Also:
    Serialized Form
    • Method Detail

      • getAlignment

        public CellAlignment getAlignment()
        Returns the alignment string.
        Returns:
        the alignment string
      • getCellSize

        public CellSize getCellSize()
        Returns the size of the cell.
        Returns:
        the cell size
      • getMinSize

        public NumberWithUnit getMinSize()
        Returns the minimum size.
        Returns:
        the minimum size
      • getWeight

        public int getWeight()
        Returns the weight factor.
        Returns:
        the weight factor
      • toSpecificationString

        public String toSpecificationString()
        Returns a string specification for this CellConstraints object. This string conforms to the definition given in the class comment. It can be passed to a Builder instance to obtain a corresponding CellConstraints instance. The string returned by this method contains the values of all properties of this object, even if some have been set to default values.
        Returns:
        a specification string for this instance
      • toString

        public String toString()
        Returns a string representation of this constraints object. This string contains the specification string produced by toSpecificationString().
        Overrides:
        toString in class Object
        Returns:
        a string representation for this object
      • equals

        public boolean equals​(Object obj)
        Compares this object with another one. Two instances of CellConstraints are equal if all of their properties are equal.
        Overrides:
        equals in class Object
        Parameters:
        obj - the object to compare to
        Returns:
        a flag whether these objects are equal
      • hashCode

        public int hashCode()
        Returns a hash code for this object.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code for this object