1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
|
/*
* VariableGridLayout.java - a grid layout manager with variable cell sizes
* :tabSize=4:indentSize=4:noTabs=false:
*
* Originally written by Dirk Moebius for the jEdit project. This work has been
* placed into the public domain. You may use this work in any way and for any
* purpose you wish.
*
* THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
* IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES
* _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,
* OR REDISTRIBUTION OF THIS SOFTWARE.
*/
package org.gjt.sp.jedit.gui;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.util.Arrays;
/** A rectangular grid layout manager with variable cell sizes
*
* The container is divided into rectangles, and one component is placed
* in each rectangle. Each row is as large as the largest component in
* that row, and each column is as wide as the widest component in
* that column.<p>
*
* This behavior is basically the same as in
* <code>java.awt.GridLayout</code>, but with different row heights and
* column widths for each row/column.<p>
*
* For example, the following is an applet that lays out six buttons
* into three rows and two columns:<p>
*
* <blockquote><pre>
* import java.awt.*;
* import java.applet.Applet;
* public class ButtonGrid extends Applet {
* public void init() {
* setLayout(new VariableGridLayout(VariableGridLayout.FIXED_NUM_COLUMNS, 2));
* add(new Button("1"));
* add(new Button("2"));
* add(new Button("3"));
* add(new Button("4"));
* add(new Button("5"));
* add(new Button("6"));
* }
* }
* </pre></blockquote><p>
*
* <b>Programmer's remark:</b> VariableGridLayout could be faster, if it would
* reside in the package java.awt, because then it could access some
* package private fields of <code>Container</code> or
* <code>Component</code>. Instead, it has to call
* <code>Component.getSize()</code>,
* which allocates memory on the heap.<p>
*
* <b>Todo:</b>
* <ul>
* <li>Ability to span components over more than one cell horizontally and vertically.
* </ul>
*
* @author Dirk Moebius, Björn "Vampire" Kautler
* @version 1.5
* @see java.awt.GridLayout
*/
public class VariableGridLayout implements LayoutManager2, java.io.Serializable
{
public static final int FIXED_NUM_ROWS = 1;
public static final int FIXED_NUM_COLUMNS = 2;
private static enum LayoutSize { MINIMUM, MAXIMUM, PREFERRED }
/**
* Creates a variable grid layout manager with the specified mode,
* size, horizontal and vertical gap, eventually taking minimum and maximum
* sizes into account when distributing free space, depending on takeSizesIntoAccount
* and the specified distance to the borders.
*
* @param mode The mode in which to operate. Either FIXED_NUM_ROWS or FIXED_NUM_COLUMNS
* @param size The amount of rows for mode FIXED_NUM_ROWS or the amount of columns for mode FIXED_NUM_COLUMNS (>0)
* @param hgap The horizontal space between cells (>=0)
* @param vgap The vertical space between cells (>=0)
* @param takeSizesIntoAccount Whether to take minimum and maximum sizes into account when distributing free space
* Javier Diaz Soto (jbds) warns in #2997417 that this may cause gui freeze and provides a patch
* @param distanceToBorders The distances to the borders
* @throws IllegalArgumentException if mode is not either FIXED_NUM_ROWS or FIXED_NUM_COLUMNS or size is <= 0 or hgap or vgap is < 0
*/
public VariableGridLayout(int mode, int size, int hgap, int vgap, boolean takeSizesIntoAccount, Insets distanceToBorders)
{
if (mode != FIXED_NUM_ROWS && mode != FIXED_NUM_COLUMNS)
{
throw new IllegalArgumentException("illegal mode; value is " + mode);
}
if (size <= 0)
{
throw new IllegalArgumentException("size cannot be zero or less; value is " + size);
}
if (hgap < 0)
{
throw new IllegalArgumentException("hgap cannot be negative; value is " + hgap);
}
if (vgap < 0)
{
throw new IllegalArgumentException("vgap cannot be negative; value is " + vgap);
}
this.mode = mode;
this.size = size;
this.hgap = hgap;
this.vgap = vgap;
this.takeSizesIntoAccount = takeSizesIntoAccount;
this.distanceToBorders = (Insets)distanceToBorders.clone();
}
/**
* Creates a variable grid layout manager with the specified mode,
* size, horizontal and vertical gap, eventually taking minimum and maximum
* sizes into account when distributing free space, depending on takeSizesIntoAccount
* and zero distance to borders.
*
* @param mode The mode in which to operate. Either FIXED_NUM_ROWS or FIXED_NUM_COLUMNS
* @param size The amount of rows for mode FIXED_NUM_ROWS or the amount of columns for mode FIXED_NUM_COLUMNS (>0)
* @param hgap The horizontal space between cells (>=0)
* @param vgap The vertical space between cells (>=0)
* @param takeSizesIntoAccount Whether to take minimum and maximum sizes into account when distributing free space
* Javier Diaz Soto (jbds) warns in #2997417 that this may cause gui freeze and provides a patch
* @throws IllegalArgumentException if mode is not either FIXED_NUM_ROWS or FIXED_NUM_COLUMNS or size is <= 0 or hgap or vgap is < 0
*/
public VariableGridLayout(int mode, int size, int hgap, int vgap, boolean takeSizesIntoAccount)
{
this(mode, size, hgap, vgap, takeSizesIntoAccount, new Insets(0,0,0,0));
}
/**
* Creates a variable grid layout manager with the specified mode,
* size, horizontal and vertical gap, and zero distance to borders.
* The minimum and maximum Component sizes are not taken into account
* when distributing free space.
*
* @param mode The mode in which to operate. Either FIXED_NUM_ROWS or FIXED_NUM_COLUMNS
* @param size The amount of rows for mode FIXED_NUM_ROWS or the amount of columns for mode FIXED_NUM_COLUMNS
* @param hgap The horizontal space between cells
* @param vgap The vertical space between cells
* @throws IllegalArgumentException if mode is not either FIXED_NUM_ROWS or FIXED_NUM_COLUMNS or size is<= 0 or hgap or vgap is < 0
*/
public VariableGridLayout(int mode, int size, int hgap, int vgap)
{
this(mode, size, hgap, vgap, false, new Insets(0,0,0,0));
}
/**
* Creates a variable grid layout manager with the specified mode
* and size, zero horizontal and vertical gap, and zero distance to borders.
* Does not take minimum and maximum Component sizes into account when distributing
* free space.
*
* @param mode The mode in which to operate. Either FIXED_NUM_ROWS or FIXED_NUM_COLUMNS
* @param size The amount of rows for mode FIXED_NUM_ROWS or the amount of columns for mode FIXED_NUM_COLUMNS
* @throws IllegalArgumentException if mode is not either FIXED_NUM_ROWS or FIXED_NUM_COLUMNS or size is <= 0
*/
public VariableGridLayout(int mode, int size)
{
this(mode, size, 0, 0, false, new Insets(0,0,0,0));
}
/**
* Creates a variable grid layout manager with mode FIXED_NUM_ROWS,
* number of rows == 1, zero horizontal and vertical gap, and zero distance to borders.
* Does not take minimum and maximum Component sizes into account when
* distributing free space.
*/
public VariableGridLayout()
{
this(FIXED_NUM_ROWS, 1, 0, 0, false, new Insets(0,0,0,0));
}
/**
* Not used in this class.
*/
public void addLayoutComponent(String name, Component component)
{
}
/**
* Not used in this class.
*/
public void addLayoutComponent(Component component, Object constraints)
{
}
/**
* Not used in this class.
*/
public void removeLayoutComponent(Component component)
{
}
/**
* Always returns 0.5.
*/
public float getLayoutAlignmentX(Container container)
{
return 0.5f;
}
/**
* Always returns 0.5.
*/
public float getLayoutAlignmentY(Container container)
{
return 0.5f;
}
public Dimension preferredLayoutSize(Container parent)
{
return getLayoutSize(parent,LayoutSize.PREFERRED);
}
public Dimension minimumLayoutSize(Container parent)
{
return getLayoutSize(parent,LayoutSize.MINIMUM);
}
public Dimension maximumLayoutSize(Container parent)
{
return getLayoutSize(parent,LayoutSize.MAXIMUM);
}
public void layoutContainer(Container parent)
{
synchronized (parent.getTreeLock())
{
update(parent);
int ncomponents = parent.getComponentCount();
if (ncomponents == 0)
{
return;
}
// Pass 1: compute minimum, preferred and maximum row heights / column widths
int total_height = 0;
Arrays.fill(row_heights,0);
Arrays.fill(col_widths,0);
if (takeSizesIntoAccount)
{
Arrays.fill(minimum_row_heights,0);
Arrays.fill(minimum_col_widths,0);
Arrays.fill(maximum_row_heights,Integer.MAX_VALUE);
Arrays.fill(maximum_col_widths,Integer.MAX_VALUE);
}
for (int r = 0, i = 0; r < nrows; r++)
{
for (int c = 0; c < ncols; c++, i++)
{
if (i < ncomponents)
{
Component comp = parent.getComponent(i);
Dimension d = comp.getPreferredSize();
row_heights[r] = Math.max(row_heights[r], d.height);
col_widths[c] = Math.max(col_widths[c], d.width);
if (takeSizesIntoAccount)
{
d = comp.getMinimumSize();
minimum_row_heights[r] = Math.max(minimum_row_heights[r], d.height);
minimum_col_widths[c] = Math.max(minimum_col_widths[c], d.width);
d = comp.getMaximumSize();
maximum_row_heights[r] = Math.min(maximum_row_heights[r], d.height);
maximum_col_widths[c] = Math.min(maximum_col_widths[c], d.width);
}
}
else
{
break;
}
}
if (takeSizesIntoAccount)
{
// correct cases where
// minimum_row_heights[row] <= row_heights[row] <= maximum_row_heights[row]
// is not true by clipping to the minimum_row_heights and maximum_row_heights
if (minimum_row_heights[r] >= maximum_row_heights[r])
{
maximum_row_heights[r] = minimum_row_heights[r];
row_heights[r] = minimum_row_heights[r];
}
else if (row_heights[r] < minimum_row_heights[r])
{
row_heights[r] = minimum_row_heights[r];
}
else if (row_heights[r] > maximum_row_heights[r])
{
row_heights[r] = maximum_row_heights[r];
}
}
total_height += row_heights[r];
}
int total_width = 0;
for (int c = 0; c < ncols; c++)
{
if (takeSizesIntoAccount)
{
// correct cases where
// minimum_col_widths[col] <= col_widths[col] <= maximum_col_widths[col]
// is not true by clipping to the minimum_col_widths and maximum_col_widths
if (minimum_col_widths[c] >= maximum_col_widths[c])
{
maximum_col_widths[c] = minimum_col_widths[c];
col_widths[c] = minimum_col_widths[c];
}
else if (col_widths[c] < minimum_col_widths[c])
{
col_widths[c] = minimum_col_widths[c];
}
else if (col_widths[c] > maximum_col_widths[c])
{
col_widths[c] = maximum_col_widths[c];
}
}
total_width += col_widths[c];
}
// Pass 2: redistribute free space
Dimension parent_size = parent.getSize();
Insets insets = parent.getInsets();
int free_height = parent_size.height
- insets.top - insets.bottom
- (nrows - 1) * vgap
- distanceToBorders.top - distanceToBorders.bottom;
int free_width = parent_size.width
- insets.left - insets.right
- (ncols - 1) * hgap
- distanceToBorders.left - distanceToBorders.right;
redistributeSpace(total_height,free_height,
takeSizesIntoAccount,
nrows,row_heights,
minimum_row_heights,
maximum_row_heights);
redistributeSpace(total_width,free_width,
takeSizesIntoAccount,
ncols,col_widths,
minimum_col_widths,
maximum_col_widths);
// Pass 3: layout components
for (int r = 0, y = insets.top + distanceToBorders.top, i = 0; r < nrows; y += row_heights[r] + vgap, r++)
{
for (int c = 0, x = insets.left + distanceToBorders.left; c < ncols; x += col_widths[c] + hgap, c++, i++)
{
if (i < ncomponents)
{
Component comp = parent.getComponent(i);
Dimension d = comp.getMaximumSize();
int width = col_widths[c];
int height = row_heights[r];
int xCorrection = 0;
int yCorrection = 0;
if (width > d.width)
{
xCorrection = (int)((width - d.width) * comp.getAlignmentX());
width = d.width;
}
if (height > d.height)
{
yCorrection = (int)((height-d.height) * comp.getAlignmentY());
height = d.height;
}
comp.setBounds(x + xCorrection, y + yCorrection, width, height);
}
}
}
} // synchronized
}
public void invalidateLayout(Container container)
{
}
/**
* Returns the string representation of this variable grid layout's values.
* @return a string representation of this variable grid layout.
*/
public String toString()
{
return getClass().getName() + "[mode="
+ ((FIXED_NUM_ROWS == mode) ? "FIXED_NUM_ROWS"
: ((FIXED_NUM_COLUMNS == mode) ? "FIXED_NUM_COLUMNS"
: "UNKNOWN(" + mode + ")")) + ",size=" + size
+ ",hgap=" + hgap + ",vgap=" + vgap
+ ",takeSizesIntoAccount=" + takeSizesIntoAccount
+ ",distanceToBorders=" + distanceToBorders + "]";
}
/**
* @param which if LayoutSize.MINIMUM compute minimum layout size,
* if LayoutSize.MAXIMUM compute maximum layout size,
* if LayoutSize.PREFERRED compute preferred layout size.
*/
private Dimension getLayoutSize(Container parent, LayoutSize which)
{
synchronized (parent.getTreeLock())
{
update(parent);
int ncomponents = parent.getComponentCount();
long h = 0;
long w = 0;
for (int r = 0, i = 0; r < nrows; r++)
{
int row_height = 0;
for (int c = 0; c < ncols; c++, i++)
{
if (i < ncomponents)
{
switch (which)
{
case MINIMUM:
row_height = Math.max(row_height, parent.getComponent(i).getMinimumSize().height);
break;
case MAXIMUM:
row_height = Math.max(row_height, parent.getComponent(i).getMaximumSize().height);
break;
case PREFERRED:
row_height = Math.max(row_height, parent.getComponent(i).getPreferredSize().height);
break;
default:
throw new InternalError("Missing case branch for LayoutSize: " + which);
}
}
}
h += row_height;
}
for (int c = 0; c < ncols; c++)
{
int col_width = 0;
for (int r = 0; r < nrows; r++)
{
int i = r * ncols + c;
if (i < ncomponents)
{
switch (which)
{
case MINIMUM:
col_width = Math.max(col_width, parent.getComponent(i).getMinimumSize().width);
break;
case MAXIMUM:
col_width = Math.max(col_width, parent.getComponent(i).getMaximumSize().width);
break;
case PREFERRED:
col_width = Math.max(col_width, parent.getComponent(i).getPreferredSize().width);
break;
default:
throw new InternalError("Missing case branch for LayoutSize: " + which);
}
}
}
w += col_width;
}
Insets insets = parent.getInsets();
w += insets.left + insets.right + ((ncols - 1) * hgap) + distanceToBorders.left + distanceToBorders.right;
h += insets.top + insets.bottom + ((nrows - 1) * vgap) + distanceToBorders.top + distanceToBorders.bottom;
if (w > Integer.MAX_VALUE)
{
w = Integer.MAX_VALUE;
}
if (h > Integer.MAX_VALUE)
{
h = Integer.MAX_VALUE;
}
return new Dimension((int)w,(int)h);
}
}
private void update(Container container)
{
int ncomponents = container.getComponentCount();
int old_nrows = nrows;
int old_ncols = ncols;
if (this.mode == FIXED_NUM_ROWS)
{
nrows = this.size;
ncols = (ncomponents + nrows - 1) / nrows;
}
else
{
ncols = this.size;
nrows = (ncomponents + ncols - 1) / ncols;
}
if (old_nrows != nrows)
{
row_heights = new int[nrows];
if (takeSizesIntoAccount)
{
minimum_row_heights = new int[nrows];
maximum_row_heights = new int[nrows];
}
}
if (old_ncols != ncols)
{
col_widths = new int[ncols];
if (takeSizesIntoAccount)
{
minimum_col_widths = new int[ncols];
maximum_col_widths = new int[ncols];
}
}
}
private void redistributeSpace(int total_size, int free_size, boolean takeSizesIntoAccount,
int nelements, int[] element_sizes,
int[] minimum_element_sizes, int[] maximum_element_sizes)
{
if (total_size != free_size)
{
if (takeSizesIntoAccount)
{
boolean grow = total_size < free_size;
// calculate the size that is available for redistribution
free_size = (free_size - total_size) * (grow ? 1 : -1);
while (free_size != 0)
{
// calculate the amount of elements that can be resized without violating
// the minimum and maximum sizes and their current cumulated size
int modifyableAmount = 0;
int modifySize = 0;
for (int i = 0 ; i < nelements ; i++)
{
if ((grow && (element_sizes[i] < maximum_element_sizes[i])) ||
(!grow && (element_sizes[i] > minimum_element_sizes[i])))
{
modifyableAmount++;
modifySize += element_sizes[i];
}
}
boolean checkBounds = true;
// if all elements are at their minimum or maximum size, resize all elements
if (0 == modifyableAmount)
{
for (int i = 0 ; i < nelements ; i++)
{
modifySize += element_sizes[i];
}
checkBounds = false;
modifyableAmount = nelements;
}
// to prevent an endless loop if the container gets resized to a very small amount
if (modifySize == 0)
{
break;
}
// resize the elements
if (free_size < modifyableAmount)
{
for (int i = 0 ; i < nelements ; i++)
{
if ((free_size != 0) &&
(!checkBounds ||
(checkBounds &&
(grow && (element_sizes[i] < maximum_element_sizes[i])) ||
(!grow && (element_sizes[i] > minimum_element_sizes[i])))))
{
element_sizes[i] += (grow ? 1 : -1);
if (0 > element_sizes[i])
{
element_sizes[i] = 0;
}
free_size--;
}
}
}
else
{
int modifySizeAddition = 0;
for (int i = 0 ; i < nelements ; i++)
{
int modifyableSize = (checkBounds ? (grow ? maximum_element_sizes[i] - element_sizes[i] : element_sizes[i] - minimum_element_sizes[i]) : Integer.MAX_VALUE - element_sizes[i]);
int elementModifySize = (int)((double)free_size / (double)modifySize * (double)element_sizes[i]);
if (elementModifySize <= modifyableSize)
{
element_sizes[i] += (grow ? elementModifySize : -elementModifySize);
modifySizeAddition += (grow ? elementModifySize : -elementModifySize);
free_size -= elementModifySize;
}
else
{
element_sizes[i] += (grow ? modifyableSize : -modifyableSize);
modifySizeAddition += (grow ? modifyableSize : -modifyableSize);
free_size -= modifyableSize;
}
if (0 > element_sizes[i])
{
element_sizes[i] = 0;
}
}
modifySize += modifySizeAddition;
}
}
}
else
{
double d = (double)free_size / (double)total_size;
for (int i = 0; i < nelements; i++)
{
element_sizes[i] = (int)(element_sizes[i] * d);
}
}
}
}
private int mode;
private int size;
private int hgap;
private int vgap;
private boolean takeSizesIntoAccount;
private Insets distanceToBorders;
private transient int nrows = -1;
private transient int ncols = -1;
private transient int[] minimum_row_heights = null;
private transient int[] minimum_col_widths = null;
private transient int[] row_heights = null;
private transient int[] col_widths = null;
private transient int[] maximum_row_heights = null;
private transient int[] maximum_col_widths = null;
}
|