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