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