Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/VariableGridLayout.java
40948 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jconsole;2627import java.awt.*;2829import javax.swing.*;3031@SuppressWarnings("serial")32public class VariableGridLayout extends GridLayout {3334private boolean fillRows, fillColumns;3536public VariableGridLayout(int rows, int cols,37int hgap, int vgap,38boolean fillRows, boolean fillColumns) {39super(rows, cols, hgap, vgap);4041this.fillRows = fillRows;42this.fillColumns = fillColumns;43}4445public void setFillRow(JComponent c, boolean b) {46c.putClientProperty("VariableGridLayout.fillRow", b);47}4849public void setFillColumn(JComponent c, boolean b) {50c.putClientProperty("VariableGridLayout.fillColumn", b);51}5253public boolean getFillRow(JComponent c) {54Boolean b = (Boolean)c.getClientProperty("VariableGridLayout.fillRow");55return (b != null) ? b : fillRows;56}5758public boolean getFillColumn(JComponent c) {59Boolean b = (Boolean)c.getClientProperty("VariableGridLayout.fillColumn");60return (b != null) ? b : fillColumns;61}6263public void layoutContainer(Container parent) {64Insets insets = parent.getInsets();65int ncomponents = parent.getComponentCount();66int nrows = getRows();67int ncols = getColumns();68int hgap = getHgap();69int vgap = getVgap();7071if (nrows > 0) {72ncols = (ncomponents + nrows - 1) / nrows;73} else {74nrows = (ncomponents + ncols - 1) / ncols;75}7677// Set heights78int x;79int y;80int nFills = 0;81boolean[] fills = new boolean[nrows];82int lastFillRow = -1;83int nComps = parent.getComponentCount();8485y = insets.top;86for (int row = 0; row < nrows; row++) {87// Find largest minimum height for this row88int h = 0;89for (int col = 0; col < ncols; col++) {90if (row * ncols + col < nComps) {91Component c = parent.getComponent(row * ncols + col);92h = Math.max(h, c.getMinimumSize().height);93}94}95// Set heights for this row96x = insets.left;97for (int col = 0; col < ncols; col++) {98if (row * ncols + col < nComps) {99JComponent c = (JComponent)parent.getComponent(row * ncols + col);100int w = c.getWidth();101c.setBounds(x, y, w, h);102x += w + hgap;103if (col == 0 && getFillRow(c)) {104fills[row] = true;105}106}107}108y += h + vgap;109if (fills[row]) {110nFills++;111lastFillRow = row;112}113}114115// Fill heights116if (nFills > 0 && y < parent.getHeight()) {117// How much height to add118int hAdd = (parent.getHeight() - y) / nFills;119int hAdded = 0;120for (int row = 0; row < nrows; row++) {121if (fills[row]) {122if (row == lastFillRow) {123// Compensate for rounding error124hAdd = parent.getHeight() - (y+hAdded);125}126for (int col = 0; col < ncols; col++) {127if (row * ncols + col < nComps) {128Component c = parent.getComponent(row * ncols + col);129Rectangle b = c.getBounds();130c.setBounds(b.x, b.y + hAdded, b.width, b.height + hAdd);131}132}133hAdded += hAdd;134}135}136}137138// Set widths139nFills = 0;140fills = new boolean[ncols];141int lastFillCol = -1;142143x = insets.left;144for (int col = 0; col < ncols; col++) {145// Find largest minimum width for this column146int w = 0;147for (int row = 0; row < nrows; row++) {148if (row * ncols + col < nComps) {149Component c = parent.getComponent(row * ncols + col);150w = Math.max(w, c.getMinimumSize().width);151}152}153// Set widths for this column154y = insets.top;155for (int row = 0; row < nrows; row++) {156if (row * ncols + col < nComps) {157JComponent c = (JComponent)parent.getComponent(row * ncols + col);158int h = c.getHeight();159c.setBounds(x, y, w, h);160y += h + vgap;161if (row == 0 && getFillColumn(c)) {162fills[col] = true;163}164}165}166x += w + hgap;167if (fills[col]) {168nFills++;169lastFillCol = col;170}171}172173// Fill widths174if (nFills > 0 && x < parent.getWidth()) {175// How much width to add176int wAdd = (parent.getWidth() - x) / nFills;177int wAdded = 0;178for (int col = 0; col < ncols; col++) {179if (fills[col]) {180if (col == lastFillCol) {181wAdd = parent.getWidth() - (x+wAdded);182}183for (int row = 0; row < nrows; row++) {184if (row * ncols + col < nComps) {185Component c = parent.getComponent(row * ncols + col);186Rectangle b = c.getBounds();187c.setBounds(b.x + wAdded, b.y, b.width + wAdd, b.height);188}189}190wAdded += wAdd;191}192}193}194}195196public Dimension preferredLayoutSize(Container parent) {197Insets insets = parent.getInsets();198int ncomponents = parent.getComponentCount();199int nrows = getRows();200int ncols = getColumns();201int hgap = getHgap();202int vgap = getVgap();203204if (nrows > 0) {205ncols = (ncomponents + nrows - 1) / nrows;206} else {207nrows = (ncomponents + ncols - 1) / ncols;208}209210int nComps = parent.getComponentCount();211212int y = insets.top;213for (int row = 0; row < nrows; row++) {214int h = 0;215for (int col = 0; col < ncols; col++) {216if (row * ncols + col < nComps) {217Component c = parent.getComponent(row * ncols + col);218h = Math.max(h, c.getMinimumSize().height);219}220}221y += h + vgap;222}223224int x = insets.left;225for (int col = 0; col < ncols; col++) {226int w = 0;227for (int row = 0; row < nrows; row++) {228if (row * ncols + col < nComps) {229Component c = parent.getComponent(row * ncols + col);230w = Math.max(w, c.getMinimumSize().width);231}232}233x += w + hgap;234}235return new Dimension(x, y);236}237}238239240