source: trunk/src/gcc/libjava/java/awt/BorderLayout.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 15.3 KB
Line 
1/* BorderLayout.java -- A layout manager class
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41/**
42 * This class implements a layout manager that positions components
43 * in certain sectors of the parent container.
44 *
45 * @author Aaron M. Renn ([email protected])
46 * @author Rolf W. Rasmussen <[email protected]>
47 */
48public class BorderLayout implements LayoutManager2, java.io.Serializable
49{
50
51/*
52 * Static Variables
53 */
54
55/**
56 * Constant indicating the top of the container
57 */
58public static final String NORTH = "North";
59
60/**
61 * Constant indicating the bottom of the container
62 */
63public static final String SOUTH = "South";
64
65/**
66 * Constant indicating the right side of the container
67 */
68public static final String EAST = "East";
69
70/**
71 * Constant indicating the left side of the container
72 */
73public static final String WEST = "West";
74
75/**
76 * Constant indicating the center of the container
77 */
78public static final String CENTER = "Center";
79
80/**
81 * Constant indicating the position just after the last line of the
82 * layout.
83 */
84public static final String AFTER_LAST_LINE = "Last";
85
86/**
87 * Constant indicating the position just after the end of the line.
88 */
89public static final String AFTER_LINE_ENDS = "After";
90
91/**
92 * Constant indicating the position just before the first line of the
93 * layout.
94 */
95public static final String BEFORE_FIRST_LINE = "First";
96
97/**
98 * Constant indicating the position at the beginning of the line.
99 */
100public static final String BEFORE_LINE_BEGINS = "Before";
101
102// Serialization constant
103private static final long serialVersionUID = -8658291919501921765L;
104
105/*************************************************************************/
106
107/*
108 * Instance Variables
109 */
110
111/**
112 * @serial
113 */
114private Component north;
115
116/**
117 * @serial
118 */
119private Component south;
120
121/**
122 * @serial
123 */
124private Component east;
125
126/**
127 * @serial
128 */
129private Component west;
130
131/**
132 * @serial
133 */
134private Component center;
135
136/**
137 * @serial
138 */
139private Component firstLine;
140
141/**
142 * @serial
143 */
144private Component lastLine;
145
146/**
147 * @serial
148 */
149private Component firstItem;
150
151/**
152 * @serial
153 */
154private Component lastItem;
155
156/**
157 * @serial The horizontal gap between components
158 */
159private int hgap;
160
161/**
162 * @serial The vertical gap between components
163 */
164private int vgap;
165
166/*************************************************************************/
167
168/*
169 * Constructors
170 */
171
172/**
173 * Initializes a new instance of <code>BorderLayout</code> with no
174 * horiztonal or vertical gaps between components.
175 */
176public
177BorderLayout()
178{
179 this(0,0);
180}
181
182/*************************************************************************/
183
184/**
185 * Initializes a new instance of <code>BorderLayout</code> with the
186 * specified horiztonal and vertical gaps between components.
187 *
188 * @param hgap The horizontal gap between components.
189 * @param vgap The vertical gap between components.
190 */
191public
192BorderLayout(int hgap, int vgap)
193{
194 this.hgap = hgap;
195 this.vgap = vgap;
196}
197
198/*************************************************************************/
199
200/*
201 * Instance Variables
202 */
203
204/**
205 * Returns the horitzontal gap value.
206 *
207 * @return The horitzontal gap value.
208 */
209public int
210getHgap()
211{
212 return(hgap);
213}
214
215/*************************************************************************/
216
217/**
218 * Sets the horizontal gap to the specified value.
219 *
220 * @param hgap The new horizontal gap.
221 */
222public void
223setHgap(int hgap)
224{
225 this.hgap = hgap;
226}
227
228/*************************************************************************/
229
230/**
231 * Returns the vertical gap value.
232 *
233 * @return The vertical gap value.
234 */
235public int
236getVgap()
237{
238 return(vgap);
239}
240
241/*************************************************************************/
242
243/**
244 * Sets the vertical gap to the specified value.
245 *
246 * @param vgap The new vertical gap value.
247 */
248public void
249setVgap(int vgap)
250{
251 this.vgap = vgap;
252}
253
254/*************************************************************************/
255
256/**
257 * Adds a component to the layout in the specified constraint position,
258 * which must be one of the string constants defined in this class.
259 *
260 * @param component The component to add.
261 * @param constraints The constraint string.
262 *
263 * @exception IllegalArgumentException If the constraint object is not
264 * a string, or is not one of the specified constants in this class.
265 */
266public void
267addLayoutComponent(Component component, Object constraints)
268{
269 if (constraints != null && ! (constraints instanceof String))
270 throw new IllegalArgumentException("Constraint must be a string");
271
272 String str = (String)constraints;
273
274 if (str == null || str.equals(CENTER))
275 center = component;
276 else if (str.equals(NORTH))
277 north = component;
278 else if (str.equals(SOUTH))
279 south = component;
280 else if (str.equals(EAST))
281 east = component;
282 else if (str.equals(WEST))
283 west = component;
284 else if (str.equals(BEFORE_FIRST_LINE))
285 firstLine = component;
286 else if (str.equals(AFTER_LAST_LINE))
287 lastLine = component;
288 else if (str.equals(BEFORE_LINE_BEGINS))
289 firstItem = component;
290 else if (str.equals(AFTER_LINE_ENDS))
291 lastItem = component;
292 else
293 throw new IllegalArgumentException("Constraint value not valid: " + str);
294}
295
296/*************************************************************************/
297
298/**
299 * Adds a component to the layout in the specified constraint position,
300 * which must be one of the string constants defined in this class.
301 *
302 * @param constraints The constraint string.
303 * @param component The component to add.
304 *
305 * @exception IllegalArgumentException If the constraint object is not
306 * one of the specified constants in this class.
307 *
308 * @deprecated This method is deprecated in favor of
309 * <code>addLayoutComponent(Component, Object)</code>.
310 */
311public void
312addLayoutComponent(String constraints, Component component)
313{
314 addLayoutComponent(component, constraints);
315}
316
317/*************************************************************************/
318
319/**
320 * Removes the specified component from the layout.
321 *
322 * @param component The component to remove from the layout.
323 */
324public void
325removeLayoutComponent(Component component)
326{
327 if (north == component)
328 north = null;
329 if (south == component)
330 south = null;
331 if (east == component)
332 east = null;
333 if (west == component)
334 west = null;
335 if (center == component)
336 center = null;
337 if (firstItem == component)
338 firstItem = null;
339 if (lastItem == component)
340 lastItem = null;
341 if (firstLine == component)
342 firstLine = null;
343 if (lastLine == component)
344 lastLine = null;
345}
346
347/*************************************************************************/
348
349/**
350 * Returns the minimum size of the specified container using this layout.
351 *
352 * @param target The container to calculate the minimum size for.
353 *
354 * @return The minimum size of the container
355 */
356public Dimension
357minimumLayoutSize(Container target)
358{
359 return calcSize(target, MIN);
360}
361
362/*************************************************************************/
363
364/**
365 * Returns the preferred size of the specified container using this layout.
366 *
367 * @param target The container to calculate the preferred size for.
368 *
369 * @return The preferred size of the container
370 */
371public Dimension
372preferredLayoutSize(Container target)
373{
374 return calcSize(target, PREF);
375}
376
377/*************************************************************************/
378
379/**
380 * Returns the maximum size of the specified container using this layout.
381 *
382 * @param target The container to calculate the maximum size for.
383 *
384 * @return The maximum size of the container
385 */
386public Dimension
387maximumLayoutSize(Container target)
388{
389 return calcSize(target, MAX);
390}
391
392/*************************************************************************/
393
394/**
395 * Returns the X axis alignment, which is a <code>float</code> indicating
396 * where along the X axis this container wishs to position its layout.
397 * 0 indicates align to the left, 1 indicates align to the right, and 0.5
398 * indicates align to the center.
399 *
400 * @param parent The parent container.
401 *
402 * @return The X alignment value.
403 */
404public float
405getLayoutAlignmentX(Container parent)
406{
407 return(parent.getAlignmentX());
408}
409
410/*************************************************************************/
411
412/**
413 * Returns the Y axis alignment, which is a <code>float</code> indicating
414 * where along the Y axis this container wishs to position its layout.
415 * 0 indicates align to the top, 1 indicates align to the bottom, and 0.5
416 * indicates align to the center.
417 *
418 * @param parent The parent container.
419 *
420 * @return The Y alignment value.
421 */
422public float
423getLayoutAlignmentY(Container parent)
424{
425 return(parent.getAlignmentY());
426}
427
428/*************************************************************************/
429
430/**
431 * Instructs this object to discard any layout information it might
432 * have cached.
433 *
434 * @param parent The parent container.
435 */
436public void
437invalidateLayout(Container parent)
438{
439}
440
441/*************************************************************************/
442
443/**
444 * Lays out the specified container according to the constraints
445 * in this object.
446 *
447 * @param target The container to lay out.
448 */
449public void
450layoutContainer(Container target)
451{
452 Insets i = target.getInsets();
453
454 ComponentOrientation orient = target.getComponentOrientation ();
455 boolean left_to_right = orient.isLeftToRight ();
456
457 Component my_north = north;
458 Component my_east = east;
459 Component my_south = south;
460 Component my_west = west;
461
462 // Note that we currently don't handle vertical layouts. Neither
463 // does JDK 1.3.
464 if (firstLine != null)
465 my_north = firstLine;
466 if (lastLine != null)
467 my_south = lastLine;
468 if (firstItem != null)
469 {
470 if (left_to_right)
471 my_west = firstItem;
472 else
473 my_east = firstItem;
474 }
475 if (lastItem != null)
476 {
477 if (left_to_right)
478 my_east = lastItem;
479 else
480 my_west = lastItem;
481 }
482
483 Dimension c = calcCompSize(center, PREF);
484 Dimension n = calcCompSize(my_north, PREF);
485 Dimension s = calcCompSize(my_south, PREF);
486 Dimension e = calcCompSize(my_east, PREF);
487 Dimension w = calcCompSize(my_west, PREF);
488 Dimension t = target.getSize();
489
490 /*
491 <-> hgap <-> hgap
492 +----------------------------+ }
493 |t | } i.top
494 | +----------------------+ | --- y1 }
495 | |n | |
496 | +----------------------+ | } vgap
497 | +---+ +----------+ +---+ | --- y2 } }
498 | |w | |c | |e | | } hh
499 | +---+ +----------+ +---+ | } vgap }
500 | +----------------------+ | --- y3 }
501 | |s | |
502 | +----------------------+ | }
503 | | } i.bottom
504 +----------------------------+ }
505 |x1 |x2 |x3
506 <---------------------->
507 <--> ww <-->
508 i.left i.right
509 */
510
511 int x1 = i.left;
512 int x2 = x1 + w.width + hgap;
513 int x3 = t.width - i.right - e.width;
514 int ww = t.width - i.right - i.left;
515
516 int y1 = i.top;
517 int y2 = y1 + n.height + vgap;
518 int y3 = t.height - i.bottom - s.height;
519 int hh = y3-y2-vgap;
520
521 setBounds(center, x2, y2, x3-x2-hgap, hh);
522 setBounds(my_north, x1, y1, ww, n.height);
523 setBounds(my_south, x1, y3, ww, s.height);
524 setBounds(my_west, x1, y2, w.width, hh);
525 setBounds(my_east, x3, y2, e.width, hh);
526}
527
528/*************************************************************************/
529
530/**
531 * Returns a string representation of this layout manager.
532 *
533 * @return A string representation of this object.
534 */
535public String
536toString()
537{
538 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
539}
540
541private void
542setBounds(Component comp, int x, int y, int w, int h)
543{
544 if (comp == null)
545 return;
546 comp.setBounds(x, y, w, h);
547}
548
549// Some constants for use with calcSize().
550private static final int MIN = 0;
551private static final int MAX = 1;
552private static final int PREF = 2;
553
554private Dimension
555calcCompSize(Component comp, int what)
556{
557 if (comp == null)
558 return new Dimension(0, 0);
559 if (what == MIN)
560 return comp.getMinimumSize();
561 else if (what == MAX)
562 return comp.getMaximumSize();
563 return comp.getPreferredSize();
564}
565
566// This is a helper function used to compute the various sizes for
567// this layout.
568private Dimension
569calcSize(Container target, int what)
570{
571 Insets ins = target.getInsets();
572
573 ComponentOrientation orient = target.getComponentOrientation ();
574 boolean left_to_right = orient.isLeftToRight ();
575
576 Component my_north = north;
577 Component my_east = east;
578 Component my_south = south;
579 Component my_west = west;
580
581 // Note that we currently don't handle vertical layouts. Neither
582 // does JDK 1.3.
583 if (firstLine != null)
584 my_north = firstLine;
585 if (lastLine != null)
586 my_south = lastLine;
587 if (firstItem != null)
588 {
589 if (left_to_right)
590 my_west = firstItem;
591 else
592 my_east = firstItem;
593 }
594 if (lastItem != null)
595 {
596 if (left_to_right)
597 my_east = lastItem;
598 else
599 my_west = lastItem;
600 }
601
602 Dimension ndim = calcCompSize(my_north, what);
603 Dimension sdim = calcCompSize(my_south, what);
604 Dimension edim = calcCompSize(my_east, what);
605 Dimension wdim = calcCompSize(my_west, what);
606 Dimension cdim = calcCompSize(center, what);
607
608 int width = edim.width + cdim.width + wdim.width + (hgap * 2);
609 if (ndim.width > width)
610 width = ndim.width;
611 if (sdim.width > width)
612 width = sdim.width;
613
614 width += (ins.left + ins.right);
615
616 int height = edim.height;
617 if (cdim.height > height)
618 height = cdim.height;
619 if (wdim.height > height)
620 height = wdim.height;
621
622 height += (ndim.height + sdim.height + (vgap * 2) + ins.top + ins.bottom);
623
624 return(new Dimension(width, height));
625}
626} // class BorderLayout
Note: See TracBrowser for help on using the repository browser.