1 12 package org.eclipse.jface.layout; 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.graphics.Point; 15 import org.eclipse.swt.layout.GridData; 16 import org.eclipse.swt.widgets.Control; 17 18 110 public final class GridDataFactory { 111 private GridData data; 112 113 118 private GridDataFactory(GridData d) { 119 this.data = d; 120 } 121 122 144 public static GridDataFactory swtDefaults() { 145 return new GridDataFactory(new GridData()); 146 } 147 148 155 public static GridDataFactory createFrom(GridData data) { 156 return new GridDataFactory(copyData(data)); 157 } 158 159 184 public static GridDataFactory fillDefaults() { 185 GridData data = new GridData(); 186 data.minimumWidth = 1; 187 data.minimumHeight = 1; 188 data.horizontalAlignment = SWT.FILL; 189 data.verticalAlignment = SWT.FILL; 190 191 return new GridDataFactory(data); 192 } 193 194 212 public static GridDataFactory defaultsFor(Control theControl) { 213 return LayoutGenerator.defaultsFor(theControl); 214 } 215 216 231 public static void generate(Control theControl, int hSpan, int vSpan) { 232 defaultsFor(theControl).span(hSpan, vSpan).applyTo(theControl); 233 } 234 235 250 public static void generate(Control theControl, Point span) { 251 defaultsFor(theControl).span(span).applyTo(theControl); 252 } 253 254 262 public GridDataFactory span(int hSpan, int vSpan) { 263 data.horizontalSpan = hSpan; 264 data.verticalSpan = vSpan; 265 return this; 266 } 267 268 276 public GridDataFactory span(Point span) { 277 data.horizontalSpan = span.x; 278 data.verticalSpan = span.y; 279 return this; 280 } 281 282 291 public GridDataFactory hint(int xHint, int yHint) { 292 data.widthHint = xHint; 293 data.heightHint = yHint; 294 return this; 295 } 296 297 307 public GridDataFactory hint(Point hint) { 308 data.widthHint = hint.x; 309 data.heightHint = hint.y; 310 return this; 311 } 312 313 320 public GridDataFactory align(int hAlign, int vAlign) { 321 if (hAlign != SWT.BEGINNING && hAlign != SWT.CENTER && hAlign != GridData.CENTER && hAlign != SWT.END && hAlign != GridData.END && hAlign != SWT.FILL && hAlign != SWT.LEFT && hAlign != SWT.RIGHT) { 322 throw new IllegalArgumentException (); 323 } 324 if (vAlign != SWT.BEGINNING && vAlign != SWT.CENTER && vAlign != GridData.CENTER && vAlign != SWT.END && vAlign != GridData.END && vAlign != SWT.FILL && vAlign != SWT.TOP && vAlign != SWT.BOTTOM) { 325 throw new IllegalArgumentException (); 326 } 327 data.horizontalAlignment = hAlign; 328 data.verticalAlignment = vAlign; 329 return this; 330 } 331 332 341 public GridDataFactory indent(int hIndent, int vIndent) { 342 data.horizontalIndent = hIndent; 343 data.verticalIndent = vIndent; 344 return this; 345 } 346 347 355 public GridDataFactory indent(Point indent) { 356 data.horizontalIndent = indent.x; 357 data.verticalIndent = indent.y; 358 return this; 359 } 360 361 372 public GridDataFactory grab(boolean horizontal, boolean vertical) { 373 data.grabExcessHorizontalSpace = horizontal; 374 data.grabExcessVerticalSpace = vertical; 375 return this; 376 } 377 378 395 public GridDataFactory minSize(int minX, int minY) { 396 data.minimumWidth = minX; 397 data.minimumHeight = minY; 398 return this; 399 } 400 401 412 public GridDataFactory minSize(Point min) { 413 data.minimumWidth = min.x; 414 data.minimumHeight = min.y; 415 return this; 416 } 417 418 424 public GridDataFactory exclude(boolean shouldExclude) { 425 data.exclude = shouldExclude; 426 return this; 427 } 428 429 435 public GridData create() { 436 return copyData(data); 437 } 438 439 444 public GridDataFactory copy() { 445 return new GridDataFactory(create()); 446 } 447 448 454 public static GridData copyData(GridData data) { 455 GridData newData = new GridData(data.horizontalAlignment, data.verticalAlignment, data.grabExcessHorizontalSpace, data.grabExcessVerticalSpace, data.horizontalSpan, 456 data.verticalSpan); 457 newData.exclude = data.exclude; 458 newData.heightHint = data.heightHint; 459 newData.horizontalIndent = data.horizontalIndent; 460 newData.minimumHeight = data.minimumHeight; 461 newData.minimumWidth = data.minimumWidth; 462 newData.verticalIndent = data.verticalIndent; 463 newData.widthHint = data.widthHint; 464 465 return newData; 466 } 467 468 474 public void applyTo(Control control) { 475 control.setLayoutData(create()); 476 } 477 478 } 479 | Popular Tags |