1 11 12 package org.eclipse.ui.internal.layout; 13 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.graphics.Point; 16 import org.eclipse.swt.graphics.Rectangle; 17 import org.eclipse.swt.layout.GridData; 18 19 29 public final class CellData { 30 31 36 public final static int NONE = 0; 37 38 47 public final static int OVERRIDE = 1; 48 49 59 public final static int MINIMUM = 2; 60 61 72 public final static int MAXIMUM = 3; 73 74 78 public int hintType = OVERRIDE; 79 80 86 public int widthHint = SWT.DEFAULT; 87 88 94 public int heightHint = SWT.DEFAULT; 95 96 99 public int verticalSpan = 1; 100 101 104 public int horizontalSpan = 1; 105 106 111 public int horizontalAlignment = SWT.FILL; 112 113 118 public int verticalAlignment = SWT.FILL; 119 120 124 public int horizontalIndent = 0; 125 126 130 public int verticalIndent = 0; 131 132 135 public CellData() { 136 } 138 139 145 public CellData(GridData data) { 146 verticalSpan = data.verticalSpan; 147 horizontalSpan = data.horizontalSpan; 148 149 switch (data.horizontalAlignment) { 150 case GridData.BEGINNING: 151 horizontalAlignment = SWT.LEFT; 152 break; 153 case GridData.CENTER: 154 horizontalAlignment = SWT.CENTER; 155 break; 156 case GridData.END: 157 horizontalAlignment = SWT.RIGHT; 158 break; 159 case GridData.FILL: 160 horizontalAlignment = SWT.FILL; 161 break; 162 } 163 164 switch (data.verticalAlignment) { 165 case GridData.BEGINNING: 166 verticalAlignment = SWT.LEFT; 167 break; 168 case GridData.CENTER: 169 verticalAlignment = SWT.CENTER; 170 break; 171 case GridData.END: 172 verticalAlignment = SWT.RIGHT; 173 break; 174 case GridData.FILL: 175 verticalAlignment = SWT.FILL; 176 break; 177 } 178 179 widthHint = data.widthHint; 180 heightHint = data.heightHint; 181 horizontalIndent = data.horizontalIndent; 182 hintType = OVERRIDE; 183 } 184 185 190 public CellData(CellData newData) { 191 hintType = newData.hintType; 192 widthHint = newData.widthHint; 193 heightHint = newData.heightHint; 194 horizontalAlignment = newData.horizontalAlignment; 195 verticalAlignment = newData.verticalAlignment; 196 horizontalSpan = newData.horizontalSpan; 197 verticalSpan = newData.verticalSpan; 198 } 199 200 213 public CellData setHint(int hintType, Point hint) { 214 return setHint(hintType, hint.x, hint.y); 215 } 216 217 233 public CellData setHint(int hintType, int horizontal, int vertical) { 234 this.hintType = hintType; 235 this.heightHint = vertical; 236 this.widthHint = horizontal; 237 238 return this; 239 } 240 241 248 public CellData align(int horizontalAlignment, int verticalAlignment) { 249 this.horizontalAlignment = horizontalAlignment; 250 this.verticalAlignment = verticalAlignment; 251 252 return this; 253 } 254 255 262 public CellData span(int horizontalSpan, int verticalSpan) { 263 this.horizontalSpan = horizontalSpan; 264 this.verticalSpan = verticalSpan; 265 266 return this; 267 } 268 269 277 public CellData indent(Point indent) { 278 return this.indent(indent.x, indent.y); 279 } 280 281 288 public CellData indent(int horizontalIndent, int verticalIndent) { 289 this.horizontalIndent = horizontalIndent; 290 this.verticalIndent = verticalIndent; 291 292 return this; 293 } 294 295 304 public Point computeSize(SizeCache toCompute, int cellWidth, int cellHeight) { 305 306 int absHorizontalIndent = Math.abs(horizontalIndent); 307 int absVerticalIndent = Math.abs(verticalIndent); 308 309 if (cellWidth != SWT.DEFAULT) { 312 cellWidth -= absHorizontalIndent; 313 } 314 315 if (cellHeight != SWT.DEFAULT) { 316 cellHeight -= absVerticalIndent; 317 } 318 319 int controlWidth = horizontalAlignment == SWT.FILL ? cellWidth 320 : SWT.DEFAULT; 321 int controlHeight = verticalAlignment == SWT.FILL ? cellHeight 322 : SWT.DEFAULT; 323 324 Point controlSize = computeControlSize(toCompute, controlWidth, 330 controlHeight); 331 332 if (cellWidth != SWT.DEFAULT && controlSize.x > cellWidth) { 333 controlSize = computeControlSize(toCompute, cellWidth, 334 controlHeight); 335 if (cellHeight != SWT.DEFAULT && controlSize.y > cellHeight) { 336 controlSize.y = cellHeight; 337 } 338 } else if (cellHeight != SWT.DEFAULT && controlSize.y > cellHeight) { 339 controlSize = computeControlSize(toCompute, controlWidth, 340 cellHeight); 341 if (cellWidth != SWT.DEFAULT && controlSize.x > cellWidth) { 342 controlSize.x = cellWidth; 343 } 344 } 345 346 controlSize.x += absHorizontalIndent; 348 controlSize.y += absVerticalIndent; 349 350 return controlSize; 351 } 352 353 361 public void positionControl(SizeCache cache, Rectangle cellBounds) { 362 363 int startx = cellBounds.x; 364 int starty = cellBounds.y; 365 int availableWidth = cellBounds.width - horizontalIndent; 366 int availableHeight = cellBounds.height - verticalIndent; 367 368 Point size = computeSize(cache, availableWidth, availableHeight); 369 370 switch (horizontalAlignment) { 372 case SWT.RIGHT: 373 startx = cellBounds.x + availableWidth - size.x; 374 break; 375 case SWT.CENTER: 376 startx = cellBounds.x + (availableWidth - size.x) / 2; 377 break; 378 } 379 380 switch (verticalAlignment) { 382 case SWT.BOTTOM: 383 starty = cellBounds.y + availableHeight - size.y; 384 break; 385 case SWT.CENTER: 386 starty = cellBounds.y + (availableHeight - size.y) / 2; 387 break; 388 } 389 390 cache.getControl().setBounds(startx + horizontalIndent, 392 starty + verticalIndent, size.x, size.y); 393 } 394 395 406 private Point computeControlSize(SizeCache toCompute, int controlWidth, 407 int controlHeight) { 408 switch (hintType) { 409 case OVERRIDE: 410 return computeOverrideSize(toCompute, controlWidth, controlHeight, 411 widthHint, heightHint); 412 case MINIMUM: 413 return computeMinimumBoundedSize(toCompute, controlWidth, 414 controlHeight, widthHint, heightHint); 415 case MAXIMUM: 416 return computeMaximumBoundedSize(toCompute, controlWidth, 417 controlHeight, widthHint, heightHint); 418 } 419 420 return computeRawSize(toCompute, controlWidth, controlHeight); 421 } 422 423 433 private static Point computeRawSize(SizeCache toCompute, int controlWidth, 434 int controlHeight) { 435 if (controlWidth != SWT.DEFAULT && controlHeight != SWT.DEFAULT) { 436 return new Point(controlWidth, controlHeight); 437 } 438 439 Point result = toCompute.computeSize(controlWidth, controlHeight); 450 451 if (controlWidth != SWT.DEFAULT) { 456 result.x = controlWidth; 457 } else if (controlHeight != SWT.DEFAULT) { 458 result.y = controlHeight; 459 } 460 461 return result; 462 } 463 464 477 private static Point computeOverrideSize(SizeCache control, int wHint, 478 int hHint, int overrideW, int overrideH) { 479 int resultWidth = overrideW; 480 int resultHeight = overrideH; 481 482 if (wHint != SWT.DEFAULT) { 483 resultWidth = wHint; 484 } 485 486 if (hHint != SWT.DEFAULT) { 487 resultHeight = hHint; 488 } 489 490 if (resultWidth == SWT.DEFAULT || resultHeight == SWT.DEFAULT) { 491 Point result = computeRawSize(control, resultWidth, resultHeight); 492 493 return result; 494 } 495 496 return new Point(resultWidth, resultHeight); 497 } 498 499 511 private static Point computeMaximumBoundedSize(SizeCache control, 512 int wHint, int hHint, int boundedWidth, int boundedHeight) { 513 Point controlSize = computeRawSize(control, wHint, hHint); 514 515 if (wHint == SWT.DEFAULT && boundedWidth != SWT.DEFAULT 516 && controlSize.x > boundedWidth) { 517 return computeMaximumBoundedSize(control, boundedWidth, hHint, 518 boundedWidth, boundedHeight); 519 } 520 521 if (hHint == SWT.DEFAULT && boundedHeight != SWT.DEFAULT 522 && controlSize.y > boundedHeight) { 523 return computeMaximumBoundedSize(control, wHint, boundedHeight, 524 boundedWidth, boundedHeight); 525 } 526 527 return controlSize; 528 } 529 530 private static Point computeMinimumBoundedSize(SizeCache control, 531 int wHint, int hHint, int minimumWidth, int minimumHeight) { 532 533 Point controlSize = computeRawSize(control, wHint, hHint); 534 535 if (minimumWidth != SWT.DEFAULT && wHint == SWT.DEFAULT 536 && controlSize.x < minimumWidth) { 537 return computeMinimumBoundedSize(control, minimumWidth, hHint, 538 minimumWidth, minimumHeight); 539 } 540 541 if (minimumHeight != SWT.DEFAULT && hHint == SWT.DEFAULT 542 && controlSize.y < minimumHeight) { 543 return computeMinimumBoundedSize(control, wHint, minimumHeight, 544 minimumWidth, minimumHeight); 545 } 546 547 return controlSize; 548 } 549 550 } 551 | Popular Tags |