VariableGridLayout.java revision 0
2370N/A/*
2685N/A * Copyright 1995 Sun Microsystems, Inc. All Rights Reserved.
2370N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2370N/A *
2370N/A * This code is free software; you can redistribute it and/or modify it
2370N/A * under the terms of the GNU General Public License version 2 only, as
2685N/A * published by the Free Software Foundation. Sun designates this
2370N/A * particular file as subject to the "Classpath" exception as provided
2685N/A * by Sun in the LICENSE file that accompanied this code.
2370N/A *
2370N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2370N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2370N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2370N/A * version 2 for more details (a copy is included in the LICENSE file that
2370N/A * accompanied this code).
2370N/A *
2370N/A * You should have received a copy of the GNU General Public License version
2370N/A * 2 along with this work; if not, write to the Free Software Foundation,
2370N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2370N/A *
2685N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2685N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2685N/A * have any questions.
2370N/A */
2370N/A
2370N/Apackage sun.awt;
2370N/A
2370N/Aimport java.awt.*;
2370N/Aimport java.util.BitSet;
2370N/A
2370N/A
2370N/A/**
2370N/A * A layout manager for a container that lays out grids. Allows setting
2370N/A * the relative sizes of rows and columns.
2370N/A *
2370N/A * @author Herb Jellinek
2370N/A */
2370N/A
2370N/A
2370N/Apublic class VariableGridLayout extends GridLayout {
2370N/A
2370N/A BitSet rowsSet = new BitSet();
2370N/A double rowFractions[] = null;
2370N/A
2370N/A BitSet colsSet = new BitSet();
2370N/A double colFractions[] = null;
2370N/A
2370N/A int rows;
2370N/A int cols;
2370N/A int hgap;
2370N/A int vgap;
2370N/A
2370N/A /**
2370N/A * Creates a grid layout with the specified rows and specified columns.
2370N/A * @param rows the rows
2370N/A * @param cols the columns
2370N/A */
2370N/A public VariableGridLayout(int rows, int cols) {
2370N/A this(rows, cols, 0, 0);
2370N/A
2370N/A if (rows != 0) {
2370N/A rowsSet = new BitSet(rows);
2370N/A stdRowFractions(rows);
2370N/A }
2370N/A
2370N/A if (cols != 0) {
2370N/A colsSet = new BitSet(cols);
2370N/A stdColFractions(cols);
2370N/A }
2370N/A }
2370N/A
6133N/A /**
6133N/A * Creates a grid layout with the specified rows, columns,
6133N/A * horizontal gap, and vertical gap.
6133N/A * @param rows the rows
6133N/A * @param cols the columns
6133N/A * @param hgap the horizontal gap variable
6133N/A * @param vgap the vertical gap variable
6133N/A * @exception IllegalArgumentException If the rows and columns are invalid.
2370N/A */
2370N/A public VariableGridLayout(int rows, int cols, int hgap, int vgap) {
2370N/A super(rows, cols, hgap, vgap);
6133N/A
6133N/A this.rows = rows;
6133N/A this.cols = cols;
6133N/A this.hgap = hgap;
6133N/A this.vgap = vgap;
6133N/A
6133N/A if (rows != 0) {
6133N/A rowsSet = new BitSet(rows);
6133N/A stdRowFractions(rows);
6133N/A }
6133N/A
2370N/A if (cols != 0) {
2370N/A colsSet = new BitSet(cols);
2370N/A stdColFractions(cols);
2370N/A }
2370N/A }
2370N/A
2370N/A void stdRowFractions(int nrows) {
2370N/A rowFractions = new double[nrows];
2370N/A for (int i = 0; i < nrows; i++) {
2370N/A rowFractions[i] = 1.0 / nrows;
2370N/A }
2370N/A }
2370N/A
2370N/A void stdColFractions(int ncols) {
2370N/A colFractions = new double[ncols];
2370N/A for (int i = 0; i < ncols; i++) {
2370N/A colFractions[i] = 1.0 / ncols;
2370N/A }
2370N/A }
2370N/A
2370N/A public void setRowFraction(int rowNum, double fraction) {
2370N/A rowsSet.set(rowNum);
2370N/A rowFractions[rowNum] = fraction;
2370N/A }
2370N/A
2370N/A public void setColFraction(int colNum, double fraction) {
2370N/A colsSet.set(colNum);
2370N/A colFractions[colNum] = fraction;
2370N/A }
2370N/A
2370N/A public double getRowFraction(int rowNum) {
6133N/A return rowFractions[rowNum];
6133N/A }
6133N/A
2370N/A public double getColFraction(int colNum) {
2370N/A return colFractions[colNum];
2370N/A }
2370N/A
2370N/A void allocateExtraSpace(double vec[], BitSet userSet) {
2370N/A // collect the space that's been explicitly allocated...
2370N/A double total = 0.0;
2370N/A int unallocated = 0;
2370N/A int i;
2370N/A for (i = 0; i < vec.length; i++) {
2370N/A if (userSet.get(i)) {
2370N/A total += vec[i];
2370N/A } else {
2370N/A unallocated++;
2370N/A }
2370N/A }
2370N/A
2370N/A // ... then spread the extra space
2370N/A if (unallocated != 0) {
2370N/A double space = (1.0 - total) / unallocated;
2370N/A for (i = 0; i < vec.length; i++) {
2370N/A if (!userSet.get(i)) {
2370N/A vec[i] = space;
2370N/A userSet.set(i);
2370N/A }
2370N/A }
2370N/A }
2370N/A }
2370N/A
2370N/A
2370N/A void allocateExtraSpace() {
2370N/A allocateExtraSpace(rowFractions, rowsSet);
2370N/A allocateExtraSpace(colFractions, colsSet);
2370N/A }
2370N/A
2370N/A /**
2370N/A * Lays out the container in the specified panel.
2370N/A * @param parent the specified component being laid out
2370N/A * @see Container
2370N/A */
2370N/A public void layoutContainer(Container parent) {
2370N/A Insets insets = parent.insets();
2370N/A int ncomponents = parent.countComponents();
2370N/A int nrows = rows;
2370N/A int ncols = cols;
2370N/A
2370N/A if (nrows > 0) {
2370N/A ncols = (ncomponents + nrows - 1) / nrows;
2370N/A } else {
2370N/A nrows = (ncomponents + ncols - 1) / ncols;
2370N/A }
2370N/A
2370N/A if (rows == 0) {
2370N/A stdRowFractions(nrows);
2370N/A }
2370N/A if (cols == 0) {
2370N/A stdColFractions(ncols);
2370N/A }
2370N/A
2370N/A Dimension size = parent.size();
2370N/A int w = size.width - (insets.left + insets.right);
2370N/A int h = size.height - (insets.top + insets.bottom);
2370N/A
2370N/A w = (w - (ncols - 1) * hgap);
2370N/A h = (h - (nrows - 1) * vgap);
2370N/A
2370N/A allocateExtraSpace();
2370N/A
2370N/A for (int c = 0, x = insets.left ; c < ncols ; c++) {
2370N/A int colWidth = (int)(getColFraction(c) * w);
2370N/A for (int r = 0, y = insets.top ; r < nrows ; r++) {
2370N/A int i = r * ncols + c;
2370N/A int rowHeight = (int)(getRowFraction(r) * h);
2370N/A
2370N/A if (i < ncomponents) {
2370N/A parent.getComponent(i).reshape(x, y, colWidth, rowHeight);
2370N/A }
2370N/A y += rowHeight + vgap;
2370N/A }
2370N/A x += colWidth + hgap;
2370N/A }
2370N/A }
2370N/A
2370N/A static String fracsToString(double array[]) {
2370N/A String result = "["+array.length+"]";
2370N/A
2370N/A for (int i = 0; i < array.length; i++) {
2370N/A result += "<"+array[i]+">";
2370N/A }
2370N/A return result;
2370N/A }
2370N/A
2370N/A /**
* Returns the String representation of this VariableGridLayout's values.
*/
public String toString() {
return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap +
",rows=" + rows + ",cols=" + cols +
",rowFracs=" +
fracsToString(rowFractions) +
",colFracs=" +
fracsToString(colFractions) + "]";
}
}