jdk/src/share/classes/sun/awt/VariableGridLayout.java
changeset 11382 9bd385be6f03
parent 11381 890ea587d133
parent 11325 0ff7113a0882
child 11383 ae090403accf
equal deleted inserted replaced
11381:890ea587d133 11382:9bd385be6f03
     1 /*
       
     2  * Copyright (c) 1995, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.awt;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.util.BitSet;
       
    30 
       
    31 
       
    32 /**
       
    33  * A layout manager for a container that lays out grids.  Allows setting
       
    34  * the relative sizes of rows and columns.
       
    35  *
       
    36  * @author Herb Jellinek
       
    37  */
       
    38 
       
    39 
       
    40 public class VariableGridLayout extends GridLayout {
       
    41 
       
    42     BitSet rowsSet = new BitSet();
       
    43     double rowFractions[] = null;
       
    44 
       
    45     BitSet colsSet = new BitSet();
       
    46     double colFractions[] = null;
       
    47 
       
    48     int rows;
       
    49     int cols;
       
    50     int hgap;
       
    51     int vgap;
       
    52 
       
    53     /**
       
    54      * Creates a grid layout with the specified rows and specified columns.
       
    55      * @param rows the rows
       
    56      * @param cols the columns
       
    57      */
       
    58     public VariableGridLayout(int rows, int cols) {
       
    59         this(rows, cols, 0, 0);
       
    60 
       
    61         if (rows != 0) {
       
    62             rowsSet = new BitSet(rows);
       
    63             stdRowFractions(rows);
       
    64         }
       
    65 
       
    66         if (cols != 0) {
       
    67             colsSet = new BitSet(cols);
       
    68             stdColFractions(cols);
       
    69         }
       
    70     }
       
    71 
       
    72     /**
       
    73      * Creates a grid layout with the specified rows, columns,
       
    74      * horizontal gap, and vertical gap.
       
    75      * @param rows the rows
       
    76      * @param cols the columns
       
    77      * @param hgap the horizontal gap variable
       
    78      * @param vgap the vertical gap variable
       
    79      * @exception IllegalArgumentException If the rows and columns are invalid.
       
    80      */
       
    81     public VariableGridLayout(int rows, int cols, int hgap, int vgap) {
       
    82         super(rows, cols, hgap, vgap);
       
    83 
       
    84         this.rows = rows;
       
    85         this.cols = cols;
       
    86         this.hgap = hgap;
       
    87         this.vgap = vgap;
       
    88 
       
    89         if (rows != 0) {
       
    90             rowsSet = new BitSet(rows);
       
    91             stdRowFractions(rows);
       
    92         }
       
    93 
       
    94         if (cols != 0) {
       
    95             colsSet = new BitSet(cols);
       
    96             stdColFractions(cols);
       
    97         }
       
    98     }
       
    99 
       
   100     void stdRowFractions(int nrows) {
       
   101         rowFractions = new double[nrows];
       
   102         for (int i = 0; i < nrows; i++) {
       
   103             rowFractions[i] = 1.0 / nrows;
       
   104         }
       
   105     }
       
   106 
       
   107     void stdColFractions(int ncols) {
       
   108         colFractions = new double[ncols];
       
   109         for (int i = 0; i < ncols; i++) {
       
   110             colFractions[i] = 1.0 / ncols;
       
   111         }
       
   112     }
       
   113 
       
   114     public void setRowFraction(int rowNum, double fraction) {
       
   115         rowsSet.set(rowNum);
       
   116         rowFractions[rowNum] = fraction;
       
   117     }
       
   118 
       
   119     public void setColFraction(int colNum, double fraction) {
       
   120         colsSet.set(colNum);
       
   121         colFractions[colNum] = fraction;
       
   122     }
       
   123 
       
   124     public double getRowFraction(int rowNum) {
       
   125         return rowFractions[rowNum];
       
   126     }
       
   127 
       
   128     public double getColFraction(int colNum) {
       
   129         return colFractions[colNum];
       
   130     }
       
   131 
       
   132     void allocateExtraSpace(double vec[], BitSet userSet) {
       
   133         // collect the space that's been explicitly allocated...
       
   134         double total = 0.0;
       
   135         int unallocated = 0;
       
   136         int i;
       
   137         for (i = 0; i < vec.length; i++) {
       
   138             if (userSet.get(i)) {
       
   139                 total += vec[i];
       
   140             } else {
       
   141                 unallocated++;
       
   142             }
       
   143         }
       
   144 
       
   145         // ... then spread the extra space
       
   146         if (unallocated != 0) {
       
   147             double space = (1.0 - total) / unallocated;
       
   148             for (i = 0; i < vec.length; i++) {
       
   149                 if (!userSet.get(i)) {
       
   150                     vec[i] = space;
       
   151                     userSet.set(i);
       
   152                 }
       
   153             }
       
   154         }
       
   155     }
       
   156 
       
   157 
       
   158     void allocateExtraSpace() {
       
   159         allocateExtraSpace(rowFractions, rowsSet);
       
   160         allocateExtraSpace(colFractions, colsSet);
       
   161     }
       
   162 
       
   163     /**
       
   164      * Lays out the container in the specified panel.
       
   165      * @param parent the specified component being laid out
       
   166      * @see Container
       
   167      */
       
   168     public void layoutContainer(Container parent) {
       
   169         Insets insets = parent.insets();
       
   170         int ncomponents = parent.countComponents();
       
   171         int nrows = rows;
       
   172         int ncols = cols;
       
   173 
       
   174         if (nrows > 0) {
       
   175             ncols = (ncomponents + nrows - 1) / nrows;
       
   176         } else {
       
   177             nrows = (ncomponents + ncols - 1) / ncols;
       
   178         }
       
   179 
       
   180         if (rows == 0) {
       
   181             stdRowFractions(nrows);
       
   182         }
       
   183         if (cols == 0) {
       
   184             stdColFractions(ncols);
       
   185         }
       
   186 
       
   187         Dimension size = parent.size();
       
   188         int w = size.width - (insets.left + insets.right);
       
   189         int h = size.height - (insets.top + insets.bottom);
       
   190 
       
   191         w = (w - (ncols - 1) * hgap);
       
   192         h = (h - (nrows - 1) * vgap);
       
   193 
       
   194         allocateExtraSpace();
       
   195 
       
   196         for (int c = 0, x = insets.left ; c < ncols ; c++) {
       
   197             int colWidth = (int)(getColFraction(c) * w);
       
   198             for (int r = 0, y = insets.top ; r < nrows ; r++) {
       
   199                 int i = r * ncols + c;
       
   200                 int rowHeight = (int)(getRowFraction(r) * h);
       
   201 
       
   202                 if (i < ncomponents) {
       
   203                     parent.getComponent(i).reshape(x, y, colWidth, rowHeight);
       
   204                 }
       
   205                 y += rowHeight + vgap;
       
   206             }
       
   207             x += colWidth + hgap;
       
   208         }
       
   209     }
       
   210 
       
   211     static String fracsToString(double array[]) {
       
   212         String result = "["+array.length+"]";
       
   213 
       
   214         for (int i = 0; i < array.length; i++) {
       
   215             result += "<"+array[i]+">";
       
   216         }
       
   217         return result;
       
   218     }
       
   219 
       
   220     /**
       
   221      * Returns the String representation of this VariableGridLayout's values.
       
   222      */
       
   223     public String toString() {
       
   224         return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap +
       
   225                                        ",rows=" + rows + ",cols=" + cols +
       
   226                                        ",rowFracs=" +
       
   227                                        fracsToString(rowFractions) +
       
   228                                        ",colFracs=" +
       
   229                                        fracsToString(colFractions) + "]";
       
   230     }
       
   231 }