package waymaker.gen; // Copyright © 2015 Michael Allan. Licence MIT. import android.widget.RelativeLayout; import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; /**

A device to hold the parameters of a relative layout during their formation. It serves for * convenience and to improve the clarity of code. For example:

*
  *     RelativeLayout.LayoutParams p =
  *       new RelativeLayout.LayoutParams( WRAP_CONTENT, WRAP_CONTENT );
  *     p.addRule( ALIGN_PARENT_RIGHT );
  *     p.addRule( ALIGN_PARENT_BOTTOM );
  *     addView( view, p );
* *

The same layout may be expressed with less clutter by using a jig:

*
  *    addView( view, jigRelative().rule(ALIGN_PARENT_BOTTOM)
  *      .rule(ALIGN_PARENT_RIGHT).unjig() );
*/ public final class RelativeLayoutJig { private RelativeLayoutJig() {} /** The single instance of RelativeLayoutJig. */ public static RelativeLayoutJig i() { return instance; } private static final RelativeLayoutJig instance = new RelativeLayoutJig(); /** Constructs a set of layout parameters with default dimensions, lays them in the {@linkplain #i() * single instance} of the jig, and returns the jig. Specifies both the width and height as WRAP_CONTENT. */ public static RelativeLayoutJig jigRelative() { return jigRelative( WRAP_CONTENT, WRAP_CONTENT ); } /** Constructs a set of layout parameters, lays them in the {@linkplain #i() single instance} of the * jig, and returns the jig. * * @see ViewGroup.LayoutParams(int, int) */ // @see RelativeLayout.LayoutParams(int, int) //// right, but it says nothing public static RelativeLayoutJig jigRelative( final int width, final int height ) { instance.params = new RelativeLayout.LayoutParams( width, height ); return instance; } /** Adds a rule to the parameters held in this jig, and returns the jig. * * @see addRule(int) * @throws NullPointerException if this jig {@linkplain #jigRelative() holds no parameters}. */ public RelativeLayoutJig rule( final int verb ) { params.addRule( verb ); return this; } /** Adds a rule to the parameters held in this jig, and returns the jig. * * @see addRule(int,int) * @throws NullPointerException if this jig {@linkplain #jigRelative() holds no parameters}. */ public RelativeLayoutJig rule( final int verb, final int anchor ) { params.addRule( verb, anchor ); return this; } /** Removes and returns the layout parameters from this jig. */ public RelativeLayout.LayoutParams unjig() { final RelativeLayout.LayoutParams p = params; params = null; // release to garbage collector return p; } //// P r i v a t e ///////////////////////////////////////////////////////////////////////////////////// private RelativeLayout.LayoutParams params; }