KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > swtcustomlayout > SWTGridLayout


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: SWTGridLayout.java,v $
11    Revision 1.5 2003/12/14 09:13:38 bobintetley
12    Added CVS log to source headers
13
14 */

15
16
17 package swingwt.awt.swtcustomlayout;
18
19 import org.eclipse.swt.widgets.*;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.graphics.Rectangle;
22
23 public class SWTGridLayout extends Layout
24 {
25     private int _hgap;
26     private int _vgap;
27     private int _rows;
28     private int _columns;
29
30     public SWTGridLayout()
31     {
32         this(1, 0);
33     }
34
35     public SWTGridLayout(int rows, int columns)
36     {
37         this(rows, columns, 0, 0);
38     }
39
40     public SWTGridLayout(int rows, int columns, int hgap, int vgap)
41     {
42         if (rows == 0 && columns == 0)
43             throw new IllegalArgumentException JavaDoc("rows and cols cannot both be zero");
44         if (rows != 0)
45             setRows(rows);
46         setColumns(columns);
47         setHgap(hgap);
48         setVgap(vgap);
49     }
50
51     protected void layout(Composite target, boolean flushCache)
52     {
53         Rectangle r = target.getClientArea();
54         Control children[] = target.getChildren();
55         int ncomponents = children.length;
56         int nrows = getRows();
57         int ncols = getColumns();
58         if (ncomponents == 0)
59             return;
60         if (nrows > 0)
61             ncols = (ncomponents + nrows - 1) / nrows;
62         else
63             nrows = (ncomponents + ncols - 1) / ncols;
64         int w = r.width;
65         int h = r.height;
66         w = (w - (ncols - 1) * getHgap()) / ncols;
67         h = (h - (nrows - 1) * getVgap()) / nrows;
68         int col = 0;
69         int x = 0;
70         while (col < ncols)
71         {
72             int row = 0;
73             int y = 0;
74             while (row < nrows)
75             {
76                 int i = row * ncols + col;
77                 if (i < ncomponents)
78                     children[i].setBounds(x, y, w, h);
79                 row++;
80                 y += h + getVgap();
81             }
82             col++;
83             x += w + getHgap();
84         }
85     }
86
87     protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache)
88     {
89         Control children[] = composite.getChildren();
90         int ncomponents = children.length;
91         int nrows = getRows();
92         int ncols = getColumns();
93         if (nrows > 0)
94             ncols = (ncomponents + nrows - 1) / nrows;
95         else
96             nrows = (ncomponents + ncols - 1) / ncols;
97         int w = 0;
98         int h = 0;
99         for (int i = 0; i < ncomponents; i++)
100         {
101             Control comp = children[i];
102             Point d = comp.computeSize(-1, -1);
103             if (w < d.x)
104                 w = d.x;
105             if (h < d.y)
106                 h = d.y;
107         }
108         return new Point(ncols * w + (ncols - 1) * getHgap(), nrows * h + (nrows - 1) * getVgap());
109     }
110
111     public String JavaDoc toString()
112     {
113         return "[" + paramString() + "]";
114     }
115
116     protected String JavaDoc paramString()
117     {
118         return getHgap() + ",vgap=" + getVgap() + ",rows=" + getRows() + ",cols=" + getColumns();
119     }
120
121     public int getRows()
122     {
123         return _rows;
124     }
125
126     public void setRows(int rows)
127     {
128         if (rows == 0 && getColumns() == 0)
129             throw new IllegalArgumentException JavaDoc("rows and cols cannot both be zero");
130         _rows = rows;
131     }
132
133     public int getColumns()
134     {
135         return _columns;
136     }
137
138     public void setColumns(int columns)
139     {
140         if (columns == 0 && getRows() == 0)
141             throw new IllegalArgumentException JavaDoc("rows and cols cannot both be zero");
142         _columns = columns;
143     }
144
145     public int getHgap()
146     {
147         return _hgap;
148     }
149
150     public void setHgap(int hgap)
151     {
152         _hgap = hgap;
153     }
154
155     public int getVgap()
156     {
157         return _vgap;
158     }
159
160     public void setVgap(int vgap)
161     {
162         _vgap = vgap;
163     }
164 }
165
Popular Tags