KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > colorchooser > SmartGridLayout


1 /*
2  * @(#)SmartGridLayout.java 1.8 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.colorchooser;
9
10 import javax.swing.*;
11 import java.awt.*;
12 import java.awt.event.*;
13 import javax.swing.event.*;
14 import javax.swing.text.*;
15 import java.io.Serializable JavaDoc;
16
17
18 /**
19   * A better GridLayout class
20   *
21   * @version 1.8 12/19/03
22   * @author Steve Wilson
23   */

24 class SmartGridLayout implements LayoutManager, Serializable JavaDoc {
25
26   int rows = 2;
27   int columns = 2;
28   int xGap = 2;
29   int yGap = 2;
30   int componentCount = 0;
31   Component[][] layoutGrid;
32
33
34   public SmartGridLayout(int numColumns, int numRows) {
35     rows = numRows;
36     columns = numColumns;
37     layoutGrid = new Component[numColumns][numRows];
38
39   }
40
41
42   public void layoutContainer(Container c) {
43
44     buildLayoutGrid(c);
45
46     int[] rowHeights = new int[rows];
47     int[] columnWidths = new int[columns];
48     
49     for (int row = 0; row < rows; row++) {
50         rowHeights[row] = computeRowHeight(row);
51     }
52
53     for (int column = 0; column < columns; column++) {
54         columnWidths[column] = computeColumnWidth(column);
55     }
56
57
58     Insets insets = c.getInsets();
59
60     if (c.getComponentOrientation().isLeftToRight()) {
61         int horizLoc = insets.left;
62         for (int column = 0; column < columns; column++) {
63           int vertLoc = insets.top;
64       
65           for (int row = 0; row < rows; row++) {
66             Component current = layoutGrid[column][row];
67
68             current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
69             // System.out.println(current.getBounds());
70
vertLoc += (rowHeights[row] + yGap);
71           }
72           horizLoc += (columnWidths[column] + xGap );
73         }
74     } else {
75         int horizLoc = c.getWidth() - insets.right;
76         for (int column = 0; column < columns; column++) {
77           int vertLoc = insets.top;
78           horizLoc -= columnWidths[column];
79       
80           for (int row = 0; row < rows; row++) {
81             Component current = layoutGrid[column][row];
82
83             current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
84             // System.out.println(current.getBounds());
85
vertLoc += (rowHeights[row] + yGap);
86           }
87           horizLoc -= xGap;
88         }
89     }
90       
91
92
93   }
94
95   public Dimension minimumLayoutSize(Container c) {
96
97     buildLayoutGrid(c);
98     Insets insets = c.getInsets();
99     
100
101
102     int height = 0;
103     int width = 0;
104     
105     for (int row = 0; row < rows; row++) {
106         height += computeRowHeight(row);
107     }
108
109     for (int column = 0; column < columns; column++) {
110         width += computeColumnWidth(column);
111     }
112
113     height += (yGap * (rows - 1)) + insets.top + insets.bottom;
114     width += (xGap * (columns - 1)) + insets.right + insets.left;
115
116     return new Dimension(width, height);
117
118
119   }
120   
121   public Dimension preferredLayoutSize(Container c) {
122       return minimumLayoutSize(c);
123   }
124    
125
126   public void addLayoutComponent(String JavaDoc s, Component c) {}
127
128   public void removeLayoutComponent(Component c) {}
129
130
131   private void buildLayoutGrid(Container c) {
132
133       Component[] children = c.getComponents();
134
135       for (int componentCount = 0; componentCount < children.length; componentCount++) {
136     // System.out.println("Children: " +componentCount);
137
int row = 0;
138     int column = 0;
139
140     if (componentCount != 0) {
141       column = componentCount % columns;
142       row = (componentCount - column) / columns;
143     }
144
145     // System.out.println("inserting into: "+ column + " " + row);
146

147     layoutGrid[column][row] = children[componentCount];
148       }
149   }
150
151   private int computeColumnWidth(int columnNum) {
152     int maxWidth = 1;
153     for (int row = 0; row < rows; row++) {
154       int width = layoutGrid[columnNum][row].getPreferredSize().width;
155       if (width > maxWidth) {
156     maxWidth = width;
157       }
158     }
159     return maxWidth;
160   }
161
162   private int computeRowHeight(int rowNum) {
163     int maxHeight = 1;
164     for (int column = 0; column < columns; column++) {
165       int height = layoutGrid[column][rowNum].getPreferredSize().height;
166       if (height > maxHeight) {
167     maxHeight = height;
168       }
169     }
170     return maxHeight;
171   }
172
173 }
174
Popular Tags