001 /*
002 * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package java.awt;
026
027 /**
028 * The <code>GridBagConstraints</code> class specifies constraints
029 * for components that are laid out using the
030 * <code>GridBagLayout</code> class.
031 *
032 * @version 1.47, 05/05/07
033 * @author Doug Stein
034 * @author Bill Spitzak (orignial NeWS & OLIT implementation)
035 * @see java.awt.GridBagLayout
036 * @since JDK1.0
037 */
038 public class GridBagConstraints implements Cloneable,
039 java.io.Serializable {
040
041 /**
042 * Specifies that this component is the next-to-last component in its
043 * column or row (<code>gridwidth</code>, <code>gridheight</code>),
044 * or that this component be placed next to the previously added
045 * component (<code>gridx</code>, <code>gridy</code>).
046 * @see java.awt.GridBagConstraints#gridwidth
047 * @see java.awt.GridBagConstraints#gridheight
048 * @see java.awt.GridBagConstraints#gridx
049 * @see java.awt.GridBagConstraints#gridy
050 */
051 public static final int RELATIVE = -1;
052
053 /**
054 * Specifies that this component is the
055 * last component in its column or row.
056 */
057 public static final int REMAINDER = 0;
058
059 /**
060 * Do not resize the component.
061 */
062 public static final int NONE = 0;
063
064 /**
065 * Resize the component both horizontally and vertically.
066 */
067 public static final int BOTH = 1;
068
069 /**
070 * Resize the component horizontally but not vertically.
071 */
072 public static final int HORIZONTAL = 2;
073
074 /**
075 * Resize the component vertically but not horizontally.
076 */
077 public static final int VERTICAL = 3;
078
079 /**
080 * Put the component in the center of its display area.
081 */
082 public static final int CENTER = 10;
083
084 /**
085 * Put the component at the top of its display area,
086 * centered horizontally.
087 */
088 public static final int NORTH = 11;
089
090 /**
091 * Put the component at the top-right corner of its display area.
092 */
093 public static final int NORTHEAST = 12;
094
095 /**
096 * Put the component on the right side of its display area,
097 * centered vertically.
098 */
099 public static final int EAST = 13;
100
101 /**
102 * Put the component at the bottom-right corner of its display area.
103 */
104 public static final int SOUTHEAST = 14;
105
106 /**
107 * Put the component at the bottom of its display area, centered
108 * horizontally.
109 */
110 public static final int SOUTH = 15;
111
112 /**
113 * Put the component at the bottom-left corner of its display area.
114 */
115 public static final int SOUTHWEST = 16;
116
117 /**
118 * Put the component on the left side of its display area,
119 * centered vertically.
120 */
121 public static final int WEST = 17;
122
123 /**
124 * Put the component at the top-left corner of its display area.
125 */
126 public static final int NORTHWEST = 18;
127
128 /**
129 * Place the component centered along the edge of its display area
130 * associated with the start of a page for the current
131 * <code>ComponentOrienation</code>. Equal to NORTH for horizontal
132 * orientations.
133 */
134 public static final int PAGE_START = 19;
135
136 /**
137 * Place the component centered along the edge of its display area
138 * associated with the end of a page for the current
139 * <code>ComponentOrienation</code>. Equal to SOUTH for horizontal
140 * orientations.
141 */
142 public static final int PAGE_END = 20;
143
144 /**
145 * Place the component centered along the edge of its display area where
146 * lines of text would normally begin for the current
147 * <code>ComponentOrienation</code>. Equal to WEST for horizontal,
148 * left-to-right orientations and EAST for horizontal, right-to-left
149 * orientations.
150 */
151 public static final int LINE_START = 21;
152
153 /**
154 * Place the component centered along the edge of its display area where
155 * lines of text would normally end for the current
156 * <code>ComponentOrienation</code>. Equal to EAST for horizontal,
157 * left-to-right orientations and WEST for horizontal, right-to-left
158 * orientations.
159 */
160 public static final int LINE_END = 22;
161
162 /**
163 * Place the component in the corner of its display area where
164 * the first line of text on a page would normally begin for the current
165 * <code>ComponentOrienation</code>. Equal to NORTHWEST for horizontal,
166 * left-to-right orientations and NORTHEAST for horizontal, right-to-left
167 * orientations.
168 */
169 public static final int FIRST_LINE_START = 23;
170
171 /**
172 * Place the component in the corner of its display area where
173 * the first line of text on a page would normally end for the current
174 * <code>ComponentOrienation</code>. Equal to NORTHEAST for horizontal,
175 * left-to-right orientations and NORTHWEST for horizontal, right-to-left
176 * orientations.
177 */
178 public static final int FIRST_LINE_END = 24;
179
180 /**
181 * Place the component in the corner of its display area where
182 * the last line of text on a page would normally start for the current
183 * <code>ComponentOrienation</code>. Equal to SOUTHWEST for horizontal,
184 * left-to-right orientations and SOUTHEAST for horizontal, right-to-left
185 * orientations.
186 */
187 public static final int LAST_LINE_START = 25;
188
189 /**
190 * Place the component in the corner of its display area where
191 * the last line of text on a page would normally end for the current
192 * <code>ComponentOrienation</code>. Equal to SOUTHEAST for horizontal,
193 * left-to-right orientations and SOUTHWEST for horizontal, right-to-left
194 * orientations.
195 */
196 public static final int LAST_LINE_END = 26;
197
198 /**
199 * Possible value for the <code>anchor</code> field. Specifies
200 * that the component should be horizontally centered and
201 * vertically aligned along the baseline of the prevailing row.
202 * If the component does not have a baseline it will be vertically
203 * centered.
204 *
205 * @since 1.6
206 */
207 public static final int BASELINE = 0x100;
208
209 /**
210 * Possible value for the <code>anchor</code> field. Specifies
211 * that the component should be horizontally placed along the
212 * leading edge. For components with a left-to-right orientation,
213 * the leading edge is the left edge. Vertically the component is
214 * aligned along the baseline of the prevailing row. If the
215 * component does not have a baseline it will be vertically
216 * centered.
217 *
218 * @since 1.6
219 */
220 public static final int BASELINE_LEADING = 0x200;
221
222 /**
223 * Possible value for the <code>anchor</code> field. Specifies
224 * that the component should be horizontally placed along the
225 * trailing edge. For components with a left-to-right
226 * orientation, the trailing edge is the right edge. Vertically
227 * the component is aligned along the baseline of the prevailing
228 * row. If the component does not have a baseline it will be
229 * vertically centered.
230 *
231 * @since 1.6
232 */
233 public static final int BASELINE_TRAILING = 0x300;
234
235 /**
236 * Possible value for the <code>anchor</code> field. Specifies
237 * that the component should be horizontally centered. Vertically
238 * the component is positioned so that its bottom edge touches
239 * the baseline of the starting row. If the starting row does not
240 * have a baseline it will be vertically centered.
241 *
242 * @since 1.6
243 */
244 public static final int ABOVE_BASELINE = 0x400;
245
246 /**
247 * Possible value for the <code>anchor</code> field. Specifies
248 * that the component should be horizontally placed along the
249 * leading edge. For components with a left-to-right orientation,
250 * the leading edge is the left edge. Vertically the component is
251 * positioned so that its bottom edge touches the baseline of the
252 * starting row. If the starting row does not have a baseline it
253 * will be vertically centered.
254 *
255 * @since 1.6
256 */
257 public static final int ABOVE_BASELINE_LEADING = 0x500;
258
259 /**
260 * Possible value for the <code>anchor</code> field. Specifies
261 * that the component should be horizontally placed along the
262 * trailing edge. For components with a left-to-right
263 * orientation, the trailing edge is the right edge. Vertically
264 * the component is positioned so that its bottom edge touches
265 * the baseline of the starting row. If the starting row does not
266 * have a baseline it will be vertically centered.
267 *
268 * @since 1.6
269 */
270 public static final int ABOVE_BASELINE_TRAILING = 0x600;
271
272 /**
273 * Possible value for the <code>anchor</code> field. Specifies
274 * that the component should be horizontally centered. Vertically
275 * the component is positioned so that its top edge touches the
276 * baseline of the starting row. If the starting row does not
277 * have a baseline it will be vertically centered.
278 *
279 * @since 1.6
280 */
281 public static final int BELOW_BASELINE = 0x700;
282
283 /**
284 * Possible value for the <code>anchor</code> field. Specifies
285 * that the component should be horizontally placed along the
286 * leading edge. For components with a left-to-right orientation,
287 * the leading edge is the left edge. Vertically the component is
288 * positioned so that its top edge touches the baseline of the
289 * starting row. If the starting row does not have a baseline it
290 * will be vertically centered.
291 *
292 * @since 1.6
293 */
294 public static final int BELOW_BASELINE_LEADING = 0x800;
295
296 /**
297 * Possible value for the <code>anchor</code> field. Specifies
298 * that the component should be horizontally placed along the
299 * trailing edge. For components with a left-to-right
300 * orientation, the trailing edge is the right edge. Vertically
301 * the component is positioned so that its top edge touches the
302 * baseline of the starting row. If the starting row does not
303 * have a baseline it will be vertically centered.
304 *
305 * @since 1.6
306 */
307 public static final int BELOW_BASELINE_TRAILING = 0x900;
308
309 /**
310 * Specifies the cell containing the leading edge of the component's
311 * display area, where the first cell in a row has <code>gridx=0</code>.
312 * The leading edge of a component's display area is its left edge for
313 * a horizontal, left-to-right container and its right edge for a
314 * horizontal, right-to-left container.
315 * The value
316 * <code>RELATIVE</code> specifies that the component be placed
317 * immediately following the component that was added to the container
318 * just before this component was added.
319 * <p>
320 * The default value is <code>RELATIVE</code>.
321 * <code>gridx</code> should be a non-negative value.
322 * @serial
323 * @see #clone()
324 * @see java.awt.GridBagConstraints#gridy
325 * @see java.awt.ComponentOrientation
326 */
327 public int gridx;
328
329 /**
330 * Specifies the cell at the top of the component's display area,
331 * where the topmost cell has <code>gridy=0</code>. The value
332 * <code>RELATIVE</code> specifies that the component be placed just
333 * below the component that was added to the container just before
334 * this component was added.
335 * <p>
336 * The default value is <code>RELATIVE</code>.
337 * <code>gridy</code> should be a non-negative value.
338 * @serial
339 * @see #clone()
340 * @see java.awt.GridBagConstraints#gridx
341 */
342 public int gridy;
343
344 /**
345 * Specifies the number of cells in a row for the component's
346 * display area.
347 * <p>
348 * Use <code>REMAINDER</code> to specify that the component's
349 * display area will be from <code>gridx</code> to the last
350 * cell in the row.
351 * Use <code>RELATIVE</code> to specify that the component's
352 * display area will be from <code>gridx</code> to the next
353 * to the last one in its row.
354 * <p>
355 * <code>gridwidth</code> should be non-negative and the default
356 * value is 1.
357 * @serial
358 * @see #clone()
359 * @see java.awt.GridBagConstraints#gridheight
360 */
361 public int gridwidth;
362
363 /**
364 * Specifies the number of cells in a column for the component's
365 * display area.
366 * <p>
367 * Use <code>REMAINDER</code> to specify that the component's
368 * display area will be from <code>gridy</code> to the last
369 * cell in the column.
370 * Use <code>RELATIVE</code> to specify that the component's
371 * display area will be from <code>gridy</code> to the next
372 * to the last one in its column.
373 * <p>
374 * <code>gridheight</code> should be a non-negative value and the
375 * default value is 1.
376 * @serial
377 * @see #clone()
378 * @see java.awt.GridBagConstraints#gridwidth
379 */
380 public int gridheight;
381
382 /**
383 * Specifies how to distribute extra horizontal space.
384 * <p>
385 * The grid bag layout manager calculates the weight of a column to
386 * be the maximum <code>weightx</code> of all the components in a
387 * column. If the resulting layout is smaller horizontally than the area
388 * it needs to fill, the extra space is distributed to each column in
389 * proportion to its weight. A column that has a weight of zero receives
390 * no extra space.
391 * <p>
392 * If all the weights are zero, all the extra space appears between
393 * the grids of the cell and the left and right edges.
394 * <p>
395 * The default value of this field is <code>0</code>.
396 * <code>weightx</code> should be a non-negative value.
397 * @serial
398 * @see #clone()
399 * @see java.awt.GridBagConstraints#weighty
400 */
401 public double weightx;
402
403 /**
404 * Specifies how to distribute extra vertical space.
405 * <p>
406 * The grid bag layout manager calculates the weight of a row to be
407 * the maximum <code>weighty</code> of all the components in a row.
408 * If the resulting layout is smaller vertically than the area it
409 * needs to fill, the extra space is distributed to each row in
410 * proportion to its weight. A row that has a weight of zero receives no
411 * extra space.
412 * <p>
413 * If all the weights are zero, all the extra space appears between
414 * the grids of the cell and the top and bottom edges.
415 * <p>
416 * The default value of this field is <code>0</code>.
417 * <code>weighty</code> should be a non-negative value.
418 * @serial
419 * @see #clone()
420 * @see java.awt.GridBagConstraints#weightx
421 */
422 public double weighty;
423
424 /**
425 * This field is used when the component is smaller than its
426 * display area. It determines where, within the display area, to
427 * place the component.
428 * <p> There are three kinds of possible values: orientation
429 * relative, baseline relative and absolute. Orientation relative
430 * values are interpreted relative to the container's component
431 * orientation property, baseline relative values are interpreted
432 * relative to the baseline and absolute values are not. The
433 * absolute values are:
434 * <code>CENTER</code>, <code>NORTH</code>, <code>NORTHEAST</code>,
435 * <code>EAST</code>, <code>SOUTHEAST</code>, <code>SOUTH</code>,
436 * <code>SOUTHWEST</code>, <code>WEST</code>, and <code>NORTHWEST</code>.
437 * The orientation relative values are: <code>PAGE_START</code>,
438 * <code>PAGE_END</code>,
439 * <code>LINE_START</code>, <code>LINE_END</code>,
440 * <code>FIRST_LINE_START</code>, <code>FIRST_LINE_END</code>,
441 * <code>LAST_LINE_START</code> and <code>LAST_LINE_END</code>. The
442 * baseline relvative values are:
443 * <code>BASELINE</code>, <code>BASELINE_LEADING</code>,
444 * <code>BASELINE_TRAILING</code>,
445 * <code>ABOVE_BASELINE</code>, <code>ABOVE_BASELINE_LEADING</code>,
446 * <code>ABOVE_BASELINE_TRAILING</code>,
447 * <code>BELOW_BASELINE</code>, <code>BELOW_BASELINE_LEADING</code>,
448 * and <code>BELOW_BASELINE_TRAILING</code>.
449 * The default value is <code>CENTER</code>.
450 * @serial
451 * @see #clone()
452 * @see java.awt.ComponentOrientation
453 */
454 public int anchor;
455
456 /**
457 * This field is used when the component's display area is larger
458 * than the component's requested size. It determines whether to
459 * resize the component, and if so, how.
460 * <p>
461 * The following values are valid for <code>fill</code>:
462 * <p>
463 * <ul>
464 * <li>
465 * <code>NONE</code>: Do not resize the component.
466 * <li>
467 * <code>HORIZONTAL</code>: Make the component wide enough to fill
468 * its display area horizontally, but do not change its height.
469 * <li>
470 * <code>VERTICAL</code>: Make the component tall enough to fill its
471 * display area vertically, but do not change its width.
472 * <li>
473 * <code>BOTH</code>: Make the component fill its display area
474 * entirely.
475 * </ul>
476 * <p>
477 * The default value is <code>NONE</code>.
478 * @serial
479 * @see #clone()
480 */
481 public int fill;
482
483 /**
484 * This field specifies the external padding of the component, the
485 * minimum amount of space between the component and the edges of its
486 * display area.
487 * <p>
488 * The default value is <code>new Insets(0, 0, 0, 0)</code>.
489 * @serial
490 * @see #clone()
491 */
492 public Insets insets;
493
494 /**
495 * This field specifies the internal padding of the component, how much
496 * space to add to the minimum width of the component. The width of
497 * the component is at least its minimum width plus
498 * <code>ipadx</code> pixels.
499 * <p>
500 * The default value is <code>0</code>.
501 * @serial
502 * @see #clone()
503 * @see java.awt.GridBagConstraints#ipady
504 */
505 public int ipadx;
506
507 /**
508 * This field specifies the internal padding, that is, how much
509 * space to add to the minimum height of the component. The height of
510 * the component is at least its minimum height plus
511 * <code>ipady</code> pixels.
512 * <p>
513 * The default value is 0.
514 * @serial
515 * @see #clone()
516 * @see java.awt.GridBagConstraints#ipadx
517 */
518 public int ipady;
519
520 /**
521 * Temporary place holder for the x coordinate.
522 * @serial
523 */
524 int tempX;
525 /**
526 * Temporary place holder for the y coordinate.
527 * @serial
528 */
529 int tempY;
530 /**
531 * Temporary place holder for the Width of the component.
532 * @serial
533 */
534 int tempWidth;
535 /**
536 * Temporary place holder for the Height of the component.
537 * @serial
538 */
539 int tempHeight;
540 /**
541 * The minimum width of the component. It is used to calculate
542 * <code>ipady</code>, where the default will be 0.
543 * @serial
544 * @see #ipady
545 */
546 int minWidth;
547 /**
548 * The minimum height of the component. It is used to calculate
549 * <code>ipadx</code>, where the default will be 0.
550 * @serial
551 * @see #ipadx
552 */
553 int minHeight;
554
555 // The following fields are only used if the anchor is
556 // one of BASELINE, BASELINE_LEADING or BASELINE_TRAILING.
557 // ascent and descent include the insets and ipady values.
558 transient int ascent;
559 transient int descent;
560 transient Component.BaselineResizeBehavior baselineResizeBehavior;
561 // The folllowing two fields are used if the baseline type is
562 // CENTER_OFFSET.
563 // centerPadding is either 0 or 1 and indicates if
564 // the height needs to be padded by one when calculating where the
565 // baseline lands
566 transient int centerPadding;
567 // Where the baseline lands relative to the center of the component.
568 transient int centerOffset;
569
570 /*
571 * JDK 1.1 serialVersionUID
572 */
573 private static final long serialVersionUID = -1000070633030801713L;
574
575 /**
576 * Creates a <code>GridBagConstraint</code> object with
577 * all of its fields set to their default value.
578 */
579 public GridBagConstraints() {
580 gridx = RELATIVE;
581 gridy = RELATIVE;
582 gridwidth = 1;
583 gridheight = 1;
584
585 weightx = 0;
586 weighty = 0;
587 anchor = CENTER;
588 fill = NONE;
589
590 insets = new Insets(0, 0, 0, 0);
591 ipadx = 0;
592 ipady = 0;
593 }
594
595 /**
596 * Creates a <code>GridBagConstraints</code> object with
597 * all of its fields set to the passed-in arguments.
598 *
599 * Note: Because the use of this constructor hinders readability
600 * of source code, this constructor should only be used by
601 * automatic source code generation tools.
602 *
603 * @param gridx The initial gridx value.
604 * @param gridy The initial gridy value.
605 * @param gridwidth The initial gridwidth value.
606 * @param gridheight The initial gridheight value.
607 * @param weightx The initial weightx value.
608 * @param weighty The initial weighty value.
609 * @param anchor The initial anchor value.
610 * @param fill The initial fill value.
611 * @param insets The initial insets value.
612 * @param ipadx The initial ipadx value.
613 * @param ipady The initial ipady value.
614 *
615 * @see java.awt.GridBagConstraints#gridx
616 * @see java.awt.GridBagConstraints#gridy
617 * @see java.awt.GridBagConstraints#gridwidth
618 * @see java.awt.GridBagConstraints#gridheight
619 * @see java.awt.GridBagConstraints#weightx
620 * @see java.awt.GridBagConstraints#weighty
621 * @see java.awt.GridBagConstraints#anchor
622 * @see java.awt.GridBagConstraints#fill
623 * @see java.awt.GridBagConstraints#insets
624 * @see java.awt.GridBagConstraints#ipadx
625 * @see java.awt.GridBagConstraints#ipady
626 *
627 * @since 1.2
628 */
629 public GridBagConstraints(int gridx, int gridy, int gridwidth,
630 int gridheight, double weightx, double weighty, int anchor,
631 int fill, Insets insets, int ipadx, int ipady) {
632 this .gridx = gridx;
633 this .gridy = gridy;
634 this .gridwidth = gridwidth;
635 this .gridheight = gridheight;
636 this .fill = fill;
637 this .ipadx = ipadx;
638 this .ipady = ipady;
639 this .insets = insets;
640 this .anchor = anchor;
641 this .weightx = weightx;
642 this .weighty = weighty;
643 }
644
645 /**
646 * Creates a copy of this grid bag constraint.
647 * @return a copy of this grid bag constraint
648 */
649 public Object clone() {
650 try {
651 GridBagConstraints c = (GridBagConstraints) super .clone();
652 c.insets = (Insets) insets.clone();
653 return c;
654 } catch (CloneNotSupportedException e) {
655 // this shouldn't happen, since we are Cloneable
656 throw new InternalError();
657 }
658 }
659
660 boolean isVerticallyResizable() {
661 return (fill == BOTH || fill == VERTICAL);
662 }
663 }
|