LayoutConstraint.java

  1. package swingtree.layout;

  2. /**
  3.  *  A wrapper for mig layout constraint string to avoid the inherent brittleness of strings...
  4.  *  which can be merged with other instances of this class through the {@link #and(LayoutConstraint)} method,
  5.  *  which is in essence a wither method.
  6.  *  <br>
  7.  *  Here how this class would typically be used in a swing-tree UI:
  8.  *  <pre>{@code
  9.  *      import static swingtree.UI.*;
  10.  *
  11.  *      public class MyView extends JPanel {
  12.  *          public MyView() {
  13.  *              of(this).withLayout(FILL.and(GROW).and(WRAP(3)))
  14.  *              .add(LEFT, label("Name:") )
  15.  *              .add(GROW.and(SPAN), textField("name") )
  16.  *              .add(LEFT, label("Address:") )
  17.  *              .add(GROW.and(SPAN), textField("address") )
  18.  *           }
  19.  *      }
  20.  *  }</pre>
  21.  *  As you can see this class is not used directly, but rather in the form of static constants
  22.  *  as part of the UI class.
  23.  *  You can define your own mig layout constraints as static constants in your own code
  24.  *  by using the {@link #of(String...)} method.
  25.  */
  26. public final class LayoutConstraint extends AbstractConstraint
  27. {
  28.     /**
  29.      *  Create a new LayoutConstraint with the given layout constraints
  30.      *  @param layoutConstraints the layout constraints in the form of a string array.
  31.      *  @return a new LayoutConstraint, which may represent a single component or a group of layout constraints.
  32.      */
  33.     public static LayoutConstraint of( String... layoutConstraints ) { return new LayoutConstraint(layoutConstraints); }

  34.     private LayoutConstraint( String[] layoutConstraints ) { super(layoutConstraints); }

  35.     private LayoutConstraint() { super(); }

  36.     /**
  37.      *  Create a new {@link LayoutConstraint} with the provided {@link LayoutConstraint} merged with this one.
  38.      *
  39.      * @param attr the {@link LayoutConstraint} to merge with this one
  40.      * @return a new {@link LayoutConstraint} with the provided {@link LayoutConstraint} merged with this one
  41.      */
  42.     public LayoutConstraint and( LayoutConstraint attr ) { return (LayoutConstraint) _and( attr, new LayoutConstraint() ); }

  43.     /**
  44.      *  Create a new {@link LayoutConstraint} with the provided layout constraints merged with this one.
  45.      *
  46.      * @param layoutConstraints the string layout constraints to merge with this one
  47.      * @return a new {@link LayoutConstraint} with the provided layout constraints merged with this one
  48.      */
  49.     public LayoutConstraint and( String... layoutConstraints ) {
  50.         LayoutConstraint attr = new LayoutConstraint( layoutConstraints );
  51.         return (LayoutConstraint) _and( attr, new LayoutConstraint() );
  52.     }
  53. }