KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > awt > BorderPanel


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.awt;
32
33 import java.awt.*;
34
35 public class BorderPanel extends Panel {
36
37     int left, top, bottom, right;
38
39     public BorderPanel (Insets insets) {
40         this.left = insets.left;
41         this.top = insets.top;
42         this.bottom = insets.bottom;
43         this.right = insets.right;
44     }
45
46     public BorderPanel (int left, int top, int bottom, int right) {
47         this.left = left;
48         this.top = top;
49         this.bottom = bottom;
50         this.right = right;
51     }
52
53
54     public void layout () {
55         Dimension d = getSize ();
56
57         int x = left;
58         int y = top;
59         int w = d.width - left - right;
60         int h = d.height - top - bottom;
61
62         Component[] comps = getComponents ();
63         for (int i=0; i<comps.length; ++i)
64             comps[i].setBounds (x, y, w, h);
65     }
66
67     public Dimension getPreferredSize () {
68         Dimension max = new Dimension (0, 0);
69
70         Component[] comps = getComponents ();
71         for (int i=0; i<comps.length; ++i) {
72             Dimension d = comps[i].getPreferredSize ();
73             max.width = Math.max (d.width, max.width);
74             max.height = Math.max (d.height, max.height);
75         }
76
77         max.width += left+right;
78         max.height += top+bottom;
79         return max;
80     }
81
82     public Dimension getMinimumSize () {
83         Dimension max = new Dimension (0, 0);
84
85         Component[] comps = getComponents ();
86         for (int i=0; i<comps.length; ++i) {
87             Dimension d = comps[i].getMinimumSize ();
88             max.width = Math.max (d.width, max.width);
89             max.height = Math.max (d.height, max.height);
90         }
91
92         max.width += left+right;
93         max.height += top+bottom;
94         return max;
95     }
96
97     public static Panel wrap (Component comp, Insets insets) {
98         Panel p = new BorderPanel (insets);
99         p.add (comp);
100         return p;
101     }
102
103     public static Panel wrap (Component comp, int left, int top, int bottom, int right) {
104         Panel p = new BorderPanel (left, top, bottom, right);
105         p.add (comp);
106         return p;
107     }
108
109     public static Panel wrap (Component comp, int horiz, int vert) {
110         Panel p = new BorderPanel (horiz, vert, horiz, vert);
111         p.add (comp);
112         return p;
113     }
114
115     public static Panel wrap (Component comp, int all) {
116         Panel p = new BorderPanel (all, all, all, all);
117         p.add (comp);
118         return p;
119     }
120 }
121
Popular Tags