1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45:
46:
50: public class GridBagLayout
51: implements Serializable, LayoutManager2
52: {
53: private static final long serialVersionUID = 8838754796412211005L;
54:
55: protected static final int MINSIZE = 1;
56: protected static final int PREFERREDSIZE = 2;
57: protected static final int MAXGRIDSIZE = 512;
58:
59:
60:
61:
62:
63:
64:
65: protected Hashtable comptable;
66: private Hashtable internalcomptable;
67: protected GridBagLayoutInfo layoutInfo;
68: protected GridBagConstraints defaultConstraints;
69:
70: public double[] columnWeights;
71: public int[] columnWidths;
72: public double[] rowWeights;
73: public int[] rowHeights;
74:
75: public GridBagLayout ()
76: {
77: this.comptable = new Hashtable();
78: this.internalcomptable = new Hashtable();
79: this.defaultConstraints= new GridBagConstraints();
80: }
81:
82:
85: private int sumIntArray (int[] array, int upto)
86: {
87: int result = 0;
88:
89: for (int i = 0; i < upto; i++)
90: result += array [i];
91:
92: return result;
93: }
94:
95:
98: private int sumIntArray (int[] array)
99: {
100: return sumIntArray(array, array.length);
101: }
102:
103:
106: private double sumDoubleArray (double[] array)
107: {
108: double result = 0;
109:
110: for (int i = 0; i < array.length; i++)
111: result += array [i];
112:
113: return result;
114: }
115:
116: public void addLayoutComponent (String name, Component component)
117: {
118:
119: }
120:
121: public void removeLayoutComponent (Component component)
122: {
123:
124: }
125:
126: public void addLayoutComponent (Component component, Object constraints)
127: {
128: if (constraints == null)
129: return;
130:
131: if (!(constraints instanceof GridBagConstraints))
132: throw new IllegalArgumentException("constraints "
133: + constraints
134: + " are not an instance of GridBagConstraints");
135:
136: setConstraints (component, (GridBagConstraints) constraints);
137: }
138:
139: public Dimension preferredLayoutSize (Container parent)
140: {
141: if (parent == null)
142: return new Dimension (0, 0);
143:
144: GridBagLayoutInfo li = getLayoutInfo (parent, PREFERREDSIZE);
145: return getMinSize (parent, li);
146: }
147:
148: public Dimension minimumLayoutSize (Container parent)
149: {
150: if (parent == null)
151: return new Dimension (0, 0);
152:
153: GridBagLayoutInfo li = getLayoutInfo (parent, MINSIZE);
154: return getMinSize (parent, li);
155: }
156:
157: public Dimension maximumLayoutSize (Container target)
158: {
159: return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE);
160: }
161:
162: public void layoutContainer (Container parent)
163: {
164: arrangeGrid (parent);
165: }
166:
167: public float getLayoutAlignmentX (Container target)
168: {
169: return Component.CENTER_ALIGNMENT;
170: }
171:
172: public float getLayoutAlignmentY (Container target)
173: {
174: return Component.CENTER_ALIGNMENT;
175: }
176:
177: public void invalidateLayout (Container target)
178: {
179: this.layoutInfo = null;
180: }
181:
182: public void setConstraints (Component component,
183: GridBagConstraints constraints)
184: {
185: GridBagConstraints clone = (GridBagConstraints) constraints.clone();
186:
187: if (clone.gridx < 0)
188: clone.gridx = GridBagConstraints.RELATIVE;
189:
190: if (clone.gridy < 0)
191: clone.gridy = GridBagConstraints.RELATIVE;
192:
193: if (clone.gridwidth == 0)
194: clone.gridwidth = GridBagConstraints.REMAINDER;
195: else if (clone.gridwidth < 0)
196: clone.gridwidth = 1;
197:
198: if (clone.gridheight == 0)
199: clone.gridheight = GridBagConstraints.REMAINDER;
200: else if (clone.gridheight < 0)
201: clone.gridheight = 1;
202:
203: comptable.put (component, clone);
204: }
205:
206: public GridBagConstraints getConstraints (Component component)
207: {
208: return (GridBagConstraints) (lookupConstraints (component).clone());
209: }
210:
211: protected GridBagConstraints lookupConstraints (Component component)
212: {
213: GridBagConstraints result = (GridBagConstraints) comptable.get (component);
214:
215: if (result == null)
216: {
217: setConstraints (component, defaultConstraints);
218: result = (GridBagConstraints) comptable.get (component);
219: }
220:
221: return result;
222: }
223:
224: private GridBagConstraints lookupInternalConstraints (Component component)
225: {
226: GridBagConstraints result =
227: (GridBagConstraints) internalcomptable.get (component);
228:
229: if (result == null)
230: {
231: result = (GridBagConstraints) lookupConstraints(component).clone();
232: internalcomptable.put (component, result);
233: }
234:
235: return result;
236: }
237:
238:
241: public Point getLayoutOrigin ()
242: {
243: if (layoutInfo == null)
244: return new Point (0, 0);
245:
246: return new Point (layoutInfo.pos_x, layoutInfo.pos_y);
247: }
248:
249:
252: public int[][] getLayoutDimensions ()
253: {
254: int[][] result = new int [2][];
255: if (layoutInfo == null)
256: {
257: result[0] = new int[0];
258: result[1] = new int[0];
259:
260: return result;
261: }
262:
263: result [0] = new int [layoutInfo.cols];
264: System.arraycopy (layoutInfo.colWidths, 0, result [0], 0, layoutInfo.cols);
265: result [1] = new int [layoutInfo.rows];
266: System.arraycopy (layoutInfo.rowHeights, 0, result [1], 0, layoutInfo.rows);
267: return result;
268: }
269:
270: public double[][] getLayoutWeights ()
271: {
272: double[][] result = new double [2][];
273: if (layoutInfo == null)
274: {
275: result[0] = new double[0];
276: result[1] = new double[0];
277:
278: return result;
279: }
280:
281: result [0] = new double [layoutInfo.cols];
282: System.arraycopy (layoutInfo.colWeights, 0, result [0], 0, layoutInfo.cols);
283: result [1] = new double [layoutInfo.rows];
284: System.arraycopy (layoutInfo.rowWeights, 0, result [1], 0, layoutInfo.rows);
285: return result;
286: }
287:
288:
291: public Point location (int x, int y)
292: {
293: if (layoutInfo == null)
294: return new Point (0, 0);
295:
296: int col;
297: int row;
298: int pixel_x = layoutInfo.pos_x;
299: int pixel_y = layoutInfo.pos_y;
300:
301: for (col = 0; col < layoutInfo.cols; col++)
302: {
303: int w = layoutInfo.colWidths [col];
304: if (x < pixel_x + w)
305: break;
306:
307: pixel_x += w;
308: }
309:
310: for (row = 0; row < layoutInfo.rows; row++)
311: {
312: int h = layoutInfo.rowHeights [row];
313: if (y < pixel_y + h)
314: break;
315:
316: pixel_y += h;
317: }
318:
319: return new Point (col, row);
320: }
321:
322:
325: protected void AdjustForGravity (GridBagConstraints gbc, Rectangle rect)
326: {
327:
328: throw new Error ("Not implemented");
329: }
330:
331:
334: protected void ArrangeGrid (Container parent)
335: {
336: Component[] components = parent.getComponents();
337:
338: if (components.length == 0)
339: return;
340:
341: GridBagLayoutInfo info = getLayoutInfo (parent, PREFERREDSIZE);
342: if (info.cols == 0 && info.rows == 0)
343: return;
344:
345:
346:
347:
348:
349:
350:
351:
352: for(int i = 0; i < components.length; i++)
353: {
354: Component component = components [i];
355:
356:
357: if (!component.isVisible())
358: continue;
359:
360: GridBagConstraints constraints =
361: lookupInternalConstraints(component);
362:
363: int cellx = sumIntArray(info.colWidths, constraints.gridx);
364: int celly = sumIntArray(info.rowHeights, constraints.gridy);
365: int cellw = sumIntArray(info.colWidths,
366: constraints.gridx + constraints.gridwidth) - cellx;
367: int cellh = sumIntArray(info.rowHeights,
368: constraints.gridy + constraints.gridheight) - celly;
369:
370: Insets insets = constraints.insets;
371: if (insets != null)
372: {
373: cellx += insets.left;
374: celly += insets.top;
375: cellw -= insets.left + insets.right;
376: cellh -= insets.top + insets.bottom;
377: }
378:
379: Dimension dim = component.getPreferredSize();
380:
381:
382:
383:
384: dim.width += constraints.ipadx;
385: dim.height += constraints.ipady;
386:
387: switch(constraints.fill)
388: {
389: case GridBagConstraints.HORIZONTAL:
390: dim.width = cellw;
391: break;
392: case GridBagConstraints.VERTICAL:
393: dim.height = cellh;
394: break;
395: case GridBagConstraints.BOTH:
396: dim.width = cellw;
397: dim.height = cellh;
398: break;
399: }
400:
401: int x;
402: int y;
403:
404: switch(constraints.anchor)
405: {
406: case GridBagConstraints.NORTH:
407: x = cellx + (cellw - dim.width) / 2;
408: y = celly;
409: break;
410: case GridBagConstraints.SOUTH:
411: x = cellx + (cellw - dim.width) / 2;
412: y = celly + cellh - dim.height;
413: break;
414: case GridBagConstraints.WEST:
415: x = cellx;
416: y = celly + (cellh - dim.height) / 2;
417: break;
418: case GridBagConstraints.EAST:
419: x = cellx + cellw - dim.width;
420: y = celly + (cellh - dim.height) / 2;
421: break;
422: case GridBagConstraints.NORTHEAST:
423: x = cellx + cellw - dim.width;
424: y = celly;
425: break;
426: case GridBagConstraints.NORTHWEST:
427: x = cellx;
428: y = celly;
429: break;
430: case GridBagConstraints.SOUTHEAST:
431: x = cellx + cellw - dim.width;
432: y = celly + cellh - dim.height;
433: break;
434: case GridBagConstraints.SOUTHWEST:
435: x = cellx;
436: y = celly + cellh - dim.height;
437: break;
438: default:
439: x = cellx + (cellw - dim.width) / 2;
440: y = celly + (cellh - dim.height) / 2;
441: break;
442: }
443:
444: component.setBounds(info.pos_x + x, info.pos_y + y, dim.width, dim.height);
445: }
446:
447:
448:
449:
450:
451: layoutInfo = getLayoutInfo (parent, PREFERREDSIZE);
452: }
453:
454:
457: protected GridBagLayoutInfo GetLayoutInfo (Container parent, int sizeflag)
458: {
459: if (sizeflag != MINSIZE && sizeflag != PREFERREDSIZE)
460: throw new IllegalArgumentException();
461:
462: Dimension parentDim = parent.getSize ();
463: Insets parentInsets = parent.getInsets ();
464: parentDim.width -= parentInsets.left + parentInsets.right;
465: parentDim.height -= parentInsets.top + parentInsets.bottom;
466:
467: int current_y = 0;
468: int max_x = 0;
469: int max_y = 0;
470:
471:
472:
473: HashMap lastInRow = new HashMap();
474: HashMap lastInCol = new HashMap();
475:
476: Component[] components = parent.getComponents();
477:
478:
479:
480:
481: ArrayList sortedByWidth = new ArrayList(components.length);
482: ArrayList sortedByHeight = new ArrayList(components.length);
483:
484:
485: for (int i = 0; i < components.length; i++)
486: {
487: Component component = components [i];
488:
489:
490: if (!component.isVisible())
491: continue;
492:
493:
494:
495:
496: GridBagConstraints originalConstraints = lookupConstraints (component);
497: GridBagConstraints constraints = (GridBagConstraints) originalConstraints.clone();
498: internalcomptable.put(component, constraints);
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521: if(constraints.gridx == GridBagConstraints.RELATIVE)
522: {
523: if (constraints.gridy == GridBagConstraints.RELATIVE)
524: constraints.gridy = current_y;
525:
526: int x;
527:
528:
529:
530:
531: if (!lastInRow.containsKey(new Integer(constraints.gridy)))
532: x = 0;
533: else
534: {
535: Component lastComponent = (Component) lastInRow.get(new Integer(constraints.gridy));
536: GridBagConstraints lastConstraints = lookupInternalConstraints(lastComponent);
537: x = lastConstraints.gridx + Math.max(1, lastConstraints.gridwidth);
538: }
539:
540:
541:
542: for (int y = constraints.gridy + 1; y < constraints.gridy + Math.max(1, constraints.gridheight); y++)
543: {
544: if (lastInRow.containsKey(new Integer(y)))
545: {
546: Component lastComponent = (Component) lastInRow.get(new Integer(y));
547: GridBagConstraints lastConstraints = lookupInternalConstraints(lastComponent);
548: x = Math.max (x,
549: lastConstraints.gridx + Math.max(1, lastConstraints.gridwidth));
550: }
551: }
552:
553: constraints.gridx = x;
554: }
555:
556: else if(constraints.gridy == GridBagConstraints.RELATIVE)
557: {
558: int y;
559:
560:
561:
562: if (!lastInCol.containsKey(new Integer(constraints.gridx)))
563: y = 0;
564: else
565: {
566: Component lastComponent = (Component)lastInCol.get(new Integer(constraints.gridx));
567: GridBagConstraints lastConstraints = lookupInternalConstraints(lastComponent);
568: y = lastConstraints.gridy + Math.max(1, lastConstraints.gridheight);
569: }
570:
571:
572:
573: for (int x = constraints.gridx + 1; x < constraints.gridx + Math.max(1, constraints.gridwidth); x++)
574: {
575: if (lastInCol.containsKey(new Integer(x)))
576: {
577: Component lastComponent = (Component) lastInCol.get(new Integer(x));
578: GridBagConstraints lastConstraints = lookupInternalConstraints(lastComponent);
579: y = Math.max (y,
580: lastConstraints.gridy + Math.max(1, lastConstraints.gridheight));
581: }
582: }
583:
584: constraints.gridy = y;
585: }
586:
587:
588: max_x = Math.max(max_x,
589: constraints.gridx + Math.max(1, constraints.gridwidth));
590: max_y = Math.max(max_y,
591: constraints.gridy + Math.max(1, constraints.gridheight));
592:
593: sortBySpan(component, constraints.gridwidth, sortedByWidth, true);
594: sortBySpan(component, constraints.gridheight, sortedByHeight, false);
595:
596:
597: if(constraints.gridwidth == GridBagConstraints.REMAINDER)
598: {
599: current_y = constraints.gridy + Math.max(1, constraints.gridheight);
600: }
601: else if (constraints.gridwidth != GridBagConstraints.REMAINDER)
602: {
603: for (int y = constraints.gridy; y < constraints.gridy + Math.max(1, constraints.gridheight); y++)
604: {
605: if(lastInRow.containsKey(new Integer(y)))
606: {
607: Component lastComponent = (Component) lastInRow.get(new Integer(y));
608: GridBagConstraints lastConstraints = lookupInternalConstraints(lastComponent);
609: if (constraints.gridx > lastConstraints.gridx)
610: {
611: lastInRow.put(new Integer(y), component);
612: }
613: }
614: else
615: {
616: lastInRow.put(new Integer(y), component);
617: }
618: }
619:
620: for (int x = constraints.gridx; x < constraints.gridx + Math.max(1, constraints.gridwidth); x++)
621: {
622: if(lastInCol.containsKey(new Integer(x)))
623: {
624: Component lastComponent = (Component) lastInCol.get(new Integer(x));
625: GridBagConstraints lastConstraints = lookupInternalConstraints(lastComponent);
626: if (constraints.gridy > lastConstraints.gridy)
627: {
628: lastInCol.put(new Integer(x), component);
629: }
630: }
631: else
632: {
633: lastInCol.put(new Integer(x), component);
634: }
635: }
636: }
637: }
638:
639: GridBagLayoutInfo info = new GridBagLayoutInfo(max_x, max_y);
640:
641:
642:
643: for (int x = 0; x < max_x; x++)
644: {
645: if(columnWidths != null && columnWidths.length > x)
646: info.colWidths[x] = columnWidths[x];
647: if(columnWeights != null && columnWeights.length > x)
648: info.colWeights[x] = columnWeights[x];
649: }
650:
651: for (int y = 0; y < max_y; y++)
652: {
653: if(rowHeights != null && rowHeights.length > y)
654: info.rowHeights[y] = rowHeights[y];
655: if(rowWeights != null && rowWeights.length > y)
656: info.rowWeights[y] = rowWeights[y];
657: }
658:
659:
660: for (int i = 0; i < components.length; i++)
661: {
662: Component component = components [i];
663:
664:
665: if (!component.isVisible())
666: continue;
667:
668: GridBagConstraints constraints = lookupInternalConstraints (component);
669:
670: if(constraints.gridwidth == GridBagConstraints.REMAINDER || constraints.gridwidth == GridBagConstraints.RELATIVE)
671: {
672: if(constraints.gridwidth == GridBagConstraints.REMAINDER)
673: {
674: for (int y = constraints.gridy; y < constraints.gridy + Math.max(1, constraints.gridheight); y++)
675: {
676: if (lastInRow.containsKey(new Integer(y)))
677: {
678: Component lastComponent = (Component) lastInRow.get(new Integer(y));
679: GridBagConstraints lastConstraints = lookupInternalConstraints(lastComponent);
680:
681: if (lastConstraints.gridwidth == GridBagConstraints.RELATIVE)
682: {
683: constraints.gridx = max_x - 1;
684: break;
685: }
686: else
687: {
688: constraints.gridx = Math.max (constraints.gridx,
689: lastConstraints.gridx + Math.max (1, lastConstraints.gridwidth));
690: }
691: }
692: }
693: constraints.gridwidth = max_x - constraints.gridx;
694: }
695: else if (constraints.gridwidth == GridBagConstraints.RELATIVE)
696: {
697: constraints.gridwidth = max_x - constraints.gridx - 1;
698: }
699:
700:
701: sortedByWidth.remove(sortedByWidth.indexOf(component));
702: sortBySpan(component, constraints.gridwidth, sortedByWidth, true);
703: }
704:
705: if(constraints.gridheight == GridBagConstraints.REMAINDER || constraints.gridheight == GridBagConstraints.RELATIVE)
706: {
707: if(constraints.gridheight == GridBagConstraints.REMAINDER)
708: {
709: for (int x = constraints.gridx; x < constraints.gridx + Math.max(1, constraints.gridwidth); x++)
710: {
711: if (lastInCol.containsKey(new Integer(x)))
712: {
713: Component lastComponent = (Component) lastInRow.get(new Integer(x));
714: if (lastComponent != null)
715: {
716: GridBagConstraints lastConstraints = lookupInternalConstraints(lastComponent);
717:
718: if (lastConstraints.gridheight == GridBagConstraints.RELATIVE)
719: {
720: constraints.gridy = max_y - 1;
721: break;
722: }
723: else
724: {
725: constraints.gridy = Math.max (constraints.gridy,
726: lastConstraints.gridy + Math.max (1, lastConstraints.gridheight));
727: }
728: }
729: }
730: }
731: constraints.gridheight = max_y - constraints.gridy;
732: }
733: else if (constraints.gridheight == GridBagConstraints.RELATIVE)
734: {
735: constraints.gridheight = max_y - constraints.gridy - 1;
736: }
737:
738:
739: sortedByHeight.remove(sortedByHeight.indexOf(component));
740: sortBySpan(component, constraints.gridheight, sortedByHeight, false);
741: }
742: }
743:
744:
745: for (int i = 0; i < sortedByWidth.size(); i++)
746: {
747: Component component = (Component) sortedByWidth.get(i);
748:
749:
750: if (!component.isVisible())
751: continue;
752:
753: GridBagConstraints constraints = lookupInternalConstraints (component);
754:
755: int width = (sizeflag == PREFERREDSIZE) ?
756: component.getPreferredSize().width :
757: component.getMinimumSize().width;
758:
759: if(constraints.insets != null)
760: width += constraints.insets.left + constraints.insets.right;
761:
762: width += constraints.ipadx;
763:
764: distributeSizeAndWeight(width,
765: constraints.weightx,
766: constraints.gridx,
767: constraints.gridwidth,
768: info.colWidths,
769: info.colWeights);
770: }
771:
772:
773: for (int i = 0; i < sortedByHeight.size(); i++)
774: {
775: Component component = (Component) sortedByHeight.get(i);
776:
777:
778: if (!component.isVisible())
779: continue;
780:
781: GridBagConstraints constraints = lookupInternalConstraints (component);
782:
783: int height = (sizeflag == PREFERREDSIZE) ?
784: component.getPreferredSize().height :
785: component.getMinimumSize().height;
786:
787: if(constraints.insets != null)
788: height += constraints.insets.top + constraints.insets.bottom;
789:
790: height += constraints.ipady;
791:
792: distributeSizeAndWeight(height,
793: constraints.weighty,
794: constraints.gridy,
795: constraints.gridheight,
796: info.rowHeights,
797: info.rowWeights);
798: }
799:
800:
801: if (parentDim.width > 0 && parentDim.height > 0)
802: {
803: calcCellSizes (info.colWidths, info.colWeights, parentDim.width);
804: calcCellSizes (info.rowHeights, info.rowWeights, parentDim.height);
805: }
806:
807: int totalWidth = sumIntArray(info.colWidths);
808: int totalHeight = sumIntArray(info.rowHeights);
809:
810:
811: if (totalWidth >= parentDim.width)
812: info.pos_x = parentInsets.left;
813: else
814: info.pos_x = parentInsets.left + (parentDim.width - totalWidth) / 2;
815:
816: if (totalHeight >= parentDim.height)
817: info.pos_y = parentInsets.top;
818: else
819: info.pos_y = parentInsets.top + (parentDim.height - totalHeight) / 2;
820:
821:
822:
823:
824: return info;
825: }
826:
827:
830: protected Dimension GetMinSize (Container parent, GridBagLayoutInfo info)
831: {
832: if (parent == null || info == null)
833: return new Dimension (0, 0);
834:
835: Insets insets = parent.getInsets();
836: int width = sumIntArray (info.colWidths) + insets.left + insets.right;
837: int height = sumIntArray (info.rowHeights) + insets.top + insets.bottom;
838: return new Dimension (width, height);
839: }
840:
841:
844: protected Dimension getMinSize (Container parent, GridBagLayoutInfo info)
845: {
846: return GetMinSize (parent, info);
847: }
848:
849:
861: private void sortBySpan (Component component, int span, ArrayList list, boolean sortByWidth)
862: {
863: if (span == GridBagConstraints.REMAINDER
864: || span == GridBagConstraints.RELATIVE)
865: {
866:
867: list.add(component);
868: }
869: else
870: {
871: int i = 0;
872: if (list.size() > 0)
873: {
874: GridBagConstraints gbc = lookupInternalConstraints((Component) list.get(i));
875: int otherspan = sortByWidth ?
876: gbc.gridwidth :
877: gbc.gridheight;
878: while (otherspan != GridBagConstraints.REMAINDER
879: && otherspan != GridBagConstraints.RELATIVE
880: && span >= otherspan)
881: {
882: i++;
883: if (i < list.size())
884: {
885: gbc = lookupInternalConstraints((Component) list.get(i));
886: otherspan = sortByWidth ?
887: gbc.gridwidth :
888: gbc.gridheight;
889: }
890: else
891: break;
892: }
893: }
894: list.add(i, component);
895: }
896: }
897:
898:
912: private void distributeSizeAndWeight (int size, double weight,
913: int start, int span,
914: int[] sizes, double[] weights)
915: {
916: if (span == 1)
917: {
918: sizes[start] = Math.max(sizes[start], size);
919: weights[start] = Math.max(weights[start], weight);
920: }
921: else if (span > 1)
922: {
923: int numOccupied = span;
924: int lastOccupied = -1;
925:
926: for(int i = start; i < start + span; i++)
927: {
928: if (sizes[i] == 0.0)
929: numOccupied--;
930: else
931: {
932: size -= sizes[i];
933: lastOccupied = i;
934: }
935: }
936:
937:
938: if(numOccupied == 0)
939: sizes[start + span - 1] = size;
940: else if (size > 0)
941: sizes[lastOccupied] += size;
942:
943: calcCellWeights(weight, weights, start, span);
944: }
945: }
946:
947:
954: private void calcCellWeights (double weight, double[] weights, int start, int span)
955: {
956: double totalWeight = 0.0;
957: for(int k = start; k < start + span; k++)
958: totalWeight += weights[k];
959:
960: if(weight > totalWeight)
961: {
962: if (totalWeight == 0.0)
963: {
964: weights[start + span - 1] += weight;
965: }
966: else
967: {
968: double diff = weight - totalWeight ;
969: double remaining = diff;
970:
971: for(int k = start; k < start + span; k++)
972: {
973: double extraWeight = diff * weights[k] / totalWeight;
974: weights[k] += extraWeight;
975: remaining -= extraWeight;
976: }
977:
978: if (remaining > 0.0 && weights[start + span - 1] != 0.0)
979: {
980: weights[start + span - 1] += remaining;
981: }
982: }
983: }
984: }
985:
986:
994: private void calcCellSizes (int[] sizes, double[] weights, int range)
995: {
996: int totalSize = sumIntArray (sizes);
997: double totalWeight = sumDoubleArray (weights);
998:
999: int diff = range - totalSize;
1000:
1001: if (diff == 0)
1002: return;
1003:
1004: for (int i = 0; i < sizes.length; i++)
1005: {
1006: int newsize = (int) (sizes[i] + (((double) diff) * weights [i] / totalWeight ));
1007:
1008: if (newsize > 0)
1009: sizes[i] = newsize;
1010: }
1011: }
1012:
1013: private void dumpLayoutInfo (GridBagLayoutInfo info)
1014: {
1015: System.out.println ("GridBagLayoutInfo:");
1016: System.out.println ("cols: " + info.cols + ", rows: " + info.rows);
1017: System.out.print ("colWidths: ");
1018: dumpArray(info.colWidths);
1019: System.out.print ("rowHeights: ");
1020: dumpArray(info.rowHeights);
1021: System.out.print ("colWeights: ");
1022: dumpArray(info.colWeights);
1023: System.out.print ("rowWeights: ");
1024: dumpArray(info.rowWeights);
1025: }
1026:
1027: private void dumpArray(int[] array)
1028: {
1029: String sep = "";
1030: for(int i = 0; i < array.length; i++)
1031: {
1032: System.out.print(sep);
1033: System.out.print(array[i]);
1034: sep = ", ";
1035: }
1036: System.out.println();
1037: }
1038:
1039: private void dumpArray(double[] array)
1040: {
1041: String sep = "";
1042: for(int i = 0; i < array.length; i++)
1043: {
1044: System.out.print(sep);
1045: System.out.print(array[i]);
1046: sep = ", ";
1047: }
1048: System.out.println();
1049: }
1050:
1051:
1054: protected void arrangeGrid (Container parent)
1055: {
1056: ArrangeGrid (parent);
1057: }
1058:
1059:
1062: protected GridBagLayoutInfo getLayoutInfo (Container parent, int sizeflag)
1063: {
1064: return GetLayoutInfo (parent, sizeflag);
1065: }
1066:
1067:
1070: protected void adjustForGravity (GridBagConstraints gbc, Rectangle rect)
1071: {
1072: AdjustForGravity (gbc, rect);
1073: }
1074: }