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 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
|
/*
* GridLayout2.java
*/
package org.tigris.swidgets;
import java.awt.*;
/**
* Same as the standard java class GridLayout but allows more
* flexability for sizing of columns and rows.
*
* @author Bob Tarling
*/
public class GridLayout2 implements LayoutManager, java.io.Serializable {
/**
* Do not resize the child components.
*/
public static final int NONE = 0;
/**
* Resize all child components to fit their cell both
* horizontally and vertically.
*/
public static final int BOTH = 1;
/**
* Resize all child components to fit their cell horizontally
* but not vertically.
*/
public static final int HORIZONTAL = 2;
/**
* Resize all child components to fit their cell vertically but
* not horizontally.
*/
public static final int VERTICAL = 3;
/**
* Put the component in the center of its display area.
*/
public static final int CENTER = 10;
/**
* Put the component at the top of its display area,
* centered horizontally.
*/
public static final int NORTH = 11;
/**
* Put the component at the top-right corner of its display area.
*/
public static final int NORTHEAST = 12;
/**
* Put the component on the right side of its display area,
* centered vertically.
*/
public static final int EAST = 13;
/**
* Put the component at the bottom-right corner of its display area.
*/
public static final int SOUTHEAST = 14;
/**
* Put the component at the bottom of its display area, centered
* horizontally.
*/
public static final int SOUTH = 15;
/**
* Put the component at the bottom-left corner of its display area.
*/
public static final int SOUTHWEST = 16;
/**
* Put the component on the left side of its display area,
* centered vertically.
*/
public static final int WEST = 17;
/**
* Put each component in the top-left corner of its display area.
*/
public static final int NORTHWEST = 18;
/**
* This is the horizontal gap (in pixels) which specifies the space
* between columns. They can be changed at any time.
* This should be a non negative integer.
*
* @serial
* @see #getHgap()
* @see #setHgap(int)
*/
private int hgap;
/**
* This is the vertical gap (in pixels) which specifies the space
* between rows. They can be changed at any time.
* This should be a non negative integer.
*
* @serial
* @see #getVgap()
* @see #setVgap(int)
*/
private int vgap;
/**
* This is the number of rows specified for the grid. The number
* of rows can be changed at any time.
* This should be a non negative integer, where '0' means
* 'any number' meaning that the number of Rows in that
* dimension depends on the other dimension.
*
* @serial
* @see #getRows()
* @see #setRows(int)
*/
private int rows;
/**
* This is the number of columns specified for the grid. The number
* of columns can be changed at any time.
* This should be a non negative integer, where '0' means
* 'any number' meaning that the number of Columns in that
* dimension depends on the other dimension.
*
* @serial
* @see #getColumns()
* @see #setCols(int)
*/
private int cols;
/**
* Size all cells as the largest prefered width and height
* component. <p>
*
* A possible value for the cellSizing parameter of the
* constructor in order to size cells so that each have the same
* width and height. The width is the largest prefered width and
* the height is the largest prefered height of all these child
* components.
*/
public static final int MAXPREFERRED = 20;
/**
* Size all cells so that all in the same row are the same height
* and all in the same column are the same width.<p>
*
* A possible value for the cellSizing parameter of the
* constructor. The width is the largest prefered width of all
* components in the same column and the height is the largest
* prefered height of all components in the same row.
*/
public static final int ROWCOLPREFERRED = 21;
/**
* Size all cells as the same width and height to fit the parent
* component. <p>
*
* A possible value for the cellSizing parameter of the
* constructor in order to size cells so that each has the same
* height and width and are sized to fit their parent. This
* emulates the sizing done by a standard GridLayout.
*/
public static final int FITPARENT = 22;
private int cellSizing = FITPARENT;
private int fill = BOTH;
private int anchor = WEST;
/**
* The height of the child component with the largest height
*/
private int largestHeight;
/**
* The width of the child component with the largest width
*/
private int largestWidth;
/**
* The required cell width of each column
*/
private int[] colWidth;
/**
* The required cell height of each row
*/
private int[] rowHeight;
/**
* Construct a new GridLayout2 with a default of one column per
* component, in a single row.
*/
public GridLayout2() {
this(1, 0, 0, 0);
}
/**
* Construct a new GridLayout2 with the specified number of rows
* and columns.
*
* @param r the number of rows in the layout
* @param c the number of columns in the layout
*/
public GridLayout2(int r, int c) {
this(r, c, 0, 0);
}
/**
* Construct a new GridLayout2 with the specified number of rows
* and columns and cell spacing.
*
* @param r the number of rows in the layout
* @param c the number of columns in the layout
* @param h the horizontal gap between cells
* @param v the vertical gap between cells
*/
public GridLayout2(int r, int c, int h, int v) {
if ((r == 0) && (c == 0)) {
throw new IllegalArgumentException("rows and cols cannot "
+ "both be zero");
}
this.rows = r;
this.cols = c;
this.hgap = h;
this.vgap = v;
}
/**
* Construct a new GridLayout2 with the specified number of rows
* and columns and cell sizing scheme.
*
* @param r the number of rows in the layout
* @param c the number of columns in the layout
* @param cs the required cell sizing scheme
*/
public GridLayout2(int r, int c, int cs) {
this(r, c, 0, 0, cs);
}
/**
* Construct a new GridLayout2 with the specified number of rows
* and columns, cell spacing and cell sizing scheme.
*
* @param r the number of rows in the layout
* @param c the number of columns in the layout
* @param h the horizontal gap between cells
* @param v the vertical gap between cells
* @param cs the required cell sizing scheme
*/
public GridLayout2(int r, int c, int h, int v, int cs) {
this(r, c, h, v);
this.cellSizing = cs;
}
/**
* Construct a new GridLayout2 with the specified number of rows
* and columns, cell spacing, cell sizing scheme and filling
* scheme.
*
* @param r the number of rows in the layout
* @param c the number of columns in the layout
* @param h the horizontal gap between cells
* @param v the vertical gap between cells
* @param cs the required cell sizing scheme
* @param f the required cell filling scheme
*/
public GridLayout2(int r, int c, int h, int v,
int cs, int f)
{
this(r, c, h, v, cs);
this.fill = f;
}
/**
* Construct a new GridLayout2 with the specified number of rows
* and columns, cell spacing, cell sizing scheme and component
* sizing and anchoring scheme.
*
* @param r the number of rows in the layout
* @param c the number of columns in the layout
* @param h the horizontal gap between cells
* @param v the vertical gap between cells
* @param cs the required cell sizing scheme
* @param f the required cell filling scheme
* @param a the required anchoring of a child component within its cell
*/
public GridLayout2(int r, int c, int h, int v,
int cs, int f, int a) {
this(r, c, h, v, cs, f);
this.anchor = a;
}
/**
* Adds the specified component with the specified name to the
* layout. This is included to satisfy the LayoutManager interface
* but is not actually used in this layout implementation.
*
* @param name the name of the component
* @param comp the component to be added
*/
public void addLayoutComponent(String name, Component comp) {
}
/**
* Removes the specified component with the specified name from
* the layout. This is included to satisfy the LayoutManager
* interface but is not actually used in this layout
* implementation.<p>
*
* @param comp the name of the component
*/
public void removeLayoutComponent(Component comp) {
}
/**
* Determines the preferred size of the container argument using
* this grid layout. The preferred size of a grid layout is
* dependant on the cellSizing scheme.<p>
*
* MAXPREFERRED and FITPARENT use the same formula to calculate
* prefered size.<p>
*
* The prefered width using MAXPREFERRED or FITPARENT is the
* largest preferred width of any of the widths in the container
* times the number of columns, plus the horizontal padding times
* the number of columns plus one, plus the left and right insets
* of the target container.<p>
*
* The preferred height using MAXPREFERRED or FITPARENT is the
* largest preferred height of any of the heights in the container
* times the number of rows, plus the vertical padding times the
* number of rows plus one, plus the top and bottom insets of the
* target container.<p>
*
* The prefered width using ROWCOLPREFERRED is the largest
* preferred is the sum of the widths of of all columns, plus the
* horizontal padding times the number of columns plus one, plus
* the left and right insets of the target container.<p>
*
* The prefered height using ROWCOLPREFERRED is the largest
* preferred is the sum of the heights of of all columns, plus the
* horizontal padding times the number of columns plus one, plus
* the left and right insets of the target container.
*
* @param parent the container to be laid out
* @return the preferred dimensions to lay out the subcomponents
* of the specified container
*/
public Dimension preferredLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
int componentCount = parent.getComponentCount();
int actualRows = rows;
int actualCols = cols;
if (actualRows > 0) {
actualCols = (componentCount + actualRows - 1) / actualRows;
} else {
actualRows = (componentCount + actualCols - 1) / actualCols;
}
colWidth = new int[actualCols];
rowHeight = new int[actualRows];
int largestPreferredWidth = 0;
int largestPreferredHeight = 0;
for (int c = 0; c < actualCols; ++c) {
for (int r = 0; r < actualRows; ++r) {
int i = r * actualCols + c;
if (i < componentCount) {
if (parent.getComponent(i).getPreferredSize().getWidth()
> colWidth[c]) {
colWidth[c] =
(int) parent.getComponent(i)
.getPreferredSize().getWidth();
if (colWidth[c] > largestPreferredWidth)
largestPreferredWidth = colWidth[c];
}
if (parent.getComponent(i)
.getPreferredSize().getHeight()
> rowHeight[r])
{
rowHeight[r] =
(int) parent.getComponent(i)
.getPreferredSize().getHeight();
if (rowHeight[r] > largestPreferredHeight)
largestPreferredHeight = rowHeight[r];
}
}
}
}
return calculateSizes(parent, colWidth, rowHeight,
actualRows, actualCols,
largestPreferredWidth,
largestPreferredHeight);
}
}
/**
* Required by LayoutManager.
*
* @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
*/
public Dimension minimumLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
int componentCount = parent.getComponentCount();
int actualRows = rows;
int actualCols = cols;
if (actualRows > 0) {
actualCols = (componentCount + actualRows - 1) / actualRows;
} else {
actualRows = (componentCount + actualCols - 1) / actualCols;
}
colWidth = new int[actualCols];
rowHeight = new int[actualRows];
int largestMinimumWidth = 0;
int largestMinimumHeight = 0;
for (int c = 0; c < actualCols; ++c) {
for (int r = 0; r < actualRows; ++r) {
int i = r * actualCols + c;
if (i < componentCount) {
if (parent.getComponent(i).getMinimumSize().getWidth()
> colWidth[c])
{
colWidth[c] =
(int) parent.getComponent(i)
.getMinimumSize().getWidth();
if (colWidth[c] > largestMinimumWidth)
largestMinimumWidth = colWidth[c];
}
if (parent.getComponent(i).getMinimumSize().getHeight()
> rowHeight[r]) {
rowHeight[r] =
(int) parent.getComponent(i)
.getMinimumSize().getHeight();
if (rowHeight[r] > largestMinimumHeight)
largestMinimumHeight = rowHeight[r];
}
}
}
}
return calculateSizes(parent, colWidth, rowHeight,
actualRows, actualCols,
largestMinimumWidth, largestMinimumHeight);
}
}
/**
* @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
*/
public void layoutContainer(Container parent) {
synchronized (parent.getTreeLock()) {
int ncomponents = parent.getComponentCount();
if (ncomponents == 0) {
return;
}
Insets insets = parent.getInsets();
int nrows = this.rows;
int ncols = this.cols;
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
} else {
nrows = (ncomponents + ncols - 1) / ncols;
}
colWidth = new int[ncols];
rowHeight = new int[nrows];
largestWidth = 0;
largestHeight = 0;
if (cellSizing == FITPARENT) {
int availableWidth =
parent.getWidth()
- (insets.left + insets.right + (ncols - 1) * hgap);
int availableHeight =
parent.getHeight()
- (insets.top + insets.bottom + (nrows - 1) * vgap);
largestWidth = availableWidth / ncols;
largestHeight = availableHeight / nrows;
}
else {
for (int c = 0; c < ncols; ++c) {
for (int r = 0; r < nrows; ++r) {
int i = r * ncols + c;
if (i < ncomponents) {
if (parent.getComponent(i)
.getPreferredSize().getWidth()
> colWidth[c])
{
colWidth[c] =
(int) parent.getComponent(i)
.getPreferredSize().getWidth();
if (colWidth[c] > largestWidth)
largestWidth = colWidth[c];
}
if (parent.getComponent(i)
.getPreferredSize().getHeight()
> rowHeight[r])
{
rowHeight[r] =
(int) parent.getComponent(i)
.getPreferredSize().getHeight();
if (rowHeight[r] > largestHeight)
largestHeight = rowHeight[r];
}
}
}
}
}
int cellWidth;
int cellHeight;
for (int c = 0, x = insets.left;
c < ncols;
x += cellWidth + hgap, ++c)
{
cellWidth = getComponentCellWidth(c);
for (int r = 0, y = insets.top;
r < nrows;
y += cellHeight + vgap, ++r)
{
cellHeight = getComponentCellHeight(r);
int i = r * ncols + c;
if (i < ncomponents) {
positionComponentInCell(parent.getComponent(i), x, y,
cellWidth, cellHeight);
}
}
}
}
}
/**
* Calculate the layout size.
*
* @param parent the container
* @param theColWidth the column width
* @param theRowHeight the row height
* @param actualRows the number of rows specified for the grid.
* This should be a non negative integer, where '0' means
* 'any number' meaning that the number of rows in that
* dimension depends on the other dimension.
* @param actualCols the number of columns specified for the grid.
* This should be a non negative integer, where '0' means
* 'any number' meaning that the number of columns in that
* dimension depends on the other dimension.
* @param theLargestWidth the largest width
* @param theLargestHeight the largest height
* @return the dimension of the layout
*/
protected Dimension calculateSizes(Container parent,
int theColWidth[], int theRowHeight[],
int actualRows, int actualCols,
int theLargestWidth,
int theLargestHeight)
{
int w = 0;
int h = 0;
if (cellSizing == GridLayout2.ROWCOLPREFERRED) {
for (int c = 0; c < actualCols; ++c) w += theColWidth[c];
for (int r = 0; r < actualRows; ++r) h += theRowHeight[r];
}
else {
w = theLargestWidth * actualCols;
h = theLargestHeight * actualRows;
}
Insets insets = parent.getInsets();
return new Dimension(insets.left + insets.right + w
+ (actualCols - 1) * hgap,
insets.top + insets.bottom + h
+ (actualRows - 1) * vgap);
}
/**
* Calculate the cell height.
*
* @param row the row for this cell
* @return the height
*/
protected int getComponentCellHeight(int row) {
if (cellSizing == ROWCOLPREFERRED) return rowHeight[row];
return largestHeight;
}
/**
* Calculate the cell width.
*
* @param col the column of this cell
* @return the width
*/
protected int getComponentCellWidth(int col) {
if (cellSizing == ROWCOLPREFERRED) return colWidth[col];
return largestWidth;
}
/**
* @param comp the component to be positioned
* @param x the x
* @param y the y
* @param cellWidth the width of the cell
* @param cellHeight the height of the cell
*/
protected void positionComponentInCell(Component comp,
int x, int y,
int cellWidth, int cellHeight)
{
int componentWidth;
int componentHeight;
if (fill == VERTICAL || fill == NONE) {
componentWidth = (int) comp.getPreferredSize().getWidth();
}
else {
componentWidth = cellWidth;
}
if (fill == HORIZONTAL || fill == NONE) {
componentHeight = (int) comp.getPreferredSize().getHeight();
}
else {
componentHeight = cellHeight;
}
int xAnchor, yAnchor;
if (fill == BOTH || fill == HORIZONTAL
|| anchor == NORTHWEST || anchor == WEST || anchor == SOUTHWEST)
{
xAnchor = x;
}
else if (anchor == NORTHEAST || anchor == EAST || anchor == SOUTHEAST)
{
xAnchor = x + cellWidth - componentWidth;
}
else {
xAnchor = x + (cellWidth - componentWidth) / 2;
}
if (fill == BOTH || fill == VERTICAL
|| anchor == NORTH || anchor == NORTHWEST || anchor == NORTHEAST)
{
yAnchor = y;
}
else if (anchor == SOUTHEAST || anchor == SOUTH || anchor == SOUTHWEST)
{
yAnchor = y + cellHeight - componentHeight;
}
else {
yAnchor = y + (cellHeight - componentHeight) / 2;
}
comp.setBounds(xAnchor, yAnchor, componentWidth, componentHeight);
}
/**
* @return the number of rows
*/
public int getRows() {
return rows;
}
/**
* @param r the number of rows. It can not be zero.
*/
public void setRows(int r) {
if ((r == 0) && (this.cols == 0)) {
throw new IllegalArgumentException("rows and cols cannot "
+ "both be zero");
}
this.rows = r;
}
/**
* @return the number of columns
*/
public int getColumns() {
return cols;
}
/**
* @param c the number of columns. It can not be zero.
*/
public void setCols(int c) {
if ((c == 0) && (this.rows == 0)) {
throw new IllegalArgumentException("rows and cols cannot "
+ "both be zero");
}
this.cols = c;
}
/**
* @return the horizontal gap
*/
public int getHgap() {
return hgap;
}
/**
* @param h the horizontal gap
*/
public void setHgap(int h) {
this.hgap = h;
}
/**
* @return the vertical gap
*/
public int getVgap() {
return vgap;
}
/**
* @param v the vertical gap
*/
public void setVgap(int v) {
this.vgap = v;
}
/**
* @param lh The largestHeight to set.
*/
protected void setLargestHeight(int lh) {
this.largestHeight = lh;
}
/**
* @return Returns the largestHeight.
*/
protected int getLargestHeight() {
return largestHeight;
}
/**
* @param lw The largestWidth to set.
*/
protected void setLargestWidth(int lw) {
this.largestWidth = lw;
}
/**
* @return Returns the largestWidth.
*/
protected int getLargestWidth() {
return largestWidth;
}
/**
* @param cw The colWidth to set.
*/
protected void setColWidth(int[] cw) {
this.colWidth = cw;
}
/**
* @return Returns the colWidth.
*/
protected int[] getColWidth() {
return colWidth;
}
/**
* @param rh The rowHeight to set.
*/
protected void setRowHeight(int[] rh) {
this.rowHeight = rh;
}
/**
* @return Returns the rowHeight.
*/
protected int[] getRowHeight() {
return rowHeight;
}
/**
* @return Returns the cellSizing.
*/
int getCellSizing() {
return cellSizing;
}
}
|