KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > GridLayout


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: GridLayout.java,v $
11    Revision 1.9 2004/04/20 16:35:50 bobintetley
12    Code cleanup
13
14    Revision 1.8 2004/02/02 12:36:36 bobintetley
15    Proper JScrollPane/ScrollBar implementation
16
17    Revision 1.7 2004/01/06 08:28:02 bobintetley
18    Header fixes
19
20  
21  */

22
23 package swingwt.awt;
24
25 public class GridLayout implements LayoutManager {
26     
27     int hgap;
28     int vgap;
29     int rows;
30     int cols;
31     
32     public GridLayout() {
33         this(1, 0, 0, 0);
34     }
35     
36     public GridLayout(int rows, int cols) {
37         this(rows, cols, 0, 0);
38     }
39     
40     public GridLayout(int rows, int cols, int hgap, int vgap) {
41         if ((rows == 0) && (cols == 0)) {
42             throw new IllegalArgumentException JavaDoc("zero size");
43         }
44         this.cols = cols;
45         this.rows = rows;
46         this.hgap = hgap;
47         this.vgap = vgap;
48     }
49     
50     public int getRows() {
51         return rows;
52     }
53     
54     public void setRows(int rows) {
55         if ((rows == 0) && (this.cols == 0)) {
56             throw new IllegalArgumentException JavaDoc("zero size");
57         }
58         this.rows = rows;
59     }
60     
61     public int getColumns() {
62         return cols;
63     }
64     
65     public void setColumns(int cols) {
66         if ((cols == 0) && (this.rows == 0)) {
67             throw new IllegalArgumentException JavaDoc("zero size");
68         }
69         this.cols = cols;
70     }
71     
72     public int getHgap() {
73         return hgap;
74     }
75     
76     public void setHgap(int hgap) {
77         this.hgap = hgap;
78     }
79     
80     public int getVgap() {
81         return vgap;
82     }
83     
84     public void setVgap(int vgap) {
85         this.vgap = vgap;
86     }
87     
88     public void addLayoutComponent(String JavaDoc name, Component comp) {
89     }
90     
91     public void removeLayoutComponent(Component comp) {
92     }
93     
94     public Dimension preferredLayoutSize(Container parent) {
95         Insets insets = parent.getInsets();
96         int ncomponents = parent.getComponentCount();
97         int nrows = rows;
98         int ncols = cols;
99
100         if (nrows > 0) {
101             ncols = (ncomponents + nrows - 1) / nrows;
102         } else {
103             nrows = (ncomponents + ncols - 1) / ncols;
104         }
105         int w = 0;
106         int h = 0;
107         for (int i = 0; i < ncomponents; i++) {
108             Component comp = parent.getComponent(i);
109             Dimension d = comp.getPreferredSize();
110             if (w < d.width) {
111                 w = d.width;
112             }
113             if (h < d.height) {
114                 h = d.height;
115             }
116         }
117         Dimension d = new Dimension(
118         insets.left + insets.right + ncols * w + (ncols - 1) * hgap,
119         insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap);
120         return d;
121     }
122     
123     public Dimension minimumLayoutSize(Container parent) {
124         Insets insets = parent.getInsets();
125         int ncomponents = parent.getComponentCount();
126         int nrows = rows;
127         int ncols = cols;
128         if (nrows > 0) {
129             ncols = (ncomponents + nrows - 1) / nrows;
130         }
131         else {
132             nrows = (ncomponents + ncols - 1) / ncols;
133         }
134         int w = 0;
135         int h = 0;
136         for (int i = 0; i < ncomponents; i++) {
137             Component comp = parent.getComponent(i);
138             Dimension d = comp.getMinimumSize();
139             if (w < d.width) {
140                 w = d.width;
141             }
142             if (h < d.height) {
143                 h = d.height;
144             }
145         }
146         return new Dimension(
147         insets.left + insets.right + ncols * w + (ncols - 1) * hgap,
148         insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap);
149     }
150     
151     public void layoutContainer(Container parent) {
152         Insets insets = parent.getInsets();
153         int ncomponents = parent.getComponentCount();
154         int nrows = rows;
155         int ncols = cols;
156         boolean ltr = parent.getComponentOrientation().isLeftToRight();
157         if (ncomponents == 0) {
158             return;
159         }
160         if (nrows > 0) {
161             ncols = (ncomponents + nrows - 1) / nrows;
162         } else {
163             nrows = (ncomponents + ncols - 1) / ncols;
164         }
165         int w = parent.getWidth() - (insets.left + insets.right);
166         int h = parent.getHeight() - (insets.top + insets.bottom);
167         w = (w - (ncols - 1) * hgap) / ncols;
168         h = (h - (nrows - 1) * vgap) / nrows;
169         if (ltr) {
170             for (int c = 0, x = insets.left;
171             c < ncols;
172             c++, x += w + hgap) {
173                 for (int r = 0, y = insets.top;
174                 r < nrows;
175                 r++, y += h + vgap) {
176                     int i = r * ncols + c;
177                     if (i < ncomponents) {
178                         parent.getComponent(i).setBounds(x, y, w, h);
179                     }
180                 }
181             }
182         }
183         else {
184             for (int c = 0, x = parent.getWidth() - insets.right - w; c < ncols; c++, x -= w + hgap) {
185                 for (int r = 0, y = insets.top; r < nrows; r++, y += h + vgap) {
186                     int i = r * ncols + c;
187                     if (i < ncomponents) {
188                         parent.getComponent(i).setBounds(x, y, w, h);
189                     }
190                 }
191             }
192         }
193     }
194
195 }
196
Popular Tags