KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > gui > layout > VerticalLayout


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/gui/layout/VerticalLayout.java,v 1.4 2004/02/11 23:48:59 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.gui.layout;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Container JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.LayoutManager JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28 /**
29  * A vertical layout manager similar to java.awt.FlowLayout. Like FlowLayout
30  * components do not expand to fill available space except when the horizontal
31  * alignment is <code>BOTH</code> in which case components are stretched
32  * horizontally. Unlike FlowLayout, components will not wrap to form another
33  * column if there isn't enough space vertically. VerticalLayout can optionally
34  * anchor components to the top or bottom of the display area or center them
35  * between the top and bottom. Revision date 04 April 1999
36  *
37  * @author Colin Mummery e-mail:equitysoft@iname.com
38  * Homepage:www.kagi.com/equitysoft - Based on 'FlexLayout' in Java class
39  * libraries Vol 2 Chan/Lee Addison-Wesley 1998
40  * @version $Revision: 1.4 $
41  */

42 public class VerticalLayout implements LayoutManager JavaDoc, Serializable JavaDoc
43 {
44     /**
45      * The horizontal alignment constant that designates centering. Also used
46      * to designate center anchoring.
47      */

48     public final static int CENTER = 0;
49     
50     /**
51      * The horizontal alignment constant that designates right justification.
52      */

53     public final static int RIGHT = 1;
54
55     /**
56      * The horizontal alignment constant that designates left justification.
57      */

58     public final static int LEFT = 2;
59
60     /**
61      * The horizontal alignment constant that designates stretching the
62      * component horizontally.
63      */

64     public final static int BOTH = 3;
65
66     /**
67      * The anchoring constant that designates anchoring to the top of the
68      * display area.
69      */

70     public final static int TOP = 1;
71
72     /**
73      * The anchoring constant that designates anchoring to the bottom of the
74      * display area.
75      */

76     public final static int BOTTOM = 2;
77
78     /** The vertical vgap between components...defaults to 5. */
79     private int vgap;
80
81     /** LEFT, RIGHT, CENTER or BOTH...how the components are justified. */
82     private int alignment;
83
84     /**
85      * TOP, BOTTOM or CENTER ...where are the components positioned in an
86      * overlarge space.
87      */

88     private int anchor;
89     
90     //Constructors
91
/**
92      * Constructs an instance of VerticalLayout with a vertical vgap of 5
93      * pixels, horizontal centering and anchored to the top of the display area.
94      */

95     public VerticalLayout()
96     {
97         this(5, CENTER, TOP);
98     }
99
100     /**
101      * Constructs a VerticalLayout instance with horizontal centering, anchored
102      * to the top with the specified vgap.
103      *
104      *@param vgap an int value indicating the vertical seperation of the
105      * components
106      */

107     public VerticalLayout(int vgap)
108     {
109         this(vgap, CENTER, TOP);
110     }
111
112     /**
113      * Constructs a VerticalLayout instance anchored to the top with the
114      * specified vgap and horizontal alignment.
115      *
116      * @param vgap an int value indicating the vertical seperation of the
117      * components
118      * @param alignment an int value which is one of <code>RIGHT, LEFT,
119      * CENTER, BOTH</code> for the horizontal alignment.
120      */

121     public VerticalLayout(int vgap, int alignment)
122     {
123         this(vgap, alignment, TOP);
124     }
125
126     /**
127      * Constructs a VerticalLayout instance with the specified vgap, horizontal
128      * alignment and anchoring
129      *
130      *@param vgap an int value indicating the vertical seperation of the
131      * components
132      *@param alignment an int value which is one of <code>RIGHT, LEFT, CENTER,
133      * BOTH</code> for the horizontal alignment.
134      *@param anchor an int value which is one of <code>TOP, BOTTOM,
135      * CENTER</code> indicating where the components are to
136      * appear if the display area exceeds the minimum
137      * necessary.
138      */

139     public VerticalLayout(int vgap, int alignment, int anchor)
140     {
141         this.vgap = vgap;
142         this.alignment = alignment;
143         this.anchor = anchor;
144     }
145
146     /**
147      * Lays out the container.
148      */

149     public void layoutContainer(Container JavaDoc parent)
150     {
151         Insets JavaDoc insets = parent.getInsets();
152         //NOTUSED Dimension dim = layoutSize(parent, false);
153
synchronized (parent.getTreeLock())
154         {
155             int n = parent.getComponentCount();
156             Dimension JavaDoc pd = parent.getSize();
157             int y = 0;
158             //work out the total size
159
for (int i = 0; i < n; i++)
160             {
161                 Component JavaDoc c = parent.getComponent(i);
162                 Dimension JavaDoc d = c.getPreferredSize();
163                 y += d.height + vgap;
164             }
165             y -= vgap; //otherwise there's a vgap too many
166
//Work out the anchor paint
167
if (anchor == TOP)
168             {
169                 y = insets.top;
170             }
171             else if (anchor == CENTER)
172             {
173                 y = (pd.height - y) / 2;
174             }
175             else
176             {
177                 y = pd.height - y - insets.bottom;
178             }
179             //do layout
180
for (int i = 0; i < n; i++)
181             {
182                 Component JavaDoc c = parent.getComponent(i);
183                 Dimension JavaDoc d = c.getPreferredSize();
184                 int x = insets.left;
185                 int wid = d.width;
186                 if (alignment == CENTER)
187                 {
188                     x = (pd.width - d.width) / 2;
189                 }
190                 else if (alignment == RIGHT)
191                 {
192                     x = pd.width - d.width - insets.right;
193                 }
194                 else if (alignment == BOTH)
195                 {
196                     wid = pd.width - insets.left - insets.right;
197                 }
198                 c.setBounds(x, y, wid, d.height);
199                 y += d.height + vgap;
200             }
201         }
202     }
203
204     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc parent)
205     {
206         return layoutSize(parent, true);
207     }
208
209     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc parent)
210     {
211         return layoutSize(parent, false);
212     }
213
214     /**
215      * Not used by this class.
216      */

217     public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp)
218     {
219     }
220
221     /**
222      * Not used by this class.
223      */

224     public void removeLayoutComponent(Component JavaDoc comp)
225     {
226     }
227
228     public String JavaDoc toString()
229     {
230         return getClass().getName()
231             + "[vgap="
232             + vgap
233             + " align="
234             + alignment
235             + " anchor="
236             + anchor
237             + "]";
238     }
239
240     private Dimension JavaDoc layoutSize(Container JavaDoc parent, boolean minimum)
241     {
242         Dimension JavaDoc dim = new Dimension JavaDoc(0, 0);
243         Dimension JavaDoc d;
244         synchronized (parent.getTreeLock())
245         {
246             int n = parent.getComponentCount();
247             for (int i = 0; i < n; i++)
248             {
249                 Component JavaDoc c = parent.getComponent(i);
250                 if (c.isVisible())
251                 {
252                     d = minimum ? c.getMinimumSize() : c.getPreferredSize();
253                     dim.width = Math.max(dim.width, d.width);
254                     dim.height += d.height;
255                     if (i > 0)
256                     {
257                         dim.height += vgap;
258                     }
259                 }
260             }
261         }
262         Insets JavaDoc insets = parent.getInsets();
263         dim.width += insets.left + insets.right;
264         dim.height += insets.top + insets.bottom + vgap + vgap;
265         return dim;
266     }
267 }
268
Popular Tags