001: /*
002: * Copyright 1995 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.awt;
027:
028: import java.awt.*;
029: import java.util.BitSet;
030:
031: /**
032: * A layout manager for a container that lays out grids. Allows setting
033: * the relative sizes of rows and columns.
034: *
035: * @version 1.19, 05/05/07
036: * @author Herb Jellinek
037: */
038:
039: public class VariableGridLayout extends GridLayout {
040:
041: BitSet rowsSet = new BitSet();
042: double rowFractions[] = null;
043:
044: BitSet colsSet = new BitSet();
045: double colFractions[] = null;
046:
047: int rows;
048: int cols;
049: int hgap;
050: int vgap;
051:
052: /**
053: * Creates a grid layout with the specified rows and specified columns.
054: * @param rows the rows
055: * @param cols the columns
056: */
057: public VariableGridLayout(int rows, int cols) {
058: this (rows, cols, 0, 0);
059:
060: if (rows != 0) {
061: rowsSet = new BitSet(rows);
062: stdRowFractions(rows);
063: }
064:
065: if (cols != 0) {
066: colsSet = new BitSet(cols);
067: stdColFractions(cols);
068: }
069: }
070:
071: /**
072: * Creates a grid layout with the specified rows, columns,
073: * horizontal gap, and vertical gap.
074: * @param rows the rows
075: * @param cols the columns
076: * @param hgap the horizontal gap variable
077: * @param vgap the vertical gap variable
078: * @exception IllegalArgumentException If the rows and columns are invalid.
079: */
080: public VariableGridLayout(int rows, int cols, int hgap, int vgap) {
081: super (rows, cols, hgap, vgap);
082:
083: this .rows = rows;
084: this .cols = cols;
085: this .hgap = hgap;
086: this .vgap = vgap;
087:
088: if (rows != 0) {
089: rowsSet = new BitSet(rows);
090: stdRowFractions(rows);
091: }
092:
093: if (cols != 0) {
094: colsSet = new BitSet(cols);
095: stdColFractions(cols);
096: }
097: }
098:
099: void stdRowFractions(int nrows) {
100: rowFractions = new double[nrows];
101: for (int i = 0; i < nrows; i++) {
102: rowFractions[i] = 1.0 / nrows;
103: }
104: }
105:
106: void stdColFractions(int ncols) {
107: colFractions = new double[ncols];
108: for (int i = 0; i < ncols; i++) {
109: colFractions[i] = 1.0 / ncols;
110: }
111: }
112:
113: public void setRowFraction(int rowNum, double fraction) {
114: rowsSet.set(rowNum);
115: rowFractions[rowNum] = fraction;
116: }
117:
118: public void setColFraction(int colNum, double fraction) {
119: colsSet.set(colNum);
120: colFractions[colNum] = fraction;
121: }
122:
123: public double getRowFraction(int rowNum) {
124: return rowFractions[rowNum];
125: }
126:
127: public double getColFraction(int colNum) {
128: return colFractions[colNum];
129: }
130:
131: void allocateExtraSpace(double vec[], BitSet userSet) {
132: // collect the space that's been explicitly allocated...
133: double total = 0.0;
134: int unallocated = 0;
135: int i;
136: for (i = 0; i < vec.length; i++) {
137: if (userSet.get(i)) {
138: total += vec[i];
139: } else {
140: unallocated++;
141: }
142: }
143:
144: // ... then spread the extra space
145: if (unallocated != 0) {
146: double space = (1.0 - total) / unallocated;
147: for (i = 0; i < vec.length; i++) {
148: if (!userSet.get(i)) {
149: vec[i] = space;
150: userSet.set(i);
151: }
152: }
153: }
154: }
155:
156: void allocateExtraSpace() {
157: allocateExtraSpace(rowFractions, rowsSet);
158: allocateExtraSpace(colFractions, colsSet);
159: }
160:
161: /**
162: * Lays out the container in the specified panel.
163: * @param parent the specified component being laid out
164: * @see Container
165: */
166: public void layoutContainer(Container parent) {
167: Insets insets = parent.insets();
168: int ncomponents = parent.countComponents();
169: int nrows = rows;
170: int ncols = cols;
171:
172: if (nrows > 0) {
173: ncols = (ncomponents + nrows - 1) / nrows;
174: } else {
175: nrows = (ncomponents + ncols - 1) / ncols;
176: }
177:
178: if (rows == 0) {
179: stdRowFractions(nrows);
180: }
181: if (cols == 0) {
182: stdColFractions(ncols);
183: }
184:
185: Dimension size = parent.size();
186: int w = size.width - (insets.left + insets.right);
187: int h = size.height - (insets.top + insets.bottom);
188:
189: w = (w - (ncols - 1) * hgap);
190: h = (h - (nrows - 1) * vgap);
191:
192: allocateExtraSpace();
193:
194: for (int c = 0, x = insets.left; c < ncols; c++) {
195: int colWidth = (int) (getColFraction(c) * w);
196: for (int r = 0, y = insets.top; r < nrows; r++) {
197: int i = r * ncols + c;
198: int rowHeight = (int) (getRowFraction(r) * h);
199:
200: if (i < ncomponents) {
201: parent.getComponent(i).reshape(x, y, colWidth,
202: rowHeight);
203: }
204: y += rowHeight + vgap;
205: }
206: x += colWidth + hgap;
207: }
208: }
209:
210: static String fracsToString(double array[]) {
211: String result = "[" + array.length + "]";
212:
213: for (int i = 0; i < array.length; i++) {
214: result += "<" + array[i] + ">";
215: }
216: return result;
217: }
218:
219: /**
220: * Returns the String representation of this VariableGridLayout's values.
221: */
222: public String toString() {
223: return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap
224: + ",rows=" + rows + ",cols=" + cols + ",rowFracs="
225: + fracsToString(rowFractions) + ",colFracs="
226: + fracsToString(colFractions) + "]";
227: }
228: }
|