1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client.ui;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.ComponentConnector;
import com.vaadin.terminal.gwt.client.ConnectorMap;
import com.vaadin.terminal.gwt.client.LayoutManager;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.Util;
import com.vaadin.terminal.gwt.client.VCaption;
import com.vaadin.terminal.gwt.client.ui.layout.ComponentConnectorLayoutSlot;
import com.vaadin.terminal.gwt.client.ui.layout.VLayoutSlot;
public class VGridLayout extends ComplexPanel {
public static final String CLASSNAME = "v-gridlayout";
ApplicationConnection client;
HashMap<Widget, Cell> widgetToCell = new HashMap<Widget, Cell>();
int[] columnWidths;
int[] rowHeights;
int[] colExpandRatioArray;
int[] rowExpandRatioArray;
int[] minColumnWidths;
private int[] minRowHeights;
DivElement spacingMeasureElement;
public VGridLayout() {
super();
setElement(Document.get().createDivElement());
spacingMeasureElement = Document.get().createDivElement();
Style spacingStyle = spacingMeasureElement.getStyle();
spacingStyle.setPosition(Position.ABSOLUTE);
getElement().appendChild(spacingMeasureElement);
setStyleName(CLASSNAME);
}
private GridLayoutConnector getConnector() {
return (GridLayoutConnector) ConnectorMap.get(client)
.getConnector(this);
}
/**
* Returns the column widths measured in pixels
*
* @return
*/
protected int[] getColumnWidths() {
return columnWidths;
}
/**
* Returns the row heights measured in pixels
*
* @return
*/
protected int[] getRowHeights() {
return rowHeights;
}
/**
* Returns the spacing between the cells horizontally in pixels
*
* @return
*/
protected int getHorizontalSpacing() {
return LayoutManager.get(client).getOuterWidth(spacingMeasureElement);
}
/**
* Returns the spacing between the cells vertically in pixels
*
* @return
*/
protected int getVerticalSpacing() {
return LayoutManager.get(client).getOuterHeight(spacingMeasureElement);
}
static int[] cloneArray(int[] toBeCloned) {
int[] clone = new int[toBeCloned.length];
for (int i = 0; i < clone.length; i++) {
clone[i] = toBeCloned[i] * 1;
}
return clone;
}
void expandRows() {
if (!isUndefinedHeight()) {
int usedSpace = minRowHeights[0];
int verticalSpacing = getVerticalSpacing();
for (int i = 1; i < minRowHeights.length; i++) {
usedSpace += verticalSpacing + minRowHeights[i];
}
int availableSpace = LayoutManager.get(client).getInnerHeight(
getElement());
int excessSpace = availableSpace - usedSpace;
int distributed = 0;
if (excessSpace > 0) {
for (int i = 0; i < rowHeights.length; i++) {
int ew = excessSpace * rowExpandRatioArray[i] / 1000;
rowHeights[i] = minRowHeights[i] + ew;
distributed += ew;
}
excessSpace -= distributed;
int c = 0;
while (excessSpace > 0) {
rowHeights[c % rowHeights.length]++;
excessSpace--;
c++;
}
}
}
}
void updateHeight() {
// Detect minimum heights & calculate spans
detectRowHeights();
// Expand
expandRows();
// Position
layoutCellsVertically();
}
void updateWidth() {
// Detect widths & calculate spans
detectColWidths();
// Expand
expandColumns();
// Position
layoutCellsHorizontally();
}
void expandColumns() {
if (!isUndefinedWidth()) {
int usedSpace = minColumnWidths[0];
int horizontalSpacing = getHorizontalSpacing();
for (int i = 1; i < minColumnWidths.length; i++) {
usedSpace += horizontalSpacing + minColumnWidths[i];
}
int availableSpace = LayoutManager.get(client).getInnerWidth(
getElement());
int excessSpace = availableSpace - usedSpace;
int distributed = 0;
if (excessSpace > 0) {
for (int i = 0; i < columnWidths.length; i++) {
int ew = excessSpace * colExpandRatioArray[i] / 1000;
columnWidths[i] = minColumnWidths[i] + ew;
distributed += ew;
}
excessSpace -= distributed;
int c = 0;
while (excessSpace > 0) {
columnWidths[c % columnWidths.length]++;
excessSpace--;
c++;
}
}
}
}
void layoutCellsVertically() {
int verticalSpacing = getVerticalSpacing();
LayoutManager layoutManager = LayoutManager.get(client);
Element element = getElement();
int paddingTop = layoutManager.getPaddingTop(element);
int y = paddingTop;
for (int i = 0; i < cells.length; i++) {
y = paddingTop;
for (int j = 0; j < cells[i].length; j++) {
Cell cell = cells[i][j];
if (cell != null) {
cell.layoutVertically(y);
}
y += rowHeights[j] + verticalSpacing;
}
}
if (isUndefinedHeight()) {
int outerHeight = y - verticalSpacing
+ layoutManager.getPaddingBottom(element)
+ layoutManager.getBorderHeight(element);
element.getStyle().setHeight(outerHeight, Unit.PX);
}
}
void layoutCellsHorizontally() {
LayoutManager layoutManager = LayoutManager.get(client);
Element element = getElement();
int x = layoutManager.getPaddingLeft(element);
int horizontalSpacing = getHorizontalSpacing();
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
Cell cell = cells[i][j];
if (cell != null) {
cell.layoutHorizontally(x);
}
}
x += columnWidths[i] + horizontalSpacing;
}
if (isUndefinedWidth()) {
int outerWidth = x - horizontalSpacing
+ layoutManager.getPaddingRight(element)
+ layoutManager.getBorderWidth(element);
element.getStyle().setWidth(outerWidth, Unit.PX);
}
}
private boolean isUndefinedHeight() {
return getConnector().isUndefinedHeight();
}
private boolean isUndefinedWidth() {
return getConnector().isUndefinedWidth();
}
private void detectRowHeights() {
for (int i = 0; i < rowHeights.length; i++) {
rowHeights[i] = 0;
}
// collect min rowheight from non-rowspanned cells
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
Cell cell = cells[i][j];
if (cell != null) {
if (cell.rowspan == 1) {
if (!cell.hasRelativeHeight()
&& rowHeights[j] < cell.getHeight()) {
rowHeights[j] = cell.getHeight();
}
} else {
storeRowSpannedCell(cell);
}
}
}
}
distributeRowSpanHeights();
minRowHeights = cloneArray(rowHeights);
}
private void detectColWidths() {
// collect min colwidths from non-colspanned cells
for (int i = 0; i < columnWidths.length; i++) {
columnWidths[i] = 0;
}
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
Cell cell = cells[i][j];
if (cell != null) {
if (cell.colspan == 1) {
if (!cell.hasRelativeWidth()
&& columnWidths[i] < cell.getWidth()) {
columnWidths[i] = cell.getWidth();
}
} else {
storeColSpannedCell(cell);
}
}
}
}
distributeColSpanWidths();
minColumnWidths = cloneArray(columnWidths);
}
private void storeRowSpannedCell(Cell cell) {
SpanList l = null;
for (SpanList list : rowSpans) {
if (list.span < cell.rowspan) {
continue;
} else {
// insert before this
l = list;
break;
}
}
if (l == null) {
l = new SpanList(cell.rowspan);
rowSpans.add(l);
} else if (l.span != cell.rowspan) {
SpanList newL = new SpanList(cell.rowspan);
rowSpans.add(rowSpans.indexOf(l), newL);
l = newL;
}
l.cells.add(cell);
}
/**
* Iterates colspanned cells, ensures cols have enough space to accommodate
* them
*/
void distributeColSpanWidths() {
for (SpanList list : colSpans) {
for (Cell cell : list.cells) {
// cells with relative content may return non 0 here if on
// subsequent renders
int width = cell.hasRelativeWidth() ? 0 : cell.getWidth();
distributeSpanSize(columnWidths, cell.col, cell.colspan,
getHorizontalSpacing(), width, colExpandRatioArray);
}
}
}
/**
* Iterates rowspanned cells, ensures rows have enough space to accommodate
* them
*/
private void distributeRowSpanHeights() {
for (SpanList list : rowSpans) {
for (Cell cell : list.cells) {
// cells with relative content may return non 0 here if on
// subsequent renders
int height = cell.hasRelativeHeight() ? 0 : cell.getHeight();
distributeSpanSize(rowHeights, cell.row, cell.rowspan,
getVerticalSpacing(), height, rowExpandRatioArray);
}
}
}
private static void distributeSpanSize(int[] dimensions,
int spanStartIndex, int spanSize, int spacingSize, int size,
int[] expansionRatios) {
int allocated = dimensions[spanStartIndex];
for (int i = 1; i < spanSize; i++) {
allocated += spacingSize + dimensions[spanStartIndex + i];
}
if (allocated < size) {
// dimensions needs to be expanded due spanned cell
int neededExtraSpace = size - allocated;
int allocatedExtraSpace = 0;
// Divide space according to expansion ratios if any span has a
// ratio
int totalExpansion = 0;
for (int i = 0; i < spanSize; i++) {
int itemIndex = spanStartIndex + i;
totalExpansion += expansionRatios[itemIndex];
}
for (int i = 0; i < spanSize; i++) {
int itemIndex = spanStartIndex + i;
int expansion;
if (totalExpansion == 0) {
// Divide equally among all cells if there are no
// expansion ratios
expansion = neededExtraSpace / spanSize;
} else {
expansion = neededExtraSpace * expansionRatios[itemIndex]
/ totalExpansion;
}
dimensions[itemIndex] += expansion;
allocatedExtraSpace += expansion;
}
// We might still miss a couple of pixels because of
// rounding errors...
if (neededExtraSpace > allocatedExtraSpace) {
for (int i = 0; i < spanSize; i++) {
// Add one pixel to every cell until we have
// compensated for any rounding error
int itemIndex = spanStartIndex + i;
dimensions[itemIndex] += 1;
allocatedExtraSpace += 1;
if (neededExtraSpace == allocatedExtraSpace) {
break;
}
}
}
}
}
private LinkedList<SpanList> colSpans = new LinkedList<SpanList>();
private LinkedList<SpanList> rowSpans = new LinkedList<SpanList>();
private class SpanList {
final int span;
List<Cell> cells = new LinkedList<Cell>();
public SpanList(int span) {
this.span = span;
}
}
void storeColSpannedCell(Cell cell) {
SpanList l = null;
for (SpanList list : colSpans) {
if (list.span < cell.colspan) {
continue;
} else {
// insert before this
l = list;
break;
}
}
if (l == null) {
l = new SpanList(cell.colspan);
colSpans.add(l);
} else if (l.span != cell.colspan) {
SpanList newL = new SpanList(cell.colspan);
colSpans.add(colSpans.indexOf(l), newL);
l = newL;
}
l.cells.add(cell);
}
Cell[][] cells;
/**
* Private helper class.
*/
class Cell {
public Cell(UIDL c) {
row = c.getIntAttribute("y");
col = c.getIntAttribute("x");
updateFromUidl(c);
}
public boolean hasContent() {
return hasContent;
}
public boolean hasRelativeHeight() {
if (slot != null) {
return slot.getChild().isRelativeHeight();
} else {
return true;
}
}
/**
* @return total of spanned cols
*/
private int getAvailableWidth() {
int width = columnWidths[col];
for (int i = 1; i < colspan; i++) {
width += getHorizontalSpacing() + columnWidths[col + i];
}
return width;
}
/**
* @return total of spanned rows
*/
private int getAvailableHeight() {
int height = rowHeights[row];
for (int i = 1; i < rowspan; i++) {
height += getVerticalSpacing() + rowHeights[row + i];
}
return height;
}
public void layoutHorizontally(int x) {
if (slot != null) {
slot.positionHorizontally(x, getAvailableWidth());
}
}
public void layoutVertically(int y) {
if (slot != null) {
slot.positionVertically(y, getAvailableHeight());
}
}
public int getWidth() {
if (slot != null) {
return slot.getUsedWidth();
} else {
return 0;
}
}
public int getHeight() {
if (slot != null) {
return slot.getUsedHeight();
} else {
return 0;
}
}
protected boolean hasRelativeWidth() {
if (slot != null) {
return slot.getChild().isRelativeWidth();
} else {
return true;
}
}
final int row;
final int col;
int colspan = 1;
int rowspan = 1;
private boolean hasContent;
private AlignmentInfo alignment;
ComponentConnectorLayoutSlot slot;
public void updateFromUidl(UIDL cellUidl) {
// Set cell width
colspan = cellUidl.hasAttribute("w") ? cellUidl
.getIntAttribute("w") : 1;
// Set cell height
rowspan = cellUidl.hasAttribute("h") ? cellUidl
.getIntAttribute("h") : 1;
// ensure we will lose reference to old cells, now overlapped by
// this cell
for (int i = 0; i < colspan; i++) {
for (int j = 0; j < rowspan; j++) {
if (i > 0 || j > 0) {
cells[col + i][row + j] = null;
}
}
}
UIDL childUidl = cellUidl.getChildUIDL(0); // we are interested
// about childUidl
hasContent = childUidl != null;
if (hasContent) {
ComponentConnector childConnector = client
.getPaintable(childUidl);
if (slot == null || slot.getChild() != childConnector) {
slot = new ComponentConnectorLayoutSlot(CLASSNAME,
childConnector, getConnector());
Element slotWrapper = slot.getWrapperElement();
getElement().appendChild(slotWrapper);
Widget widget = childConnector.getWidget();
insert(widget, slotWrapper, getWidgetCount(), false);
Cell oldCell = widgetToCell.put(widget, this);
if (oldCell != null) {
oldCell.slot.getWrapperElement().removeFromParent();
oldCell.slot = null;
}
}
childConnector.updateFromUIDL(childUidl, client);
}
}
public void setAlignment(AlignmentInfo alignmentInfo) {
slot.setAlignment(alignmentInfo);
}
}
Cell getCell(UIDL c) {
int row = c.getIntAttribute("y");
int col = c.getIntAttribute("x");
Cell cell = cells[col][row];
if (cell == null) {
cell = new Cell(c);
cells[col][row] = cell;
} else {
cell.updateFromUidl(c);
}
return cell;
}
/**
* Returns the deepest nested child component which contains "element". The
* child component is also returned if "element" is part of its caption.
*
* @param element
* An element that is a nested sub element of the root element in
* this layout
* @return The Paintable which the element is a part of. Null if the element
* belongs to the layout and not to a child.
*/
ComponentConnector getComponent(Element element) {
return Util.getConnectorForElement(client, this, element);
}
void setCaption(Widget widget, VCaption caption) {
VLayoutSlot slot = widgetToCell.get(widget).slot;
if (caption != null) {
// Logical attach.
getChildren().add(caption);
}
// Physical attach if not null, also removes old caption
slot.setCaption(caption);
if (caption != null) {
// Adopt.
adopt(caption);
}
}
private void togglePrefixedStyleName(String name, boolean enabled) {
if (enabled) {
addStyleDependentName(name);
} else {
removeStyleDependentName(name);
}
}
void updateMarginStyleNames(VMarginInfo marginInfo) {
togglePrefixedStyleName("margin-top", marginInfo.hasTop());
togglePrefixedStyleName("margin-right", marginInfo.hasRight());
togglePrefixedStyleName("margin-bottom", marginInfo.hasBottom());
togglePrefixedStyleName("margin-left", marginInfo.hasLeft());
}
void updateSpacingStyleName(boolean spacingEnabled) {
String styleName = getStylePrimaryName();
if (spacingEnabled) {
spacingMeasureElement.addClassName(styleName + "-spacing-on");
spacingMeasureElement.removeClassName(styleName + "-spacing-off");
} else {
spacingMeasureElement.removeClassName(styleName + "-spacing-on");
spacingMeasureElement.addClassName(styleName + "-spacing-off");
}
}
}
|