|
Added
Link Here
|
| 1 |
package org.eclipse.gmf.internal.codegen.draw2d; |
| 2 |
|
| 3 |
import java.util.HashMap; |
| 4 |
import java.util.List; |
| 5 |
import java.util.Map; |
| 6 |
|
| 7 |
import org.eclipse.draw2d.AbstractLayout; |
| 8 |
import org.eclipse.draw2d.IFigure; |
| 9 |
import org.eclipse.draw2d.LayoutManager; |
| 10 |
import org.eclipse.draw2d.geometry.Dimension; |
| 11 |
import org.eclipse.draw2d.geometry.Rectangle; |
| 12 |
import org.eclipse.swt.SWT; |
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* Lays out children into a Grid arrangement in which overall aligment and |
| 17 |
* spacing can be configured, as well as specfic layout requirements for the |
| 18 |
* each individual member of the GridLayout. This layout is a Draw2D port of the |
| 19 |
* swt GridLayout. |
| 20 |
* |
| 21 |
* <code>GridLayout</code> has a number of configuration fields, and the |
| 22 |
* Figures it lays out can have an associated layout data object, called |
| 23 |
* <code>GridLayoutData</code> (similar to the swt GridLayoutData object). The power of |
| 24 |
* <code>GridLayout</code> lies in the ability to configure |
| 25 |
* <code>GridLayoutData</code> for each Figure in the layout. |
| 26 |
* <p> |
| 27 |
* The following code creates a container Figure managed by a |
| 28 |
* <code>GridLayout</code> with 2 columns, containing 3 RectangleFigure |
| 29 |
* shapes, the last of which has been given further layout instructions. |
| 30 |
* Note that it is the <code>GridLayout</code> method <code>setConstraint</code> |
| 31 |
* that binds the child <code>Figure</code> to its layout <code>GridLayoutData</code> object. |
| 32 |
* |
| 33 |
* <pre> |
| 34 |
* Figure container = new Figure(); |
| 35 |
* GridLayout gridLayout = new GridLayout(); |
| 36 |
* gridLayout.numColumns = 2; |
| 37 |
* container.setLayout(gridLayout); |
| 38 |
* |
| 39 |
* Shape rect; |
| 40 |
* rect = new RectangleFigure(); |
| 41 |
* container.add(rect); |
| 42 |
* |
| 43 |
* rect = new RectangleFigure(); |
| 44 |
* container.add(rect); |
| 45 |
* |
| 46 |
* rect = new RectangleFigure(); |
| 47 |
* GridLayoutData GridLayoutData = new GridLayoutData(); |
| 48 |
* GridLayoutData.widthHint = 150; |
| 49 |
* layout.setConstraint(rect, GridLayoutData); |
| 50 |
* |
| 51 |
* container.add(rect); |
| 52 |
* </pre> |
| 53 |
* |
| 54 |
* <p> |
| 55 |
* The <code>numColumns</code> field is the most important field in a |
| 56 |
* <code>GridLayout</code>. Widgets are laid out in columns from left to |
| 57 |
* right, and a new row is created when <code>numColumns</code>+ 1 figures |
| 58 |
* are added to the <code>Figure<code> parent container. |
| 59 |
* |
| 60 |
* @see GridLayoutData |
| 61 |
* |
| 62 |
* @author Asim Ullah |
| 63 |
* created: Sep 10, 2004 |
| 64 |
*/ |
| 65 |
public class GridLayout extends AbstractLayout { |
| 66 |
/** |
| 67 |
* numColumns specifies the number of cell columns in the layout. |
| 68 |
* |
| 69 |
* The default value is 1. |
| 70 |
*/ |
| 71 |
public int numColumns = 1; |
| 72 |
|
| 73 |
/** |
| 74 |
* makeColumnsEqualWidth specifies whether all columns in the layout will be |
| 75 |
* forced to have the same width. |
| 76 |
* |
| 77 |
* The default value is false. |
| 78 |
*/ |
| 79 |
public boolean makeColumnsEqualWidth = false; |
| 80 |
|
| 81 |
/** |
| 82 |
* marginWidth specifies the number of pixels of horizontal margin that will |
| 83 |
* be placed along the left and right edges of the layout. |
| 84 |
* |
| 85 |
* The default value is 5. |
| 86 |
*/ |
| 87 |
public int marginWidth = 5; |
| 88 |
|
| 89 |
/** |
| 90 |
* marginHeight specifies the number of pixels of vertical margin that will |
| 91 |
* be placed along the top and bottom edges of the layout. |
| 92 |
* |
| 93 |
* The default value is 5. |
| 94 |
*/ |
| 95 |
public int marginHeight = 5; |
| 96 |
|
| 97 |
/** |
| 98 |
* horizontalSpacing specifies the number of pixels between the right edge |
| 99 |
* of one cell and the left edge of its neighbouring cell to the right. |
| 100 |
* |
| 101 |
* The default value is 5. |
| 102 |
*/ |
| 103 |
public int horizontalSpacing = 5; |
| 104 |
|
| 105 |
/** |
| 106 |
* verticalSpacing specifies the number of pixels between the bottom edge of |
| 107 |
* one cell and the top edge of its neighbouring cell underneath. |
| 108 |
* |
| 109 |
* The default value is 5. |
| 110 |
*/ |
| 111 |
public int verticalSpacing = 5; |
| 112 |
|
| 113 |
/** The layout contraints */ |
| 114 |
protected Map constraints = new HashMap(); |
| 115 |
|
| 116 |
/** |
| 117 |
* Default Constructor |
| 118 |
*/ |
| 119 |
public GridLayout() { |
| 120 |
super(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Constructs a new instance of this class given the number of columns, and |
| 125 |
* whether or not the columns should be forced to have the same width. |
| 126 |
* |
| 127 |
* @param numColumns |
| 128 |
* the number of columns in the grid |
| 129 |
* @param makeColumnsEqualWidth |
| 130 |
* whether or not the columns will have equal width |
| 131 |
* |
| 132 |
*/ |
| 133 |
public GridLayout(int numColumns, boolean makeColumnsEqualWidth) { |
| 134 |
this.numColumns = numColumns; |
| 135 |
this.makeColumnsEqualWidth = makeColumnsEqualWidth; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @param child |
| 140 |
* @param wHint |
| 141 |
* @param hHint |
| 142 |
* @return |
| 143 |
*/ |
| 144 |
protected Dimension getChildSize(IFigure child, int wHint, int hHint) { |
| 145 |
return child.getPreferredSize(wHint, hHint); |
| 146 |
} |
| 147 |
|
| 148 |
GridLayoutData getData(IFigure[][] grid, int row, int column, int rowCount, |
| 149 |
int columnCount, boolean first) { |
| 150 |
IFigure figure = grid[row][column]; |
| 151 |
if (figure != null) { |
| 152 |
GridLayoutData data = (GridLayoutData) getConstraint(figure); |
| 153 |
int hSpan = Math.max(1, Math.min(data.horizontalSpan, columnCount)); |
| 154 |
int vSpan = Math.max(1, data.verticalSpan); |
| 155 |
int i = first ? row + vSpan - 1 : row - vSpan + 1; |
| 156 |
int j = first ? column + hSpan - 1 : column - hSpan + 1; |
| 157 |
if (0 <= i && i < rowCount) { |
| 158 |
if (0 <= j && j < columnCount) { |
| 159 |
if (figure == grid[i][j]) |
| 160 |
return data; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
return null; |
| 165 |
} |
| 166 |
|
| 167 |
void initChildren(IFigure container) |
| 168 |
{ |
| 169 |
List children = container.getChildren(); |
| 170 |
for (int i = 0; i < children.size(); i++) { |
| 171 |
IFigure child = (IFigure) children.get(i); |
| 172 |
if (child.getLayoutManager()==null) |
| 173 |
child.setLayoutManager(this); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/* |
| 178 |
* (non-Javadoc) |
| 179 |
* |
| 180 |
* @see org.eclipse.draw2d.AbstractLayout#calculatePreferredSize(org.eclipse.draw2d.IFigure, |
| 181 |
* int, int) |
| 182 |
*/ |
| 183 |
protected Dimension calculatePreferredSize(IFigure container, int wHint, |
| 184 |
int hHint) { |
| 185 |
Dimension size = layout(container, false, 0, 0, wHint, hHint, /* flushCache */ |
| 186 |
true); |
| 187 |
if (wHint != SWT.DEFAULT) |
| 188 |
size.width = wHint; |
| 189 |
if (hHint != SWT.DEFAULT) |
| 190 |
size.height = hHint; |
| 191 |
|
| 192 |
return size; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* (non-Javadoc) |
| 197 |
* |
| 198 |
* @see org.eclipse.draw2d.LayoutManager#layout(org.eclipse.draw2d.IFigure) |
| 199 |
*/ |
| 200 |
public void layout(IFigure container) { |
| 201 |
//initChildren( container); |
| 202 |
Rectangle rect = container.getClientArea(); |
| 203 |
layout(container, true, rect.x, rect.y, rect.width, rect.height, /* flushCache */ |
| 204 |
true); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
Dimension layout(IFigure container, boolean move, int x, int y, int width, |
| 209 |
int height, boolean flushCache) { |
| 210 |
if (numColumns < 1) |
| 211 |
return new Dimension(marginWidth * 2, marginHeight * 2); |
| 212 |
List children = container.getChildren(); |
| 213 |
for (int i = 0; i < children.size(); i++) { |
| 214 |
IFigure child = (IFigure) children.get(i); |
| 215 |
|
| 216 |
GridLayoutData data = (GridLayoutData) getConstraint(child); |
| 217 |
if (data == null) |
| 218 |
setConstraint(child, data = new GridLayoutData()); |
| 219 |
if (flushCache) |
| 220 |
data.flushCache(); |
| 221 |
data.computeSize(child, flushCache); |
| 222 |
} |
| 223 |
|
| 224 |
/* Build the grid */ |
| 225 |
int row = 0, column = 0, rowCount = 0, columnCount = numColumns; |
| 226 |
IFigure[][] grid = new IFigure[4][columnCount]; |
| 227 |
for (int i = 0; i < children.size(); i++) { |
| 228 |
IFigure child = (IFigure) children.get(i); |
| 229 |
GridLayoutData data = (GridLayoutData) getConstraint(child); |
| 230 |
int hSpan = Math.max(1, Math.min(data.horizontalSpan, columnCount)); |
| 231 |
int vSpan = Math.max(1, data.verticalSpan); |
| 232 |
while (true) { |
| 233 |
int lastRow = row + vSpan; |
| 234 |
if (lastRow >= grid.length) { |
| 235 |
IFigure[][] newGrid = new IFigure[lastRow + 4][columnCount]; |
| 236 |
System.arraycopy(grid, 0, newGrid, 0, grid.length); |
| 237 |
grid = newGrid; |
| 238 |
} |
| 239 |
if (grid[row] == null) { |
| 240 |
grid[row] = new IFigure[columnCount]; |
| 241 |
} |
| 242 |
while (column < columnCount && grid[row][column] != null) { |
| 243 |
column++; |
| 244 |
} |
| 245 |
int endCount = column + hSpan; |
| 246 |
if (endCount <= columnCount) { |
| 247 |
int index = column; |
| 248 |
while (index < endCount && grid[row][index] == null) { |
| 249 |
index++; |
| 250 |
} |
| 251 |
if (index == endCount) |
| 252 |
break; |
| 253 |
column = index; |
| 254 |
} |
| 255 |
if (column + hSpan >= columnCount) { |
| 256 |
column = 0; |
| 257 |
row++; |
| 258 |
} |
| 259 |
} |
| 260 |
for (int j = 0; j < vSpan; j++) { |
| 261 |
if (grid[row + j] == null) { |
| 262 |
grid[row + j] = new IFigure[columnCount]; |
| 263 |
} |
| 264 |
for (int k = 0; k < hSpan; k++) { |
| 265 |
grid[row + j][column + k] = child; |
| 266 |
} |
| 267 |
} |
| 268 |
rowCount = Math.max(rowCount, row + vSpan); |
| 269 |
column += hSpan; |
| 270 |
} |
| 271 |
|
| 272 |
/* Column widths */ |
| 273 |
int availableWidth = width - horizontalSpacing * (columnCount - 1) |
| 274 |
- marginWidth * 2; |
| 275 |
int expandCount = 0; |
| 276 |
int[] widths = new int[columnCount]; |
| 277 |
int[] minWidths = new int[columnCount]; |
| 278 |
boolean[] expandColumn = new boolean[columnCount]; |
| 279 |
for (int j = 0; j < columnCount; j++) { |
| 280 |
for (int i = 0; i < rowCount; i++) { |
| 281 |
GridLayoutData data = getData(grid, i, j, rowCount, columnCount, true); |
| 282 |
if (data != null) { |
| 283 |
int hSpan = Math.max(1, Math.min(data.horizontalSpan, |
| 284 |
columnCount)); |
| 285 |
if (hSpan == 1) { |
| 286 |
int w = data.cacheWidth + data.horizontalIndent; |
| 287 |
widths[j] = Math.max(widths[j], w); |
| 288 |
if (data.grabExcessHorizontalSpace) { |
| 289 |
if (!expandColumn[j]) |
| 290 |
expandCount++; |
| 291 |
expandColumn[j] = true; |
| 292 |
} |
| 293 |
if (data.widthHint != SWT.DEFAULT |
| 294 |
|| !data.grabExcessHorizontalSpace) { |
| 295 |
minWidths[j] = Math.max(minWidths[j], w); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
for (int i = 0; i < rowCount; i++) { |
| 301 |
GridLayoutData data = getData(grid, i, j, rowCount, columnCount, |
| 302 |
false); |
| 303 |
if (data != null) { |
| 304 |
int hSpan = Math.max(1, Math.min(data.horizontalSpan, |
| 305 |
columnCount)); |
| 306 |
if (hSpan > 1) { |
| 307 |
int spanWidth = 0, spanMinWidth = 0, spanExpandCount = 0; |
| 308 |
for (int k = 0; k < hSpan; k++) { |
| 309 |
spanWidth += widths[j - k]; |
| 310 |
spanMinWidth += minWidths[j - k]; |
| 311 |
if (expandColumn[j - k]) |
| 312 |
spanExpandCount++; |
| 313 |
} |
| 314 |
if (data.grabExcessHorizontalSpace |
| 315 |
&& spanExpandCount == 0) { |
| 316 |
expandCount++; |
| 317 |
expandColumn[j] = true; |
| 318 |
} |
| 319 |
int w = data.cacheWidth + data.horizontalIndent |
| 320 |
- spanWidth - (hSpan - 1) * horizontalSpacing; |
| 321 |
if (w > 0) { |
| 322 |
if (spanExpandCount == 0) { |
| 323 |
widths[j] += w; |
| 324 |
} else { |
| 325 |
int delta = w / spanExpandCount; |
| 326 |
int remainder = w % spanExpandCount, last = -1; |
| 327 |
for (int k = 0; k < hSpan; k++) { |
| 328 |
if (expandColumn[j - k]) { |
| 329 |
widths[last = j - k] += delta; |
| 330 |
} |
| 331 |
} |
| 332 |
if (last > -1) |
| 333 |
widths[last] += remainder; |
| 334 |
} |
| 335 |
} |
| 336 |
if (data.widthHint != SWT.DEFAULT |
| 337 |
|| !data.grabExcessHorizontalSpace) { |
| 338 |
w = data.cacheWidth + data.horizontalIndent |
| 339 |
- spanMinWidth - (hSpan - 1) |
| 340 |
* horizontalSpacing; |
| 341 |
if (w > 0) { |
| 342 |
if (spanExpandCount == 0) { |
| 343 |
minWidths[j] += w; |
| 344 |
} else { |
| 345 |
int delta = w / spanExpandCount; |
| 346 |
int remainder = w % spanExpandCount, last = -1; |
| 347 |
for (int k = 0; k < hSpan; k++) { |
| 348 |
if (expandColumn[j - k]) { |
| 349 |
minWidths[last = j - k] += delta; |
| 350 |
} |
| 351 |
} |
| 352 |
if (last > -1) |
| 353 |
minWidths[last] += remainder; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
if (makeColumnsEqualWidth) { |
| 362 |
int minColumnWidth = 0; |
| 363 |
int columnWidth = 0; |
| 364 |
for (int i = 0; i < columnCount; i++) { |
| 365 |
minColumnWidth = Math.max(minColumnWidth, minWidths[i]); |
| 366 |
columnWidth = Math.max(columnWidth, widths[i]); |
| 367 |
} |
| 368 |
columnWidth = width == SWT.DEFAULT || expandCount == 0 |
| 369 |
? columnWidth |
| 370 |
: Math.max(minColumnWidth, availableWidth / columnCount); |
| 371 |
for (int i = 0; i < columnCount; i++) { |
| 372 |
expandColumn[i] = expandCount > 0; |
| 373 |
widths[i] = columnWidth; |
| 374 |
} |
| 375 |
} else { |
| 376 |
if (width != SWT.DEFAULT && expandCount > 0) { |
| 377 |
int totalWidth = 0; |
| 378 |
for (int i = 0; i < columnCount; i++) { |
| 379 |
totalWidth += widths[i]; |
| 380 |
} |
| 381 |
int count = expandCount; |
| 382 |
int delta = (availableWidth - totalWidth) / count; |
| 383 |
int remainder = (availableWidth - totalWidth) % count; |
| 384 |
int last = -1; |
| 385 |
while (totalWidth != availableWidth) { |
| 386 |
for (int j = 0; j < columnCount; j++) { |
| 387 |
if (expandColumn[j]) { |
| 388 |
if (widths[j] + delta > minWidths[j]) { |
| 389 |
widths[last = j] = widths[j] + delta; |
| 390 |
} else { |
| 391 |
widths[j] = minWidths[j]; |
| 392 |
expandColumn[j] = false; |
| 393 |
count--; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
if (last > -1) |
| 398 |
widths[last] += remainder; |
| 399 |
|
| 400 |
for (int j = 0; j < columnCount; j++) { |
| 401 |
for (int i = 0; i < rowCount; i++) { |
| 402 |
GridLayoutData data = getData(grid, i, j, rowCount, |
| 403 |
columnCount, false); |
| 404 |
if (data != null) { |
| 405 |
int hSpan = Math.max(1, Math.min( |
| 406 |
data.horizontalSpan, columnCount)); |
| 407 |
if (hSpan > 1) { |
| 408 |
if (data.widthHint != SWT.DEFAULT |
| 409 |
|| !data.grabExcessHorizontalSpace) { |
| 410 |
int spanWidth = 0, spanExpandCount = 0; |
| 411 |
for (int k = 0; k < hSpan; k++) { |
| 412 |
spanWidth += widths[j - k]; |
| 413 |
if (expandColumn[j - k]) |
| 414 |
spanExpandCount++; |
| 415 |
} |
| 416 |
int w = data.cacheWidth |
| 417 |
+ data.horizontalIndent |
| 418 |
- spanWidth - (hSpan - 1) |
| 419 |
* horizontalSpacing; |
| 420 |
if (w > 0) { |
| 421 |
if (spanExpandCount == 0) { |
| 422 |
widths[j] += w; |
| 423 |
} else { |
| 424 |
int delta2 = w |
| 425 |
/ spanExpandCount; |
| 426 |
int remainder2 = w |
| 427 |
% spanExpandCount, last2 = -1; |
| 428 |
for (int k = 0; k < hSpan; k++) { |
| 429 |
if (expandColumn[j - k]) { |
| 430 |
widths[last2 = j - k] += delta2; |
| 431 |
} |
| 432 |
} |
| 433 |
if (last2 > -1) |
| 434 |
widths[last2] += remainder2; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
if (count == 0) |
| 443 |
break; |
| 444 |
totalWidth = 0; |
| 445 |
for (int i = 0; i < columnCount; i++) { |
| 446 |
totalWidth += widths[i]; |
| 447 |
} |
| 448 |
delta = (availableWidth - totalWidth) / count; |
| 449 |
remainder = (availableWidth - totalWidth) % count; |
| 450 |
last = -1; |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
/* Wrapping */ |
| 456 |
GridLayoutData[] flush = null; |
| 457 |
int flushLength = 0; |
| 458 |
if (width != SWT.DEFAULT) { |
| 459 |
for (int j = 0; j < columnCount; j++) { |
| 460 |
for (int i = 0; i < rowCount; i++) { |
| 461 |
GridLayoutData data = getData(grid, i, j, rowCount, columnCount, |
| 462 |
false); |
| 463 |
if (data != null) { |
| 464 |
if (data.heightHint == SWT.DEFAULT) { |
| 465 |
IFigure child = grid[i][j]; |
| 466 |
//TEMPORARY CODE |
| 467 |
int hSpan = Math.max(1, Math.min( |
| 468 |
data.horizontalSpan, columnCount)); |
| 469 |
int currentWidth = 0; |
| 470 |
for (int k = 0; k < hSpan; k++) { |
| 471 |
currentWidth += widths[j - k]; |
| 472 |
} |
| 473 |
currentWidth += (hSpan - 1) * horizontalSpacing |
| 474 |
- data.horizontalIndent; |
| 475 |
if ((currentWidth != data.cacheWidth && data.horizontalAlignment == GridLayoutData.FILL) |
| 476 |
|| (data.cacheWidth > currentWidth)) { |
| 477 |
int trim = 0; |
| 478 |
/* |
| 479 |
// *Note*: Left this in place from SWT |
| 480 |
// GridLayout. Not sure if Draw2D Borders or |
| 481 |
// Scrollbars 'trim' will need to be takeninto account. |
| 482 |
|
| 483 |
if (child instanceof Group) { |
| 484 |
Group g =(Group)child; trim = g.getSize ().x - |
| 485 |
g.getClientArea ().width; |
| 486 |
} else if (child instanceof Scrollable) { |
| 487 |
Rectangle rect = |
| 488 |
((Scrollable) child).computeTrim (0, 0, 0,0); |
| 489 |
trim = rect.width; } |
| 490 |
else { |
| 491 |
trim = child.getBorderWidth () * 2; |
| 492 |
} |
| 493 |
*/ |
| 494 |
int oldWidthHint = data.widthHint; |
| 495 |
data.widthHint = Math.max(0, currentWidth |
| 496 |
- trim); |
| 497 |
data.cacheWidth = data.cacheHeight = SWT.DEFAULT; |
| 498 |
data.computeSize(child, false); |
| 499 |
data.widthHint = oldWidthHint; |
| 500 |
if (flush == null) |
| 501 |
flush = new GridLayoutData[children.size()]; |
| 502 |
flush[flushLength++] = data; |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
/* Row heights */ |
| 511 |
int availableHeight = height - verticalSpacing * (rowCount - 1) |
| 512 |
- marginHeight * 2; |
| 513 |
expandCount = 0; |
| 514 |
int[] heights = new int[rowCount]; |
| 515 |
int[] minHeights = new int[rowCount]; |
| 516 |
boolean[] expandRow = new boolean[rowCount]; |
| 517 |
for (int i = 0; i < rowCount; i++) { |
| 518 |
for (int j = 0; j < columnCount; j++) { |
| 519 |
GridLayoutData data = getData(grid, i, j, rowCount, columnCount, true); |
| 520 |
if (data != null) { |
| 521 |
int vSpan = Math.max(1, Math.min(data.verticalSpan, |
| 522 |
rowCount)); |
| 523 |
if (vSpan == 1) { |
| 524 |
int h = data.cacheHeight; // + data.verticalIndent; |
| 525 |
heights[i] = Math.max(heights[i], h); |
| 526 |
if (data.grabExcessVerticalSpace) { |
| 527 |
if (!expandRow[i]) |
| 528 |
expandCount++; |
| 529 |
expandRow[i] = true; |
| 530 |
} |
| 531 |
if (data.heightHint != SWT.DEFAULT |
| 532 |
|| !data.grabExcessVerticalSpace) { |
| 533 |
minHeights[i] = Math.max(minHeights[i], h); |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
} |
| 538 |
for (int j = 0; j < columnCount; j++) { |
| 539 |
GridLayoutData data = getData(grid, i, j, rowCount, columnCount, |
| 540 |
false); |
| 541 |
if (data != null) { |
| 542 |
int vSpan = Math.max(1, Math.min(data.verticalSpan, |
| 543 |
rowCount)); |
| 544 |
if (vSpan > 1) { |
| 545 |
int spanHeight = 0, spanMinHeight = 0, spanExpandCount = 0; |
| 546 |
for (int k = 0; k < vSpan; k++) { |
| 547 |
spanHeight += heights[i - k]; |
| 548 |
spanMinHeight += minHeights[i - k]; |
| 549 |
if (expandRow[i - k]) |
| 550 |
spanExpandCount++; |
| 551 |
} |
| 552 |
if (data.grabExcessVerticalSpace |
| 553 |
&& spanExpandCount == 0) { |
| 554 |
expandCount++; |
| 555 |
expandRow[i] = true; |
| 556 |
} |
| 557 |
int h = data.cacheHeight - spanHeight - (vSpan - 1) |
| 558 |
* verticalSpacing; // + data.verticalalIndent |
| 559 |
if (h > 0) { |
| 560 |
if (spanExpandCount == 0) { |
| 561 |
heights[i] += h; |
| 562 |
} else { |
| 563 |
int delta = h / spanExpandCount; |
| 564 |
int remainder = h % spanExpandCount, last = -1; |
| 565 |
for (int k = 0; k < vSpan; k++) { |
| 566 |
if (expandRow[i - k]) { |
| 567 |
heights[last = i - k] += delta; |
| 568 |
} |
| 569 |
} |
| 570 |
if (last > -1) |
| 571 |
heights[last] += remainder; |
| 572 |
} |
| 573 |
} |
| 574 |
if (data.heightHint != SWT.DEFAULT |
| 575 |
|| !data.grabExcessVerticalSpace) { |
| 576 |
h = data.cacheHeight - spanMinHeight - (vSpan - 1) |
| 577 |
* verticalSpacing; // + data.verticalIndent |
| 578 |
if (h > 0) { |
| 579 |
if (spanExpandCount == 0) { |
| 580 |
minHeights[i] += h; |
| 581 |
} else { |
| 582 |
int delta = h / spanExpandCount; |
| 583 |
int remainder = h % spanExpandCount, last = -1; |
| 584 |
for (int k = 0; k < vSpan; k++) { |
| 585 |
if (expandRow[i - k]) { |
| 586 |
minHeights[last = i - k] += delta; |
| 587 |
} |
| 588 |
} |
| 589 |
if (last > -1) |
| 590 |
minHeights[last] += remainder; |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
if (height != SWT.DEFAULT && expandCount > 0) { |
| 599 |
int totalHeight = 0; |
| 600 |
for (int i = 0; i < rowCount; i++) { |
| 601 |
totalHeight += heights[i]; |
| 602 |
} |
| 603 |
int count = expandCount; |
| 604 |
int delta = (availableHeight - totalHeight) / count; |
| 605 |
int remainder = (availableHeight - totalHeight) % count; |
| 606 |
int last = -1; |
| 607 |
while (totalHeight != availableHeight) { |
| 608 |
for (int i = 0; i < rowCount; i++) { |
| 609 |
if (expandRow[i]) { |
| 610 |
if (heights[i] + delta > minHeights[i]) { |
| 611 |
heights[last = i] = heights[i] + delta; |
| 612 |
} else { |
| 613 |
heights[i] = minHeights[i]; |
| 614 |
expandRow[i] = false; |
| 615 |
count--; |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
if (last > -1) |
| 620 |
heights[last] += remainder; |
| 621 |
|
| 622 |
for (int i = 0; i < rowCount; i++) { |
| 623 |
for (int j = 0; j < columnCount; j++) { |
| 624 |
GridLayoutData data = getData(grid, i, j, rowCount, |
| 625 |
columnCount, false); |
| 626 |
if (data != null) { |
| 627 |
int vSpan = Math.max(1, Math.min(data.verticalSpan, |
| 628 |
rowCount)); |
| 629 |
if (vSpan > 1) { |
| 630 |
if (data.heightHint != SWT.DEFAULT |
| 631 |
|| !data.grabExcessVerticalSpace) { |
| 632 |
int spanHeight = 0, spanExpandCount = 0; |
| 633 |
for (int k = 0; k < vSpan; k++) { |
| 634 |
spanHeight += heights[i - k]; |
| 635 |
if (expandRow[i - k]) |
| 636 |
spanExpandCount++; |
| 637 |
} |
| 638 |
int h = data.cacheHeight - spanHeight |
| 639 |
- (vSpan - 1) * verticalSpacing; // + |
| 640 |
// data.verticalIndent |
| 641 |
if (h > 0) { |
| 642 |
if (spanExpandCount == 0) { |
| 643 |
heights[i] += h; |
| 644 |
} else { |
| 645 |
int delta2 = h / spanExpandCount; |
| 646 |
int remainder2 = h |
| 647 |
% spanExpandCount, last2 = -1; |
| 648 |
for (int k = 0; k < vSpan; k++) { |
| 649 |
if (expandRow[i - k]) { |
| 650 |
heights[last2 = i - k] += delta2; |
| 651 |
} |
| 652 |
} |
| 653 |
if (last2 > -1) |
| 654 |
heights[last2] += remainder2; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
if (count == 0) |
| 663 |
break; |
| 664 |
totalHeight = 0; |
| 665 |
for (int i = 0; i < rowCount; i++) { |
| 666 |
totalHeight += heights[i]; |
| 667 |
} |
| 668 |
delta = (availableHeight - totalHeight) / count; |
| 669 |
remainder = (availableHeight - totalHeight) % count; |
| 670 |
last = -1; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
/* Position the IFigures */ |
| 675 |
if (move) { |
| 676 |
int gridY = y + marginHeight; |
| 677 |
for (int i = 0; i < rowCount; i++) { |
| 678 |
int gridX = x + marginWidth; |
| 679 |
for (int j = 0; j < columnCount; j++) { |
| 680 |
GridLayoutData data = getData(grid, i, j, rowCount, columnCount, |
| 681 |
true); |
| 682 |
if (data != null) { |
| 683 |
int hSpan = Math.max(1, Math.min(data.horizontalSpan, |
| 684 |
columnCount)); |
| 685 |
int vSpan = Math.max(1, data.verticalSpan); |
| 686 |
int cellWidth = 0, cellHeight = 0; |
| 687 |
for (int k = 0; k < hSpan; k++) { |
| 688 |
cellWidth += widths[j + k]; |
| 689 |
} |
| 690 |
for (int k = 0; k < vSpan; k++) { |
| 691 |
cellHeight += heights[i + k]; |
| 692 |
} |
| 693 |
cellWidth += horizontalSpacing * (hSpan - 1); |
| 694 |
int childX = gridX + data.horizontalIndent; |
| 695 |
int childWidth = Math.min(data.cacheWidth, cellWidth); |
| 696 |
switch (data.horizontalAlignment) { |
| 697 |
case GridLayoutData.CENTER : |
| 698 |
childX = gridX |
| 699 |
+ Math.max(0, |
| 700 |
(cellWidth - childWidth) / 2); |
| 701 |
break; |
| 702 |
case GridLayoutData.END : |
| 703 |
childX = gridX |
| 704 |
+ Math.max(0, cellWidth - childWidth); |
| 705 |
break; |
| 706 |
case GridLayoutData.FILL : |
| 707 |
childWidth = cellWidth - data.horizontalIndent; |
| 708 |
break; |
| 709 |
} |
| 710 |
cellHeight += verticalSpacing * (vSpan - 1); |
| 711 |
int childY = gridY; // + data.verticalIndent; |
| 712 |
int childHeight = Math |
| 713 |
.min(data.cacheHeight, cellHeight); |
| 714 |
switch (data.verticalAlignment) { |
| 715 |
case GridLayoutData.CENTER : |
| 716 |
childY = gridY |
| 717 |
+ Math.max(0, |
| 718 |
(cellHeight - childHeight) / 2); |
| 719 |
break; |
| 720 |
case GridLayoutData.END : |
| 721 |
childY = gridY |
| 722 |
+ Math.max(0, cellHeight - childHeight); |
| 723 |
break; |
| 724 |
case GridLayoutData.FILL : |
| 725 |
childHeight = cellHeight; // - |
| 726 |
// data.verticalIndent; |
| 727 |
break; |
| 728 |
} |
| 729 |
IFigure child = grid[i][j]; |
| 730 |
if (child != null) { |
| 731 |
// following param could be replaced by |
| 732 |
// Rectangle.SINGLETON |
| 733 |
child.setBounds(new Rectangle(childX, childY, |
| 734 |
childWidth, childHeight)); |
| 735 |
} |
| 736 |
} |
| 737 |
gridX += widths[j] + horizontalSpacing; |
| 738 |
} |
| 739 |
gridY += heights[i] + verticalSpacing; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
// clean up cache |
| 744 |
for (int i = 0; i < flushLength; i++) { |
| 745 |
flush[i].cacheWidth = flush[i].cacheHeight = -1; |
| 746 |
} |
| 747 |
|
| 748 |
int totalDefaultWidth = 0; |
| 749 |
int totalDefaultHeight = 0; |
| 750 |
for (int i = 0; i < columnCount; i++) { |
| 751 |
totalDefaultWidth += widths[i]; |
| 752 |
} |
| 753 |
for (int i = 0; i < rowCount; i++) { |
| 754 |
totalDefaultHeight += heights[i]; |
| 755 |
} |
| 756 |
totalDefaultWidth += horizontalSpacing * (columnCount - 1) |
| 757 |
+ marginWidth * 2; |
| 758 |
totalDefaultHeight += verticalSpacing * (rowCount - 1) + marginHeight |
| 759 |
* 2; |
| 760 |
return new Dimension(totalDefaultWidth, totalDefaultHeight); |
| 761 |
} |
| 762 |
|
| 763 |
/* |
| 764 |
* (non-Javadoc) |
| 765 |
* |
| 766 |
* @see org.eclipse.draw2d.LayoutManager#getConstraint(org.eclipse.draw2d.IFigure) |
| 767 |
*/ |
| 768 |
public Object getConstraint(IFigure child) { |
| 769 |
return constraints.get(child); |
| 770 |
} |
| 771 |
|
| 772 |
/** |
| 773 |
* Sets the layout constraint of the given figure. The constraints can only |
| 774 |
* be of type {@link GridLayoutData}. |
| 775 |
* |
| 776 |
* @see LayoutManager#setConstraint(IFigure, Object) |
| 777 |
*/ |
| 778 |
public void setConstraint(IFigure figure, Object newConstraint) { |
| 779 |
super.setConstraint(figure, newConstraint); |
| 780 |
if (newConstraint != null) |
| 781 |
{ |
| 782 |
constraints.put(figure, newConstraint); |
| 783 |
|
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
} |