001: /*
002: * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jconsole;
027:
028: import java.awt.*;
029: import java.util.*;
030:
031: import javax.swing.*;
032:
033: @SuppressWarnings("serial")
034: public class VariableGridLayout extends GridLayout {
035:
036: private boolean fillRows, fillColumns;
037:
038: public VariableGridLayout(int rows, int cols, int hgap, int vgap,
039: boolean fillRows, boolean fillColumns) {
040: super (rows, cols, hgap, vgap);
041:
042: this .fillRows = fillRows;
043: this .fillColumns = fillColumns;
044: }
045:
046: public void setFillRow(JComponent c, boolean b) {
047: c.putClientProperty("VariableGridLayout.fillRow", b);
048: }
049:
050: public void setFillColumn(JComponent c, boolean b) {
051: c.putClientProperty("VariableGridLayout.fillColumn", b);
052: }
053:
054: public boolean getFillRow(JComponent c) {
055: Boolean b = (Boolean) c
056: .getClientProperty("VariableGridLayout.fillRow");
057: return (b != null) ? b : fillRows;
058: }
059:
060: public boolean getFillColumn(JComponent c) {
061: Boolean b = (Boolean) c
062: .getClientProperty("VariableGridLayout.fillColumn");
063: return (b != null) ? b : fillColumns;
064: }
065:
066: public void layoutContainer(Container parent) {
067: Insets insets = parent.getInsets();
068: int ncomponents = parent.getComponentCount();
069: int nrows = getRows();
070: int ncols = getColumns();
071: int hgap = getHgap();
072: int vgap = getVgap();
073:
074: if (nrows > 0) {
075: ncols = (ncomponents + nrows - 1) / nrows;
076: } else {
077: nrows = (ncomponents + ncols - 1) / ncols;
078: }
079:
080: // Set heights
081: int x;
082: int y;
083: int nFills = 0;
084: boolean[] fills = new boolean[nrows];
085: int lastFillRow = -1;
086: int nComps = parent.getComponentCount();
087:
088: y = insets.top;
089: for (int row = 0; row < nrows; row++) {
090: // Find largest minimum height for this row
091: int h = 0;
092: for (int col = 0; col < ncols; col++) {
093: if (row * ncols + col < nComps) {
094: Component c = parent
095: .getComponent(row * ncols + col);
096: h = Math.max(h, c.getMinimumSize().height);
097: }
098: }
099: // Set heights for this row
100: x = insets.left;
101: for (int col = 0; col < ncols; col++) {
102: if (row * ncols + col < nComps) {
103: JComponent c = (JComponent) parent.getComponent(row
104: * ncols + col);
105: int w = c.getWidth();
106: c.setBounds(x, y, w, h);
107: x += w + hgap;
108: if (col == 0 && getFillRow(c)) {
109: fills[row] = true;
110: }
111: }
112: }
113: y += h + vgap;
114: if (fills[row]) {
115: nFills++;
116: lastFillRow = row;
117: }
118: }
119:
120: // Fill heights
121: if (nFills > 0 && y < parent.getHeight()) {
122: // How much height to add
123: int hAdd = (parent.getHeight() - y) / nFills;
124: int hAdded = 0;
125: for (int row = 0; row < nrows; row++) {
126: if (fills[row]) {
127: if (row == lastFillRow) {
128: // Compensate for rounding error
129: hAdd = parent.getHeight() - (y + hAdded);
130: }
131: for (int col = 0; col < ncols; col++) {
132: if (row * ncols + col < nComps) {
133: Component c = parent.getComponent(row
134: * ncols + col);
135: Rectangle b = c.getBounds();
136: c.setBounds(b.x, b.y + hAdded, b.width,
137: b.height + hAdd);
138: }
139: }
140: hAdded += hAdd;
141: }
142: }
143: }
144:
145: // Set widths
146: nFills = 0;
147: fills = new boolean[ncols];
148: int lastFillCol = -1;
149:
150: x = insets.left;
151: for (int col = 0; col < ncols; col++) {
152: // Find largest minimum width for this column
153: int w = 0;
154: for (int row = 0; row < nrows; row++) {
155: if (row * ncols + col < nComps) {
156: Component c = parent
157: .getComponent(row * ncols + col);
158: w = Math.max(w, c.getMinimumSize().width);
159: }
160: }
161: // Set widths for this column
162: y = insets.top;
163: for (int row = 0; row < nrows; row++) {
164: if (row * ncols + col < nComps) {
165: JComponent c = (JComponent) parent.getComponent(row
166: * ncols + col);
167: int h = c.getHeight();
168: c.setBounds(x, y, w, h);
169: y += h + vgap;
170: if (row == 0 && getFillColumn(c)) {
171: fills[col] = true;
172: }
173: }
174: }
175: x += w + hgap;
176: if (fills[col]) {
177: nFills++;
178: lastFillCol = col;
179: }
180: }
181:
182: // Fill widths
183: if (nFills > 0 && x < parent.getWidth()) {
184: // How much width to add
185: int wAdd = (parent.getWidth() - x) / nFills;
186: int wAdded = 0;
187: for (int col = 0; col < ncols; col++) {
188: if (fills[col]) {
189: if (col == lastFillCol) {
190: wAdd = parent.getWidth() - (x + wAdded);
191: }
192: for (int row = 0; row < nrows; row++) {
193: if (row * ncols + col < nComps) {
194: Component c = parent.getComponent(row
195: * ncols + col);
196: Rectangle b = c.getBounds();
197: c.setBounds(b.x + wAdded, b.y, b.width
198: + wAdd, b.height);
199: }
200: }
201: wAdded += wAdd;
202: }
203: }
204: }
205: }
206:
207: public Dimension preferredLayoutSize(Container parent) {
208: Insets insets = parent.getInsets();
209: int ncomponents = parent.getComponentCount();
210: int nrows = getRows();
211: int ncols = getColumns();
212: int hgap = getHgap();
213: int vgap = getVgap();
214:
215: if (nrows > 0) {
216: ncols = (ncomponents + nrows - 1) / nrows;
217: } else {
218: nrows = (ncomponents + ncols - 1) / ncols;
219: }
220:
221: int nComps = parent.getComponentCount();
222:
223: int y = insets.top;
224: for (int row = 0; row < nrows; row++) {
225: int h = 0;
226: for (int col = 0; col < ncols; col++) {
227: if (row * ncols + col < nComps) {
228: Component c = parent
229: .getComponent(row * ncols + col);
230: h = Math.max(h, c.getMinimumSize().height);
231: }
232: }
233: y += h + vgap;
234: }
235:
236: int x = insets.left;
237: for (int col = 0; col < ncols; col++) {
238: int w = 0;
239: for (int row = 0; row < nrows; row++) {
240: if (row * ncols + col < nComps) {
241: Component c = parent
242: .getComponent(row * ncols + col);
243: w = Math.max(w, c.getMinimumSize().width);
244: }
245: }
246: x += w + hgap;
247: }
248: return new Dimension(x, y);
249: }
250: }
|