KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > TableLayout


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: TableLayout.java,v $
11    Revision 1.14 2004/04/20 19:05:38 bobintetley
12    Code cleanup/refactoring
13
14    Revision 1.13 2004/01/26 08:10:59 bobintetley
15    Many bugfixes and addition of SwingSet
16
17    Revision 1.12 2004/01/08 12:02:05 bobintetley
18    Top to bottom layout
19
20    Revision 1.11 2004/01/06 08:28:02 bobintetley
21    Header fixes
22
23    Revision 1.10 2004/01/05 15:57:01 bobintetley
24    Bad calculation in TableLayout h pos
25
26    Revision 1.9 2004/01/05 15:35:56 bobintetley
27    Left debug messages in...
28
29    Revision 1.8 2004/01/05 15:29:36 bobintetley
30    TableLayout fixes
31
32    Revision 1.7 2004/01/05 13:13:45 bobintetley
33    Reimplemented SWT's GridLayout in AWT!
34
35 */

36
37 package swingwt.awt;
38
39 /**
40  * Well here's a shocker - I didn't expect to be implementing
41  * an SWT layout in AWT!
42  *
43  * @author Robin Rawson-Tetley
44  */

45 public class TableLayout implements LayoutManager, java.io.Serializable JavaDoc {
46
47     protected int hgap = 4;
48     protected int vgap = 4;
49     protected int rows;
50     protected int cols;
51         /**
52          * Lays out left to right by default, otherwise lays out top
53          * to bottom going left to right (like newspaper).
54          */

55         protected boolean leftToRight = true;
56
57     public TableLayout() {
58         this(1, 0, 4, 4);
59     }
60
61     public TableLayout(int rows, int cols) {
62         this(rows, cols, 4, 4, true);
63     }
64         
65         public TableLayout(int rows, int cols, boolean leftToRight) {
66         this(rows, cols, 4, 4, leftToRight);
67     }
68
69         public TableLayout(int rows, int cols, int hgap, int vgap) {
70                 this(rows, cols, hgap, vgap, true);
71         }
72         
73     public TableLayout(int rows, int cols, int hgap, int vgap, boolean leftToRight) {
74         if ((rows == 0) && (cols == 0)) {
75             throw new IllegalArgumentException JavaDoc("rows and cols cannot both be zero");
76         }
77         this.rows = rows;
78         this.cols = cols;
79         this.hgap = hgap;
80         this.vgap = vgap;
81                 this.leftToRight = leftToRight;
82     }
83
84     public int getRows() {
85         return rows;
86     }
87
88     public void setRows(int rows) {
89         if ((rows == 0) && (this.cols == 0)) {
90             throw new IllegalArgumentException JavaDoc("rows and cols cannot both be zero");
91         }
92         this.rows = rows;
93     }
94
95     public int getColumns() {
96         return cols;
97     }
98
99     public void setColumns(int cols) {
100         if ((cols == 0) && (this.rows == 0)) {
101             throw new IllegalArgumentException JavaDoc("rows and cols cannot both be zero");
102         }
103         this.cols = cols;
104     }
105
106     public int getHgap() {
107         return hgap;
108     }
109
110     public void setHgap(int hgap) {
111         this.hgap = hgap;
112     }
113
114     public int getVgap() {
115         return vgap;
116     }
117     
118     public void setVgap(int vgap) {
119         this.vgap = vgap;
120     }
121
122     public void addLayoutComponent(String JavaDoc name, Component comp) {
123     }
124
125     public void removeLayoutComponent(Component comp) {
126     }
127
128     public Dimension preferredLayoutSize(Container parent) {
129             Insets insets = parent.getInsets();
130             int ncomponents = parent.getComponentCount();
131             int nrows = rows;
132             int ncols = cols;
133
134             if (nrows > 0) {
135                 ncols = (ncomponents + nrows - 1) / nrows;
136             } else {
137                 nrows = (ncomponents + ncols - 1) / ncols;
138             }
139             int w = 0;
140             int h = 0;
141             for (int i = 0; i < ncomponents; i++) {
142                 Component comp = parent.getComponent(i);
143                 Dimension d = comp.getPreferredSize();
144                 if (w < d.width) {
145                     w = d.width;
146                 }
147                 if (h < d.height) {
148                     h = d.height;
149                 }
150             }
151             return new Dimension(
152                 insets.left + insets.right + ncols * w + (ncols - 1) * hgap,
153                 insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap);
154     }
155
156     public Dimension minimumLayoutSize(Container parent) {
157             Insets insets = parent.getInsets();
158             int ncomponents = parent.getComponentCount();
159             int nrows = rows;
160             int ncols = cols;
161
162             if (nrows > 0) {
163                 ncols = (ncomponents + nrows - 1) / nrows;
164             } else {
165                 nrows = (ncomponents + ncols - 1) / ncols;
166             }
167             int w = 0;
168             int h = 0;
169             for (int i = 0; i < ncomponents; i++) {
170                 Component comp = parent.getComponent(i);
171                 Dimension d = comp.getMinimumSize();
172                 if (w < d.width) {
173                     w = d.width;
174                 }
175                 if (h < d.height) {
176                     h = d.height;
177                 }
178             }
179             return new Dimension(
180                 insets.left + insets.right + ncols * w + (ncols - 1) * hgap,
181                 insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap);
182     }
183
184     public void layoutContainer(Container parent) {
185                     
186             Insets insets = parent.getInsets();
187             int ncomponents = parent.getComponentCount();
188             int nrows = rows;
189             int ncols = cols;
190
191             if (ncomponents == 0) {
192                 return;
193             }
194             
195                         if (leftToRight) {
196                             // Scan the list of components in the container and find the widest
197
// one for each colum
198
int[] maxWidths = new int[(cols)];
199
200                             int cc = 0; // current column
201
for (int i = 0; i < ncomponents; i++) {
202                                 int cw = parent.getComponent(i).getPreferredSize().width;
203                                 if (cw > maxWidths[cc])
204                                     maxWidths[cc] = cw;
205                                 cc++;
206                                 if (cc == cols) cc = 0;
207                             }
208
209                             // Now generate a list of starting points for the columns, based
210
// on the hgap and the widest values for each.
211
int[] colPoints = new int[(cols)];
212                             for (int i = 0; i < cols; i++) {
213                                 if (i != 0)
214                                     colPoints[i] = colPoints[i-1] + hgap + maxWidths[i-1];
215                                 else
216                                     colPoints[i] = hgap;
217                             }
218
219                             cc = 0;
220                             int highestInRow = 0;
221                             int curRowPos = 0;
222                             for (int i = 0; i < parent.getComponentCount(); i++) {
223                                 parent.getComponent(i).setBounds(
224                                     colPoints[cc],
225                                     curRowPos,
226                                     parent.getComponent(i).getPreferredSize().width,
227                                     parent.getComponent(i).getPreferredSize().height
228                                     );
229                                 if (parent.getComponent(i).getPreferredSize().height > highestInRow)
230                                     highestInRow = parent.getComponent(i).getPreferredSize().height;
231                                 cc++;
232                                 if (cc == cols) {
233                                     cc = 0;
234                                     // Start laying out at the tallest component on our
235
// row plus the HGap
236
curRowPos += highestInRow + vgap;
237                                     highestInRow = 0;
238                                 }
239                             }
240                     }
241                     else {
242                         
243                         // We are laying the objects out top to bottom going left to
244
// right instead of left to right top to bottom.
245

246                         // Work out how many rows to use based on the size of the
247
// container and the height of the first object (and vgap)
248
// hacky I know, but good enough
249
int totRows = parent.getHeight() / (parent.getComponent(0).getPreferredSize().height + vgap);
250
251                         // Scan the list of components in the container and find the widest
252
// one for each colum
253
int[] maxWidths = new int[(cols * 20)];
254
255                         int cr = 0; // current row
256
int cc = 0; // current col
257
for (int i = 0; i < ncomponents; i++) {
258                             int ch = parent.getComponent(i).getPreferredSize().width;
259                             if (ch > maxWidths[cc])
260                                 maxWidths[cc] = ch;
261                             cr++;
262                             if (cr == totRows) { cr = 0; cc++; }
263                         }
264                         // If there are more components than fit in the columns given
265
// screen width, then ramp up the cols to match
266
if (cols < cc) cols = cc;
267
268                         // Now generate a list of starting points for the columns, based
269
// on the hgap and the widest values for each.
270
int[] colPoints = new int[(cols * 2)];
271                         for (int i = 0; i < cols; i++) {
272                             if (i != 0)
273                                 colPoints[i] = colPoints[i-1] + hgap + maxWidths[i-1];
274                             else
275                                 colPoints[i] = hgap;
276                         }
277
278                         cc = 0;
279                         int curRowPos = 0;
280                         cr = 0;
281                         for (int i = 0; i < parent.getComponentCount(); i++) {
282                             parent.getComponent(i).setBounds(
283                                 colPoints[cc],
284                                 curRowPos,
285                                 parent.getComponent(i).getPreferredSize().width,
286                                 parent.getComponent(i).getPreferredSize().height
287                                 );
288                             cr++;
289
290                             // Vert pos
291
curRowPos += parent.getComponent(i).getPreferredSize().height + vgap;
292
293                             if (cr == totRows) {
294                                 cr = 0;
295                                 curRowPos = 0;
296                                 cc++;
297                             }
298                         }
299                     }
300             }
301 }
302
Popular Tags