KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > GridLayout2


1 package snow.utils.gui;
2
3 import java.awt.*;
4
5 // Grid Layout which allows components of differrent sizes
6
public class GridLayout2 extends GridLayout
7 {
8   public GridLayout2() {
9     this(1, 0, 0, 0);
10   }
11
12   public GridLayout2(int rows, int cols) {
13     this(rows, cols, 0, 0);
14   }
15
16   public GridLayout2(int rows, int cols, int hgap, int vgap) {
17     super(rows, cols, hgap, vgap);
18   }
19
20   public Dimension preferredLayoutSize(Container parent) {
21     //System.err.println("preferredLayoutSize");
22
synchronized (parent.getTreeLock()) {
23       Insets insets = parent.getInsets();
24       int ncomponents = parent.getComponentCount();
25       int nrows = getRows();
26       int ncols = getColumns();
27       if (nrows > 0) {
28         ncols = (ncomponents + nrows - 1) / nrows;
29       }
30       else {
31         nrows = (ncomponents + ncols - 1) / ncols;
32       }
33       int[] w = new int[ncols];
34       int[] h = new int[nrows];
35       for (int i = 0; i < ncomponents; i ++) {
36         int r = i / ncols;
37         int c = i % ncols;
38         Component comp = parent.getComponent(i);
39         Dimension d = comp.getPreferredSize();
40         if (w[c] < d.width) {
41           w[c] = d.width;
42         }
43         if (h[r] < d.height) {
44           h[r] = d.height;
45         }
46       }
47       int nw = 0;
48       for (int j = 0; j < ncols; j ++) {
49         nw += w[j];
50       }
51       int nh = 0;
52       for (int i = 0; i < nrows; i ++) {
53         nh += h[i];
54       }
55       return new Dimension(insets.left + insets.right + nw + (ncols-1)*getHgap(),
56           insets.top + insets.bottom + nh + (nrows-1)*getVgap());
57     }
58   }
59
60   public Dimension minimumLayoutSize(Container parent) {
61     //System.err.println("minimumLayoutSize");
62
synchronized (parent.getTreeLock()) {
63       Insets insets = parent.getInsets();
64       int ncomponents = parent.getComponentCount();
65       int nrows = getRows();
66       int ncols = getColumns();
67       if (nrows > 0) {
68         ncols = (ncomponents + nrows - 1) / nrows;
69       }
70       else {
71         nrows = (ncomponents + ncols - 1) / ncols;
72       }
73       int[] w = new int[ncols];
74       int[] h = new int[nrows];
75       for (int i = 0; i < ncomponents; i ++) {
76         int r = i / ncols;
77         int c = i % ncols;
78         Component comp = parent.getComponent(i);
79         Dimension d = comp.getMinimumSize();
80         if (w[c] < d.width) {
81           w[c] = d.width;
82         }
83         if (h[r] < d.height) {
84           h[r] = d.height;
85         }
86       }
87       int nw = 0;
88       for (int j = 0; j < ncols; j ++) {
89         nw += w[j];
90       }
91       int nh = 0;
92       for (int i = 0; i < nrows; i ++) {
93         nh += h[i];
94       }
95       return new Dimension(insets.left + insets.right + nw + (ncols-1)*getHgap(),
96           insets.top + insets.bottom + nh + (nrows-1)*getVgap());
97     }
98   }
99
100   public void layoutContainer(Container parent) {
101     //System.err.println("layoutContainer");
102
synchronized (parent.getTreeLock()) {
103       Insets insets = parent.getInsets();
104       int ncomponents = parent.getComponentCount();
105       int nrows = getRows();
106       int ncols = getColumns();
107       if (ncomponents == 0) {
108         return;
109       }
110       if (nrows > 0) {
111         ncols = (ncomponents + nrows - 1) / nrows;
112       }
113       else {
114         nrows = (ncomponents + ncols - 1) / ncols;
115       }
116       int hgap = getHgap();
117       int vgap = getVgap();
118       // scaling factors
119
Dimension pd = preferredLayoutSize(parent);
120       double sw = (1.0 * parent.getWidth()) / pd.width;
121       double sh = (1.0 * parent.getHeight()) / pd.height;
122       // scale
123
int[] w = new int[ncols];
124       int[] h = new int[nrows];
125       for (int i = 0; i < ncomponents; i ++) {
126         int r = i / ncols;
127         int c = i % ncols;
128         Component comp = parent.getComponent(i);
129         Dimension d = comp.getPreferredSize();
130         d.width = (int) (sw * d.width);
131         d.height = (int) (sh * d.height);
132         if (w[c] < d.width) {
133           w[c] = d.width;
134         }
135         if (h[r] < d.height) {
136           h[r] = d.height;
137         }
138       }
139       for (int c = 0, x = insets.left; c < ncols; c ++) {
140         for (int r = 0, y = insets.top; r < nrows; r ++) {
141           int i = r * ncols + c;
142           if (i < ncomponents) {
143             parent.getComponent(i).setBounds(x, y, w[c], h[r]);
144           }
145           y += h[r] + vgap;
146         }
147         x += w[c] + hgap;
148       }
149     }
150   }
151 }
Popular Tags