Source code: com/memoire/bu/BuGridLayout.java
1 /**
2 * @modification $Date: 2003/01/23 18:58:38 $
3 * @statut unstable
4 * @file BuGridLayout.java
5 * @version 0.36
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1998-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.bu;
13
14 import com.memoire.bu.*;
15
16 import java.awt.* ;
17 import java.io.* ;
18 import java.util.* ;
19
20 /**
21 * Lays out in a grid a set of components.
22 * Not like the java.awt.GridLayout, rows may have different heigths
23 * and columns may have different widths.
24 */
25
26 public class BuGridLayout
27 implements LayoutManager2, Serializable
28 {
29 public BuGridLayout()
30 {
31 this(3,0,0,true,true);
32 }
33
34 public BuGridLayout(int _columns)
35 {
36 this(_columns,0,0,true,true);
37 }
38
39 public BuGridLayout(int _columns, int _hgap, int _vgap)
40 {
41 this(_columns,_hgap,_vgap,true,true);
42 }
43
44 public BuGridLayout(int _columns,
45 int _hgap, int _vgap,
46 boolean _hfilled, boolean _vfilled)
47 {
48 this(_columns,_hgap,_vgap,_hfilled,_vfilled,true,false);
49 }
50
51 public BuGridLayout(int _columns,
52 int _hgap, int _vgap,
53 boolean _hfilled, boolean _vfilled,
54 boolean _cfilled, boolean _rfilled)
55 {
56 columns_=_columns;
57 hgap_=_hgap;
58 vgap_=_vgap;
59 hfilled_=_hfilled;
60 vfilled_=_vfilled;
61 cfilled_=_cfilled;
62 rfilled_=_rfilled;
63 xalign_=0.5f;
64 yalign_=0.5f;
65 }
66
67 protected int columns_;
68 public int getColumns() { return columns_; }
69 public void setColumns(int _columns) { columns_=_columns; }
70
71 protected int hgap_;
72 public int getHgap() { return hgap_; }
73 public void setHgap(int _hgap) { hgap_=_hgap; }
74
75 protected int vgap_;
76 public int getVgap() { return vgap_; }
77 public void setVgap(int _vgap) { vgap_=_vgap; }
78
79 protected boolean hfilled_;
80 public boolean getHfilled() { return hfilled_; }
81 public void setHfilled(boolean _hfilled) { hfilled_=_hfilled; }
82
83 protected boolean vfilled_;
84 public boolean getVfilled() { return vfilled_; }
85 public void setVfilled(boolean _vfilled) { vfilled_=_vfilled; }
86
87 protected boolean cfilled_;
88 public boolean getCfilled() { return cfilled_; }
89 public void setCfilled(boolean _cfilled) { cfilled_=_cfilled; }
90
91 protected boolean rfilled_;
92 public boolean getRfilled() { return rfilled_; }
93 public void setRfilled(boolean _rfilled) { rfilled_=_rfilled; }
94
95 protected float xalign_;
96 public float getXalign() { return xalign_; }
97 public void setXalign(float _xalign) { xalign_=_xalign; }
98
99 protected float yalign_;
100 public float getYalign() { return yalign_; }
101 public void setYalign(float _yalign) { yalign_=_yalign; }
102
103 public void addLayoutComponent(Component _comp, Object _cstr)
104 {
105 }
106
107 public void addLayoutComponent(String _name, Component _comp)
108 {
109 }
110
111 public void removeLayoutComponent(Component _comp)
112 {
113 }
114
115 public Dimension minimumLayoutSize(Container _parent)
116 {
117 synchronized (_parent.getTreeLock())
118 {
119 int nbc=_parent.getComponentCount();
120 int w =0;
121 int h =0;
122
123 int nbcol=columns_;
124 int nbrow=(nbc+nbcol-1)/nbcol;
125
126 int[] wcol=new int[nbcol];
127 int[] hrow=new int[nbrow];
128
129 int i;
130
131 for(i=0;i<nbc;i++)
132 {
133 Component c=_parent.getComponent(i);
134 if(c.isVisible())
135 {
136 Dimension d=c.getMinimumSize();
137 wcol[i%nbcol]=Math.max(wcol[i%nbcol],d.width);
138 hrow[i/nbcol]=Math.max(hrow[i/nbcol],d.height);
139 }
140 }
141
142 for(i=0;i<nbcol;i++)
143 if(wcol[i]>0)
144 {
145 if(i>0) w+=hgap_;
146 w+=wcol[i];
147 }
148
149 for(i=0;i<nbrow;i++)
150 if(hrow[i]>0)
151 {
152 if(i>0) h+=vgap_;
153 h+=hrow[i];
154 }
155
156 Insets insets=_parent.getInsets();
157 return new Dimension(w+insets.left+insets.right,
158 h+insets.top+insets.bottom);
159 }
160 }
161
162 public Dimension preferredLayoutSize(Container _parent)
163 {
164 synchronized (_parent.getTreeLock())
165 {
166 int nbc=_parent.getComponentCount();
167 int w =0;
168 int h =0;
169
170 int nbcol=columns_;
171 int nbrow=(nbc+nbcol-1)/nbcol;
172
173 int[] wcol=new int[nbcol];
174 int[] hrow=new int[nbrow];
175
176 int i;
177
178 for(i=0;i<nbc;i++)
179 {
180 Component c=_parent.getComponent(i);
181 if(c.isVisible())
182 {
183 Dimension d=c.getPreferredSize();
184 wcol[i%nbcol]=Math.max(wcol[i%nbcol],d.width);
185 hrow[i/nbcol]=Math.max(hrow[i/nbcol],d.height);
186 }
187 }
188
189 for(i=0;i<nbcol;i++)
190 if(wcol[i]>0)
191 {
192 if(i>0) w+=hgap_;
193 w+=wcol[i];
194 }
195
196 for(i=0;i<nbrow;i++)
197 if(hrow[i]>0)
198 {
199 if(i>0) h+=vgap_;
200 h+=hrow[i];
201 }
202
203 Insets insets=_parent.getInsets();
204 return new Dimension(w+insets.left+insets.right,
205 h+insets.top+insets.bottom);
206 }
207 }
208
209 public Dimension maximumLayoutSize(Container _parent)
210 {
211 return new Dimension(Short.MAX_VALUE,Short.MAX_VALUE);
212
213 /*
214 synchronized (_parent.getTreeLock())
215 {
216 int nbc=_parent.getComponentCount();
217 int w =Short.MAX_VALUE;
218 int h =0;
219
220 int nbcol=columns_;
221 int nbrow=(nbc+nbcol-1)/nbcol;
222
223 int[] wcol=new int[nbcol];
224 int[] hrow=new int[nbrow];
225
226 int i;
227
228 for(i=0;i<nbc;i++)
229 {
230 Component c=_parent.getComponent(i);
231 if(c.isVisible())
232 {
233 Dimension d=c.getMaximumSize();
234 wcol[i%nbcol]=Math.max(wcol[i%nbcol],d.width);
235 hrow[i/nbcol]=Math.max(hrow[i/nbcol],d.height);
236 }
237 }
238
239 for(i=0;i<nbcol;i++)
240 {
241 if(i>0) w+=hgap_;
242 w+=wcol[i];
243 }
244
245 for(i=0;i<nbrow;i++)
246 {
247 if(i>0) h+=vgap_;
248 h+=hrow[i];
249 }
250
251 Insets insets=_parent.getInsets();
252 return new Dimension(w+insets.left+insets.right,
253 h+insets.top+insets.bottom);
254 }
255 */
256 }
257
258 public void invalidateLayout(Container _parent)
259 { }
260
261 public float getLayoutAlignmentX(Container _parent)
262 { return 0.5f; }
263
264 public float getLayoutAlignmentY(Container _parent)
265 { return 0.5f; }
266
267 public void layoutContainer(Container _parent)
268 {
269 synchronized (_parent.getTreeLock())
270 {
271 Insets insets=_parent.getInsets();
272 Dimension size =_parent.getSize();
273 Rectangle bounds=new Rectangle
274 (insets.left,
275 insets.top,
276 size.width-(insets.left + insets.right),
277 size.height-(insets.top + insets.bottom));
278
279 int nbc=_parent.getComponentCount();
280 int x =0;
281 int y =0;
282 int w =0;
283 int h =0;
284
285 int nbcol=columns_;
286 int nbrow=(nbc+nbcol-1)/nbcol;
287
288 int[] wcol=new int[nbcol];
289 int[] hrow=new int[nbrow];
290
291 int i;
292
293 for(i=0;i<nbc;i++)
294 {
295 Component c=_parent.getComponent(i);
296 if(c.isVisible())
297 {
298 Dimension d=c.getPreferredSize();
299 wcol[i%nbcol]=Math.max(wcol[i%nbcol],d.width);
300 hrow[i/nbcol]=Math.max(hrow[i/nbcol],d.height);
301 }
302 }
303
304 for(i=0;i<nbcol;i++)
305 if(wcol[i]>0)
306 {
307 if(i>0) w+=hgap_;
308 w+=wcol[i];
309 }
310
311 for(i=0;i<nbrow;i++)
312 if(hrow[i]>0)
313 {
314 if(i>0) h+=vgap_;
315 h+=hrow[i];
316 }
317
318 for(i=0;i<nbc;i++)
319 {
320 Component c=_parent.getComponent(i);
321 Dimension d=null;
322
323 if(c.isVisible()) d=c.getPreferredSize();
324 else d=new Dimension(0,0);
325
326 int wmax=wcol[i%nbcol];
327 int hmax=hrow[i/nbcol];
328
329 if((i>0)&&(i%nbcol==0)&&(hmax>0)) y+=vgap_;
330 if((i%nbcol!=0) &&(wmax>0)) x+=hgap_;
331
332 w=(hfilled_ ? wmax : Math.min(wmax,d.width ));
333 h=(vfilled_ ? hmax : Math.min(hmax,d.height));
334
335 // last
336 if(cfilled_&&(i%nbcol==nbcol-1)&&(x+w<bounds.width ))
337 w=wmax=bounds.width -x;
338 if(rfilled_&&(i/nbcol==nbrow-1)&&(y+h<bounds.height))
339 h=hmax=bounds.height-y;
340
341 // center
342 int dx=0;
343 int dy=0;
344 if(!hfilled_) dx=(int)((wmax-w)*xalign_);
345 if(!vfilled_) dy=(int)((hmax-h)*yalign_);
346
347 c.setBounds(bounds.x+x+dx,bounds.y+y+dy,w,h);
348 //System.err.println("-- "+i%nbcol+","+i/nbcol+": "+c.getBounds());
349
350 if((i+1)%nbcol!=0) x+=wmax;
351 else { x=0; y+=hmax; }
352 }
353 }
354 }
355 }