1. /*
  2. * @(#)GridBagConstraints.java 1.31 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. /**
  9. * The <code>GridBagConstraints</code> class specifies constraints
  10. * for components that are laid out using the
  11. * <code>GridBagLayout</code> class.
  12. *
  13. * @version 1.31, 01/23/03
  14. * @author Doug Stein
  15. * @see java.awt.GridBagLayout
  16. * @since JDK1.0
  17. */
  18. public class GridBagConstraints implements Cloneable, java.io.Serializable {
  19. /**
  20. * Specifies that this component is the next-to-last component in its
  21. * column or row (<code>gridwidth</code>, <code>gridheight</code>),
  22. * or that this component be placed next to the previously added
  23. * component (<code>gridx</code>, <code>gridy</code>).
  24. * @see java.awt.GridBagConstraints#gridwidth
  25. * @see java.awt.GridBagConstraints#gridheight
  26. * @see java.awt.GridBagConstraints#gridx
  27. * @see java.awt.GridBagConstraints#gridy
  28. */
  29. public static final int RELATIVE = -1;
  30. /**
  31. * Specifies that this component is the
  32. * last component in its column or row.
  33. */
  34. public static final int REMAINDER = 0;
  35. /**
  36. * Do not resize the component.
  37. */
  38. public static final int NONE = 0;
  39. /**
  40. * Resize the component both horizontally and vertically.
  41. */
  42. public static final int BOTH = 1;
  43. /**
  44. * Resize the component horizontally but not vertically.
  45. */
  46. public static final int HORIZONTAL = 2;
  47. /**
  48. * Resize the component vertically but not horizontally.
  49. */
  50. public static final int VERTICAL = 3;
  51. /**
  52. * Put the component in the center of its display area.
  53. */
  54. public static final int CENTER = 10;
  55. /**
  56. * Put the component at the top of its display area,
  57. * centered horizontally.
  58. */
  59. public static final int NORTH = 11;
  60. /**
  61. * Put the component at the top-right corner of its display area.
  62. */
  63. public static final int NORTHEAST = 12;
  64. /**
  65. * Put the component on the right side of its display area,
  66. * centered vertically.
  67. */
  68. public static final int EAST = 13;
  69. /**
  70. * Put the component at the bottom-right corner of its display area.
  71. */
  72. public static final int SOUTHEAST = 14;
  73. /**
  74. * Put the component at the bottom of its display area, centered
  75. * horizontally.
  76. */
  77. public static final int SOUTH = 15;
  78. /**
  79. * Put the component at the bottom-left corner of its display area.
  80. */
  81. public static final int SOUTHWEST = 16;
  82. /**
  83. * Put the component on the left side of its display area,
  84. * centered vertically.
  85. */
  86. public static final int WEST = 17;
  87. /**
  88. * Put the component at the top-left corner of its display area.
  89. */
  90. public static final int NORTHWEST = 18;
  91. /**
  92. * Place the component centered along the edge of its display area
  93. * associated with the start of a page for the current
  94. * <code>ComponentOrienation</code>. Equal to NORTH for horizontal
  95. * orientations.
  96. */
  97. public static final int PAGE_START = 19;
  98. /**
  99. * Place the component centered along the edge of its display area
  100. * associated with the end of a page for the current
  101. * <code>ComponentOrienation</code>. Equal to SOUTH for horizontal
  102. * orientations.
  103. */
  104. public static final int PAGE_END = 20;
  105. /**
  106. * Place the component centered along the edge of its display area where
  107. * lines of text would normally begin for the current
  108. * <code>ComponentOrienation</code>. Equal to WEST for horizontal,
  109. * left-to-right orientations and EAST for horizontal, right-to-left
  110. * orientations.
  111. */
  112. public static final int LINE_START = 21;
  113. /**
  114. * Place the component centered along the edge of its display area where
  115. * lines of text would normally end for the current
  116. * <code>ComponentOrienation</code>. Equal to EAST for horizontal,
  117. * left-to-right orientations and WEST for horizontal, right-to-left
  118. * orientations.
  119. */
  120. public static final int LINE_END = 22;
  121. /**
  122. * Place the component in the corner of its display area where
  123. * the first line of text on a page would normally begin for the current
  124. * <code>ComponentOrienation</code>. Equal to NORTHWEST for horizontal,
  125. * left-to-right orientations and NORTHEAST for horizontal, right-to-left
  126. * orientations.
  127. */
  128. public static final int FIRST_LINE_START = 23;
  129. /**
  130. * Place the component in the corner of its display area where
  131. * the first line of text on a page would normally end for the current
  132. * <code>ComponentOrienation</code>. Equal to NORTHEAST for horizontal,
  133. * left-to-right orientations and NORTHWEST for horizontal, right-to-left
  134. * orientations.
  135. */
  136. public static final int FIRST_LINE_END = 24;
  137. /**
  138. * Place the component in the corner of its display area where
  139. * the last line of text on a page would normally start for the current
  140. * <code>ComponentOrienation</code>. Equal to SOUTHWEST for horizontal,
  141. * left-to-right orientations and SOUTHEAST for horizontal, right-to-left
  142. * orientations.
  143. */
  144. public static final int LAST_LINE_START = 25;
  145. /**
  146. * Place the component in the corner of its display area where
  147. * the last line of text on a page would normally end for the current
  148. * <code>ComponentOrienation</code>. Equal to SOUTHEAST for horizontal,
  149. * left-to-right orientations and SOUTHWEST for horizontal, right-to-left
  150. * orientations.
  151. */
  152. public static final int LAST_LINE_END = 26;
  153. /**
  154. * Specifies the cell containing the leading edge of the component's
  155. * display area, where the first cell in a row has <code>gridx=0</code>.
  156. * The leading edge of a component's display area is its left edge for
  157. * a horizontal, left-to-right container and its right edge for a
  158. * horizontal, right-to-left container.
  159. * The value
  160. * <code>RELATIVE</code> specifies that the component be placed
  161. * immediately following the component that was added to the container
  162. * just before this component was added.
  163. * <p>
  164. * The default value is <code>RELATIVE</code>.
  165. * <code>gridx</code> should be a non-negative value.
  166. * @serial
  167. * @see #clone()
  168. * @see java.awt.GridBagConstraints#gridy
  169. * @see java.awt.ComponentOrientation
  170. */
  171. public int gridx;
  172. /**
  173. * Specifies the cell at the top of the component's display area,
  174. * where the topmost cell has <code>gridy=0</code>. The value
  175. * <code>RELATIVE</code> specifies that the component be placed just
  176. * below the component that was added to the container just before
  177. * this component was added.
  178. * <p>
  179. * The default value is <code>RELATIVE</code>.
  180. * <code>gridy</code> should be a non-negative value.
  181. * @serial
  182. * @see #clone()
  183. * @see java.awt.GridBagConstraints#gridx
  184. */
  185. public int gridy;
  186. /**
  187. * Specifies the number of cells in a row for the component's
  188. * display area.
  189. * <p>
  190. * Use <code>REMAINDER</code> to specify that the component be the
  191. * last one in its row. Use <code>RELATIVE</code> to specify that the
  192. * component be the next-to-last one in its row.
  193. * <p>
  194. * <code>gridwidth</code> should be non-negative and the default
  195. * value is 1.
  196. * @serial
  197. * @see #clone()
  198. * @see java.awt.GridBagConstraints#gridheight
  199. */
  200. public int gridwidth;
  201. /**
  202. * Specifies the number of cells in a column for the component's
  203. * display area.
  204. * <p>
  205. * Use <code>REMAINDER</code> to specify that the component be the
  206. * last one in its column. Use <code>RELATIVE</code> to specify that
  207. * the component be the next-to-last one in its column.
  208. * <p>
  209. * <code>gridheight</code> should be a non-negative value and the
  210. * default value is 1.
  211. * @serial
  212. * @see #clone()
  213. * @see java.awt.GridBagConstraints#gridwidth
  214. */
  215. public int gridheight;
  216. /**
  217. * Specifies how to distribute extra horizontal space.
  218. * <p>
  219. * The grid bag layout manager calculates the weight of a column to
  220. * be the maximum <code>weightx</code> of all the components in a
  221. * column. If the resulting layout is smaller horizontally than the area
  222. * it needs to fill, the extra space is distributed to each column in
  223. * proportion to its weight. A column that has a weight of zero receives
  224. * no extra space.
  225. * <p>
  226. * If all the weights are zero, all the extra space appears between
  227. * the grids of the cell and the left and right edges.
  228. * <p>
  229. * The default value of this field is <code>0</code>.
  230. * <code>weightx</code> should be a non-negative value.
  231. * @serial
  232. * @see #clone()
  233. * @see java.awt.GridBagConstraints#weighty
  234. */
  235. public double weightx;
  236. /**
  237. * Specifies how to distribute extra vertical space.
  238. * <p>
  239. * The grid bag layout manager calculates the weight of a row to be
  240. * the maximum <code>weighty</code> of all the components in a row.
  241. * If the resulting layout is smaller vertically than the area it
  242. * needs to fill, the extra space is distributed to each row in
  243. * proportion to its weight. A row that has a weight of zero receives no
  244. * extra space.
  245. * <p>
  246. * If all the weights are zero, all the extra space appears between
  247. * the grids of the cell and the top and bottom edges.
  248. * <p>
  249. * The default value of this field is <code>0</code>.
  250. * <code>weighty</code> should be a non-negative value.
  251. * @serial
  252. * @see #clone()
  253. * @see java.awt.GridBagConstraints#weightx
  254. */
  255. public double weighty;
  256. /**
  257. * This field is used when the component is smaller than its display
  258. * area. It determines where, within the display area, to place the
  259. * component.
  260. * <p>
  261. * There are two kinds of possible values: relative and
  262. * absolute. Relative values are interpreted relative to the container's
  263. * component orientation property while absolute values are not. The absolute
  264. * values are:
  265. * <code>CENTER</code>, <code>NORTH</code>, <code>NORTHEAST</code>,
  266. * <code>EAST</code>, <code>SOUTHEAST</code>, <code>SOUTH</code>,
  267. * <code>SOUTHWEST</code>, <code>WEST</code>, and <code>NORTHWEST</code>.
  268. * The relative values are: <code>PAGE_START</code>, <code>PAGE_END</code>,
  269. * <code>LINE_START</code>, <code>LINE_END</code>,
  270. * <code>FIRST_LINE_START</code>, <code>FIRST_LINE_END</code>,
  271. * <code>LAST_LINE_START</code> and <code>LAST_LINE_END</code>.
  272. * The default value is <code>CENTER</code>.
  273. * @serial
  274. * @see #clone()
  275. * @see java.awt.ComponentOrientation
  276. */
  277. public int anchor;
  278. /**
  279. * This field is used when the component's display area is larger
  280. * than the component's requested size. It determines whether to
  281. * resize the component, and if so, how.
  282. * <p>
  283. * The following values are valid for <code>fill</code>:
  284. * <p>
  285. * <ul>
  286. * <li>
  287. * <code>NONE</code>: Do not resize the component.
  288. * <li>
  289. * <code>HORIZONTAL</code>: Make the component wide enough to fill
  290. * its display area horizontally, but do not change its height.
  291. * <li>
  292. * <code>VERTICAL</code>: Make the component tall enough to fill its
  293. * display area vertically, but do not change its width.
  294. * <li>
  295. * <code>BOTH</code>: Make the component fill its display area
  296. * entirely.
  297. * </ul>
  298. * <p>
  299. * The default value is <code>NONE</code>.
  300. * @serial
  301. * @see #clone()
  302. */
  303. public int fill;
  304. /**
  305. * This field specifies the external padding of the component, the
  306. * minimum amount of space between the component and the edges of its
  307. * display area.
  308. * <p>
  309. * The default value is <code>new Insets(0, 0, 0, 0)</code>.
  310. * @serial
  311. * @see #clone()
  312. */
  313. public Insets insets;
  314. /**
  315. * This field specifies the internal padding of the component, how much
  316. * space to add to the minimum width of the component. The width of
  317. * the component is at least its minimum width plus
  318. * <code>(ipadx * 2)</code> pixels.
  319. * <p>
  320. * The default value is <code>0</code>.
  321. * @serial
  322. * @see #clone()
  323. * @see java.awt.GridBagConstraints#ipady
  324. */
  325. public int ipadx;
  326. /**
  327. * This field specifies the internal padding, that is, how much
  328. * space to add to the minimum height of the component. The height of
  329. * the component is at least its minimum height plus
  330. * <code>(ipady * 2)</code> pixels.
  331. * <p>
  332. * The default value is 0.
  333. * @serial
  334. * @see #clone()
  335. * @see java.awt.GridBagConstraints#ipadx
  336. */
  337. public int ipady;
  338. /**
  339. * Temporary place holder for the x coordinate.
  340. * @serial
  341. */
  342. int tempX;
  343. /**
  344. * Temporary place holder for the y coordinate.
  345. * @serial
  346. */
  347. int tempY;
  348. /**
  349. * Temporary place holder for the Width of the component.
  350. * @serial
  351. */
  352. int tempWidth;
  353. /**
  354. * Temporary place holder for the Height of the component.
  355. * @serial
  356. */
  357. int tempHeight;
  358. /**
  359. * The minimum width of the component. It is used to calculate
  360. * <code>ipady</code>, where the default will be 0.
  361. * @serial
  362. * @see #ipady
  363. */
  364. int minWidth;
  365. /**
  366. * The minimum height of the component. It is used to calculate
  367. * <code>ipadx</code>, where the default will be 0.
  368. * @serial
  369. * @see #ipadx
  370. */
  371. int minHeight;
  372. /*
  373. * JDK 1.1 serialVersionUID
  374. */
  375. private static final long serialVersionUID = -1000070633030801713L;
  376. /**
  377. * Creates a <code>GridBagConstraint</code> object with
  378. * all of its fields set to their default value.
  379. */
  380. public GridBagConstraints () {
  381. gridx = RELATIVE;
  382. gridy = RELATIVE;
  383. gridwidth = 1;
  384. gridheight = 1;
  385. weightx = 0;
  386. weighty = 0;
  387. anchor = CENTER;
  388. fill = NONE;
  389. insets = new Insets(0, 0, 0, 0);
  390. ipadx = 0;
  391. ipady = 0;
  392. }
  393. /**
  394. * Creates a <code>GridBagConstraints</code> object with
  395. * all of its fields set to the passed-in arguments.
  396. *
  397. * Note: Because the use of this constructor hinders readability
  398. * of source code, this constructor should only be used by
  399. * automatic source code generation tools.
  400. *
  401. * @param gridx The initial gridx value.
  402. * @param gridy The initial gridy value.
  403. * @param gridwidth The initial gridwidth value.
  404. * @param gridheight The initial gridheight value.
  405. * @param weightx The initial weightx value.
  406. * @param weighty The initial weighty value.
  407. * @param anchor The initial anchor value.
  408. * @param fill The initial fill value.
  409. * @param insets The initial insets value.
  410. * @param ipadx The initial ipadx value.
  411. * @param ipady The initial ipady value.
  412. *
  413. * @see java.awt.GridBagConstraints#gridx
  414. * @see java.awt.GridBagConstraints#gridy
  415. * @see java.awt.GridBagConstraints#gridwidth
  416. * @see java.awt.GridBagConstraints#gridheight
  417. * @see java.awt.GridBagConstraints#weightx
  418. * @see java.awt.GridBagConstraints#weighty
  419. * @see java.awt.GridBagConstraints#anchor
  420. * @see java.awt.GridBagConstraints#fill
  421. * @see java.awt.GridBagConstraints#insets
  422. * @see java.awt.GridBagConstraints#ipadx
  423. * @see java.awt.GridBagConstraints#ipady
  424. *
  425. * @since 1.2
  426. */
  427. public GridBagConstraints(int gridx, int gridy,
  428. int gridwidth, int gridheight,
  429. double weightx, double weighty,
  430. int anchor, int fill,
  431. Insets insets, int ipadx, int ipady) {
  432. this.gridx = gridx;
  433. this.gridy = gridy;
  434. this.gridwidth = gridwidth;
  435. this.gridheight = gridheight;
  436. this.fill = fill;
  437. this.ipadx = ipadx;
  438. this.ipady = ipady;
  439. this.insets = insets;
  440. this.anchor = anchor;
  441. this.weightx = weightx;
  442. this.weighty = weighty;
  443. }
  444. /**
  445. * Creates a copy of this grid bag constraint.
  446. * @return a copy of this grid bag constraint
  447. */
  448. public Object clone () {
  449. try {
  450. GridBagConstraints c = (GridBagConstraints)super.clone();
  451. c.insets = (Insets)insets.clone();
  452. return c;
  453. } catch (CloneNotSupportedException e) {
  454. // this shouldn't happen, since we are Cloneable
  455. throw new InternalError();
  456. }
  457. }
  458. }