1. /*
  2. * @(#)GridBagConstraints.java 1.34 04/03/15
  3. *
  4. * Copyright 2004 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.34, 03/15/04
  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's
  191. * display area will be from <code>gridx</code> to the last
  192. * cell in the row.
  193. * Use <code>RELATIVE</code> to specify that the component's
  194. * display area will be from <code>gridx</code> to the next
  195. * to the last one in its row.
  196. * <p>
  197. * <code>gridwidth</code> should be non-negative and the default
  198. * value is 1.
  199. * @serial
  200. * @see #clone()
  201. * @see java.awt.GridBagConstraints#gridheight
  202. */
  203. public int gridwidth;
  204. /**
  205. * Specifies the number of cells in a column for the component's
  206. * display area.
  207. * <p>
  208. * Use <code>REMAINDER</code> to specify that the component's
  209. * display area will be from <code>gridy</code> to the last
  210. * cell in the column.
  211. * Use <code>RELATIVE</code> to specify that the component's
  212. * display area will be from <code>gridy</code> to the next
  213. * to the last one in its column.
  214. * <p>
  215. * <code>gridheight</code> should be a non-negative value and the
  216. * default value is 1.
  217. * @serial
  218. * @see #clone()
  219. * @see java.awt.GridBagConstraints#gridwidth
  220. */
  221. public int gridheight;
  222. /**
  223. * Specifies how to distribute extra horizontal space.
  224. * <p>
  225. * The grid bag layout manager calculates the weight of a column to
  226. * be the maximum <code>weightx</code> of all the components in a
  227. * column. If the resulting layout is smaller horizontally than the area
  228. * it needs to fill, the extra space is distributed to each column in
  229. * proportion to its weight. A column that has a weight of zero receives
  230. * no extra space.
  231. * <p>
  232. * If all the weights are zero, all the extra space appears between
  233. * the grids of the cell and the left and right edges.
  234. * <p>
  235. * The default value of this field is <code>0</code>.
  236. * <code>weightx</code> should be a non-negative value.
  237. * @serial
  238. * @see #clone()
  239. * @see java.awt.GridBagConstraints#weighty
  240. */
  241. public double weightx;
  242. /**
  243. * Specifies how to distribute extra vertical space.
  244. * <p>
  245. * The grid bag layout manager calculates the weight of a row to be
  246. * the maximum <code>weighty</code> of all the components in a row.
  247. * If the resulting layout is smaller vertically than the area it
  248. * needs to fill, the extra space is distributed to each row in
  249. * proportion to its weight. A row that has a weight of zero receives no
  250. * extra space.
  251. * <p>
  252. * If all the weights are zero, all the extra space appears between
  253. * the grids of the cell and the top and bottom edges.
  254. * <p>
  255. * The default value of this field is <code>0</code>.
  256. * <code>weighty</code> should be a non-negative value.
  257. * @serial
  258. * @see #clone()
  259. * @see java.awt.GridBagConstraints#weightx
  260. */
  261. public double weighty;
  262. /**
  263. * This field is used when the component is smaller than its display
  264. * area. It determines where, within the display area, to place the
  265. * component.
  266. * <p>
  267. * There are two kinds of possible values: relative and
  268. * absolute. Relative values are interpreted relative to the container's
  269. * component orientation property while absolute values are not. The absolute
  270. * values are:
  271. * <code>CENTER</code>, <code>NORTH</code>, <code>NORTHEAST</code>,
  272. * <code>EAST</code>, <code>SOUTHEAST</code>, <code>SOUTH</code>,
  273. * <code>SOUTHWEST</code>, <code>WEST</code>, and <code>NORTHWEST</code>.
  274. * The relative values are: <code>PAGE_START</code>, <code>PAGE_END</code>,
  275. * <code>LINE_START</code>, <code>LINE_END</code>,
  276. * <code>FIRST_LINE_START</code>, <code>FIRST_LINE_END</code>,
  277. * <code>LAST_LINE_START</code> and <code>LAST_LINE_END</code>.
  278. * The default value is <code>CENTER</code>.
  279. * @serial
  280. * @see #clone()
  281. * @see java.awt.ComponentOrientation
  282. */
  283. public int anchor;
  284. /**
  285. * This field is used when the component's display area is larger
  286. * than the component's requested size. It determines whether to
  287. * resize the component, and if so, how.
  288. * <p>
  289. * The following values are valid for <code>fill</code>:
  290. * <p>
  291. * <ul>
  292. * <li>
  293. * <code>NONE</code>: Do not resize the component.
  294. * <li>
  295. * <code>HORIZONTAL</code>: Make the component wide enough to fill
  296. * its display area horizontally, but do not change its height.
  297. * <li>
  298. * <code>VERTICAL</code>: Make the component tall enough to fill its
  299. * display area vertically, but do not change its width.
  300. * <li>
  301. * <code>BOTH</code>: Make the component fill its display area
  302. * entirely.
  303. * </ul>
  304. * <p>
  305. * The default value is <code>NONE</code>.
  306. * @serial
  307. * @see #clone()
  308. */
  309. public int fill;
  310. /**
  311. * This field specifies the external padding of the component, the
  312. * minimum amount of space between the component and the edges of its
  313. * display area.
  314. * <p>
  315. * The default value is <code>new Insets(0, 0, 0, 0)</code>.
  316. * @serial
  317. * @see #clone()
  318. */
  319. public Insets insets;
  320. /**
  321. * This field specifies the internal padding of the component, how much
  322. * space to add to the minimum width of the component. The width of
  323. * the component is at least its minimum width plus
  324. * <code>ipadx</code> pixels.
  325. * <p>
  326. * The default value is <code>0</code>.
  327. * @serial
  328. * @see #clone()
  329. * @see java.awt.GridBagConstraints#ipady
  330. */
  331. public int ipadx;
  332. /**
  333. * This field specifies the internal padding, that is, how much
  334. * space to add to the minimum height of the component. The height of
  335. * the component is at least its minimum height plus
  336. * <code>ipady</code> pixels.
  337. * <p>
  338. * The default value is 0.
  339. * @serial
  340. * @see #clone()
  341. * @see java.awt.GridBagConstraints#ipadx
  342. */
  343. public int ipady;
  344. /**
  345. * Temporary place holder for the x coordinate.
  346. * @serial
  347. */
  348. int tempX;
  349. /**
  350. * Temporary place holder for the y coordinate.
  351. * @serial
  352. */
  353. int tempY;
  354. /**
  355. * Temporary place holder for the Width of the component.
  356. * @serial
  357. */
  358. int tempWidth;
  359. /**
  360. * Temporary place holder for the Height of the component.
  361. * @serial
  362. */
  363. int tempHeight;
  364. /**
  365. * The minimum width of the component. It is used to calculate
  366. * <code>ipady</code>, where the default will be 0.
  367. * @serial
  368. * @see #ipady
  369. */
  370. int minWidth;
  371. /**
  372. * The minimum height of the component. It is used to calculate
  373. * <code>ipadx</code>, where the default will be 0.
  374. * @serial
  375. * @see #ipadx
  376. */
  377. int minHeight;
  378. /*
  379. * JDK 1.1 serialVersionUID
  380. */
  381. private static final long serialVersionUID = -1000070633030801713L;
  382. /**
  383. * Creates a <code>GridBagConstraint</code> object with
  384. * all of its fields set to their default value.
  385. */
  386. public GridBagConstraints () {
  387. gridx = RELATIVE;
  388. gridy = RELATIVE;
  389. gridwidth = 1;
  390. gridheight = 1;
  391. weightx = 0;
  392. weighty = 0;
  393. anchor = CENTER;
  394. fill = NONE;
  395. insets = new Insets(0, 0, 0, 0);
  396. ipadx = 0;
  397. ipady = 0;
  398. }
  399. /**
  400. * Creates a <code>GridBagConstraints</code> object with
  401. * all of its fields set to the passed-in arguments.
  402. *
  403. * Note: Because the use of this constructor hinders readability
  404. * of source code, this constructor should only be used by
  405. * automatic source code generation tools.
  406. *
  407. * @param gridx The initial gridx value.
  408. * @param gridy The initial gridy value.
  409. * @param gridwidth The initial gridwidth value.
  410. * @param gridheight The initial gridheight value.
  411. * @param weightx The initial weightx value.
  412. * @param weighty The initial weighty value.
  413. * @param anchor The initial anchor value.
  414. * @param fill The initial fill value.
  415. * @param insets The initial insets value.
  416. * @param ipadx The initial ipadx value.
  417. * @param ipady The initial ipady value.
  418. *
  419. * @see java.awt.GridBagConstraints#gridx
  420. * @see java.awt.GridBagConstraints#gridy
  421. * @see java.awt.GridBagConstraints#gridwidth
  422. * @see java.awt.GridBagConstraints#gridheight
  423. * @see java.awt.GridBagConstraints#weightx
  424. * @see java.awt.GridBagConstraints#weighty
  425. * @see java.awt.GridBagConstraints#anchor
  426. * @see java.awt.GridBagConstraints#fill
  427. * @see java.awt.GridBagConstraints#insets
  428. * @see java.awt.GridBagConstraints#ipadx
  429. * @see java.awt.GridBagConstraints#ipady
  430. *
  431. * @since 1.2
  432. */
  433. public GridBagConstraints(int gridx, int gridy,
  434. int gridwidth, int gridheight,
  435. double weightx, double weighty,
  436. int anchor, int fill,
  437. Insets insets, int ipadx, int ipady) {
  438. this.gridx = gridx;
  439. this.gridy = gridy;
  440. this.gridwidth = gridwidth;
  441. this.gridheight = gridheight;
  442. this.fill = fill;
  443. this.ipadx = ipadx;
  444. this.ipady = ipady;
  445. this.insets = insets;
  446. this.anchor = anchor;
  447. this.weightx = weightx;
  448. this.weighty = weighty;
  449. }
  450. /**
  451. * Creates a copy of this grid bag constraint.
  452. * @return a copy of this grid bag constraint
  453. */
  454. public Object clone () {
  455. try {
  456. GridBagConstraints c = (GridBagConstraints)super.clone();
  457. c.insets = (Insets)insets.clone();
  458. return c;
  459. } catch (CloneNotSupportedException e) {
  460. // this shouldn't happen, since we are Cloneable
  461. throw new InternalError();
  462. }
  463. }
  464. }