KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > custom > layout > 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.2 2004/04/20 19:05:45 bobintetley
12    Code cleanup/refactoring
13
14    Revision 1.1 2004/01/20 07:38:05 bobintetley
15    Bug fixes and compatibility methods
16
17
18 */

19
20 package swingwtx.custom.layout;
21
22 import swingwt.awt.*;
23
24 /**
25  * Well here's a shocker - I didn't expect to be implementing
26  * an SWT layout in AWT!
27  *
28  * @author Robin Rawson-Tetley
29  */

30 public class TableLayout implements LayoutManager, java.io.Serializable JavaDoc {
31
32     int hgap = 4;
33     int vgap = 4;
34     int rows;
35     int cols;
36         /**
37          * Lays out left to right by default, otherwise lays out top
38          * to bottom going left to right (like newspaper).
39          */

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

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