1 19 24 package org.openide.explorer.propertysheet; 25 26 import java.awt.Component ; 27 import java.awt.Container ; 28 import java.awt.Dimension ; 29 import java.awt.Insets ; 30 import java.awt.LayoutManager ; 31 import java.awt.Toolkit ; 32 33 import java.util.Arrays ; 34 import java.util.Comparator ; 35 36 37 52 class AutoGridLayout implements LayoutManager { 53 int gapY = 5; 54 boolean pack; 55 56 public AutoGridLayout(boolean pack) { 57 this.pack = pack; 58 } 59 60 public void addLayoutComponent(String name, Component comp) { 61 } 63 64 public void removeLayoutComponent(Component comp) { 65 } 67 68 private Comparator <Component > comparator() { 69 return new PreferredSizeComparator(pack); 70 } 71 72 public void layoutContainer(Container parent) { 73 Component [] c = parent.getComponents(); 74 75 if (c.length > 3) { 76 Arrays.sort(c, comparator()); 77 } 78 79 if (c.length == 2) { 80 Dimension d0 = c[0].getPreferredSize(); 84 Dimension d1 = c[1].getPreferredSize(); 85 c[0].setBounds(0, 0, d0.width, d0.height); 86 c[1].setBounds(d0.width, 0, d1.width, d1.height); 87 88 return; 89 } 90 91 Insets insets = parent.getInsets(); 92 93 int w = parent.getWidth() - insets.right; 94 int h = parent.getHeight() - insets.bottom; 95 int currx = insets.left; 96 int curry = insets.top; 97 boolean done = false; 98 int cols = -1; 99 100 for (int i = 0; i < c.length; i++) { 106 Dimension d = c[i].getPreferredSize(); 107 108 if ((d.width == 0) || (d.height == 0)) { 109 d = PropUtils.getMinimumPanelSize(); 112 } 113 114 if ((currx + d.width) > w) { 115 curry += (d.height + gapY); 116 currx = insets.left; 117 118 if (cols == -1) { 119 cols = i; 120 121 break; 122 } 123 } 124 125 c[i].setBounds(currx, curry, d.width, d.height); 126 currx += d.width; 127 } 128 129 if (cols == -1) { 130 cols = c.length; 131 } 132 133 int currCol = 0; 134 135 for (int i = cols; i < c.length; i++) { 136 Dimension d = c[i].getPreferredSize(); 137 138 if ((currx + d.width) > w) { 139 curry += (d.height + gapY); 143 currx = insets.left; 144 currCol = 0; 145 } 146 147 done = (curry + d.height) > h; 149 150 if (!done) { 151 int currColWidth = c[currCol].getWidth(); 153 154 if (d.width <= w) { 156 157 while (currColWidth <= d.width) { 159 currCol++; 160 161 if (currCol > cols) { 162 currCol = 0; 164 curry += (d.height + gapY); 165 currx = insets.left; 166 currColWidth = 0; 167 } 168 169 currColWidth += c[currCol].getWidth(); 172 } 173 174 c[i].setBounds(currx, curry, d.width, d.height); 175 currx += currColWidth; 176 } else { 177 c[i].setBounds(currx, curry, d.width, d.height); 179 currx += d.width; 180 181 } 183 184 if (currx > w) { 185 currx = insets.left; 188 curry += (d.height + gapY); 189 currCol = 0; 190 } else { 191 currCol++; 192 } 193 } else { 194 c[i].setBounds(0, 0, 0, 0); 197 } 198 } 199 } 200 201 public Dimension minimumLayoutSize(java.awt.Container parent) { 202 return preferredLayoutSize(parent); 203 } 204 205 public Dimension preferredLayoutSize(java.awt.Container parent) { 206 Component [] c = parent.getComponents(); 207 208 if (c.length > 3) { 209 Arrays.sort(c, comparator()); 210 } 211 212 Dimension max = Toolkit.getDefaultToolkit().getScreenSize(); 213 max.width /= 2; 214 max.height /= 2; 215 216 Insets insets = parent.getInsets(); 217 218 int w = max.width - insets.right; 219 220 int currx = insets.left; 221 int cols = -1; 222 int baseHeight = 0; 223 Dimension [] dims = new Dimension [c.length]; 224 Dimension result = new Dimension (); 225 226 for (int i = 0; i < c.length; i++) { 228 dims[i] = c[i].getPreferredSize(); 229 230 if ((dims[i].width == 0) || (dims[i].height == 0)) { 231 dims[i] = PropUtils.getMinimumPanelSize(); 234 } 235 236 baseHeight = Math.max(baseHeight, dims[i].height); 237 238 if (cols == -1) { 239 if ((currx + dims[i].width) > w) { 240 result.width = currx; 241 cols = i; 242 } 243 } 244 245 if (cols != -1) { 246 result.width = Math.max(result.width, dims[i].width + insets.left + insets.right); 249 } 250 251 currx += dims[i].width; 252 } 253 254 if (cols == -1) { cols = c.length; 256 result.width = currx; 257 } 258 259 if (!pack && (c.length > 3)) { 260 int rows = (c.length / cols) + (((c.length % cols) != 0) ? 1 : 0); 262 result.height = (baseHeight * rows) + (gapY * rows) + insets.top + insets.bottom; 263 result.width += 6; 264 assert (result.width >= 0) && (result.height >= 0); 265 266 return result; 267 } 268 269 int currRow = 0; 270 int currCol = 0; 271 currx = insets.left; 272 273 for (int i = cols; i < c.length; i++) { 276 int colspan = 1; 277 int colwidth = dims[currCol].width; 278 279 while (dims[i].width > colwidth) { 280 currCol++; 281 colwidth += dims[currCol].width; 282 colspan++; 283 284 if ((colwidth + currx) > max.width) { 285 currCol = 0; 286 currRow++; 287 colspan = 1; 288 colwidth = dims[currCol].width; 289 } 290 } 291 292 currCol += colspan; 293 currx += colwidth; 294 295 if ((currCol > cols) && (i != (c.length - 1))) { 296 currCol = 0; 297 currRow++; 298 currx = insets.left; 299 } 300 } 301 302 result.height = (baseHeight * currRow) + insets.top + insets.bottom + (gapY * currRow); 303 304 return result; 305 } 306 307 private static final class PreferredSizeComparator implements Comparator <Component > { 308 boolean smallFirst; 309 310 public PreferredSizeComparator(boolean smallFirst) { 311 this.smallFirst = smallFirst; 312 } 313 314 public int compare(Component c1, Component c2) { 315 Dimension d1 = c1.getPreferredSize(); 316 Dimension d2 = c2.getPreferredSize(); 317 318 return smallFirst ? (d1.width - d2.width) : (d2.width - d1.width); 319 } 320 } 321 } 322 | Popular Tags |