KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjVerticalFlowLayout


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import java.awt.Component JavaDoc;
33 import java.awt.Container JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.FlowLayout JavaDoc;
36 import java.awt.Insets JavaDoc;
37
38 /**
39  * VFlowLayout is similar to FlowLayout except it lays out components
40  * vertically. Extends FlowLayout because it mimics much of the
41  * behavior of the FlowLayout class, except vertically. An additional
42  * feature is that you can specify a fill to edge flag, which causes
43  * the verticalFlowLayout manager to resize all components to expand to the
44  * column width Warning: This causes problems when the main panel
45  * has less space that it needs and it seems to prohibit multi-column
46  * output. Additionally there is a vertical fill flag, which fills the last
47  * component to the remaining height of the container.
48  */

49 public class DjVerticalFlowLayout extends FlowLayout JavaDoc implements java.io.Serializable JavaDoc
50 {
51   private static final long serialVersionUID = 1L;
52
53   public static final int TOP = 0;
54   public static final int MIDDLE = 1;
55   public static final int BOTTOM = 2;
56
57   //int align;
58
int hgap;
59   int vgap;
60   boolean hfill;
61   boolean vfill;
62
63   /**
64    * Construct a new verticalFlowLayout with a middle alignemnt, and
65    * the fill to edge flag set.
66    */

67   public DjVerticalFlowLayout()
68   {
69     this(TOP, 5, 5, true, false);
70   }
71
72   /**
73    * Construct a new verticalFlowLayout with a middle alignemnt.
74    * @param fill the fill to edge flag
75    */

76   public DjVerticalFlowLayout(boolean hfill, boolean vfill)
77   {
78     this(TOP, 5, 5, hfill, vfill);
79   }
80
81   /**
82    * Construct a new verticalFlowLayout with a middle alignemnt.
83    * @param align the alignment value
84    */

85   public DjVerticalFlowLayout(int align)
86   {
87     this(align, 5, 5, true, false);
88   }
89
90   /**
91    * Construct a new verticalFlowLayout.
92    * @param align the alignment value
93    * @param fill the fill to edge flag
94    */

95   public DjVerticalFlowLayout(int align, boolean hfill, boolean vfill)
96   {
97     this(align, 5, 5, hfill, vfill);
98   }
99
100   /**
101    * Construct a new verticalFlowLayout.
102    * @param align the alignment value
103    * @param hgap the horizontal gap variable
104    * @param vgap the vertical gap variable
105    * @param fill the fill to edge flag
106    */

107   public DjVerticalFlowLayout(int align, int hgap, int vgap, boolean hfill, boolean vfill)
108   {
109     setAlignment(align);
110     this.hgap = hgap;
111     this.vgap = vgap;
112     this.hfill = hfill;
113     this.vfill = vfill;
114   }
115
116   /**
117    * Gets the horizontal gap between components.
118    * @return the horizontal gap between components.
119    * @see java.awt.FlowLayout#setHgap
120    * @since JDK1.1
121    */

122   public int getHgap()
123   {
124     return hgap;
125   }
126
127   /**
128    * Sets the horizontal gap between components.
129    */

130   public void setHgap(int hgap)
131   {
132     super.setHgap(hgap);
133     this.hgap = hgap;
134   }
135
136   /**
137    * Gets the vertical gap between components.
138    */

139   public int getVgap()
140   {
141     return vgap;
142   }
143
144   /**
145    * Sets the vertical gap between components.
146    */

147   public void setVgap(int vgap)
148   {
149     super.setVgap(vgap);
150     this.vgap = vgap;
151   }
152
153   /**
154    * Returns the preferred dimensions given the components
155    * in the target container.
156    * @param target the component to lay out
157    */

158   public Dimension JavaDoc preferredLayoutSize(Container JavaDoc target)
159   {
160     Dimension JavaDoc tarsiz = new Dimension JavaDoc(0, 0);
161
162     for (int i = 0; i < target.getComponentCount(); i++)
163     {
164       Component JavaDoc m = target.getComponent(i);
165       if (m.isVisible())
166       {
167         Dimension JavaDoc d = m.getPreferredSize();
168         tarsiz.width = Math.max(tarsiz.width, d.width);
169         if (i > 0)
170         {
171           tarsiz.height += vgap;
172         }
173         tarsiz.height += d.height;
174       }
175     }
176     Insets JavaDoc insets = target.getInsets();
177     tarsiz.width += insets.left + insets.right + hgap * 2;
178     tarsiz.height += insets.top + insets.bottom + vgap * 2;
179     return tarsiz;
180   }
181
182   /**
183    * Returns the minimum size needed to layout the target container
184    * @param target the component to lay out
185    */

186   public Dimension JavaDoc minimumLayoutSize(Container JavaDoc target)
187   {
188     Dimension JavaDoc tarsiz = new Dimension JavaDoc(0, 0);
189
190     for (int i = 0; i < target.getComponentCount(); i++)
191     {
192       Component JavaDoc m = target.getComponent(i);
193       if (m.isVisible())
194       {
195         Dimension JavaDoc d = m.getMinimumSize();
196         tarsiz.width = Math.max(tarsiz.width, d.width);
197         if (i > 0)
198         {
199           tarsiz.height += vgap;
200         }
201         tarsiz.height += d.height;
202       }
203     }
204     Insets JavaDoc insets = target.getInsets();
205     tarsiz.width += insets.left + insets.right + hgap * 2;
206     tarsiz.height += insets.top + insets.bottom + vgap * 2;
207     return tarsiz;
208   }
209
210   public void setVerticalFill(boolean vfill)
211   {
212     this.vfill = vfill;
213   }
214
215   public boolean getVerticalFill()
216   {
217     return vfill;
218   }
219
220   public void setHorizontalFill(boolean hfill)
221   {
222     this.hfill = hfill;
223   }
224
225   public boolean getHorizontalFill()
226   {
227     return hfill;
228   }
229
230   /**
231    * places the components defined by first to last within the target
232    * container using the bounds box defined
233    * @param target the container
234    * @param x the x coordinate of the area
235    * @param y the y coordinate of the area
236    * @param width the width of the area
237    * @param height the height of the area
238    * @param first the first component of the container to place
239    * @param last the last component of the container to place
240    */

241   private void placethem(Container JavaDoc target, int x, int y, int width, int height, int first, int last)
242   {
243     int align = getAlignment();
244     //if ( align == this.TOP )
245
// y = 0;
246
if (align == DjVerticalFlowLayout.MIDDLE) y += height / 2;
247     if (align == DjVerticalFlowLayout.BOTTOM) y += height;
248
249     for (int i = first; i < last; i++)
250     {
251       Component JavaDoc m = target.getComponent(i);
252       Dimension JavaDoc md = m.getSize();
253       if (m.isVisible())
254       {
255         m.setLocation(x, y);
256         y += vgap + md.height;
257       }
258     }
259   }
260
261   /**
262    * Lays out the container.
263    * @param target the container to lay out.
264    */

265   public void layoutContainer(Container JavaDoc target)
266   {
267     Insets JavaDoc insets = target.getInsets();
268     int maxheight = target.getSize().height - (insets.top + insets.bottom + vgap * 2);
269     int maxwidth = target.getSize().width - (insets.left + insets.right + hgap * 2);
270     int numcomp = target.getComponentCount();
271     int x = insets.left + hgap;
272     int y = 0;
273     int colw = 0, start = 0;
274
275     for (int i = 0; i < numcomp; i++)
276     {
277       Component JavaDoc comp = target.getComponent(i);
278       if (comp.isVisible())
279       {
280         Dimension JavaDoc compDim = comp.getPreferredSize();
281         // fit last component to remaining height
282
if ((this.vfill) && (i == (numcomp - 1)))
283         {
284           compDim.height = Math.max((maxheight - y), comp.getPreferredSize().height);
285         }
286
287         // fit componenent size to container width
288
if (this.hfill)
289         {
290           comp.setSize(maxwidth, compDim.height);
291           compDim.width = maxwidth;
292         }
293         else
294         {
295           comp.setSize(compDim.width, compDim.height);
296         }
297
298         /*
299          if ( y + compDim.height > maxheight ) {
300          placethem(target, x, insets.top + vgap, colw, maxheight-y, start, i);
301          y = compDim.height;
302          x += hgap + colw;
303          colw = compDim.width;
304          start = i;
305          }
306          else
307          */
{
308           if (y > 0) y += vgap;
309           y += compDim.height;
310           colw = Math.max(colw, compDim.width);
311         }
312       }
313     }
314     placethem(target, x, insets.top + vgap, colw, maxheight - y, start, numcomp);
315   }
316 }
Popular Tags