Package swingtree.api

Class Layout.ForMigLayout

java.lang.Object
swingtree.api.Layout.ForMigLayout
All Implemented Interfaces:
Layout
Enclosing interface:
Layout

@Immutable public static final class Layout.ForMigLayout extends Object implements Layout
An immutable Layout implementation that configures and installs a MigLayout onto a component. It holds three kinds of constraints:
  • General layout constraints (constr) — control global layout behaviour such as wrapping, filling, gaps and hiding mode.
  • Column constraints (colConstr) — define the sizing and alignment rules for each column in the grid.
  • Row constraints (rowConstr) — define the sizing and alignment rules for each row in the grid.
  • Per-child component constraints (childConstraints) — a sorted Association mapping child indices (Integer) to MigAddConstraints applied to the component's direct children. Unlike a positional tuple, the association is sparse: you only need to include entries for the children you actually want to configure; children whose index has no entry keep whatever constraint the MigLayout already has for them.

Instances are created via the Layout.mig(LayoutConstraint) family of factory methods and are further configured through the fluent with* wither methods, which all return a new immutable instance:


      import static swingtree.UI.*;
      // ...
      Layout.mig( FILL.and(WRAP(2)), "[shrink][grow]", "[]8[]" )
            .withChildConstraints( RIGHT, GROW_X, RIGHT, GROW_X )
  
Whenever any property of this configuration changes (detected by the style engine via equals(java.lang.Object)/hashCode()), the installFor(JComponent) method is called, which surgically updates only the properties that have changed and calls JComponent.revalidate() to trigger a layout refresh.
  • Method Details

    • withConstraint

      public Layout.ForMigLayout withConstraint(LayoutConstraint constr)
      Returns a new Layout.ForMigLayout instance with the supplied general layout constraints and all other properties copied from this instance. This is the preferred overload as it works with the type-safe LayoutConstraint API, which supports composition via LayoutConstraint.and(LayoutConstraint).
      Parameters:
      constr - The new general layout constraints for the MigLayout.
      Returns:
      A new Layout.ForMigLayout instance with the updated layout constraints.
    • withConstraint

      public Layout.ForMigLayout withConstraint(String constr)
      Returns a new Layout.ForMigLayout instance with the supplied general layout constraints and all other properties copied from this instance. The string is wrapped via LayoutConstraint.of(String...) and forwarded to withConstraint(LayoutConstraint). Prefer withConstraint(LayoutConstraint) for new code.
      Parameters:
      constr - The new general layout constraints string for the MigLayout.
      Returns:
      A new Layout.ForMigLayout instance with the updated layout constraints.
    • withRowConstraint

      public Layout.ForMigLayout withRowConstraint(LayoutConstraint rowConstr)
      Returns a new Layout.ForMigLayout instance with the supplied row constraints and all other properties copied from this instance. This is the preferred overload as it works with the type-safe LayoutConstraint API, which supports composition via LayoutConstraint.and(LayoutConstraint).
      Parameters:
      rowConstr - The new row constraints for the MigLayout.
      Returns:
      A new Layout.ForMigLayout instance with the updated row constraints.
    • withRowConstraint

      public Layout.ForMigLayout withRowConstraint(String rowConstr)
      Returns a new Layout.ForMigLayout instance with the supplied row constraints and all other properties copied from this instance. The string is wrapped via LayoutConstraint.of(String...) and forwarded to withRowConstraint(LayoutConstraint). Prefer withRowConstraint(LayoutConstraint) for new code.
      Parameters:
      rowConstr - The new row constraints string for the MigLayout.
      Returns:
      A new Layout.ForMigLayout instance with the updated row constraints.
    • withColumnConstraint

      public Layout.ForMigLayout withColumnConstraint(LayoutConstraint colConstr)
      Returns a new Layout.ForMigLayout instance with the supplied column constraints and all other properties copied from this instance. This is the preferred overload as it works with the type-safe LayoutConstraint API, which supports composition via LayoutConstraint.and(LayoutConstraint).
      Parameters:
      colConstr - The new column constraints for the MigLayout.
      Returns:
      A new Layout.ForMigLayout instance with the updated column constraints.
    • withColumnConstraint

      public Layout.ForMigLayout withColumnConstraint(String colConstr)
      Returns a new Layout.ForMigLayout instance with the supplied column constraints and all other properties copied from this instance. The string is wrapped via LayoutConstraint.of(String...) and forwarded to withColumnConstraint(LayoutConstraint). Prefer withColumnConstraint(LayoutConstraint) for new code.
      Parameters:
      colConstr - The new column constraints string for the MigLayout.
      Returns:
      A new Layout.ForMigLayout instance with the updated column constraints.
    • withChildConstraints

      public Layout.ForMigLayout withChildConstraints(sprouts.Association<Integer,MigAddConstraint> childConstraints)
      Returns a new Layout.ForMigLayout instance whose per-child component constraints are replaced by the supplied sorted Association.

      Keys are child indices (0 = first child, 1 = second, etc.); the association is sparse, so you only need to include entries for children you actually want to configure. Children whose index has no entry in the association are left untouched. An empty association means no constraints are stored in this layout object; any constraints previously applied to children by an earlier installFor call remain in the MigLayout until explicitly overwritten.

      Parameters:
      childConstraints - A sorted Association mapping child indices to the MigAddConstraint to apply.
      Returns:
      A new Layout.ForMigLayout with the updated child constraints.
    • withChildConstraints

      public Layout.ForMigLayout withChildConstraints(MigAddConstraint... childConstraints)
      Returns a new Layout.ForMigLayout instance whose per-child component constraints are replaced by the supplied varargs array of MigAddConstraints.

      The constraints are mapped positionally to the component's children: the first argument applies to the first child, the second to the second, and so on. Children at indices beyond the supplied array length are left untouched. Passing an empty array stores no constraints in this layout object; any constraints previously applied to children by an earlier installFor call remain in the MigLayout until explicitly overwritten.

      This is the most concise way to specify per-child constraints for common cases:

      
            import static swingtree.UI.*;
            // ...
            Layout.mig( FILL.and(WRAP(2)) )
                  .withChildConstraints( RIGHT, GROW_X, RIGHT, GROW_X )
        
      Parameters:
      childConstraints - The MigAddConstraints to apply to the component's children, in child-index order.
      Returns:
      A new Layout.ForMigLayout with the updated child constraints.
    • withChildConstraint

      public Layout.ForMigLayout withChildConstraint(int index, MigAddConstraint childConstraint)
      Returns a new Layout.ForMigLayout instance with the MigAddConstraint at the given child index replaced by the supplied value. All other child constraints and all other properties are copied unchanged.

      Because the underlying storage is a sparse Association, no padding is needed: the constraint is stored at exactly index, regardless of whether lower indices have entries.

      Parameters:
      index - The zero-based index of the child whose constraint to update. The first child has index 0.
      childConstraint - The new MigAddConstraint for the child at index.
      Returns:
      A new Layout.ForMigLayout with the updated child constraint at index.
      Throws:
      IndexOutOfBoundsException - if index is negative.
    • withChildConstraint

      public Layout.ForMigLayout withChildConstraint(int index, String childConstraint)
      Returns a new Layout.ForMigLayout instance with the MigAddConstraint at the given child index replaced by a constraint wrapping the supplied string. The string is converted via MigAddConstraint.of(String...) and then forwarded to withChildConstraint(int, MigAddConstraint).

      Because the underlying storage is a sparse Association, no padding is needed: the constraint is stored at exactly index, regardless of whether lower indices have entries.

      Parameters:
      index - The zero-based index of the child whose constraint to update.
      childConstraint - The MigLayout component-constraint string for the child.
      Returns:
      A new Layout.ForMigLayout with the updated child constraint at index.
      Throws:
      IndexOutOfBoundsException - if index is negative.
    • withAddedChildConstraint

      public Layout.ForMigLayout withAddedChildConstraint(MigAddConstraint childConstraint)
      Returns a new Layout.ForMigLayout instance with the supplied MigAddConstraint appended to the end of the existing child-constraint tuple. This is a convenient alternative to withChildConstraints(MigAddConstraint...) when building up constraints one at a time:
      
            import static swingtree.UI.*;
            // ...
            Layout.mig( FILL.and(WRAP(2)) )
                  .withAddedChildConstraint( RIGHT )
                  .withAddedChildConstraint( GROW_X )
                  .withAddedChildConstraint( RIGHT )
                  .withAddedChildConstraint( GROW_X )
        
      Parameters:
      childConstraint - The MigAddConstraint to append as the next child component constraint.
      Returns:
      A new Layout.ForMigLayout with the constraint appended.
    • withAddedChildConstraint

      public Layout.ForMigLayout withAddedChildConstraint(String childConstraint)
      Returns a new Layout.ForMigLayout instance with a MigAddConstraint wrapping the supplied string appended to the end of the existing child-constraint tuple. The string is converted via MigAddConstraint.of(String...) and then forwarded to withAddedChildConstraint(MigAddConstraint).
      Parameters:
      childConstraint - The MigLayout component-constraint string to append.
      Returns:
      A new Layout.ForMigLayout with the constraint appended.
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Layout
      Overrides:
      hashCode in class Object
      Returns:
      A hash code value for this layout.
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Layout
      Overrides:
      equals in class Object
      Parameters:
      o - The object to compare this layout to.
      Returns:
      true if the supplied object is a layout that is equal to this layout, false otherwise.
    • installFor

      public void installFor(JComponent component)
      Installs a MigLayout onto the supplied component and applies all constraints stored in this configuration.

      The installation proceeds in three phases:

      1. Self constraint — if this component's own style holds a layout constraint and its parent uses a MigLayout, that constraint is pushed into the parent layout so the component is correctly positioned within the parent grid.
      2. Layout manager — if none of the three constraint strings is empty, a MigLayout is installed (or updated in-place if one is already present) using the stored general, column, and row constraints.
      3. Child constraints — if the child-constraint tuple is non-empty, each stored MigAddConstraint is applied to the corresponding direct child of component (by position). Only entries that differ from what the MigLayout already has are written, and JComponent.revalidate() is called exactly once at the end if anything changed.
      Specified by:
      installFor in interface Layout
      Parameters:
      component - The component to install the MigLayout for.
    • toString

      public String toString()
      Overrides:
      toString in class Object