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