PageRenderTime 123ms CodeModel.GetById 112ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/util/ConstraintFactory.java

#
Java | 199 lines | 123 code | 20 blank | 56 comment | 8 complexity | 402e8e856fd6c81c6ace01fb3d9e2f94 MD5 | raw file
  1package common.gui.util;
  2
  3import javax.swing.*;
  4import java.awt.*;
  5
  6/**
  7 * Provides easier creation of GridBagConstraints
  8 *
  9 * @author    mace
 10 * @created   October 22, 2002
 11 */
 12public class ConstraintFactory {
 13	public final static int NONE = GridBagConstraints.NONE;
 14	public final static int BOTH = GridBagConstraints.BOTH;
 15	public final static int HORIZONTAL = GridBagConstraints.HORIZONTAL;
 16	public final static int H = HORIZONTAL;
 17	public final static int VERTICAL = GridBagConstraints.VERTICAL;
 18	public final static int V = VERTICAL;
 19	public final static int RELATIVE = GridBagConstraints.RELATIVE;
 20	public final static int REMAINDER = GridBagConstraints.REMAINDER;
 21	public final static int CENTER = GridBagConstraints.CENTER;
 22	public final static int N = GridBagConstraints.NORTH;
 23	public final static int E = GridBagConstraints.EAST;
 24	public final static int S = GridBagConstraints.SOUTH;
 25	public final static int W = GridBagConstraints.WEST;
 26	public final static int NW = GridBagConstraints.NORTHWEST;
 27	public final static int NE = GridBagConstraints.NORTHEAST;
 28	public final static int SW = GridBagConstraints.SOUTHWEST;
 29	public final static int SE = GridBagConstraints.SOUTHEAST;
 30
 31	protected int DEFAULT_WEIGHTX = 100;
 32	protected int DEFAULT_WEIGHTY = 100;
 33	protected int DEFAULT_ANCHOR = NW;
 34	protected int DEFAULT_FILL = BOTH;
 35	protected Insets DEFAULT_INSETS = new Insets(0, 0, 0, 0);
 36	protected int DEFAULT_IPADX = 0;
 37	protected int DEFAULT_IPADY = 0;
 38
 39	protected int LAST_X = 0;
 40	protected int LAST_Y = 0;
 41	protected int LAST_WIDTH = 0;
 42	protected int LAST_HEIGHT = 0;
 43
 44	public ConstraintFactory() { }
 45
 46	private GridBagConstraints buildConstraints() {
 47		GridBagConstraints constraints = new GridBagConstraints();
 48		constraints.weightx = DEFAULT_WEIGHTX;
 49		constraints.weighty = DEFAULT_WEIGHTY;
 50		constraints.anchor = DEFAULT_ANCHOR;
 51		constraints.fill = DEFAULT_FILL;
 52		constraints.insets = DEFAULT_INSETS;
 53		constraints.ipadx = DEFAULT_IPADX;
 54		constraints.ipady = DEFAULT_IPADY;
 55		return constraints;
 56	}
 57
 58	/**
 59	 * Build basic constraints. weights default to 100, anchor defaults to NW, fill
 60	 * defaults to BOTH. If a negative value is given, the last non-negative value
 61	 * will be re-used
 62	 *
 63	 * @param x       an int specifying the object's x grid position
 64	 * @param y       an int specifying the object's y grid position
 65	 * @param width   an int specifying the object's width
 66	 * @param height  an int specifying the object's height
 67	 * @return        a GridBagConstraints object
 68	 */
 69	public GridBagConstraints buildConstraints
 70			(int x, int y, int width, int height) {
 71		GridBagConstraints constraints = buildConstraints();
 72		if (x < 0) {
 73			constraints.gridx = LAST_X;
 74		} else {
 75			constraints.gridx = LAST_X = x;
 76		}
 77		if (y < 0) {
 78			constraints.gridx = LAST_Y;
 79		} else {
 80			constraints.gridy = LAST_Y = y;
 81		}
 82		if (width < 0) {
 83			constraints.gridwidth = LAST_WIDTH;
 84		} else {
 85			constraints.gridwidth = LAST_WIDTH = width;
 86		}
 87		if (height < 0) {
 88			constraints.gridheight = LAST_HEIGHT;
 89		} else {
 90			constraints.gridheight = LAST_HEIGHT = height;
 91		}
 92		return constraints;
 93	}
 94
 95	/**
 96	 * Builds basic constraints + anchor and fill constraints. Weights default to
 97	 * 100
 98	 *
 99	 * @param x       an int specifying the object's x grid position
100	 * @param y       an int specifying the object's y grid position
101	 * @param width   an int specifying the object's width
102	 * @param height  an int specifying the object's height
103	 * @param anchor  an int specifying the position to place the object
104	 * @param fill    an int specifying which directions to fill
105	 * @return        a GridBagConstraints object
106	 */
107	public GridBagConstraints buildConstraints
108			(int x, int y, int width, int height, int anchor, int fill) {
109		GridBagConstraints constraints = buildConstraints(x, y, width, height);
110		constraints.anchor = anchor;
111		constraints.fill = fill;
112		return constraints;
113	}
114
115	/**
116	 * Builds basic constraints + anchor,fill, and weights.
117	 *
118	 * @param x        an int specifying the object's x grid position
119	 * @param y        an int specifying the object's y grid position
120	 * @param width    an int specifying the object's width
121	 * @param height   an int specifying the object's height
122	 * @param anchor   an int specifying the position to place the object
123	 * @param fill     an int specifying which directions to fill
124	 * @param xweight  an int specifying the object's x weight
125	 * @param yweight  an int specifying the object's y weight
126	 * @return         a GridBagConstraints object
127	 */
128	public GridBagConstraints buildConstraints
129			(int x, int y, int width, int height, int anchor, int fill, int xweight,
130			int yweight) {
131		GridBagConstraints constraints
132				 = buildConstraints(x, y, width, height, anchor, fill);
133		constraints.weightx = xweight;
134		constraints.weighty = yweight;
135		return constraints;
136	}
137
138	/**
139	 * Builds basic constraints + anchor,fill, weights, and insets.
140	 *
141	 * @param x        an int specifying the object's x grid position
142	 * @param y        an int specifying the object's y grid position
143	 * @param width    an int specifying the object's width
144	 * @param height   an int specifying the object's height
145	 * @param anchor   an int specifying the position to place the object
146	 * @param fill     an int specifying which directions to fill
147	 * @param weightx  an int specifying the object's x weight
148	 * @param weighty  an int specifying the object's y weight
149	 * @param i        <TT>Insets</TT> to use for this object
150	 * @return         a GridBagConstraints object
151	 */
152	public GridBagConstraints buildConstraints
153			(int x, int y, int width, int height, int anchor, int fill, int weightx,
154			int weighty, Insets i) {
155		GridBagConstraints constraints
156				 = buildConstraints(x, y, width, height, anchor, fill, weightx, weighty);
157		constraints.insets = i;
158		return constraints;
159	}
160
161	public void setDefaultAnchor(int i) {
162		DEFAULT_ANCHOR = i;
163	}
164
165	public void setDefaultFill(int i) {
166		DEFAULT_FILL = i;
167	}
168
169	public void setDefaultWeights(int x, int y) {
170		setDefaultWeightX(x);
171		setDefaultWeightY(y);
172	}
173
174	public void setDefaultWeightX(int i) {
175		DEFAULT_WEIGHTX = i;
176	}
177
178	public void setDefaultWeightY(int i) {
179		DEFAULT_WEIGHTY = i;
180	}
181
182	public void setDefaultInsets(Insets i) {
183		DEFAULT_INSETS = i;
184	}
185
186	public void setDefaultInternalPaddingX(int i) {
187		DEFAULT_IPADX = i;
188	}
189
190	public void setDefaultInternalPaddingY(int i) {
191		DEFAULT_IPADY = i;
192	}
193
194	public void setDefaultInternalPadding(int i) {
195		setDefaultInternalPaddingX(i);
196		setDefaultInternalPaddingY(i);
197	}
198}
199