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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.client.ui;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.DomEvent.Type;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.Container;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.RenderSpace;
import com.vaadin.terminal.gwt.client.StyleConstants;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.Util;
import com.vaadin.terminal.gwt.client.ui.layout.CellBasedLayout;
import com.vaadin.terminal.gwt.client.ui.layout.ChildComponentContainer;
public class VGridLayout extends SimplePanel implements Paintable, Container {
public static final String CLASSNAME = "v-gridlayout";
public static final String CLICK_EVENT_IDENTIFIER = "click";
private DivElement margin = Document.get().createDivElement();
private final AbsolutePanel canvas = new AbsolutePanel();
private ApplicationConnection client;
protected HashMap<Widget, ChildComponentContainer> widgetToComponentContainer = new HashMap<Widget, ChildComponentContainer>();
private HashMap<Paintable, Cell> paintableToCell = new HashMap<Paintable, Cell>();
private int spacingPixelsHorizontal;
private int spacingPixelsVertical;
private int[] columnWidths;
private int[] rowHeights;
private String height;
private String width;
private int[] colExpandRatioArray;
private int[] rowExpandRatioArray;
private int[] minColumnWidths;
private int[] minRowHeights;
private boolean rendering;
private HashMap<Widget, ChildComponentContainer> nonRenderedWidgets;
private boolean sizeChangedDuringRendering = false;
private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(
this, CLICK_EVENT_IDENTIFIER) {
@Override
protected Paintable getChildComponent(Element element) {
return getComponent(element);
}
@Override
protected <H extends EventHandler> HandlerRegistration registerHandler(
H handler, Type<H> type) {
return addDomHandler(handler, type);
}
};
public VGridLayout() {
super();
getElement().appendChild(margin);
setStyleName(CLASSNAME);
setWidget(canvas);
}
@Override
protected Element getContainerElement() {
return margin.cast();
}
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
rendering = true;
this.client = client;
if (client.updateComponent(this, uidl, true)) {
rendering = false;
return;
}
clickEventHandler.handleEventHandlerRegistration(client);
canvas.setWidth("0px");
handleMargins(uidl);
detectSpacing(uidl);
int cols = uidl.getIntAttribute("w");
int rows = uidl.getIntAttribute("h");
columnWidths = new int[cols];
rowHeights = new int[rows];
if (cells == null) {
cells = new Cell[cols][rows];
} else if (cells.length != cols || cells[0].length != rows) {
Cell[][] newCells = new Cell[cols][rows];
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
if (i < cols && j < rows) {
newCells[i][j] = cells[i][j];
}
}
}
cells = newCells;
}
nonRenderedWidgets = (HashMap<Widget, ChildComponentContainer>) widgetToComponentContainer
.clone();
final int[] alignments = uidl.getIntArrayAttribute("alignments");
int alignmentIndex = 0;
LinkedList<Cell> pendingCells = new LinkedList<Cell>();
LinkedList<Cell> relativeHeighted = new LinkedList<Cell>();
for (final Iterator i = uidl.getChildIterator(); i.hasNext();) {
final UIDL r = (UIDL) i.next();
if ("gr".equals(r.getTag())) {
for (final Iterator j = r.getChildIterator(); j.hasNext();) {
final UIDL c = (UIDL) j.next();
if ("gc".equals(c.getTag())) {
Cell cell = getCell(c);
if (cell.hasContent()) {
boolean rendered = cell.renderIfNoRelativeWidth();
cell.alignment = alignments[alignmentIndex++];
if (!rendered) {
pendingCells.add(cell);
}
if (cell.colspan > 1) {
storeColSpannedCell(cell);
} else if (rendered) {
// strore non-colspanned widths to columnWidth
// array
if (columnWidths[cell.col] < cell.getWidth()) {
columnWidths[cell.col] = cell.getWidth();
}
}
if (cell.hasRelativeHeight()) {
relativeHeighted.add(cell);
}
}
}
}
}
}
distributeColSpanWidths();
colExpandRatioArray = uidl.getIntArrayAttribute("colExpand");
rowExpandRatioArray = uidl.getIntArrayAttribute("rowExpand");
minColumnWidths = cloneArray(columnWidths);
expandColumns();
renderRemainingComponentsWithNoRelativeHeight(pendingCells);
detectRowHeights();
expandRows();
renderRemainingComponents(pendingCells);
for (Cell cell : relativeHeighted) {
// rendering done above so cell.cc should not be null
Widget widget2 = cell.cc.getWidget();
client.handleComponentRelativeSize(widget2);
cell.cc.updateWidgetSize();
}
layoutCells();
// clean non rendered components
for (Widget w : nonRenderedWidgets.keySet()) {
ChildComponentContainer childComponentContainer = widgetToComponentContainer
.get(w);
paintableToCell.remove(w);
widgetToComponentContainer.remove(w);
childComponentContainer.removeFromParent();
client.unregisterPaintable((Paintable) w);
}
nonRenderedWidgets = null;
rendering = false;
sizeChangedDuringRendering = false;
boolean needsRelativeSizeCheck = false;
}
private 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;
}
private void expandRows() {
if (!"".equals(height)) {
int usedSpace = minRowHeights[0];
for (int i = 1; i < minRowHeights.length; i++) {
usedSpace += spacingPixelsVertical + minRowHeights[i];
}
int availableSpace = getOffsetHeight() - marginTopAndBottom;
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++;
}
}
}
}
@Override
public void setHeight(String height) {
super.setHeight(height);
if (!height.equals(this.height)) {
this.height = height;
if (rendering) {
sizeChangedDuringRendering = true;
} else {
expandRows();
layoutCells();
for (Paintable c : paintableToCell.keySet()) {
client.handleComponentRelativeSize((Widget) c);
}
}
}
}
@Override
public void setWidth(String width) {
super.setWidth(width);
if (!width.equals(this.width)) {
this.width = width;
if (rendering) {
sizeChangedDuringRendering = true;
} else {
int[] oldWidths = cloneArray(columnWidths);
expandColumns();
boolean heightChanged = false;
HashSet<Integer> dirtyRows = null;
for (int i = 0; i < oldWidths.length; i++) {
if (columnWidths[i] != oldWidths[i]) {
Cell[] column = cells[i];
for (int j = 0; j < column.length; j++) {
Cell c = column[j];
if (c != null && c.cc != null
&& c.widthCanAffectHeight()) {
c.cc.setContainerSize(c.getAvailableWidth(), c
.getAvailableHeight());
client.handleComponentRelativeSize(c.cc
.getWidget());
c.cc.updateWidgetSize();
int newHeight = c.getHeight();
if (columnWidths[i] < oldWidths[i]
&& newHeight > minRowHeights[j]
&& c.rowspan == 1) {
/*
* The width of this column was reduced and
* this affected the height. The height is
* now greater than the previously
* calculated minHeight for the row.
*/
minRowHeights[j] = newHeight;
if (newHeight > rowHeights[j]) {
/*
* The new height is greater than the
* previously calculated rowHeight -> we
* need to recalculate heights later on
*/
rowHeights[j] = newHeight;
heightChanged = true;
}
} else if (newHeight < minRowHeights[j]) {
/*
* The new height of the component is less
* than the previously calculated min row
* height. The min row height may be
* affected and must thus be recalculated
*/
if (dirtyRows == null) {
dirtyRows = new HashSet<Integer>();
}
dirtyRows.add(j);
}
}
}
}
}
if (dirtyRows != null) {
/* flag indicating that there is a potential row shrinking */
boolean rowMayShrink = false;
for (Integer rowIndex : dirtyRows) {
int oldMinimum = minRowHeights[rowIndex];
int newMinimum = 0;
for (int colIndex = 0; colIndex < columnWidths.length; colIndex++) {
Cell cell = cells[colIndex][rowIndex];
if (cell != null && !cell.hasRelativeHeight()
&& cell.getHeight() > newMinimum) {
newMinimum = cell.getHeight();
}
}
if (newMinimum < oldMinimum) {
minRowHeights[rowIndex] = rowHeights[rowIndex] = newMinimum;
rowMayShrink = true;
}
}
if (rowMayShrink) {
distributeRowSpanHeights();
minRowHeights = cloneArray(rowHeights);
heightChanged = true;
}
}
layoutCells();
for (Paintable c : paintableToCell.keySet()) {
client.handleComponentRelativeSize((Widget) c);
}
if (heightChanged && "".equals(height)) {
Util.notifyParentOfSizeChange(this, false);
}
}
}
}
private void expandColumns() {
if (!"".equals(width)) {
int usedSpace = minColumnWidths[0];
for (int i = 1; i < minColumnWidths.length; i++) {
usedSpace += spacingPixelsHorizontal + minColumnWidths[i];
}
canvas.setWidth("");
int availableSpace = canvas.getOffsetWidth();
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++;
}
}
}
}
private void layoutCells() {
int x = 0;
int y = 0;
for (int i = 0; i < cells.length; i++) {
y = 0;
for (int j = 0; j < cells[i].length; j++) {
Cell cell = cells[i][j];
if (cell != null) {
cell.layout(x, y);
}
y += rowHeights[j] + spacingPixelsVertical;
}
x += columnWidths[i] + spacingPixelsHorizontal;
}
if ("".equals(width)) {
canvas.setWidth((x - spacingPixelsHorizontal) + "px");
} else {
// main element defines width
canvas.setWidth("");
}
int canvasHeight;
if ("".equals(height)) {
canvasHeight = y - spacingPixelsVertical;
} else {
canvasHeight = getOffsetHeight() - marginTopAndBottom;
}
canvas.setHeight(canvasHeight + "px");
}
private void renderRemainingComponents(LinkedList<Cell> pendingCells) {
for (Cell cell : pendingCells) {
cell.render();
}
}
private void detectRowHeights() {
// 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) {
/*
* Setting fixing container width may in some situations
* affect height. Example: Label with wrapping text without
* or with relative width.
*/
if (cell.cc != null && cell.widthCanAffectHeight()) {
cell.cc.setWidth(cell.getAvailableWidth() + "px");
cell.cc.updateWidgetSize();
}
if (cell.rowspan == 1) {
if (!cell.hasRelativeHeight()
&& rowHeights[j] < cell.getHeight()) {
rowHeights[j] = cell.getHeight();
}
} else {
storeRowSpannedCell(cell);
}
}
}
}
distributeRowSpanHeights();
minRowHeights = cloneArray(rowHeights);
}
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);
}
private void renderRemainingComponentsWithNoRelativeHeight(
LinkedList<Cell> pendingCells) {
for (Iterator iterator = pendingCells.iterator(); iterator.hasNext();) {
Cell cell = (Cell) iterator.next();
if (!cell.hasRelativeHeight()) {
cell.render();
iterator.remove();
}
}
}
/**
* Iterates colspanned cells, ensures cols have enough space to accommodate
* them
*/
private void distributeColSpanWidths() {
for (SpanList list : colSpans) {
for (Cell cell : list.cells) {
int width = cell.getWidth();
int allocated = columnWidths[cell.col];
for (int i = 1; i < cell.colspan; i++) {
allocated += spacingPixelsHorizontal
+ columnWidths[cell.col + i];
}
if (allocated < width) {
// columnWidths needs to be expanded due colspanned cell
int neededExtraSpace = width - allocated;
int spaceForColunms = neededExtraSpace / cell.colspan;
for (int i = 0; i < cell.colspan; i++) {
int col = cell.col + i;
columnWidths[col] += spaceForColunms;
neededExtraSpace -= spaceForColunms;
}
if (neededExtraSpace > 0) {
for (int i = 0; i < cell.colspan; i++) {
int col = cell.col + i;
columnWidths[col] += 1;
neededExtraSpace -= 1;
if (neededExtraSpace == 0) {
break;
}
}
}
}
}
}
}
/**
* Iterates rowspanned cells, ensures rows have enough space to accommodate
* them
*/
private void distributeRowSpanHeights() {
for (SpanList list : rowSpans) {
for (Cell cell : list.cells) {
int height = cell.getHeight();
int allocated = rowHeights[cell.row];
for (int i = 1; i < cell.rowspan; i++) {
allocated += spacingPixelsVertical
+ rowHeights[cell.row + i];
}
if (allocated < height) {
// columnWidths needs to be expanded due colspanned cell
int neededExtraSpace = height - allocated;
int spaceForColunms = neededExtraSpace / cell.rowspan;
for (int i = 0; i < cell.rowspan; i++) {
int row = cell.row + i;
rowHeights[row] += spaceForColunms;
neededExtraSpace -= spaceForColunms;
}
if (neededExtraSpace > 0) {
for (int i = 0; i < cell.rowspan; i++) {
int row = cell.row + i;
rowHeights[row] += 1;
neededExtraSpace -= 1;
if (neededExtraSpace == 0) {
break;
}
}
}
}
}
}
}
private LinkedList<SpanList> colSpans = new LinkedList<SpanList>();
private LinkedList<SpanList> rowSpans = new LinkedList<SpanList>();
private int marginTopAndBottom;
private class SpanList {
final int span;
List<Cell> cells = new LinkedList<Cell>();
public SpanList(int span) {
this.span = span;
}
}
private 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);
}
private void detectSpacing(UIDL uidl) {
DivElement spacingmeter = Document.get().createDivElement();
spacingmeter.setClassName(CLASSNAME + "-" + "spacing-"
+ (uidl.getBooleanAttribute("spacing") ? "on" : "off"));
spacingmeter.getStyle().setProperty("width", "0");
spacingmeter.getStyle().setProperty("height", "0");
canvas.getElement().appendChild(spacingmeter);
spacingPixelsHorizontal = spacingmeter.getOffsetWidth();
spacingPixelsVertical = spacingmeter.getOffsetHeight();
canvas.getElement().removeChild(spacingmeter);
}
private void handleMargins(UIDL uidl) {
final VMarginInfo margins = new VMarginInfo(uidl
.getIntAttribute("margins"));
String styles = CLASSNAME + "-margin";
if (margins.hasTop()) {
styles += " " + CLASSNAME + "-" + StyleConstants.MARGIN_TOP;
}
if (margins.hasRight()) {
styles += " " + CLASSNAME + "-" + StyleConstants.MARGIN_RIGHT;
}
if (margins.hasBottom()) {
styles += " " + CLASSNAME + "-" + StyleConstants.MARGIN_BOTTOM;
}
if (margins.hasLeft()) {
styles += " " + CLASSNAME + "-" + StyleConstants.MARGIN_LEFT;
}
margin.setClassName(styles);
marginTopAndBottom = margin.getOffsetHeight()
- canvas.getOffsetHeight();
}
public boolean hasChildComponent(Widget component) {
return paintableToCell.containsKey(component);
}
public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
ChildComponentContainer componentContainer = widgetToComponentContainer
.remove(oldComponent);
if (componentContainer == null) {
return;
}
componentContainer.setWidget(newComponent);
widgetToComponentContainer.put(newComponent, componentContainer);
paintableToCell.put((Paintable) newComponent, paintableToCell
.get(oldComponent));
}
public void updateCaption(Paintable component, UIDL uidl) {
ChildComponentContainer cc = widgetToComponentContainer.get(component);
if (cc != null) {
cc.updateCaption(uidl, client);
}
if (!rendering) {
// ensure rel size details are updated
paintableToCell.get(component).updateRelSizeStatus(uidl);
}
}
public boolean requestLayout(final Set<Paintable> changedChildren) {
boolean needsLayout = false;
boolean reDistributeColSpanWidths = false;
boolean reDistributeRowSpanHeights = false;
int offsetHeight = canvas.getOffsetHeight();
int offsetWidth = canvas.getOffsetWidth();
if ("".equals(width) || "".equals(height)) {
needsLayout = true;
}
ArrayList<Integer> dirtyColumns = new ArrayList<Integer>();
ArrayList<Integer> dirtyRows = new ArrayList<Integer>();
for (Paintable paintable : changedChildren) {
Cell cell = paintableToCell.get(paintable);
if (!cell.hasRelativeHeight() || !cell.hasRelativeWidth()) {
// cell sizes will only stay still if only relatively
// sized components
// check if changed child affects min col widths
if (cell.cc != null) {
cell.cc.setWidth("");
cell.cc.setHeight("");
cell.cc.updateWidgetSize();
/*
* If this is the result of an caption icon onload event the
* caption size may have changed
*/
cell.cc.updateCaptionSize();
}
int width = cell.getWidth();
int allocated = columnWidths[cell.col];
for (int i = 1; i < cell.colspan; i++) {
allocated += spacingPixelsHorizontal
+ columnWidths[cell.col + i];
}
if (allocated < width) {
needsLayout = true;
if (cell.colspan == 1) {
// do simple column width expansion
columnWidths[cell.col] = minColumnWidths[cell.col] = width;
} else {
// mark that col span expansion is needed
reDistributeColSpanWidths = true;
}
} else if (allocated != width) {
// size is smaller thant allocated, column might
// shrink
dirtyColumns.add(cell.col);
}
int height = cell.getHeight();
allocated = rowHeights[cell.row];
for (int i = 1; i < cell.rowspan; i++) {
allocated += spacingPixelsVertical
+ rowHeights[cell.row + i];
}
if (allocated < height) {
needsLayout = true;
if (cell.rowspan == 1) {
// do simple row expansion
rowHeights[cell.row] = minRowHeights[cell.row] = height;
} else {
// mark that row span expansion is needed
reDistributeRowSpanHeights = true;
}
} else if (allocated != height) {
// size is smaller than allocated, row might shrink
dirtyRows.add(cell.row);
}
}
}
if (dirtyColumns.size() > 0) {
for (Integer colIndex : dirtyColumns) {
int colW = 0;
for (int i = 0; i < rowHeights.length; i++) {
Cell cell = cells[colIndex][i];
if (cell != null && cell.getChildUIDL() != null
&& !cell.hasRelativeWidth() && cell.colspan == 1) {
int width = cell.getWidth();
if (width > colW) {
colW = width;
}
}
}
minColumnWidths[colIndex] = colW;
}
needsLayout = true;
// ensure colspanned columns have enough space
columnWidths = cloneArray(minColumnWidths);
distributeColSpanWidths();
reDistributeColSpanWidths = false;
}
if (reDistributeColSpanWidths) {
distributeColSpanWidths();
}
if (dirtyRows.size() > 0) {
needsLayout = true;
for (Integer rowIndex : dirtyRows) {
// recalculate min row height
int rowH = minRowHeights[rowIndex] = 0;
// loop all columns on row rowIndex
for (int i = 0; i < columnWidths.length; i++) {
Cell cell = cells[i][rowIndex];
if (cell != null && cell.getChildUIDL() != null
&& !cell.hasRelativeHeight() && cell.rowspan == 1) {
int h = cell.getHeight();
if (h > rowH) {
rowH = h;
}
}
}
minRowHeights[rowIndex] = rowH;
}
// TODO could check only some row spans
rowHeights = cloneArray(minRowHeights);
distributeRowSpanHeights();
reDistributeRowSpanHeights = false;
}
if (reDistributeRowSpanHeights) {
distributeRowSpanHeights();
}
if (needsLayout) {
expandColumns();
expandRows();
layoutCells();
// loop all relative sized components and update their size
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.cc != null
&& (cell.hasRelativeHeight() || cell
.hasRelativeWidth())) {
client.handleComponentRelativeSize(cell.cc.getWidget());
}
}
}
}
if (canvas.getOffsetHeight() != offsetHeight
|| canvas.getOffsetWidth() != offsetWidth) {
return false;
} else {
return true;
}
}
public RenderSpace getAllocatedSpace(Widget child) {
Cell cell = paintableToCell.get(child);
assert cell != null;
return cell.getAllocatedSpace();
}
private Cell[][] cells;
/**
* Private helper class.
*/
private class Cell {
private boolean relHeight = false;
private boolean relWidth = false;
private boolean widthCanAffectHeight = false;
public Cell(UIDL c) {
row = c.getIntAttribute("y");
col = c.getIntAttribute("x");
setUidl(c);
}
public boolean widthCanAffectHeight() {
return widthCanAffectHeight;
}
public boolean hasRelativeHeight() {
return relHeight;
}
public RenderSpace getAllocatedSpace() {
if (cc != null) {
return new RenderSpace(getAvailableWidth()
- cc.getCaptionWidthAfterComponent(),
getAvailableHeight()
- cc.getCaptionHeightAboveComponent());
} else {
// this should not happen normally
return new RenderSpace(getAvailableWidth(),
getAvailableHeight());
}
}
public boolean hasContent() {
return childUidl != null;
}
/**
* @return total of spanned cols
*/
private int getAvailableWidth() {
int width = columnWidths[col];
for (int i = 1; i < colspan; i++) {
width += spacingPixelsHorizontal + 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 += spacingPixelsVertical + rowHeights[row + i];
}
return height;
}
public void layout(int x, int y) {
if (cc != null && cc.isAttached()) {
canvas.setWidgetPosition(cc, x, y);
cc.setContainerSize(getAvailableWidth(), getAvailableHeight());
cc.setAlignment(new AlignmentInfo(alignment));
cc.updateAlignments(getAvailableWidth(), getAvailableHeight());
}
}
public int getWidth() {
if (cc != null) {
int w = cc.getWidgetSize().getWidth()
+ cc.getCaptionWidthAfterComponent();
return w;
} else {
return 0;
}
}
public int getHeight() {
if (cc != null) {
return cc.getWidgetSize().getHeight()
+ cc.getCaptionHeightAboveComponent();
} else {
return 0;
}
}
public boolean renderIfNoRelativeWidth() {
if (childUidl == null) {
return false;
}
if (!hasRelativeWidth()) {
render();
return true;
} else {
return false;
}
}
protected boolean hasRelativeWidth() {
return relWidth;
}
protected void render() {
assert childUidl != null;
Paintable paintable = client.getPaintable(childUidl);
assert paintable != null;
if (cc == null || cc.getWidget() != paintable) {
if (widgetToComponentContainer.containsKey(paintable)) {
cc = widgetToComponentContainer.get(paintable);
cc.setWidth("");
cc.setHeight("");
} else {
cc = new ChildComponentContainer((Widget) paintable,
CellBasedLayout.ORIENTATION_VERTICAL);
widgetToComponentContainer.put((Widget) paintable, cc);
paintableToCell.put(paintable, this);
cc.setWidth("");
canvas.add(cc, 0, 0);
}
}
cc.renderChild(childUidl, client, -1);
if (sizeChangedDuringRendering && Util.isCached(childUidl)) {
client.handleComponentRelativeSize(cc.getWidget());
}
cc.updateWidgetSize();
nonRenderedWidgets.remove(paintable);
}
public UIDL getChildUIDL() {
return childUidl;
}
final int row;
final int col;
int colspan = 1;
int rowspan = 1;
UIDL childUidl;
int alignment;
// may be null after setUidl() if content has vanished or changed, set
// in render()
ChildComponentContainer cc;
public void setUidl(UIDL c) {
// Set cell width
colspan = c.hasAttribute("w") ? c.getIntAttribute("w") : 1;
// Set cell height
rowspan = c.hasAttribute("h") ? c.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;
}
}
}
c = c.getChildUIDL(0); // we are interested about childUidl
if (childUidl != null) {
if (c == null) {
// content has vanished, old content will be removed from
// canvas later during the render phase
cc = null;
} else if (cc != null
&& cc.getWidget() != client.getPaintable(c)) {
// content has changed
Paintable newPaintable = client.getPaintable(c);
if (widgetToComponentContainer.containsKey(newPaintable)) {
// if a key in the map, newPaintable must be a widget
replaceChildComponent(cc.getWidget(),
(Widget) newPaintable);
cc = widgetToComponentContainer.get(newPaintable);
cc.setWidth("");
cc.setHeight("");
} else {
cc = null;
}
}
}
childUidl = c;
updateRelSizeStatus(c);
}
protected void updateRelSizeStatus(UIDL uidl) {
if (uidl != null && !uidl.getBooleanAttribute("cached")) {
if (uidl.hasAttribute("height")
&& uidl.getStringAttribute("height").contains("%")) {
relHeight = true;
} else {
relHeight = false;
}
if (uidl.hasAttribute("width")) {
widthCanAffectHeight = relWidth = uidl.getStringAttribute(
"width").contains("%");
if (uidl.hasAttribute("height")) {
widthCanAffectHeight = false;
}
} else {
widthCanAffectHeight = !uidl.hasAttribute("height");
relWidth = false;
}
}
}
}
private 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.setUidl(c);
}
return cell;
}
/**
* Returns the 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 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.
*/
private Paintable getComponent(Element element) {
return Util.getChildPaintableForElement(client, this, element);
}
}
|