KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > GridBagLayout


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: GridBagLayout.java,v $
11    Revision 1.10 2004/04/30 21:38:04 bobintetley
12    Fixes for layout interactions with GridBagLayout
13
14    Revision 1.9 2004/04/30 16:52:14 bobintetley
15    MenuListener support, JViewport support, TreeSelectionModel stubs, additional JTree methods
16
17    Revision 1.8 2004/04/21 11:38:29 bobintetley
18    *** empty log message ***
19
20    Revision 1.7 2004/04/21 10:43:57 bobintetley
21    Code cleanup and native build script fix
22
23    Revision 1.6 2004/04/20 16:35:49 bobintetley
24    Code cleanup
25
26    Revision 1.5 2004/01/06 08:28:02 bobintetley
27    Header fixes
28
29  
30  */

31 package swingwt.awt;
32
33 import java.util.*;
34
35 public class GridBagLayout implements LayoutManager2 {
36     
37     protected GridBagConstraints defaultConstraints = null;
38     protected GridBagLayoutInfo layoutInfo = null;
39     protected Hashtable components = null;;
40     
41     public int columnWidths[] = null;
42     public int rowHeights[] = null;
43     public double columnWeights[] = null;
44     public double rowWeights[] = null;
45     
46     public GridBagLayout() {
47         components = new Hashtable();
48         defaultConstraints = new GridBagConstraints();
49     }
50     
51     public void setConstraints(Component comp, GridBagConstraints constraints) {
52         components.put(comp, constraints.clone());
53     }
54     
55     public GridBagConstraints getConstraints(Component comp) {
56         GridBagConstraints con = (GridBagConstraints) ((GridBagConstraints) components.get(comp));
57         if (con != null)
58             return (GridBagConstraints) con.clone();
59         else {
60             setConstraints(comp, defaultConstraints);
61             return (GridBagConstraints) defaultConstraints.clone();
62         }
63     }
64     
65     private void removeConstraints(Component comp) {
66         components.remove(comp);
67     }
68     
69     public Point getLayoutOrigin() {
70         Point origin = new Point(0, 0);
71         if (layoutInfo != null) {
72             origin.x = layoutInfo.startx;
73             origin.y = layoutInfo.starty;
74         }
75         return origin;
76     }
77     
78     public int[][] getLayoutDimensions() {
79         return new int[2][0];
80     }
81     
82     public double[][] getLayoutWeights() {
83         return new double[2][0];
84     }
85     
86     public Point location(int x, int y) {
87         return new Point(x, y);
88     }
89     
90     public void addLayoutComponent(String JavaDoc name, Component comp) {
91     }
92     
93     public void addLayoutComponent(Component comp, Object JavaDoc constraints) {
94         // Assume if the constraints are null that the developers
95
// code already called setConstraints() separately.
96
if (constraints != null)
97             setConstraints(comp, (GridBagConstraints) constraints);
98     }
99     
100     public void removeLayoutComponent(Component comp) {
101         removeConstraints(comp);
102     }
103     
104     public Dimension preferredLayoutSize(Container parent) {
105         GridBagLayoutInfo info = getLayoutInfo(parent, PREFERREDSIZE);
106         return getSmallestLayoutSize(parent, info);
107     }
108     
109     public Dimension minimumLayoutSize(Container parent) {
110         GridBagLayoutInfo info = getLayoutInfo(parent, MINSIZE);
111         return getSmallestLayoutSize(parent, info);
112     }
113     
114     public Dimension maximumLayoutSize(Container target) {
115         return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
116     }
117     
118     public float getLayoutAlignmentX(Container parent) {
119         return (float) 0.5;
120     }
121     
122     public float getLayoutAlignmentY(Container parent) {
123         return (float) 0.5;
124     }
125     
126     public void invalidateLayout(Container target) {
127     }
128     
129     public void layoutContainer(Container parent) {
130         
131         Component comp;
132         int compindex;
133         GridBagConstraints constraints;
134         Insets insets = parent.getInsets();
135         Component components[] = parent.getComponents();
136         Dimension d;
137         Rectangle r = new Rectangle();
138         int i, diffw, diffh;
139         double weight;
140         GridBagLayoutInfo info;
141         
142         rightToLeft = !parent.getComponentOrientation().isLeftToRight();
143         
144         // Bomb out if there aren't any components to display
145
if (components.length == 0) return;
146         
147         info = getLayoutInfo(parent, PREFERREDSIZE);
148         d = getSmallestLayoutSize(parent, info);
149         
150         if (parent.getWidth() < d.width || parent.getHeight() < d.height) {
151             info = getLayoutInfo(parent, MINSIZE);
152             d = getSmallestLayoutSize(parent, info);
153         }
154         
155         layoutInfo = info;
156         r.width = d.width;
157         r.height = d.height;
158         
159         diffw = parent.getWidth() - r.width;
160         if (diffw != 0) {
161             weight = 0.0;
162             for (i = 0; i < info.width; i++)
163                 weight += info.weightX[i];
164             if (weight > 0.0) {
165                 for (i = 0; i < info.width; i++) {
166                     int dx =
167                     (int) ((((double) diffw) * info.weightX[i]) / weight);
168                     info.minWidth[i] += dx;
169                     r.width += dx;
170                     if (info.minWidth[i] < 0) {
171                         r.width -= info.minWidth[i];
172                         info.minWidth[i] = 0;
173                     }
174                 }
175             }
176             diffw = parent.getWidth() - r.width;
177         } else {
178             diffw = 0;
179         }
180         
181         diffh = parent.getHeight() - r.height;
182         if (diffh != 0) {
183             weight = 0.0;
184             for (i = 0; i < info.height; i++)
185                 weight += info.weightY[i];
186             if (weight > 0.0) {
187                 for (i = 0; i < info.height; i++) {
188                     int dy =
189                     (int) ((((double) diffh) * info.weightY[i]) / weight);
190                     info.minHeight[i] += dy;
191                     r.height += dy;
192                     if (info.minHeight[i] < 0) {
193                         r.height -= info.minHeight[i];
194                         info.minHeight[i] = 0;
195                     }
196                 }
197             }
198             diffh = parent.getHeight() - r.height;
199         } else {
200             diffh = 0;
201         }
202         
203         info.startx = diffw / 2 + insets.left;
204         info.starty = diffh / 2 + insets.top;
205         
206         for (compindex = 0; compindex < components.length; compindex++) {
207             comp = components[compindex];
208             if (!comp.isVisible())
209                 continue;
210             constraints = lookupConstraints(comp);
211             
212             if (!rightToLeft) {
213                 r.x = info.startx;
214                 for (i = 0; i < constraints.tempX; i++)
215                     r.x += info.minWidth[i];
216             } else {
217                 r.x = parent.getWidth() - (diffw / 2 + insets.right);
218                 for (i = 0; i < constraints.tempX; i++)
219                     r.x -= info.minWidth[i];
220             }
221             
222             r.y = info.starty;
223             for (i = 0; i < constraints.tempY; i++)
224                 r.y += info.minHeight[i];
225             
226             r.width = 0;
227             for (i = constraints.tempX;
228             i < (constraints.tempX + constraints.tempWidth);
229             i++) {
230                 r.width += info.minWidth[i];
231             }
232             
233             r.height = 0;
234             for (i = constraints.tempY;
235             i < (constraints.tempY + constraints.tempHeight);
236             i++) {
237                 r.height += info.minHeight[i];
238             }
239             
240             moveComponentInsets(constraints, r);
241             
242             if (r.x < 0) {
243                 r.width -= r.x;
244                 r.x = 0;
245             }
246             
247             if (r.y < 0) {
248                 r.height -= r.y;
249                 r.y = 0;
250             }
251             
252             if ((r.width <= 0) || (r.height <= 0)) {
253                 comp.setBounds(0, 0, 0, 0);
254             } else {
255                 if (comp.getX() != r.x
256                 || comp.getY() != r.y
257                 || comp.getWidth() != r.width
258                 || comp.getHeight() != r.height) {
259                     comp.setBounds(r.x, r.y, r.width, r.height);
260                 }
261             }
262         }
263         
264     }
265         
266     protected GridBagConstraints lookupConstraints(Component comp) {
267         GridBagConstraints con = (GridBagConstraints) components.get(comp);
268         if (con != null)
269             return con;
270         else {
271             setConstraints(comp, defaultConstraints);
272             return (GridBagConstraints) components.get(comp);
273         }
274     }
275     
276     protected GridBagLayoutInfo getLayoutInfo(Container parent, int sizeflag) {
277         
278         GridBagLayoutInfo r = new GridBagLayoutInfo();
279         Component comp;
280         GridBagConstraints constraints;
281         Dimension d;
282         Component components[] = parent.getComponents();
283
284         int compindex, i, j, k, px, py, pixels_diff, nextSize;
285         int curX, curY, curWidth, curHeight, curRow, curCol;
286         double weight_diff, weight, start, size;
287         int xMax[], yMax[];
288
289         r.width = r.height = 0;
290         curRow = curCol = -1;
291         xMax = new int[MAXGRIDSIZE];
292         yMax = new int[MAXGRIDSIZE];
293
294         for (compindex = 0; compindex < components.length; compindex++) {
295             comp = components[compindex];
296             if (!comp.isVisible())
297                 continue;
298             constraints = lookupConstraints(comp);
299
300             curX = constraints.gridx;
301             curY = constraints.gridy;
302             curWidth = constraints.gridwidth;
303             if (curWidth <= 0)
304                 curWidth = 1;
305             curHeight = constraints.gridheight;
306             if (curHeight <= 0)
307                 curHeight = 1;
308
309             if (curX < 0 && curY < 0) {
310                 if (curRow >= 0)
311                     curY = curRow;
312                 else if (curCol >= 0)
313                     curX = curCol;
314                 else
315                     curY = 0;
316             }
317             if (curX < 0) {
318                 px = 0;
319                 for (i = curY; i < (curY + curHeight); i++)
320                     px = Math.max(px, xMax[i]);
321
322                 curX = px - curX - 1;
323                 if (curX < 0)
324                     curX = 0;
325             } else if (curY < 0) {
326                 py = 0;
327                 for (i = curX; i < (curX + curWidth); i++)
328                     py = Math.max(py, yMax[i]);
329
330                 curY = py - curY - 1;
331                 if (curY < 0)
332                     curY = 0;
333             }
334
335             for (px = curX + curWidth; r.width < px; r.width++);
336             for (py = curY + curHeight; r.height < py; r.height++);
337
338             for (i = curX; i < (curX + curWidth); i++) {
339                 yMax[i] = py;
340             }
341             for (i = curY; i < (curY + curHeight); i++) {
342                 xMax[i] = px;
343             }
344
345             if (sizeflag == PREFERREDSIZE)
346                 d = comp.getPreferredSize();
347             else
348                 d = comp.getMinimumSize();
349             
350             constraints.minWidth = d.width;
351             constraints.minHeight = d.height;
352
353             if (constraints.gridheight == 0 && constraints.gridwidth == 0)
354                 curRow = curCol = -1;
355
356             if (constraints.gridheight == 0 && curRow < 0)
357                 curCol = curX + curWidth;
358
359             else if (constraints.gridwidth == 0 && curCol < 0)
360                 curRow = curY + curHeight;
361         }
362
363         if (columnWidths != null && r.width < columnWidths.length)
364             r.width = columnWidths.length;
365         if (rowHeights != null && r.height < rowHeights.length)
366             r.height = rowHeights.length;
367
368         curRow = curCol = -1;
369         xMax = new int[MAXGRIDSIZE];
370         yMax = new int[MAXGRIDSIZE];
371
372         for (compindex = 0; compindex < components.length; compindex++) {
373             comp = components[compindex];
374             if (!comp.isVisible())
375                 continue;
376             constraints = lookupConstraints(comp);
377
378             curX = constraints.gridx;
379             curY = constraints.gridy;
380             curWidth = constraints.gridwidth;
381             curHeight = constraints.gridheight;
382
383             if (curX < 0 && curY < 0) {
384                 if (curRow >= 0)
385                     curY = curRow;
386                 else if (curCol >= 0)
387                     curX = curCol;
388                 else
389                     curY = 0;
390             }
391
392             if (curX < 0) {
393                 if (curHeight <= 0) {
394                     curHeight += r.height - curY;
395                     if (curHeight < 1)
396                         curHeight = 1;
397                 }
398
399                 px = 0;
400                 for (i = curY; i < (curY + curHeight); i++)
401                     px = Math.max(px, xMax[i]);
402
403                 curX = px - curX - 1;
404                 if (curX < 0)
405                     curX = 0;
406             } else if (curY < 0) {
407                 if (curWidth <= 0) {
408                     curWidth += r.width - curX;
409                     if (curWidth < 1)
410                         curWidth = 1;
411                 }
412
413                 py = 0;
414                 for (i = curX; i < (curX + curWidth); i++)
415                     py = Math.max(py, yMax[i]);
416
417                 curY = py - curY - 1;
418                 if (curY < 0)
419                     curY = 0;
420             }
421
422             if (curWidth <= 0) {
423                 curWidth += r.width - curX;
424                 if (curWidth < 1)
425                     curWidth = 1;
426             }
427
428             if (curHeight <= 0) {
429                 curHeight += r.height - curY;
430                 if (curHeight < 1)
431                     curHeight = 1;
432             }
433
434             px = curX + curWidth;
435             py = curY + curHeight;
436
437             for (i = curX; i < (curX + curWidth); i++) {
438                 yMax[i] = py;
439             }
440             for (i = curY; i < (curY + curHeight); i++) {
441                 xMax[i] = px;
442             }
443
444             if (constraints.gridheight == 0 && constraints.gridwidth == 0)
445                 curRow = curCol = -1;
446             if (constraints.gridheight == 0 && curRow < 0)
447                 curCol = curX + curWidth;
448             else if (constraints.gridwidth == 0 && curCol < 0)
449                 curRow = curY + curHeight;
450
451             constraints.tempX = curX;
452             constraints.tempY = curY;
453             constraints.tempWidth = curWidth;
454             constraints.tempHeight = curHeight;
455         }
456
457         if (columnWidths != null)
458             System.arraycopy(columnWidths, 0, r.minWidth, 0, columnWidths.length);
459         if (rowHeights != null)
460             System.arraycopy(rowHeights, 0, r.minHeight, 0, rowHeights.length);
461         if (columnWeights != null)
462             System.arraycopy(columnWeights, 0, r.weightX, 0, columnWeights.length);
463         if (rowWeights != null)
464             System.arraycopy(rowWeights, 0, r.weightY, 0, rowWeights.length);
465
466         nextSize = Integer.MAX_VALUE;
467
468         for (i = 1; i != Integer.MAX_VALUE; i = nextSize, nextSize = Integer.MAX_VALUE) {
469             for (compindex = 0;
470             compindex < components.length;
471             compindex++) {
472                 comp = components[compindex];
473                 if (!comp.isVisible())
474                     continue;
475                 constraints = lookupConstraints(comp);
476
477                 if (constraints.tempWidth == i) {
478                     px = constraints.tempX + constraints.tempWidth;
479
480                     weight_diff = constraints.weightx;
481                     for (k = constraints.tempX; k < px; k++)
482                         weight_diff -= r.weightX[k];
483                     if (weight_diff > 0.0) {
484                         weight = 0.0;
485                         for (k = constraints.tempX; k < px; k++)
486                             weight += r.weightX[k];
487                         for (k = constraints.tempX;
488                         weight > 0.0 && k < px;
489                         k++) {
490                             double wt = r.weightX[k];
491                             double dx = (wt * weight_diff) / weight;
492                             r.weightX[k] += dx;
493                             weight_diff -= dx;
494                             weight -= wt;
495                         }
496                         r.weightX[px - 1] += weight_diff;
497                     }
498
499                     pixels_diff =
500                     constraints.minWidth
501                     + constraints.ipadx
502                     + constraints.insets.left
503                     + constraints.insets.right;
504
505                     for (k = constraints.tempX; k < px; k++)
506                         pixels_diff -= r.minWidth[k];
507                     if (pixels_diff > 0) {
508                         weight = 0.0;
509                         for (k = constraints.tempX; k < px; k++)
510                             weight += r.weightX[k];
511                         for (k = constraints.tempX;
512                         weight > 0.0 && k < px;
513                         k++) {
514                             double wt = r.weightX[k];
515                             int dx =
516                             (int) ((wt * ((double) pixels_diff))
517                             / weight);
518                             r.minWidth[k] += dx;
519                             pixels_diff -= dx;
520                             weight -= wt;
521                         }
522                         r.minWidth[px - 1] += pixels_diff;
523                     }
524                 } else if (
525                 constraints.tempWidth > i
526                 && constraints.tempWidth < nextSize)
527                     nextSize = constraints.tempWidth;
528
529                 if (constraints.tempHeight == i) {
530                     py = constraints.tempY + constraints.tempHeight;
531
532                     weight_diff = constraints.weighty;
533                     for (k = constraints.tempY; k < py; k++)
534                         weight_diff -= r.weightY[k];
535                     if (weight_diff > 0.0) {
536                         weight = 0.0;
537                         for (k = constraints.tempY; k < py; k++)
538                             weight += r.weightY[k];
539                         for (k = constraints.tempY;
540                         weight > 0.0 && k < py;
541                         k++) {
542                             double wt = r.weightY[k];
543                             double dy = (wt * weight_diff) / weight;
544                             r.weightY[k] += dy;
545                             weight_diff -= dy;
546                             weight -= wt;
547                         }
548                         r.weightY[py - 1] += weight_diff;
549                     }
550
551                     pixels_diff =
552                     constraints.minHeight
553                     + constraints.ipady
554                     + constraints.insets.top
555                     + constraints.insets.bottom;
556                     for (k = constraints.tempY; k < py; k++)
557                         pixels_diff -= r.minHeight[k];
558                     if (pixels_diff > 0) {
559                         weight = 0.0;
560                         for (k = constraints.tempY; k < py; k++)
561                             weight += r.weightY[k];
562                         for (k = constraints.tempY;
563                         weight > 0.0 && k < py;
564                         k++) {
565                             double wt = r.weightY[k];
566                             int dy =
567                             (int) ((wt * ((double) pixels_diff))
568                             / weight);
569                             r.minHeight[k] += dy;
570                             pixels_diff -= dy;
571                             weight -= wt;
572                         }
573                         r.minHeight[py - 1] += pixels_diff;
574                     }
575                 } else if (
576                 constraints.tempHeight > i
577                 && constraints.tempHeight < nextSize)
578                     nextSize = constraints.tempHeight;
579             }
580         }
581
582         return r;
583     }
584     
585     protected void moveComponentInsets(GridBagConstraints constraints, Rectangle r) {
586         
587         int diffx, diffy;
588         
589         if (!rightToLeft) {
590             r.x += constraints.insets.left;
591         } else {
592             r.x -= r.width - constraints.insets.right;
593         }
594         r.width -= (constraints.insets.left + constraints.insets.right);
595         r.y += constraints.insets.top;
596         r.height -= (constraints.insets.top + constraints.insets.bottom);
597         
598         diffx = 0;
599         if ((constraints.fill != GridBagConstraints.HORIZONTAL
600         && constraints.fill != GridBagConstraints.BOTH)
601         && (r.width > (constraints.minWidth + constraints.ipadx))) {
602             diffx = r.width - (constraints.minWidth + constraints.ipadx);
603             r.width = constraints.minWidth + constraints.ipadx;
604         }
605         
606         diffy = 0;
607         if ((constraints.fill != GridBagConstraints.VERTICAL
608         && constraints.fill != GridBagConstraints.BOTH)
609         && (r.height > (constraints.minHeight + constraints.ipady))) {
610             diffy = r.height - (constraints.minHeight + constraints.ipady);
611             r.height = constraints.minHeight + constraints.ipady;
612         }
613         
614         switch (constraints.anchor) {
615             case GridBagConstraints.CENTER :
616                 r.x += diffx / 2;
617                 r.y += diffy / 2;
618                 break;
619             case GridBagConstraints.PAGE_START :
620             case GridBagConstraints.NORTH :
621                 r.x += diffx / 2;
622                 break;
623             case GridBagConstraints.NORTHEAST :
624                 r.x += diffx;
625                 break;
626             case GridBagConstraints.EAST :
627                 r.x += diffx;
628                 r.y += diffy / 2;
629                 break;
630             case GridBagConstraints.SOUTHEAST :
631                 r.x += diffx;
632                 r.y += diffy;
633                 break;
634             case GridBagConstraints.PAGE_END :
635             case GridBagConstraints.SOUTH :
636                 r.x += diffx / 2;
637                 r.y += diffy;
638                 break;
639             case GridBagConstraints.SOUTHWEST :
640                 r.y += diffy;
641                 break;
642             case GridBagConstraints.WEST :
643                 r.y += diffy / 2;
644                 break;
645             case GridBagConstraints.NORTHWEST :
646                 break;
647             case GridBagConstraints.LINE_START :
648                 if (rightToLeft) {
649                     r.x += diffx;
650                 }
651                 r.y += diffy / 2;
652                 break;
653             case GridBagConstraints.LINE_END :
654                 if (!rightToLeft) {
655                     r.x += diffx;
656                 }
657                 r.y += diffy / 2;
658                 break;
659             case GridBagConstraints.FIRST_LINE_START :
660                 if (rightToLeft) {
661                     r.x += diffx;
662                 }
663                 break;
664             case GridBagConstraints.FIRST_LINE_END :
665                 if (!rightToLeft) {
666                     r.x += diffx;
667                 }
668                 break;
669             case GridBagConstraints.LAST_LINE_START :
670                 if (rightToLeft) {
671                     r.x += diffx;
672                 }
673                 r.y += diffy;
674                 break;
675             case GridBagConstraints.LAST_LINE_END :
676                 if (!rightToLeft) {
677                     r.x += diffx;
678                 }
679                 r.y += diffy;
680                 break;
681             default :
682                 throw new IllegalArgumentException JavaDoc("illegal anchor value");
683         }
684     }
685     
686     protected Dimension getSmallestLayoutSize(Container parent, GridBagLayoutInfo info) {
687         Dimension d = new Dimension();
688         int i, t;
689         Insets insets = parent.getInsets();
690         
691         t = 0;
692         for (i = 0; i < info.width; i++)
693             t += info.minWidth[i];
694         d.width = t + insets.left + insets.right;
695         
696         t = 0;
697         for (i = 0; i < info.height; i++)
698             t += info.minHeight[i];
699         d.height = t + insets.top + insets.bottom;
700         
701         return d;
702     }
703     
704     boolean rightToLeft = false;
705     protected static final int MAXGRIDSIZE = 512;
706     protected static final int MINSIZE = 1;
707     protected static final int PREFERREDSIZE = 2;
708 }
709
710 class GridBagLayoutInfo {
711     
712     int width;
713     int height;
714     int startx;
715     int starty;
716     int minWidth[];
717     int minHeight[];
718     double weightX[];
719     double weightY[];
720     
721     GridBagLayoutInfo() {
722         minWidth = new int[GridBagLayout.MAXGRIDSIZE];
723         minHeight = new int[GridBagLayout.MAXGRIDSIZE];
724         weightX = new double[GridBagLayout.MAXGRIDSIZE];
725         weightY = new double[GridBagLayout.MAXGRIDSIZE];
726     }
727 }
728
Popular Tags