1 30 31 package com.jgoodies.forms.extras; 32 33 import java.awt.Component ; 34 import java.util.ResourceBundle ; 35 36 import javax.swing.JComponent ; 37 import javax.swing.JLabel ; 38 import javax.swing.JPanel ; 39 40 import com.jgoodies.forms.factories.FormFactory; 41 import com.jgoodies.forms.layout.ConstantSize; 42 import com.jgoodies.forms.layout.FormLayout; 43 import com.jgoodies.forms.layout.RowSpec; 44 45 208 public final class DefaultFormBuilder extends I15dPanelBuilder { 209 210 214 private RowSpec lineGapSpec = FormFactory.LINE_GAP_ROWSPEC; 215 216 220 private RowSpec paragraphGapSpec = FormFactory.PARAGRAPH_GAP_ROWSPEC; 221 222 225 private int leadingColumnOffset = 0; 226 227 230 private boolean rowGroupingEnabled = false; 231 232 233 235 241 public DefaultFormBuilder(FormLayout layout) { 242 this(new JPanel (), layout); 243 } 244 245 252 public DefaultFormBuilder(JPanel panel, FormLayout layout) { 253 this(panel, layout, null); 254 } 255 256 264 public DefaultFormBuilder(FormLayout layout, ResourceBundle bundle) { 265 this(new JPanel (), layout, bundle); 266 } 267 268 277 public DefaultFormBuilder(JPanel panel, FormLayout layout, ResourceBundle bundle) { 278 super(panel, layout, bundle); 279 } 280 281 282 284 291 public void setLineGapSize(ConstantSize lineGapSize) { 292 RowSpec rowSpec = FormFactory.createGapRowSpec(lineGapSize); 293 this.lineGapSpec = rowSpec.asUnmodifyable(); 294 } 295 296 301 public RowSpec getLineGapSpec() { 302 return lineGapSpec; 303 } 304 305 312 public void setParagraphGapSize(ConstantSize paragraphGapSize) { 313 RowSpec rowSpec = FormFactory.createGapRowSpec(paragraphGapSize); 314 this.paragraphGapSpec = rowSpec.asUnmodifyable(); 315 } 316 317 322 public int getLeadingColumnOffset() { 323 return leadingColumnOffset; 324 } 325 326 331 public void setLeadingColumnOffset(int columnOffset) { 332 this.leadingColumnOffset = columnOffset; 333 } 334 335 340 public boolean isRowGroupingEnabled() { 341 return rowGroupingEnabled; 342 } 343 344 349 public void setRowGroupingEnabled(boolean enabled) { 350 rowGroupingEnabled = enabled; 351 } 352 353 354 356 362 public void append(Component component) { 363 append(component, 1); 364 } 365 366 373 public void append(Component component, int columnSpan) { 374 ensureCursorColumnInGrid(); 375 ensureHasGapRow(lineGapSpec); 376 ensureHasComponentLine(); 377 378 setColumnSpan(columnSpan); 379 add(component); 380 setColumnSpan(1); 381 nextColumn(columnSpan + 1); 382 } 383 384 391 public void append(Component c1, Component c2) { 392 append(c1); 393 append(c2); 394 } 395 396 404 public void append(Component c1, Component c2, Component c3) { 405 append(c1); 406 append(c2); 407 append(c3); 408 } 409 410 411 413 419 public JLabel append(String textWithMnemonic) { 420 JLabel label = getComponentFactory().createLabel(textWithMnemonic); 421 append(label); 422 return label; 423 } 424 425 433 public JLabel append(String textWithMnemonic, Component component) { 434 return append(textWithMnemonic, component, 1); 435 } 436 437 450 public JLabel append(String textWithMnemonic, Component c, int columnSpan) { 451 JLabel label = append(textWithMnemonic); 452 label.setLabelFor(c); 453 append(c, columnSpan); 454 return label; 455 } 456 457 466 public JLabel append(String textWithMnemonic, Component c1, Component c2) { 467 JLabel label = append(textWithMnemonic, c1); 468 append(c2); 469 return label; 470 } 471 472 481 public void append(String textWithMnemonic, Component c1, Component c2, int colSpan) { 482 append(textWithMnemonic, c1); 483 append(c2, colSpan); 484 } 485 486 496 public JLabel append(String textWithMnemonic, Component c1, Component c2, Component c3) { 497 JLabel label = append(textWithMnemonic, c1, c2); 498 append(c3); 499 return label; 500 } 501 502 513 public JLabel append(String textWithMnemonic, Component c1, Component c2, Component c3, Component c4) { 514 JLabel label = append(textWithMnemonic, c1, c2, c3); 515 append(c4); 516 return label; 517 } 518 519 520 522 529 public JLabel appendI15d(String resourceKey) { 530 return append(getI15dString(resourceKey)); 531 } 532 533 543 public JLabel appendI15d(String resourceKey, Component c, int columnSpan) { 544 JLabel label = appendI15d(resourceKey); 545 append(c, columnSpan); 546 return label; 547 } 548 549 557 public JLabel appendI15d(String resourceKey, Component component) { 558 return appendI15d(resourceKey, component, 1); 559 } 560 561 571 public JLabel appendI15d(String resourceKey, Component component, boolean nextLine) { 572 JLabel label = appendI15d(resourceKey, component, 1); 573 if (nextLine) { 574 nextLine(); 575 } 576 return label; 577 } 578 579 588 public JLabel appendI15d(String resourceKey, Component c1, Component c2) { 589 JLabel label = appendI15d(resourceKey, c1); 590 append(c2); 591 return label; 592 } 593 594 604 public JLabel appendI15d(String resourceKey, Component c1, Component c2, int colSpan) { 605 JLabel label = appendI15d(resourceKey, c1); 606 append(c2, colSpan); 607 return label; 608 } 609 610 620 public JLabel appendI15d(String resourceKey, Component c1, Component c2, Component c3) { 621 JLabel label = appendI15d(resourceKey, c1, c2); 622 append(c3); 623 return label; 624 } 625 626 637 public JLabel appendI15d(String resourceKey, Component c1, Component c2, Component c3, Component c4) { 638 JLabel label = appendI15d(resourceKey, c1, c2, c3); 639 append(c4); 640 return label; 641 } 642 643 644 646 652 public JLabel appendTitle(String textWithMnemonic) { 653 JLabel titleLabel = getComponentFactory().createTitle(textWithMnemonic); 654 append(titleLabel); 655 return titleLabel; 656 } 657 658 665 public JLabel appendI15dTitle(String resourceKey) { 666 return appendTitle(getI15dString(resourceKey)); 667 } 668 669 670 672 677 public JComponent appendSeparator() { 678 return appendSeparator(""); 679 } 680 681 687 public JComponent appendSeparator(String text) { 688 ensureCursorColumnInGrid(); 689 ensureHasGapRow(paragraphGapSpec); 690 ensureHasComponentLine(); 691 692 setColumn(super.getLeadingColumn()); 693 int columnSpan = getColumnCount(); 694 setColumnSpan(getColumnCount()); 695 JComponent titledSeparator = addSeparator(text); 696 setColumnSpan(1); 697 nextColumn(columnSpan); 698 return titledSeparator; 699 } 700 701 707 public void appendI15dSeparator(String resourceKey) { 708 appendSeparator(getI15dString(resourceKey)); 709 } 710 711 712 714 720 protected int getLeadingColumn() { 721 int column = super.getLeadingColumn(); 722 return column + getLeadingColumnOffset() * getColumnIncrementSign(); 723 } 724 725 726 728 733 private void ensureCursorColumnInGrid() { 734 if (getColumn() > getColumnCount()) { 735 nextLine(); 736 } 737 } 738 739 746 private void ensureHasGapRow(RowSpec gapRowSpec) { 747 if ((getRow() == 1) || (getRow() <= getRowCount())) 748 return; 749 750 if (getRow() <= getRowCount()) { 751 RowSpec rowSpec = getCursorRowSpec(); 752 if ((rowSpec == gapRowSpec)) 753 return; 754 } 755 appendRow(gapRowSpec); 756 nextLine(); 757 } 758 759 763 private void ensureHasComponentLine() { 764 if (getRow() <= getRowCount()) return; 765 appendRow(FormFactory.PREF_ROWSPEC); 766 if (isRowGroupingEnabled()) { 767 getLayout().addGroupedRow(getRow()); 768 } 769 } 770 771 776 private RowSpec getCursorRowSpec() { 777 return getLayout().getRowSpec(getRow()); 778 } 779 780 781 } 782 | Popular Tags |