1 30 31 package com.jgoodies.forms.builder; 32 33 import java.awt.Component ; 34 import java.awt.Container ; 35 36 import com.jgoodies.forms.factories.FormFactory; 37 import com.jgoodies.forms.layout.CellConstraints; 38 import com.jgoodies.forms.layout.ColumnSpec; 39 import com.jgoodies.forms.layout.FormLayout; 40 import com.jgoodies.forms.layout.RowSpec; 41 42 60 public abstract class AbstractFormBuilder { 61 62 65 private final Container container; 66 67 71 private final FormLayout layout; 72 73 78 private CellConstraints currentCellConstraints; 79 80 83 private boolean leftToRight = true; 84 85 86 87 89 96 public AbstractFormBuilder(Container container, FormLayout layout){ 97 this.container = container; 98 this.layout = layout; 99 100 container.setLayout(layout); 101 currentCellConstraints = new CellConstraints(); 102 } 103 104 105 107 112 public final Container getContainer() { 113 return container; 114 } 115 116 121 public final FormLayout getLayout() { 122 return layout; 123 } 124 125 130 public final int getColumnCount() { 131 return getLayout().getColumnCount(); 132 } 133 134 139 public final int getRowCount() { 140 return getLayout().getRowCount(); 141 } 142 143 144 146 152 public final boolean isLeftToRight() { 153 return leftToRight; 154 } 155 156 161 public final void setLeftToRight(boolean b) { 162 this.leftToRight = b; 163 } 164 165 166 168 173 public final int getColumn() { 174 return currentCellConstraints.gridX; 175 } 176 177 182 public final void setColumn(int column) { 183 currentCellConstraints.gridX = column; 184 } 185 186 191 public final int getRow() { 192 return currentCellConstraints.gridY; 193 } 194 195 200 public final void setRow(int row) { 201 currentCellConstraints.gridY = row; 202 } 203 204 209 public final void setColumnSpan(int columnSpan) { 210 currentCellConstraints.gridWidth = columnSpan; 211 } 212 213 218 public final void setRowSpan(int rowSpan) { 219 currentCellConstraints.gridHeight = rowSpan; 220 } 221 222 228 public final void setOrigin(int column, int row) { 229 setColumn(column); 230 setRow(row); 231 } 232 233 239 public final void setExtent(int columnSpan, int rowSpan) { 240 setColumnSpan(columnSpan); 241 setRowSpan(rowSpan); 242 } 243 244 253 public final void setBounds(int column, int row, int columnSpan, int rowSpan) { 254 setColumn(column); 255 setRow(row); 256 setColumnSpan(columnSpan); 257 setRowSpan(rowSpan); 258 } 259 260 263 public final void nextColumn() { 264 nextColumn(1); 265 } 266 267 272 public final void nextColumn(int columns) { 273 currentCellConstraints.gridX += columns * getColumnIncrementSign(); 274 } 275 276 279 public final void nextRow() { 280 nextRow(1); 281 } 282 283 288 public final void nextRow(int rows) { 289 currentCellConstraints.gridY += rows; 290 } 291 292 296 public final void nextLine() { 297 nextLine(1); 298 } 299 300 306 public final void nextLine(int lines) { 307 nextRow(lines); 308 setColumn(getLeadingColumn()); 309 } 310 311 312 314 319 public final void setHAlignment(CellConstraints.Alignment alignment) { 320 currentCellConstraints.hAlign = alignment; 321 } 322 323 328 public final void setVAlignment(CellConstraints.Alignment alignment) { 329 currentCellConstraints.vAlign = alignment; 330 } 331 332 338 public final void setAlignment(CellConstraints.Alignment hAlign, 339 CellConstraints.Alignment vAlign) { 340 setHAlignment(hAlign); 341 setVAlignment(vAlign); 342 } 343 344 345 347 352 public final void appendColumn(ColumnSpec columnSpec) { 353 getLayout().appendColumn(columnSpec); 354 } 355 356 362 public final void appendColumn(String encodedColumnSpec) { 363 appendColumn(new ColumnSpec(encodedColumnSpec)); 364 } 365 366 369 public final void appendGlueColumn() { 370 appendColumn(FormFactory.GLUE_COLSPEC); 371 } 372 373 376 public final void appendRelatedComponentsGapColumn() { 377 appendColumn(FormFactory.RELATED_GAP_COLSPEC); 378 } 379 380 383 public final void appendUnrelatedComponentsGapColumn() { 384 appendColumn(FormFactory.UNRELATED_GAP_COLSPEC); 385 } 386 387 392 public final void appendRow(RowSpec rowSpec) { 393 getLayout().appendRow(rowSpec); 394 } 395 396 402 public final void appendRow(String encodedRowSpec) { 403 appendRow(new RowSpec(encodedRowSpec)); 404 } 405 406 409 public final void appendGlueRow() { 410 appendRow(FormFactory.GLUE_ROWSPEC); 411 } 412 413 416 public final void appendRelatedComponentsGapRow() { 417 appendRow(FormFactory.RELATED_GAP_ROWSPEC); 418 } 419 420 423 public final void appendUnrelatedComponentsGapRow() { 424 appendRow(FormFactory.UNRELATED_GAP_ROWSPEC); 425 } 426 427 428 430 437 public final Component add(Component component, CellConstraints cellConstraints) { 438 container.add(component, cellConstraints); 439 return component; 440 } 441 442 449 public final Component add(Component component, String encodedCellConstraints) { 450 container.add(component, new CellConstraints(encodedCellConstraints)); 451 return component; 452 } 453 454 460 public final Component add(Component component) { 461 add(component, currentCellConstraints); 462 return component; 463 } 464 465 466 468 473 protected final CellConstraints cellConstraints() { 474 return currentCellConstraints; 475 } 476 477 485 protected int getLeadingColumn() { 486 return isLeftToRight() ? 1 : getColumnCount(); 487 } 488 489 495 protected final int getColumnIncrementSign() { 496 return isLeftToRight() ? 1 : -1; 497 } 498 499 500 } 501 | Popular Tags |