0N/A/*
2362N/A * Copyright (c) 2004, 2006, 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.tools.jconsole;
0N/A
0N/Aimport java.awt.*;
0N/A
0N/Aimport javax.swing.*;
0N/A
0N/A@SuppressWarnings("serial")
0N/Apublic class VariableGridLayout extends GridLayout {
0N/A
0N/A private boolean fillRows, fillColumns;
0N/A
0N/A public VariableGridLayout(int rows, int cols,
0N/A int hgap, int vgap,
0N/A boolean fillRows, boolean fillColumns) {
0N/A super(rows, cols, hgap, vgap);
0N/A
0N/A this.fillRows = fillRows;
0N/A this.fillColumns = fillColumns;
0N/A }
0N/A
0N/A public void setFillRow(JComponent c, boolean b) {
0N/A c.putClientProperty("VariableGridLayout.fillRow", b);
0N/A }
0N/A
0N/A public void setFillColumn(JComponent c, boolean b) {
0N/A c.putClientProperty("VariableGridLayout.fillColumn", b);
0N/A }
0N/A
0N/A public boolean getFillRow(JComponent c) {
0N/A Boolean b = (Boolean)c.getClientProperty("VariableGridLayout.fillRow");
0N/A return (b != null) ? b : fillRows;
0N/A }
0N/A
0N/A public boolean getFillColumn(JComponent c) {
0N/A Boolean b = (Boolean)c.getClientProperty("VariableGridLayout.fillColumn");
0N/A return (b != null) ? b : fillColumns;
0N/A }
0N/A
0N/A public void layoutContainer(Container parent) {
0N/A Insets insets = parent.getInsets();
0N/A int ncomponents = parent.getComponentCount();
0N/A int nrows = getRows();
0N/A int ncols = getColumns();
0N/A int hgap = getHgap();
0N/A int vgap = getVgap();
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 // Set heights
0N/A int x;
0N/A int y;
0N/A int nFills = 0;
0N/A boolean[] fills = new boolean[nrows];
0N/A int lastFillRow = -1;
0N/A int nComps = parent.getComponentCount();
0N/A
0N/A y = insets.top;
0N/A for (int row = 0; row < nrows; row++) {
0N/A // Find largest minimum height for this row
0N/A int h = 0;
0N/A for (int col = 0; col < ncols; col++) {
0N/A if (row * ncols + col < nComps) {
0N/A Component c = parent.getComponent(row * ncols + col);
0N/A h = Math.max(h, c.getMinimumSize().height);
0N/A }
0N/A }
0N/A // Set heights for this row
0N/A x = insets.left;
0N/A for (int col = 0; col < ncols; col++) {
0N/A if (row * ncols + col < nComps) {
0N/A JComponent c = (JComponent)parent.getComponent(row * ncols + col);
0N/A int w = c.getWidth();
0N/A c.setBounds(x, y, w, h);
0N/A x += w + hgap;
0N/A if (col == 0 && getFillRow(c)) {
0N/A fills[row] = true;
0N/A }
0N/A }
0N/A }
0N/A y += h + vgap;
0N/A if (fills[row]) {
0N/A nFills++;
0N/A lastFillRow = row;
0N/A }
0N/A }
0N/A
0N/A // Fill heights
0N/A if (nFills > 0 && y < parent.getHeight()) {
0N/A // How much height to add
0N/A int hAdd = (parent.getHeight() - y) / nFills;
0N/A int hAdded = 0;
0N/A for (int row = 0; row < nrows; row++) {
0N/A if (fills[row]) {
0N/A if (row == lastFillRow) {
0N/A // Compensate for rounding error
0N/A hAdd = parent.getHeight() - (y+hAdded);
0N/A }
0N/A for (int col = 0; col < ncols; col++) {
0N/A if (row * ncols + col < nComps) {
0N/A Component c = parent.getComponent(row * ncols + col);
0N/A Rectangle b = c.getBounds();
0N/A c.setBounds(b.x, b.y + hAdded, b.width, b.height + hAdd);
0N/A }
0N/A }
0N/A hAdded += hAdd;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Set widths
0N/A nFills = 0;
0N/A fills = new boolean[ncols];
0N/A int lastFillCol = -1;
0N/A
0N/A x = insets.left;
0N/A for (int col = 0; col < ncols; col++) {
0N/A // Find largest minimum width for this column
0N/A int w = 0;
0N/A for (int row = 0; row < nrows; row++) {
0N/A if (row * ncols + col < nComps) {
0N/A Component c = parent.getComponent(row * ncols + col);
0N/A w = Math.max(w, c.getMinimumSize().width);
0N/A }
0N/A }
0N/A // Set widths for this column
0N/A y = insets.top;
0N/A for (int row = 0; row < nrows; row++) {
0N/A if (row * ncols + col < nComps) {
0N/A JComponent c = (JComponent)parent.getComponent(row * ncols + col);
0N/A int h = c.getHeight();
0N/A c.setBounds(x, y, w, h);
0N/A y += h + vgap;
0N/A if (row == 0 && getFillColumn(c)) {
0N/A fills[col] = true;
0N/A }
0N/A }
0N/A }
0N/A x += w + hgap;
0N/A if (fills[col]) {
0N/A nFills++;
0N/A lastFillCol = col;
0N/A }
0N/A }
0N/A
0N/A // Fill widths
0N/A if (nFills > 0 && x < parent.getWidth()) {
0N/A // How much width to add
0N/A int wAdd = (parent.getWidth() - x) / nFills;
0N/A int wAdded = 0;
0N/A for (int col = 0; col < ncols; col++) {
0N/A if (fills[col]) {
0N/A if (col == lastFillCol) {
0N/A wAdd = parent.getWidth() - (x+wAdded);
0N/A }
0N/A for (int row = 0; row < nrows; row++) {
0N/A if (row * ncols + col < nComps) {
0N/A Component c = parent.getComponent(row * ncols + col);
0N/A Rectangle b = c.getBounds();
0N/A c.setBounds(b.x + wAdded, b.y, b.width + wAdd, b.height);
0N/A }
0N/A }
0N/A wAdded += wAdd;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Dimension preferredLayoutSize(Container parent) {
0N/A Insets insets = parent.getInsets();
0N/A int ncomponents = parent.getComponentCount();
0N/A int nrows = getRows();
0N/A int ncols = getColumns();
0N/A int hgap = getHgap();
0N/A int vgap = getVgap();
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 int nComps = parent.getComponentCount();
0N/A
0N/A int y = insets.top;
0N/A for (int row = 0; row < nrows; row++) {
0N/A int h = 0;
0N/A for (int col = 0; col < ncols; col++) {
0N/A if (row * ncols + col < nComps) {
0N/A Component c = parent.getComponent(row * ncols + col);
0N/A h = Math.max(h, c.getMinimumSize().height);
0N/A }
0N/A }
0N/A y += h + vgap;
0N/A }
0N/A
0N/A int x = insets.left;
0N/A for (int col = 0; col < ncols; col++) {
0N/A int w = 0;
0N/A for (int row = 0; row < nrows; row++) {
0N/A if (row * ncols + col < nComps) {
0N/A Component c = parent.getComponent(row * ncols + col);
0N/A w = Math.max(w, c.getMinimumSize().width);
0N/A }
0N/A }
0N/A x += w + hgap;
0N/A }
0N/A return new Dimension(x, y);
0N/A }
0N/A}