J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.tools.jconsole; |
| 27 | |
| 28 | import java.awt.*; |
| 29 | import java.util.*; |
| 30 | |
| 31 | import javax.swing.*; |
| 32 | |
| 33 | @SuppressWarnings("serial") |
| 34 | public class VariableGridLayout extends GridLayout { |
| 35 | |
| 36 | private boolean fillRows, fillColumns; |
| 37 | |
| 38 | public VariableGridLayout(int rows, int cols, |
| 39 | int hgap, int vgap, |
| 40 | boolean fillRows, boolean fillColumns) { |
| 41 | super(rows, cols, hgap, vgap); |
| 42 | |
| 43 | this.fillRows = fillRows; |
| 44 | this.fillColumns = fillColumns; |
| 45 | } |
| 46 | |
| 47 | public void setFillRow(JComponent c, boolean b) { |
| 48 | c.putClientProperty("VariableGridLayout.fillRow", b); |
| 49 | } |
| 50 | |
| 51 | public void setFillColumn(JComponent c, boolean b) { |
| 52 | c.putClientProperty("VariableGridLayout.fillColumn", b); |
| 53 | } |
| 54 | |
| 55 | public boolean getFillRow(JComponent c) { |
| 56 | Boolean b = (Boolean)c.getClientProperty("VariableGridLayout.fillRow"); |
| 57 | return (b != null) ? b : fillRows; |
| 58 | } |
| 59 | |
| 60 | public boolean getFillColumn(JComponent c) { |
| 61 | Boolean b = (Boolean)c.getClientProperty("VariableGridLayout.fillColumn"); |
| 62 | return (b != null) ? b : fillColumns; |
| 63 | } |
| 64 | |
| 65 | public void layoutContainer(Container parent) { |
| 66 | Insets insets = parent.getInsets(); |
| 67 | int ncomponents = parent.getComponentCount(); |
| 68 | int nrows = getRows(); |
| 69 | int ncols = getColumns(); |
| 70 | int hgap = getHgap(); |
| 71 | int vgap = getVgap(); |
| 72 | |
| 73 | if (nrows > 0) { |
| 74 | ncols = (ncomponents + nrows - 1) / nrows; |
| 75 | } else { |
| 76 | nrows = (ncomponents + ncols - 1) / ncols; |
| 77 | } |
| 78 | |
| 79 | // Set heights |
| 80 | int x; |
| 81 | int y; |
| 82 | int nFills = 0; |
| 83 | boolean[] fills = new boolean[nrows]; |
| 84 | int lastFillRow = -1; |
| 85 | int nComps = parent.getComponentCount(); |
| 86 | |
| 87 | y = insets.top; |
| 88 | for (int row = 0; row < nrows; row++) { |
| 89 | // Find largest minimum height for this row |
| 90 | int h = 0; |
| 91 | for (int col = 0; col < ncols; col++) { |
| 92 | if (row * ncols + col < nComps) { |
| 93 | Component c = parent.getComponent(row * ncols + col); |
| 94 | h = Math.max(h, c.getMinimumSize().height); |
| 95 | } |
| 96 | } |
| 97 | // Set heights for this row |
| 98 | x = insets.left; |
| 99 | for (int col = 0; col < ncols; col++) { |
| 100 | if (row * ncols + col < nComps) { |
| 101 | JComponent c = (JComponent)parent.getComponent(row * ncols + col); |
| 102 | int w = c.getWidth(); |
| 103 | c.setBounds(x, y, w, h); |
| 104 | x += w + hgap; |
| 105 | if (col == 0 && getFillRow(c)) { |
| 106 | fills[row] = true; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | y += h + vgap; |
| 111 | if (fills[row]) { |
| 112 | nFills++; |
| 113 | lastFillRow = row; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Fill heights |
| 118 | if (nFills > 0 && y < parent.getHeight()) { |
| 119 | // How much height to add |
| 120 | int hAdd = (parent.getHeight() - y) / nFills; |
| 121 | int hAdded = 0; |
| 122 | for (int row = 0; row < nrows; row++) { |
| 123 | if (fills[row]) { |
| 124 | if (row == lastFillRow) { |
| 125 | // Compensate for rounding error |
| 126 | hAdd = parent.getHeight() - (y+hAdded); |
| 127 | } |
| 128 | for (int col = 0; col < ncols; col++) { |
| 129 | if (row * ncols + col < nComps) { |
| 130 | Component c = parent.getComponent(row * ncols + col); |
| 131 | Rectangle b = c.getBounds(); |
| 132 | c.setBounds(b.x, b.y + hAdded, b.width, b.height + hAdd); |
| 133 | } |
| 134 | } |
| 135 | hAdded += hAdd; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Set widths |
| 141 | nFills = 0; |
| 142 | fills = new boolean[ncols]; |
| 143 | int lastFillCol = -1; |
| 144 | |
| 145 | x = insets.left; |
| 146 | for (int col = 0; col < ncols; col++) { |
| 147 | // Find largest minimum width for this column |
| 148 | int w = 0; |
| 149 | for (int row = 0; row < nrows; row++) { |
| 150 | if (row * ncols + col < nComps) { |
| 151 | Component c = parent.getComponent(row * ncols + col); |
| 152 | w = Math.max(w, c.getMinimumSize().width); |
| 153 | } |
| 154 | } |
| 155 | // Set widths for this column |
| 156 | y = insets.top; |
| 157 | for (int row = 0; row < nrows; row++) { |
| 158 | if (row * ncols + col < nComps) { |
| 159 | JComponent c = (JComponent)parent.getComponent(row * ncols + col); |
| 160 | int h = c.getHeight(); |
| 161 | c.setBounds(x, y, w, h); |
| 162 | y += h + vgap; |
| 163 | if (row == 0 && getFillColumn(c)) { |
| 164 | fills[col] = true; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | x += w + hgap; |
| 169 | if (fills[col]) { |
| 170 | nFills++; |
| 171 | lastFillCol = col; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Fill widths |
| 176 | if (nFills > 0 && x < parent.getWidth()) { |
| 177 | // How much width to add |
| 178 | int wAdd = (parent.getWidth() - x) / nFills; |
| 179 | int wAdded = 0; |
| 180 | for (int col = 0; col < ncols; col++) { |
| 181 | if (fills[col]) { |
| 182 | if (col == lastFillCol) { |
| 183 | wAdd = parent.getWidth() - (x+wAdded); |
| 184 | } |
| 185 | for (int row = 0; row < nrows; row++) { |
| 186 | if (row * ncols + col < nComps) { |
| 187 | Component c = parent.getComponent(row * ncols + col); |
| 188 | Rectangle b = c.getBounds(); |
| 189 | c.setBounds(b.x + wAdded, b.y, b.width + wAdd, b.height); |
| 190 | } |
| 191 | } |
| 192 | wAdded += wAdd; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | public Dimension preferredLayoutSize(Container parent) { |
| 199 | Insets insets = parent.getInsets(); |
| 200 | int ncomponents = parent.getComponentCount(); |
| 201 | int nrows = getRows(); |
| 202 | int ncols = getColumns(); |
| 203 | int hgap = getHgap(); |
| 204 | int vgap = getVgap(); |
| 205 | |
| 206 | if (nrows > 0) { |
| 207 | ncols = (ncomponents + nrows - 1) / nrows; |
| 208 | } else { |
| 209 | nrows = (ncomponents + ncols - 1) / ncols; |
| 210 | } |
| 211 | |
| 212 | int nComps = parent.getComponentCount(); |
| 213 | |
| 214 | int y = insets.top; |
| 215 | for (int row = 0; row < nrows; row++) { |
| 216 | int h = 0; |
| 217 | for (int col = 0; col < ncols; col++) { |
| 218 | if (row * ncols + col < nComps) { |
| 219 | Component c = parent.getComponent(row * ncols + col); |
| 220 | h = Math.max(h, c.getMinimumSize().height); |
| 221 | } |
| 222 | } |
| 223 | y += h + vgap; |
| 224 | } |
| 225 | |
| 226 | int x = insets.left; |
| 227 | for (int col = 0; col < ncols; col++) { |
| 228 | int w = 0; |
| 229 | for (int row = 0; row < nrows; row++) { |
| 230 | if (row * ncols + col < nComps) { |
| 231 | Component c = parent.getComponent(row * ncols + col); |
| 232 | w = Math.max(w, c.getMinimumSize().width); |
| 233 | } |
| 234 | } |
| 235 | x += w + hgap; |
| 236 | } |
| 237 | return new Dimension(x, y); |
| 238 | } |
| 239 | } |