43 |
|
|
44 |
/** |
/** |
45 |
* @author Michael Koch <konqueror@gmx.de> |
* @author Michael Koch <konqueror@gmx.de> |
46 |
|
* @author Jeroen Frijters <jeroen@frijters.net> |
47 |
*/ |
*/ |
48 |
public class GridBagLayout |
public class GridBagLayout |
49 |
implements Serializable, LayoutManager2 |
implements Serializable, LayoutManager2 |
50 |
{ |
{ |
51 |
private static final long serialVersionUID = 8838754796412211005L; |
private static final long serialVersionUID = 8838754796412211005L; |
52 |
|
|
53 |
protected static final int MINSIZE = 1; |
protected static final int MINSIZE = 1; |
54 |
protected static final int PREFERREDSIZE = 2; |
protected static final int PREFERREDSIZE = 2; |
55 |
protected static final int MAXGRIDSIZE = 512; |
protected static final int MAXGRIDSIZE = 512; |
56 |
|
|
57 |
protected Hashtable comptable; |
protected Hashtable comptable; |
58 |
protected GridBagLayoutInfo layoutInfo; |
protected GridBagLayoutInfo layoutInfo; |
59 |
protected GridBagConstraints defaultConstraints; |
protected GridBagConstraints defaultConstraints; |
60 |
|
|
61 |
public double[] colWeights; |
public double[] columnWeights; |
62 |
public int[] colWidths; |
public int[] columnWidths; |
63 |
public double[] rowWeights; |
public double[] rowWeights; |
64 |
public int[] rowHeights; |
public int[] rowHeights; |
65 |
|
|
66 |
public GridBagLayout () |
public GridBagLayout () |
67 |
{ |
{ |
68 |
this.comptable = new Hashtable(); |
this.comptable = new Hashtable(); |
69 |
this.defaultConstraints= new GridBagConstraints(); |
this.defaultConstraints= new GridBagConstraints(); |
70 |
} |
} |
71 |
|
|
72 |
/** |
/** |
73 |
* Helper method to calc the sum of all elements in an int array. |
* Helper method to calc the sum of a range of elements in an int array. |
74 |
*/ |
*/ |
75 |
private int sumIntArray (int[] array) |
private int sumIntArray (int[] array, int upto) |
76 |
{ |
{ |
77 |
int result = 0; |
int result = 0; |
78 |
|
|
79 |
for (int i = 0; i < array.length; i++) |
for (int i = 0; i < upto; i++) |
80 |
result += array [i]; |
result += array [i]; |
81 |
|
|
82 |
return result; |
return result; |
83 |
} |
} |
84 |
|
|
85 |
/** |
/** |
86 |
* Helper method to calc the sum of all elements in an double array. |
* Helper method to calc the sum of all elements in an int array. |
87 |
*/ |
*/ |
88 |
private double sumDoubleArray (double[] array) |
private int sumIntArray (int[] array) |
89 |
{ |
{ |
90 |
double result = 0; |
return sumIntArray(array, array.length); |
91 |
|
} |
92 |
for (int i = 0; i < array.length; i++) |
|
93 |
result += array [i]; |
/** |
94 |
|
* Helper method to calc the sum of all elements in an double array. |
95 |
return result; |
*/ |
96 |
} |
private double sumDoubleArray (double[] array) |
97 |
|
{ |
98 |
public void addLayoutComponent (String name, Component component) |
double result = 0; |
99 |
{ |
|
100 |
// do nothing here. |
for (int i = 0; i < array.length; i++) |
101 |
} |
result += array [i]; |
102 |
|
|
103 |
public void removeLayoutComponent (Component component) |
return result; |
104 |
{ |
} |
105 |
// do nothing here |
|
106 |
} |
public void addLayoutComponent (String name, Component component) |
107 |
|
{ |
108 |
public void addLayoutComponent (Component component, Object constraints) |
// do nothing here. |
109 |
{ |
} |
110 |
if (constraints == null) |
|
111 |
return; |
public void removeLayoutComponent (Component component) |
112 |
|
{ |
113 |
if (!(constraints instanceof GridBagConstraints)) |
// do nothing here |
114 |
throw new IllegalArgumentException(); |
} |
115 |
|
|
116 |
setConstraints (component, (GridBagConstraints) constraints); |
public void addLayoutComponent (Component component, Object constraints) |
117 |
} |
{ |
118 |
|
if (constraints == null) |
119 |
public Dimension preferredLayoutSize (Container parent) |
return; |
120 |
{ |
|
121 |
if (parent == null) |
if (!(constraints instanceof GridBagConstraints)) |
122 |
return new Dimension (0, 0); |
throw new IllegalArgumentException(); |
123 |
|
|
124 |
GridBagLayoutInfo li = getLayoutInfo (parent, PREFERREDSIZE); |
setConstraints (component, (GridBagConstraints) constraints); |
125 |
return getMinSize (parent, li); |
} |
126 |
} |
|
127 |
|
public Dimension preferredLayoutSize (Container parent) |
128 |
public Dimension minimumLayoutSize (Container parent) |
{ |
129 |
{ |
if (parent == null) |
130 |
if (parent == null) |
return new Dimension (0, 0); |
131 |
return new Dimension (0, 0); |
|
132 |
|
GridBagLayoutInfo li = getLayoutInfo (parent, PREFERREDSIZE); |
133 |
GridBagLayoutInfo li = getLayoutInfo (parent, MINSIZE); |
return getMinSize (parent, li); |
134 |
return getMinSize (parent, li); |
} |
135 |
} |
|
136 |
|
public Dimension minimumLayoutSize (Container parent) |
137 |
public Dimension maximumLayoutSize (Container target) |
{ |
138 |
{ |
if (parent == null) |
139 |
return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE); |
return new Dimension (0, 0); |
140 |
} |
|
141 |
|
GridBagLayoutInfo li = getLayoutInfo (parent, MINSIZE); |
142 |
public void layoutContainer (Container parent) |
return getMinSize (parent, li); |
143 |
{ |
} |
144 |
arrangeGrid (parent); |
|
145 |
} |
public Dimension maximumLayoutSize (Container target) |
146 |
|
{ |
147 |
public float getLayoutAlignmentX (Container target) |
return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE); |
148 |
{ |
} |
149 |
return Component.CENTER_ALIGNMENT; |
|
150 |
} |
public void layoutContainer (Container parent) |
151 |
|
{ |
152 |
public float getLayoutAlignmentY (Container target) |
arrangeGrid (parent); |
153 |
{ |
} |
154 |
return Component.CENTER_ALIGNMENT; |
|
155 |
} |
public float getLayoutAlignmentX (Container target) |
156 |
|
{ |
157 |
public void invalidateLayout (Container target) |
return Component.CENTER_ALIGNMENT; |
158 |
{ |
} |
159 |
this.layoutInfo = null; |
|
160 |
} |
public float getLayoutAlignmentY (Container target) |
161 |
|
{ |
162 |
public void setConstraints (Component component, |
return Component.CENTER_ALIGNMENT; |
163 |
GridBagConstraints constraints) |
} |
164 |
{ |
|
165 |
GridBagConstraints clone = (GridBagConstraints) constraints.clone(); |
public void invalidateLayout (Container target) |
166 |
|
{ |
167 |
if (clone.gridx < 0) |
this.layoutInfo = null; |
168 |
clone.gridx = GridBagConstraints.RELATIVE; |
} |
169 |
|
|
170 |
if (clone.gridy < 0) |
public void setConstraints (Component component, |
171 |
clone.gridy = GridBagConstraints.RELATIVE; |
GridBagConstraints constraints) |
172 |
|
{ |
173 |
if (clone.gridwidth == 0) |
GridBagConstraints clone = (GridBagConstraints) constraints.clone(); |
174 |
clone.gridwidth = GridBagConstraints.REMAINDER; |
|
175 |
else if (clone.gridwidth < 0 |
if (clone.gridx < 0) |
176 |
&& clone.gridwidth != GridBagConstraints.REMAINDER |
clone.gridx = GridBagConstraints.RELATIVE; |
177 |
&& clone.gridwidth != GridBagConstraints.RELATIVE) |
|
178 |
clone.gridwidth = 1; |
if (clone.gridy < 0) |
179 |
|
clone.gridy = GridBagConstraints.RELATIVE; |
180 |
if (clone.gridheight == 0) |
|
181 |
clone.gridheight = GridBagConstraints.REMAINDER; |
if (clone.gridwidth == 0) |
182 |
else if (clone.gridheight < 0 |
clone.gridwidth = GridBagConstraints.REMAINDER; |
183 |
&& clone.gridheight != GridBagConstraints.REMAINDER |
else if (clone.gridwidth < 0 |
184 |
&& clone.gridheight != GridBagConstraints.RELATIVE) |
&& clone.gridwidth != GridBagConstraints.REMAINDER |
185 |
clone.gridheight = 1; |
&& clone.gridwidth != GridBagConstraints.RELATIVE) |
186 |
|
clone.gridwidth = 1; |
187 |
comptable.put (component, clone); |
|
188 |
} |
if (clone.gridheight == 0) |
189 |
|
clone.gridheight = GridBagConstraints.REMAINDER; |
190 |
public GridBagConstraints getConstraints (Component component) |
else if (clone.gridheight < 0 |
191 |
{ |
&& clone.gridheight != GridBagConstraints.REMAINDER |
192 |
return (GridBagConstraints) (lookupConstraints (component).clone()); |
&& clone.gridheight != GridBagConstraints.RELATIVE) |
193 |
} |
clone.gridheight = 1; |
194 |
|
|
195 |
protected GridBagConstraints lookupConstraints (Component component) |
comptable.put (component, clone); |
196 |
{ |
} |
197 |
GridBagConstraints result = (GridBagConstraints) comptable.get (component); |
|
198 |
|
public GridBagConstraints getConstraints (Component component) |
199 |
if (result == null) |
{ |
200 |
{ |
return (GridBagConstraints) (lookupConstraints (component).clone()); |
201 |
setConstraints (component, defaultConstraints); |
} |
202 |
result = (GridBagConstraints) comptable.get (component); |
|
203 |
} |
protected GridBagConstraints lookupConstraints (Component component) |
204 |
|
{ |
205 |
return result; |
GridBagConstraints result = (GridBagConstraints) comptable.get (component); |
206 |
} |
|
207 |
|
if (result == null) |
208 |
/** |
{ |
209 |
* @since 1.1 |
setConstraints (component, defaultConstraints); |
210 |
*/ |
result = (GridBagConstraints) comptable.get (component); |
211 |
public Point getLayoutOrigin () |
} |
212 |
{ |
|
213 |
if (layoutInfo == null) |
return result; |
214 |
return new Point (0, 0); |
} |
215 |
|
|
216 |
return new Point (layoutInfo.pos_x, layoutInfo.pos_y); |
/** |
217 |
} |
* @since 1.1 |
218 |
|
*/ |
219 |
/** |
public Point getLayoutOrigin () |
220 |
* @since 1.1 |
{ |
221 |
*/ |
if (layoutInfo == null) |
222 |
public int[][] getLayoutDimensions () |
return new Point (0, 0); |
223 |
{ |
|
224 |
if (layoutInfo == null) |
return new Point (layoutInfo.pos_x, layoutInfo.pos_y); |
225 |
return new int [2][]; |
} |
226 |
|
|
227 |
int[][] result = new int [2][]; |
/** |
228 |
result [0] = new int [layoutInfo.cols]; |
* @since 1.1 |
229 |
System.arraycopy (layoutInfo.colWidths, 0, result [0], 0, layoutInfo.cols); |
*/ |
230 |
result [1] = new int [layoutInfo.rows]; |
public int[][] getLayoutDimensions () |
231 |
System.arraycopy (layoutInfo.rowHeights, 0, result [1], 0, layoutInfo.rows); |
{ |
232 |
return result; |
if (layoutInfo == null) |
233 |
} |
return new int [2][]; |
234 |
|
|
235 |
public double[][] getLayoutWeights () |
int[][] result = new int [2][]; |
236 |
{ |
result [0] = new int [layoutInfo.cols]; |
237 |
if (layoutInfo == null) |
System.arraycopy (layoutInfo.colWidths, 0, result [0], 0, layoutInfo.cols); |
238 |
return new double [2][]; |
result [1] = new int [layoutInfo.rows]; |
239 |
|
System.arraycopy (layoutInfo.rowHeights, 0, result [1], 0, layoutInfo.rows); |
240 |
|
return result; |
241 |
|
} |
242 |
|
|
243 |
|
public double[][] getLayoutWeights () |
244 |
|
{ |
245 |
|
if (layoutInfo == null) |
246 |
|
return new double [2][]; |
247 |
|
|
248 |
double[][] result = new double [2][]; |
double[][] result = new double [2][]; |
249 |
result [0] = new double [layoutInfo.cols]; |
result [0] = new double [layoutInfo.cols]; |
250 |
System.arraycopy (layoutInfo.colWeights, 0, result [0], 0, layoutInfo.cols); |
System.arraycopy (layoutInfo.colWeights, 0, result [0], 0, layoutInfo.cols); |
251 |
result [1] = new double [layoutInfo.rows]; |
result [1] = new double [layoutInfo.rows]; |
252 |
System.arraycopy (layoutInfo.rowWeights, 0, result [1], 0, layoutInfo.rows); |
System.arraycopy (layoutInfo.rowWeights, 0, result [1], 0, layoutInfo.rows); |
253 |
return result; |
return result; |
254 |
} |
} |
255 |
|
|
256 |
/** |
/** |
257 |
* @since 1.1 |
* @since 1.1 |
258 |
*/ |
*/ |
259 |
public Point location (int x, int y) |
public Point location (int x, int y) |
260 |
{ |
{ |
261 |
if (layoutInfo == null) |
if (layoutInfo == null) |
262 |
return new Point (0, 0); |
return new Point (0, 0); |
263 |
|
|
264 |
int col; |
int col; |
265 |
int row; |
int row; |
266 |
int pixel_x = layoutInfo.pos_x; |
int pixel_x = layoutInfo.pos_x; |
267 |
int pixel_y = layoutInfo.pos_y; |
int pixel_y = layoutInfo.pos_y; |
268 |
|
|
269 |
for (col = 0; col < layoutInfo.cols; col++) |
for (col = 0; col < layoutInfo.cols; col++) |
270 |
{ |
{ |
271 |
if (pixel_x < x) |
int w = layoutInfo.colWidths [col]; |
272 |
break; |
if (x < pixel_x + w) |
273 |
|
break; |
274 |
pixel_x += layoutInfo.colWidths [col]; |
|
275 |
} |
pixel_x += w; |
276 |
|
} |
277 |
for (row = 0; row < layoutInfo.rows; row++) |
|
278 |
{ |
for (row = 0; row < layoutInfo.rows; row++) |
279 |
if (pixel_y < y) |
{ |
280 |
break; |
int h = layoutInfo.rowHeights [row]; |
281 |
|
if (y < pixel_y + h) |
282 |
pixel_y += layoutInfo.rowHeights [row]; |
break; |
283 |
} |
|
284 |
|
pixel_y += h; |
285 |
return new Point (col, row); |
} |
286 |
} |
|
287 |
|
return new Point (col, row); |
288 |
/** |
} |
289 |
* Obsolete. |
|
290 |
*/ |
/** |
291 |
protected void AdjustForGravity (GridBagConstraints gbc, Rectangle rect) |
* Obsolete. |
292 |
{ |
*/ |
293 |
adjustForGravity (gbc, rect); |
protected void AdjustForGravity (GridBagConstraints gbc, Rectangle rect) |
294 |
} |
{ |
295 |
|
adjustForGravity (gbc, rect); |
296 |
/** |
} |
297 |
* Obsolete. |
|
298 |
*/ |
/** |
299 |
protected void ArrangeGrid (Container parent) |
* Obsolete. |
300 |
{ |
*/ |
301 |
arrangeGrid (parent); |
protected void ArrangeGrid (Container parent) |
302 |
} |
{ |
303 |
|
arrangeGrid (parent); |
304 |
/** |
} |
305 |
* Obsolete. |
|
306 |
*/ |
/** |
307 |
protected GridBagLayoutInfo GetLayoutInfo (Container parent, int sizeflag) |
* Obsolete. |
308 |
{ |
*/ |
309 |
return getLayoutInfo (parent, sizeflag); |
protected GridBagLayoutInfo GetLayoutInfo (Container parent, int sizeflag) |
310 |
} |
{ |
311 |
|
return getLayoutInfo (parent, sizeflag); |
312 |
/** |
} |
313 |
* Obsolete. |
|
314 |
*/ |
/** |
315 |
protected Dimension GetMinSize (Container parent, GridBagLayoutInfo info) |
* Obsolete. |
316 |
{ |
*/ |
317 |
return getMinSize (parent, info); |
protected Dimension GetMinSize (Container parent, GridBagLayoutInfo info) |
318 |
} |
{ |
319 |
|
return getMinSize (parent, info); |
320 |
/** |
} |
321 |
* @since 1.4 |
|
322 |
*/ |
/** |
323 |
protected Dimension getMinSize (Container parent, GridBagLayoutInfo info) |
* @since 1.4 |
324 |
{ |
*/ |
325 |
if (parent == null || info == null) |
protected Dimension getMinSize (Container parent, GridBagLayoutInfo info) |
326 |
return new Dimension (0, 0); |
{ |
327 |
|
if (parent == null || info == null) |
328 |
Insets insets = parent.getInsets(); |
return new Dimension (0, 0); |
329 |
int width = sumIntArray (info.colWidths) + insets.left + insets.right; |
|
330 |
int height = sumIntArray (info.rowHeights) + insets.top + insets.bottom; |
Insets insets = parent.getInsets(); |
331 |
return new Dimension (width, height); |
int width = sumIntArray (info.colWidths) + insets.left + insets.right; |
332 |
} |
int height = sumIntArray (info.rowHeights) + insets.top + insets.bottom; |
333 |
|
return new Dimension (width, height); |
334 |
private void calcCellSizes (int[] sizes, double[] weights) |
} |
335 |
{ |
|
336 |
int diff = sumIntArray (sizes); |
private void calcCellSizes (int[] sizes, double[] weights, int range) |
337 |
|
{ |
338 |
if (diff == 0) |
int diff = range - sumIntArray (sizes); |
339 |
return; |
|
340 |
|
if (diff == 0) |
341 |
double weight = sumDoubleArray (weights); |
return; |
342 |
|
|
343 |
for (int i = 0; i < sizes.length; i++) |
double weight = sumDoubleArray (weights); |
344 |
{ |
|
345 |
sizes [i] += (int) (((double) diff) * weights [i] / weight ); |
for (int i = 0; i < sizes.length; i++) |
346 |
|
{ |
347 |
if (sizes [i] < 0) |
sizes [i] += (int) (((double) diff) * weights [i] / weight ); |
348 |
sizes [i] = 0; |
|
349 |
} |
if (sizes [i] < 0) |
350 |
} |
sizes [i] = 0; |
351 |
|
} |
352 |
private void dumpLayoutInfo (GridBagLayoutInfo info) |
} |
353 |
{ |
|
354 |
System.out.println ("GridBagLayoutInfo:"); |
private void dumpLayoutInfo (GridBagLayoutInfo info) |
355 |
System.out.println ("cols: " + info.cols + ", rows: " + info.rows); |
{ |
356 |
System.out.println ("colWiths: " + info.colWidths); |
System.out.println ("GridBagLayoutInfo:"); |
357 |
System.out.println ("rowHeights: " + info.rowHeights); |
System.out.println ("cols: " + info.cols + ", rows: " + info.rows); |
358 |
System.out.println ("colWeights: " + info.colWeights); |
System.out.print ("colWidths: "); |
359 |
System.out.println ("rowWeights: " + info.rowWeights); |
dumpArray(info.colWidths); |
360 |
} |
System.out.print ("rowHeights: "); |
361 |
|
dumpArray(info.rowHeights); |
362 |
|
System.out.print ("colWeights: "); |
363 |
|
dumpArray(info.colWeights); |
364 |
|
System.out.print ("rowWeights: "); |
365 |
|
dumpArray(info.rowWeights); |
366 |
|
} |
367 |
|
|
368 |
|
private void dumpArray(int[] array) |
369 |
|
{ |
370 |
|
String sep = ""; |
371 |
|
for(int i = 0; i < array.length; i++) |
372 |
|
{ |
373 |
|
System.out.print(sep); |
374 |
|
System.out.print(array[i]); |
375 |
|
sep = ", "; |
376 |
|
} |
377 |
|
System.out.println(); |
378 |
|
} |
379 |
|
|
380 |
|
private void dumpArray(double[] array) |
381 |
|
{ |
382 |
|
String sep = ""; |
383 |
|
for(int i = 0; i < array.length; i++) |
384 |
|
{ |
385 |
|
System.out.print(sep); |
386 |
|
System.out.print(array[i]); |
387 |
|
sep = ", "; |
388 |
|
} |
389 |
|
System.out.println(); |
390 |
|
} |
391 |
|
|
392 |
/** |
/** |
393 |
* @since 1.4 |
* @since 1.4 |
394 |
*/ |
*/ |
395 |
protected void arrangeGrid (Container parent) |
protected void arrangeGrid (Container parent) |
396 |
{ |
{ |
397 |
Insets insets = parent.getInsets(); |
Component[] components = parent.getComponents(); |
398 |
Component[] components = parent.getComponents(); |
|
399 |
|
if (components.length == 0) |
400 |
if (components.length == 0 |
return; |
401 |
&& (colWidths == null || colWidths.length == 0) |
|
402 |
&& (rowHeights == null || rowHeights.length == 0)) |
GridBagLayoutInfo info = getLayoutInfo (parent, PREFERREDSIZE); |
403 |
return; |
if (info.cols == 0 && info.rows == 0) |
404 |
|
return; |
405 |
GridBagLayoutInfo info = getLayoutInfo (parent, PREFERREDSIZE); |
layoutInfo = info; |
406 |
if (info.cols == 0 && info.rows == 0) |
|
407 |
return; |
// DEBUG |
408 |
layoutInfo = info; |
//dumpLayoutInfo (layoutInfo); |
409 |
|
|
410 |
// DEBUG |
for(int i = 0; i < components.length; i++) |
411 |
dumpLayoutInfo (layoutInfo); |
{ |
412 |
|
Component component = components [i]; |
413 |
calcCellSizes (layoutInfo.colWidths, layoutInfo.colWeights); |
|
414 |
calcCellSizes (layoutInfo.rowHeights, layoutInfo.rowWeights); |
// If component is not visible we dont have to care about it. |
415 |
|
if (!component.isVisible()) |
416 |
// DEBUG |
continue; |
417 |
dumpLayoutInfo (layoutInfo); |
|
418 |
|
GridBagConstraints constraints = lookupConstraints (component); |
419 |
throw new Error ("Not implemented"); |
|
420 |
} |
int cellx = sumIntArray(layoutInfo.colWidths, constraints.gridx); |
421 |
|
int celly = sumIntArray(layoutInfo.rowHeights, constraints.gridy); |
422 |
/** |
int cellw = sumIntArray(layoutInfo.colWidths, |
423 |
* @since 1.4 |
constraints.gridx + constraints.gridwidth) - cellx; |
424 |
*/ |
int cellh = sumIntArray(layoutInfo.rowHeights, |
425 |
protected GridBagLayoutInfo getLayoutInfo (Container parent, int sizeflag) |
constraints.gridy + constraints.gridheight) - celly; |
426 |
{ |
|
427 |
if (sizeflag != MINSIZE && sizeflag != PREFERREDSIZE) |
Insets insets = constraints.insets; |
428 |
throw new IllegalArgumentException(); |
if (insets != null) |
429 |
|
{ |
430 |
GridBagLayoutInfo info = new GridBagLayoutInfo (0, 0); |
cellx += insets.left; |
431 |
|
celly += insets.top; |
432 |
Component[] components = parent.getComponents(); |
cellw -= insets.left + insets.right; |
433 |
for (int i = 0; i < components.length; i++) |
cellh -= insets.top + insets.bottom; |
434 |
{ |
} |
435 |
Component component = components [i]; |
|
436 |
|
Dimension dim = component.preferredSize(); |
437 |
|
|
438 |
|
// Note: Documentation says that padding is added on both sides, but |
439 |
|
// visual inspection shows that the Sun implementation only adds it |
440 |
|
// once, so we do the same. |
441 |
|
dim.width += constraints.ipadx; |
442 |
|
dim.height += constraints.ipady; |
443 |
|
|
444 |
|
switch(constraints.fill) |
445 |
|
{ |
446 |
|
case GridBagConstraints.HORIZONTAL: |
447 |
|
dim.width = cellw; |
448 |
|
break; |
449 |
|
case GridBagConstraints.VERTICAL: |
450 |
|
dim.height = cellh; |
451 |
|
break; |
452 |
|
case GridBagConstraints.BOTH: |
453 |
|
dim.width = cellw; |
454 |
|
dim.height = cellh; |
455 |
|
break; |
456 |
|
} |
457 |
|
|
458 |
|
int x; |
459 |
|
int y; |
460 |
|
|
461 |
|
switch(constraints.anchor) |
462 |
|
{ |
463 |
|
case GridBagConstraints.NORTH: |
464 |
|
x = cellx + (cellw - dim.width) / 2; |
465 |
|
y = celly; |
466 |
|
break; |
467 |
|
case GridBagConstraints.SOUTH: |
468 |
|
x = cellx + (cellw - dim.width) / 2; |
469 |
|
y = celly + cellh - dim.height; |
470 |
|
break; |
471 |
|
case GridBagConstraints.WEST: |
472 |
|
x = cellx; |
473 |
|
y = celly + (cellh - dim.height) / 2; |
474 |
|
break; |
475 |
|
case GridBagConstraints.EAST: |
476 |
|
x = cellx + cellw - dim.width; |
477 |
|
y = celly + (cellh - dim.height) / 2; |
478 |
|
break; |
479 |
|
case GridBagConstraints.NORTHEAST: |
480 |
|
x = cellx + cellw - dim.width; |
481 |
|
y = celly; |
482 |
|
break; |
483 |
|
case GridBagConstraints.NORTHWEST: |
484 |
|
x = cellx; |
485 |
|
y = celly; |
486 |
|
break; |
487 |
|
case GridBagConstraints.SOUTHEAST: |
488 |
|
x = cellx + cellw - dim.width; |
489 |
|
y = celly + cellh - dim.height; |
490 |
|
break; |
491 |
|
case GridBagConstraints.SOUTHWEST: |
492 |
|
x = cellx; |
493 |
|
y = celly + cellh - dim.height; |
494 |
|
break; |
495 |
|
default: |
496 |
|
x = cellx + (cellw - dim.width) / 2; |
497 |
|
y = celly + (cellh - dim.height) / 2; |
498 |
|
break; |
499 |
|
} |
500 |
|
|
501 |
|
component.setBounds(layoutInfo.pos_x + x, layoutInfo.pos_y + y, dim.width, dim.height); |
502 |
|
} |
503 |
|
|
504 |
|
// DEBUG |
505 |
|
//dumpLayoutInfo (layoutInfo); |
506 |
|
|
507 |
|
} |
508 |
|
|
509 |
|
/** |
510 |
|
* @since 1.4 |
511 |
|
*/ |
512 |
|
protected GridBagLayoutInfo getLayoutInfo (Container parent, int sizeflag) |
513 |
|
{ |
514 |
|
if (sizeflag != MINSIZE && sizeflag != PREFERREDSIZE) |
515 |
|
throw new IllegalArgumentException(); |
516 |
|
|
517 |
|
Dimension parentDim = parent.size(); |
518 |
|
Insets parentInsets = parent.insets(); |
519 |
|
parentDim.width -= parentInsets.left + parentInsets.right; |
520 |
|
parentDim.height -= parentInsets.top + parentInsets.bottom; |
521 |
|
|
522 |
|
int x = 0; |
523 |
|
int y = 0; |
524 |
|
int max_x = 0; |
525 |
|
int max_y = 0; |
526 |
|
|
527 |
|
// first we figure out how many rows/columns |
528 |
|
Component[] components = parent.getComponents(); |
529 |
|
for (int i = 0; i < components.length; i++) |
530 |
|
{ |
531 |
|
Component component = components [i]; |
532 |
|
|
533 |
|
// If component is not visible we dont have to care about it. |
534 |
|
if (!component.isVisible()) |
535 |
|
continue; |
536 |
|
|
537 |
|
GridBagConstraints constraints = lookupConstraints (component); |
538 |
|
|
539 |
|
if(constraints.gridx == GridBagConstraints.RELATIVE) |
540 |
|
constraints.gridx = x; |
541 |
|
|
542 |
|
if(constraints.gridy == GridBagConstraints.RELATIVE) |
543 |
|
constraints.gridy = y; |
544 |
|
|
545 |
|
max_x = Math.max(max_x, |
546 |
|
constraints.gridx + Math.max(1, constraints.gridwidth)); |
547 |
|
max_y = Math.max(max_y, |
548 |
|
constraints.gridy + Math.max(1, constraints.gridheight)); |
549 |
|
|
550 |
|
if(constraints.gridwidth == GridBagConstraints.REMAINDER) |
551 |
|
{ |
552 |
|
x = 0; |
553 |
|
y++; |
554 |
|
} |
555 |
|
else |
556 |
|
{ |
557 |
|
x = constraints.gridx + Math.max(1, constraints.gridwidth); |
558 |
|
y = constraints.gridy; |
559 |
|
} |
560 |
|
} |
561 |
|
|
562 |
// If component is not visible we dont have to care about it. |
GridBagLayoutInfo info = new GridBagLayoutInfo(max_x, max_y); |
|
if (!component.isVisible()) |
|
|
continue; |
|
563 |
|
|
564 |
GridBagConstraints constraints = lookupConstraints (component); |
for (x = 0; x <= max_x; x++) |
565 |
|
{ |
566 |
int max_x = 1; |
if(columnWidths != null && columnWidths.length > x) |
567 |
int max_y = 1; |
{ |
568 |
int cur_x = 0; |
info.colWidths[x] = columnWidths[x]; |
569 |
|
} |
570 |
|
if(columnWeights != null && columnWeights.length > x) |
571 |
|
{ |
572 |
|
info.colWeights[x] = columnWeights[x]; |
573 |
|
} |
574 |
|
for (int i = 0; i < components.length; i++) |
575 |
|
{ |
576 |
|
Component component = components [i]; |
577 |
|
|
578 |
|
// If component is not visible we dont have to care about it. |
579 |
|
if (!component.isVisible()) |
580 |
|
continue; |
581 |
|
|
582 |
|
GridBagConstraints constraints = lookupConstraints (component); |
583 |
|
|
584 |
|
// first we fix up any REMAINDER cells |
585 |
|
if(constraints.gridwidth == GridBagConstraints.REMAINDER) |
586 |
|
{ |
587 |
|
constraints.gridwidth = max_x - constraints.gridx; |
588 |
|
} |
589 |
|
if(constraints.gridheight == GridBagConstraints.REMAINDER) |
590 |
|
{ |
591 |
|
constraints.gridheight = max_y - constraints.gridy; |
592 |
|
} |
593 |
|
|
594 |
|
if(constraints.gridx + constraints.gridwidth - 1 == x) |
595 |
|
{ |
596 |
|
int width = (sizeflag == PREFERREDSIZE) ? |
597 |
|
component.preferredSize().width : |
598 |
|
component.minimumSize().width; |
599 |
|
if(constraints.insets != null) |
600 |
|
{ |
601 |
|
width += constraints.insets.left + constraints.insets.right; |
602 |
|
} |
603 |
|
width += constraints.ipadx; |
604 |
|
for(int w = 1; w < constraints.gridwidth; w++) |
605 |
|
{ |
606 |
|
width -= info.colWidths[x - w]; |
607 |
|
} |
608 |
|
info.colWidths[x] = Math.max(info.colWidths[x], width); |
609 |
|
info.colWeights[x] = |
610 |
|
Math.max(info.colWeights[x], constraints.weightx); |
611 |
|
} |
612 |
|
} |
613 |
|
} |
614 |
|
|
615 |
|
for (y = 0; y <= max_y; y++) |
616 |
|
{ |
617 |
|
if(rowHeights != null && rowHeights.length > y) |
618 |
|
{ |
619 |
|
info.rowHeights[y] = rowHeights[y]; |
620 |
|
} |
621 |
|
if(rowWeights != null && rowWeights.length > y) |
622 |
|
{ |
623 |
|
info.rowWeights[y] = rowWeights[y]; |
624 |
|
} |
625 |
|
for (int i = 0; i < components.length; i++) |
626 |
|
{ |
627 |
|
Component component = components [i]; |
628 |
|
|
629 |
|
// If component is not visible we dont have to care about it. |
630 |
|
if (!component.isVisible()) |
631 |
|
continue; |
632 |
|
|
633 |
|
GridBagConstraints constraints = lookupConstraints (component); |
634 |
|
|
635 |
|
if(constraints.gridy + constraints.gridheight - 1 == y) |
636 |
|
{ |
637 |
|
int height = (sizeflag == PREFERREDSIZE) ? |
638 |
|
component.preferredSize().height : |
639 |
|
component.minimumSize().height; |
640 |
|
if(constraints.insets != null) |
641 |
|
{ |
642 |
|
height += constraints.insets.top + constraints.insets.bottom; |
643 |
|
} |
644 |
|
height += constraints.ipady; |
645 |
|
for(int h = 1; h < constraints.gridheight; h++) |
646 |
|
{ |
647 |
|
height -= info.rowHeights[y - h]; |
648 |
|
} |
649 |
|
info.rowHeights[y] = Math.max(info.rowHeights[y], height); |
650 |
|
info.rowWeights[y] = |
651 |
|
Math.max(info.rowWeights[y], constraints.weighty); |
652 |
|
} |
653 |
|
} |
654 |
|
} |
655 |
|
|
656 |
|
calcCellSizes (info.colWidths, info.colWeights, parentDim.width); |
657 |
|
calcCellSizes (info.rowHeights, info.rowWeights, parentDim.height); |
658 |
|
|
659 |
|
int totalWidth = sumIntArray(info.colWidths); |
660 |
|
int totalHeight = sumIntArray(info.rowHeights); |
661 |
|
info.pos_x = parentInsets.left + (parentDim.width - totalWidth) / 2; |
662 |
|
info.pos_y = parentInsets.top + (parentDim.height - totalHeight) / 2; |
663 |
|
|
664 |
|
// DEBUG |
665 |
|
//dumpLayoutInfo (info); |
666 |
|
|
667 |
|
return info; |
668 |
|
} |
669 |
|
|
670 |
|
/** |
671 |
|
* @since 1.4 |
672 |
|
*/ |
673 |
|
protected void adjustForGravity (GridBagConstraints gbc, Rectangle rect) |
674 |
|
{ |
675 |
// FIXME |
// FIXME |
676 |
} |
throw new Error ("Not implemented"); |
677 |
|
} |
|
// FIXME |
|
|
|
|
|
// DEBUG |
|
|
dumpLayoutInfo (info); |
|
|
|
|
|
return info; |
|
|
} |
|
|
|
|
|
/** |
|
|
* @since 1.4 |
|
|
*/ |
|
|
protected void adjustForGravity (GridBagConstraints gbc, Rectangle rect) |
|
|
{ |
|
|
// FIXME |
|
|
throw new Error ("Not implemented"); |
|
|
} |
|
678 |
} |
} |