| 1 44 45 package org.jfree.layout; 46 47 import java.awt.Component ; 48 import java.awt.Container ; 49 import java.awt.Dimension ; 50 import java.awt.Insets ; 51 import java.awt.LayoutManager ; 52 import java.io.Serializable ; 53 54 60 public class FormatLayout implements LayoutManager , Serializable { 61 62 63 private static final long serialVersionUID = 2866692886323930722L; 64 65 66 public static final int C = 1; 67 68 69 public static final int LC = 2; 70 71 72 public static final int LCB = 3; 73 74 75 public static final int LCLC = 4; 76 77 78 public static final int LCLCB = 5; 79 80 81 public static final int LCBLC = 6; 82 83 84 public static final int LCBLCB = 7; 85 86 87 private int[] rowFormats; 88 89 90 private int rowGap; 91 92 95 private int[] columnGaps; 96 97 98 private int[] rowHeights; 99 100 101 private int totalHeight; 102 103 104 private int[] columnWidths; 105 106 107 private int totalWidth; 108 109 110 private int columns1and2Width; 111 112 113 private int columns4and5Width; 114 115 116 private int columns1to4Width; 117 118 119 private int columns1to5Width; 120 121 122 private int columns0to5Width; 123 124 178 public FormatLayout(final int rowCount, final int[] rowFormats) { 179 180 this.rowFormats = rowFormats; 181 this.rowGap = 2; 182 this.columnGaps = new int[5]; 183 this.columnGaps[0] = 10; 184 this.columnGaps[1] = 5; 185 this.columnGaps[2] = 5; 186 this.columnGaps[3] = 10; 187 this.columnGaps[4] = 5; 188 189 this.rowHeights = new int[rowCount]; 191 this.columnWidths = new int[6]; 192 } 193 194 201 public Dimension preferredLayoutSize(final Container parent) { 202 203 Component c0, c1, c2, c3, c4, c5; 204 205 synchronized (parent.getTreeLock()) { 206 final Insets insets = parent.getInsets(); 207 int componentIndex = 0; 208 final int rowCount = this.rowHeights.length; 209 for (int i = 0; i < this.columnWidths.length; i++) { 210 this.columnWidths[i] = 0; 211 } 212 this.columns1and2Width = 0; 213 this.columns4and5Width = 0; 214 this.columns1to4Width = 0; 215 this.columns1to5Width = 0; 216 this.columns0to5Width = 0; 217 218 this.totalHeight = 0; 219 for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { 220 final int format 221 = this.rowFormats[rowIndex % this.rowFormats.length]; 222 switch (format) { 223 case FormatLayout.C: 224 c0 = parent.getComponent(componentIndex); 225 updateC(rowIndex, c0.getPreferredSize()); 226 componentIndex = componentIndex + 1; 227 break; 228 case FormatLayout.LC: 229 c0 = parent.getComponent(componentIndex); 230 c1 = parent.getComponent(componentIndex + 1); 231 updateLC(rowIndex, c0.getPreferredSize(), 232 c1.getPreferredSize()); 233 componentIndex = componentIndex + 2; 234 break; 235 case FormatLayout.LCB: 236 c0 = parent.getComponent(componentIndex); 237 c1 = parent.getComponent(componentIndex + 1); 238 c2 = parent.getComponent(componentIndex + 2); 239 updateLCB(rowIndex, 240 c0.getPreferredSize(), 241 c1.getPreferredSize(), 242 c2.getPreferredSize()); 243 componentIndex = componentIndex + 3; 244 break; 245 case FormatLayout.LCLC: 246 c0 = parent.getComponent(componentIndex); 247 c1 = parent.getComponent(componentIndex + 1); 248 c2 = parent.getComponent(componentIndex + 2); 249 c3 = parent.getComponent(componentIndex + 3); 250 updateLCLC(rowIndex, 251 c0.getPreferredSize(), 252 c1.getPreferredSize(), 253 c2.getPreferredSize(), 254 c3.getPreferredSize()); 255 componentIndex = componentIndex + 4; 256 break; 257 case FormatLayout.LCBLC: 258 c0 = parent.getComponent(componentIndex); 259 c1 = parent.getComponent(componentIndex + 1); 260 c2 = parent.getComponent(componentIndex + 2); 261 c3 = parent.getComponent(componentIndex + 3); 262 c4 = parent.getComponent(componentIndex + 4); 263 updateLCBLC(rowIndex, 264 c0.getPreferredSize(), 265 c1.getPreferredSize(), 266 c2.getPreferredSize(), 267 c3.getPreferredSize(), 268 c4.getPreferredSize()); 269 componentIndex = componentIndex + 5; 270 break; 271 case FormatLayout.LCLCB: 272 c0 = parent.getComponent(componentIndex); 273 c1 = parent.getComponent(componentIndex + 1); 274 c2 = parent.getComponent(componentIndex + 2); 275 c3 = parent.getComponent(componentIndex + 3); 276 c4 = parent.getComponent(componentIndex + 4); 277 updateLCLCB(rowIndex, 278 c0.getPreferredSize(), 279 c1.getPreferredSize(), 280 c2.getPreferredSize(), 281 c3.getPreferredSize(), 282 c4.getPreferredSize()); 283 componentIndex = componentIndex + 5; 284 break; 285 case FormatLayout.LCBLCB: 286 c0 = parent.getComponent(componentIndex); 287 c1 = parent.getComponent(componentIndex + 1); 288 c2 = parent.getComponent(componentIndex + 2); 289 c3 = parent.getComponent(componentIndex + 3); 290 c4 = parent.getComponent(componentIndex + 4); 291 c5 = parent.getComponent(componentIndex + 5); 292 updateLCBLCB(rowIndex, 293 c0.getPreferredSize(), 294 c1.getPreferredSize(), 295 c2.getPreferredSize(), 296 c3.getPreferredSize(), 297 c4.getPreferredSize(), 298 c5.getPreferredSize()); 299 componentIndex = componentIndex + 6; 300 break; 301 } 302 } 303 complete(); 304 return new Dimension (this.totalWidth + insets.left + insets.right, 305 this.totalHeight + (rowCount - 1) * this.rowGap 306 + insets.top + insets.bottom); 307 } 308 } 309 310 317 public Dimension minimumLayoutSize(final Container parent) { 318 319 Component c0, c1, c2, c3, c4, c5; 320 321 synchronized (parent.getTreeLock()) { 322 final Insets insets = parent.getInsets(); 323 int componentIndex = 0; 324 final int rowCount = this.rowHeights.length; 325 for (int i = 0; i < this.columnWidths.length; i++) { 326 this.columnWidths[i] = 0; 327 } 328 this.columns1and2Width = 0; 329 this.columns4and5Width = 0; 330 this.columns1to4Width = 0; 331 this.columns1to5Width = 0; 332 this.columns0to5Width = 0; 333 final int totalHeight = 0; 334 for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { 335 336 final int format 337 = this.rowFormats[rowIndex % this.rowFormats.length]; 338 339 switch (format) { 340 case FormatLayout.C: 341 c0 = parent.getComponent(componentIndex); 342 this.columns0to5Width = Math.max( 343 this.columns0to5Width, c0.getMinimumSize().width 344 ); 345 componentIndex = componentIndex + 1; 346 break; 347 case FormatLayout.LC: 348 c0 = parent.getComponent(componentIndex); 349 c1 = parent.getComponent(componentIndex + 1); 350 updateLC(rowIndex, 351 c0.getMinimumSize(), 352 c1.getMinimumSize()); 353 componentIndex = componentIndex + 2; 354 break; 355 case FormatLayout.LCB: 356 c0 = parent.getComponent(componentIndex); 357 c1 = parent.getComponent(componentIndex + 1); 358 c2 = parent.getComponent(componentIndex + 2); 359 updateLCB(rowIndex, 360 c0.getMinimumSize(), 361 c1.getMinimumSize(), 362 c2.getMinimumSize()); 363 componentIndex = componentIndex + 3; 364 break; 365 case FormatLayout.LCLC: 366 c0 = parent.getComponent(componentIndex); 367 c1 = parent.getComponent(componentIndex + 1); 368 c2 = parent.getComponent(componentIndex + 2); 369 c3 = parent.getComponent(componentIndex + 3); 370 updateLCLC(rowIndex, 371 c0.getMinimumSize(), 372 c1.getMinimumSize(), 373 c2.getMinimumSize(), 374 c3.getMinimumSize()); 375 componentIndex = componentIndex + 3; 376 break; 377 case FormatLayout.LCBLC: 378 c0 = parent.getComponent(componentIndex); 379 c1 = parent.getComponent(componentIndex + 1); 380 c2 = parent.getComponent(componentIndex + 2); 381 c3 = parent.getComponent(componentIndex + 3); 382 c4 = parent.getComponent(componentIndex + 4); 383 updateLCBLC(rowIndex, 384 c0.getMinimumSize(), 385 c1.getMinimumSize(), 386 c2.getMinimumSize(), 387 c3.getMinimumSize(), 388 c4.getMinimumSize()); 389 componentIndex = componentIndex + 4; 390 break; 391 case FormatLayout.LCLCB: 392 c0 = parent.getComponent(componentIndex); 393 c1 = parent.getComponent(componentIndex + 1); 394 c2 = parent.getComponent(componentIndex + 2); 395 c3 = parent.getComponent(componentIndex + 3); 396 c4 = parent.getComponent(componentIndex + 4); 397 updateLCLCB(rowIndex, 398 c0.getMinimumSize(), 399 c1.getMinimumSize(), 400 c2.getMinimumSize(), 401 c3.getMinimumSize(), 402 c4.getMinimumSize()); 403 componentIndex = componentIndex + 4; 404 break; 405 case FormatLayout.LCBLCB: 406 c0 = parent.getComponent(componentIndex); 407 c1 = parent.getComponent(componentIndex + 1); 408 c2 = parent.getComponent(componentIndex + 2); 409 c3 = parent.getComponent(componentIndex + 3); 410 c4 = parent.getComponent(componentIndex + 4); 411 c5 = parent.getComponent(componentIndex + 5); 412 updateLCBLCB(rowIndex, 413 c0.getMinimumSize(), 414 c1.getMinimumSize(), 415 c2.getMinimumSize(), 416 c3.getMinimumSize(), 417 c4.getMinimumSize(), 418 c5.getMinimumSize()); 419 componentIndex = componentIndex + 5; 420 break; 421 } 422 } 423 complete(); 424 return new Dimension (this.totalWidth + insets.left + insets.right, 425 totalHeight + (rowCount - 1) * this.rowGap 426 + insets.top + insets.bottom); 427 } 428 } 429 430 435 public void layoutContainer(final Container parent) { 436 Component c0, c1, c2, c3, c4, c5; 437 438 synchronized (parent.getTreeLock()) { 439 final Insets insets = parent.getInsets(); 440 int componentIndex = 0; 441 final int rowCount = this.rowHeights.length; 442 for (int i = 0; i < this.columnWidths.length; i++) { 443 this.columnWidths[i] = 0; 444 } 445 this.columns1and2Width = 0; 446 this.columns4and5Width = 0; 447 this.columns1to4Width = 0; 448 this.columns1to5Width = 0; 449 this.columns0to5Width 450 = parent.getBounds().width - insets.left - insets.right; 451 452 this.totalHeight = 0; 453 for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { 454 final int format 455 = this.rowFormats[rowIndex % this.rowFormats.length]; 456 switch (format) { 457 case FormatLayout.C: 458 c0 = parent.getComponent(componentIndex); 459 updateC(rowIndex, c0.getPreferredSize()); 460 componentIndex = componentIndex + 1; 461 break; 462 case FormatLayout.LC: 463 c0 = parent.getComponent(componentIndex); 464 c1 = parent.getComponent(componentIndex + 1); 465 updateLC(rowIndex, c0.getPreferredSize(), 466 c1.getPreferredSize()); 467 componentIndex = componentIndex + 2; 468 break; 469 case FormatLayout.LCB: 470 c0 = parent.getComponent(componentIndex); 471 c1 = parent.getComponent(componentIndex + 1); 472 c2 = parent.getComponent(componentIndex + 2); 473 updateLCB(rowIndex, 474 c0.getPreferredSize(), 475 c1.getPreferredSize(), 476 c2.getPreferredSize()); 477 componentIndex = componentIndex + 3; 478 break; 479 case FormatLayout.LCLC: 480 c0 = parent.getComponent(componentIndex); 481 c1 = parent.getComponent(componentIndex + 1); 482 c2 = parent.getComponent(componentIndex + 2); 483 c3 = parent.getComponent(componentIndex + 3); 484 updateLCLC(rowIndex, 485 c0.getPreferredSize(), 486 c1.getPreferredSize(), 487 c2.getPreferredSize(), 488 c3.getPreferredSize()); 489 componentIndex = componentIndex + 4; 490 break; 491 case FormatLayout.LCBLC: 492 c0 = parent.getComponent(componentIndex); 493 c1 = parent.getComponent(componentIndex + 1); 494 c2 = parent.getComponent(componentIndex + 2); 495 c3 = parent.getComponent(componentIndex + 3); 496 c4 = parent.getComponent(componentIndex + 4); 497 updateLCBLC(rowIndex, 498 c0.getPreferredSize(), 499 c1.getPreferredSize(), 500 c2.getPreferredSize(), 501 c3.getPreferredSize(), 502 c4.getPreferredSize()); 503 componentIndex = componentIndex + 5; 504 break; 505 case FormatLayout.LCLCB: 506 c0 = parent.getComponent(componentIndex); 507 c1 = parent.getComponent(componentIndex + 1); 508 c2 = parent.getComponent(componentIndex + 2); 509 c3 = parent.getComponent(componentIndex + 3); 510 c4 = parent.getComponent(componentIndex + 4); 511 updateLCLCB(rowIndex, 512 c0.getPreferredSize(), 513 c1.getPreferredSize(), 514 c2.getPreferredSize(), 515 c3.getPreferredSize(), 516 c4.getPreferredSize()); 517 componentIndex = componentIndex + 5; 518 break; 519 case FormatLayout.LCBLCB: 520 c0 = parent.getComponent(componentIndex); 521 c1 = parent.getComponent(componentIndex + 1); 522 c2 = parent.getComponent(componentIndex + 2); 523 c3 = parent.getComponent(componentIndex + 3); 524 c4 = parent.getComponent(componentIndex + 4); 525 c5 = parent.getComponent(componentIndex + 5); 526 updateLCBLCB(rowIndex, 527 c0.getPreferredSize(), 528 c1.getPreferredSize(), 529 c2.getPreferredSize(), 530 c3.getPreferredSize(), 531 c4.getPreferredSize(), 532 c5.getPreferredSize()); 533 componentIndex = componentIndex + 6; 534 break; 535 } 536 } 537 complete(); 538 539 componentIndex = 0; 540 int rowY = insets.top; 541 final int[] rowX = new int[6]; 542 rowX[0] = insets.left; 543 rowX[1] = rowX[0] + this.columnWidths[0] + this.columnGaps[0]; 544 rowX[2] = rowX[1] + this.columnWidths[1] + this.columnGaps[1]; 545 rowX[3] = rowX[2] + this.columnWidths[2] + this.columnGaps[2]; 546 rowX[4] = rowX[3] + this.columnWidths[3] + this.columnGaps[3]; 547 rowX[5] = rowX[4] + this.columnWidths[4] + this.columnGaps[4]; 548 final int w1to2 = this.columnWidths[1] + this.columnGaps[1] 549 + this.columnWidths[2]; 550 final int w4to5 = this.columnWidths[4] + this.columnGaps[4] 551 + this.columnWidths[5]; 552 final int w1to4 = w1to2 + this.columnGaps[2] + this.columnWidths[3] 553 + this.columnGaps[3] + this.columnWidths[4]; 554 final int w1to5 = w1to4 + this.columnGaps[4] + this.columnWidths[5]; 555 final int w0to5 = w1to5 + this.columnWidths[0] + this.columnGaps[0]; 556 for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { 557 final int format 558 = this.rowFormats[rowIndex % this.rowFormats.length]; 559 560 switch (format) { 561 case FormatLayout.C: 562 c0 = parent.getComponent(componentIndex); 563 c0.setBounds(rowX[0], rowY, w0to5, 564 c0.getPreferredSize().height); 565 componentIndex = componentIndex + 1; 566 break; 567 case FormatLayout.LC: 568 c0 = parent.getComponent(componentIndex); 569 c0.setBounds( 570 rowX[0], 571 rowY + (this.rowHeights[rowIndex] 572 - c0.getPreferredSize().height) / 2, 573 this.columnWidths[0], c0.getPreferredSize().height 574 ); 575 c1 = parent.getComponent(componentIndex + 1); 576 c1.setBounds( 577 rowX[1], 578 rowY + (this.rowHeights[rowIndex] 579 - c1.getPreferredSize().height) / 2, 580 w1to5, c1.getPreferredSize().height 581 ); 582 componentIndex = componentIndex + 2; 583 break; 584 case FormatLayout.LCB: 585 c0 = parent.getComponent(componentIndex); 586 c0.setBounds( 587 rowX[0], 588 rowY + (this.rowHeights[rowIndex] 589 - c0.getPreferredSize().height) / 2, 590 this.columnWidths[0], c0.getPreferredSize().height 591 ); 592 c1 = parent.getComponent(componentIndex + 1); 593 c1.setBounds( 594 rowX[1], 595 rowY + (this.rowHeights[rowIndex] 596 - c1.getPreferredSize().height) / 2, 597 w1to4, c1.getPreferredSize().height 598 ); 599 c2 = parent.getComponent(componentIndex + 2); 600 c2.setBounds( 601 rowX[5], 602 rowY + (this.rowHeights[rowIndex] 603 - c2.getPreferredSize().height) / 2, 604 this.columnWidths[5], c2.getPreferredSize().height 605 ); 606 componentIndex = componentIndex + 3; 607 break; 608 case FormatLayout.LCLC: 609 c0 = parent.getComponent(componentIndex); 610 c0.setBounds( 611 rowX[0], 612 rowY + (this.rowHeights[rowIndex] 613 - c0.getPreferredSize().height) / 2, 614 this.columnWidths[0], c0.getPreferredSize().height 615 ); 616 c1 = parent.getComponent(componentIndex + 1); 617 c1.setBounds( 618 rowX[1], 619 rowY + (this.rowHeights[rowIndex] 620 - c1.getPreferredSize().height) / 2, 621 w1to2, c1.getPreferredSize().height 622 ); 623 c2 = parent.getComponent(componentIndex + 2); 624 c2.setBounds( 625 rowX[3], 626 rowY + (this.rowHeights[rowIndex] 627 - c2.getPreferredSize().height) / 2, 628 this.columnWidths[3], c2.getPreferredSize().height 629 ); 630 c3 = parent.getComponent(componentIndex + 3); 631 c3.setBounds( 632 rowX[4], 633 rowY + (this.rowHeights[rowIndex] 634 - c3.getPreferredSize().height) / 2, 635 w4to5, c3.getPreferredSize().height 636 );
|