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
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
|
/*******************************************************************************
* Copyright (c) 2012 BestSolution.at and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
*******************************************************************************/
package org.eclipse.fx.ui.panes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.css.CssMetaData;
import javafx.css.SimpleStyleableBooleanProperty;
import javafx.css.SimpleStyleableIntegerProperty;
import javafx.css.StyleConverter;
import javafx.css.Styleable;
import javafx.css.StyleableProperty;
import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
import javafx.scene.Node;
/**
* Layout items from top to bottom or left to right
*/
public class RowLayoutPane extends AbstractLayoutPane<RowData> {
private static final CssMetaData<RowLayoutPane, Number> MARGIN_WIDTH = new CssMetaData<RowLayoutPane, Number>("-fx-inner-margin-width", StyleConverter.getSizeConverter(), Integer.valueOf(0)) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.marginWidthProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Number> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Number>) node.marginWidthProperty();
}
};
private static final CssMetaData<RowLayoutPane, Number> MARGIN_HEIGHT = new CssMetaData<RowLayoutPane, Number>("-fx-inner-margin-height", StyleConverter.getSizeConverter(), Integer.valueOf(0)) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.marginHeightProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Number> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Number>) node.marginHeightProperty();
}
};
private static final CssMetaData<RowLayoutPane, Number> MARGIN_LEFT = new CssMetaData<RowLayoutPane, Number>("-fx-inner-margin-left", StyleConverter.getSizeConverter(), Integer.valueOf(0)) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.marginLeftProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Number> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Number>) node.marginLeftProperty();
}
};
private static final CssMetaData<RowLayoutPane, Number> MARGIN_TOP = new CssMetaData<RowLayoutPane, Number>("-fx-inner-margin-top", StyleConverter.getSizeConverter(), Integer.valueOf(0)) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.marginTopProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Number> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Number>) node.marginTopProperty();
}
};
private static final CssMetaData<RowLayoutPane, Number> MARGIN_RIGHT = new CssMetaData<RowLayoutPane, Number>("-fx-inner-margin-right", StyleConverter.getSizeConverter(), Integer.valueOf(0)) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.marginRightProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Number> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Number>) node.marginRightProperty();
}
};
private static final CssMetaData<RowLayoutPane, Number> MARGIN_BOTTOM = new CssMetaData<RowLayoutPane, Number>("-fx-inner-margin-bottom", StyleConverter.getSizeConverter(), Integer.valueOf(0)) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.marginBottomProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Number> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Number>) node.marginBottomProperty();
}
};
private static final CssMetaData<RowLayoutPane, Number> SPACING = new CssMetaData<RowLayoutPane, Number>("-fx-spacing", StyleConverter.getSizeConverter(), Integer.valueOf(3)) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.spacingProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Number> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Number>) node.spacingProperty();
}
};
private static final CssMetaData<RowLayoutPane, Boolean> WRAP = new CssMetaData<RowLayoutPane, Boolean>("-fx-wrap", StyleConverter.getBooleanConverter(), Boolean.TRUE) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.wrapProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Boolean> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Boolean>) node.wrapProperty();
}
};
private static final CssMetaData<RowLayoutPane, Boolean> PACK = new CssMetaData<RowLayoutPane, Boolean>("-fx-pack", StyleConverter.getBooleanConverter(), Boolean.TRUE) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.packProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Boolean> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Boolean>) node.packProperty();
}
};
private static final CssMetaData<RowLayoutPane, Boolean> FILL = new CssMetaData<RowLayoutPane, Boolean>("-fx-fill", StyleConverter.getBooleanConverter(), Boolean.FALSE) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.fillProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Boolean> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Boolean>) node.fillProperty();
}
};
private static final CssMetaData<RowLayoutPane, Boolean> CENTER = new CssMetaData<RowLayoutPane, Boolean>("-fx-center", StyleConverter.getBooleanConverter(), Boolean.FALSE) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.centerProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Boolean> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Boolean>) node.centerProperty();
}
};
private static final CssMetaData<RowLayoutPane, Boolean> JUSTIFY = new CssMetaData<RowLayoutPane, Boolean>("-fx-justify", StyleConverter.getBooleanConverter(), Boolean.FALSE) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.justifyProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Boolean> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Boolean>) node.justifyProperty();
}
};
private static final CssMetaData<RowLayoutPane, Boolean> HORIZONTAL = new CssMetaData<RowLayoutPane, Boolean>("-fx-horizontal", StyleConverter.getBooleanConverter(), Boolean.FALSE) { //$NON-NLS-1$
@Override
public boolean isSettable(RowLayoutPane node) {
return !node.horizontalProperty().isBound();
}
@SuppressWarnings("unchecked")
@Override
public StyleableProperty<Boolean> getStyleableProperty(RowLayoutPane node) {
return (StyleableProperty<Boolean>) node.horizontalProperty();
}
};
private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
static {
@SuppressWarnings("static-access")
final List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<CssMetaData<? extends Styleable, ?>>(AbstractLayoutPane.getClassCssMetaData());
styleables.add(CENTER);
styleables.add(FILL);
styleables.add(HORIZONTAL);
styleables.add(JUSTIFY);
styleables.add(MARGIN_BOTTOM);
styleables.add(MARGIN_HEIGHT);
styleables.add(MARGIN_LEFT);
styleables.add(MARGIN_RIGHT);
styleables.add(MARGIN_TOP);
styleables.add(PACK);
styleables.add(SPACING);
styleables.add(WRAP);
STYLEABLES = Collections.unmodifiableList(styleables);
}
public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
return STYLEABLES;
}
@Override
public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
return getClassCssMetaData();
}
@NonNull
private final IntegerProperty marginWidth = new SimpleStyleableIntegerProperty(MARGIN_WIDTH, this, "marginWidth", Integer.valueOf(0)); //$NON-NLS-1$
@NonNull
private final IntegerProperty marginHeight = new SimpleStyleableIntegerProperty(MARGIN_HEIGHT, this, "marginHeight", Integer.valueOf(0)); //$NON-NLS-1$
@NonNull
private final IntegerProperty marginLeft = new SimpleStyleableIntegerProperty(MARGIN_LEFT, this, "marginLeft", Integer.valueOf(0)); //$NON-NLS-1$
@NonNull
private final IntegerProperty marginTop = new SimpleStyleableIntegerProperty(MARGIN_TOP, this, "marginTop", Integer.valueOf(0)); //$NON-NLS-1$
@NonNull
private final IntegerProperty marginRight = new SimpleStyleableIntegerProperty(MARGIN_RIGHT, this, "marginRight", Integer.valueOf(0)); //$NON-NLS-1$
@NonNull
private final IntegerProperty marginBottom = new SimpleStyleableIntegerProperty(MARGIN_BOTTOM, this, "marginBottom", Integer.valueOf(0)); //$NON-NLS-1$
@NonNull
private IntegerProperty spacing = new SimpleStyleableIntegerProperty(SPACING, this, "spacing", Integer.valueOf(3)); //$NON-NLS-1$
@NonNull
private BooleanProperty wrap = new SimpleStyleableBooleanProperty(WRAP, this, "wrap", true); //$NON-NLS-1$
@NonNull
private BooleanProperty pack = new SimpleStyleableBooleanProperty(PACK, this, "pack", true); //$NON-NLS-1$
@NonNull
private BooleanProperty fill = new SimpleStyleableBooleanProperty(FILL, this, "fill", false); //$NON-NLS-1$
@NonNull
private BooleanProperty center = new SimpleStyleableBooleanProperty(CENTER, this, "center", false); //$NON-NLS-1$
@NonNull
private BooleanProperty justify = new SimpleStyleableBooleanProperty(JUSTIFY, this, "justify", false); //$NON-NLS-1$
@NonNull
private BooleanProperty horizontal = new SimpleStyleableBooleanProperty(HORIZONTAL, this, "horizontal", true); //$NON-NLS-1$
@NonNull
private static final String LAYOUT_KEY = "rowData"; //$NON-NLS-1$
/**
* Associate a layout constraint with the node
*
* @param n
* the node
* @param griddata
* the constraint
*/
public final static void setConstraint(@NonNull Node n, @NonNull RowData griddata) {
setConstraint(n, LAYOUT_KEY, griddata);
}
/**
* Get the layout constraint for the node
*
* @param n
* the node
* @return the layout constraint or <code>null</code>
*/
public final static @Nullable RowData getConstraint(@NonNull Node n) {
return (RowData) getConstraint(n, LAYOUT_KEY);
}
@Override
protected org.eclipse.fx.ui.panes.AbstractLayoutPane.Size computeSize(double wHint, double hHint, boolean flushCache) {
Size extent;
if (isHorizontal()) {
extent = layoutHorizontal(false, (wHint != FX_DEFAULT) && isWrap(), wHint, flushCache);
} else {
extent = layoutVertical(false, (hHint != FX_DEFAULT) && isWrap(), hHint, flushCache);
}
double width = extent.width;
double height = extent.height;
if (wHint != FX_DEFAULT)
width = wHint;
if (hHint != FX_DEFAULT)
height = hHint;
return new Size(width, height);
}
private static Size computeSize(@NonNull Node control, boolean flushCache) {
int wHint = FX_DEFAULT, hHint = FX_DEFAULT;
RowData data = getConstraint(control);
if (data != null) {
wHint = data.getWidth();
hHint = data.getHeight();
}
return new Size(control.prefWidth(wHint), control.prefHeight(hHint));
}
@Override
protected void layoutChildren() {
super.layoutChildren();
Bounds clientArea = getLayoutBounds();
if (isHorizontal()) {
layoutHorizontal(true, isWrap(), clientArea.getWidth(), true);
} else {
layoutVertical(true, isWrap(), clientArea.getHeight(), true);
}
}
private int getInnerMarginLeft() {
return (int) Math.ceil(getMarginWidth() + getMarginLeft() + getPadding().getLeft());
}
private int getInnerMarginRight() {
return (int) Math.ceil(getMarginWidth() + getMarginRight() + getPadding().getRight());
}
private int getInnerMarginTop() {
return (int) Math.ceil(getMarginHeight() + getMarginTop() + getPadding().getTop());
}
private int getInnerMarginBottom() {
return (int) Math.ceil(getMarginHeight() + getMarginBottom() + getPadding().getBottom());
}
Size layoutHorizontal(boolean move, boolean wrap, double width, boolean flushCache) {
@SuppressWarnings("null")
@NonNull
Node[] children = getChildren().toArray(new Node[0]);
int count = 0;
for (int i = 0; i < children.length; i++) {
Node control = children[i];
RowData data = getConstraint(control);
if (data == null || !data.isExclude()) {
children[count++] = children[i];
}
}
if (count == 0) {
return new Size(getInnerMarginLeft() + getInnerMarginRight(), getInnerMarginTop() + getInnerMarginBottom());
}
double childWidth = 0, childHeight = 0, maxHeight = 0;
if (!isPack()) {
for (int i = 0; i < count; i++) {
Node child = children[i];
Size size = computeSize(child, flushCache);
childWidth = Math.max(childWidth, size.width);
childHeight = Math.max(childHeight, size.height);
}
maxHeight = childHeight;
}
double clientX = 0, clientY = 0;
if (move) {
Bounds rect = getLayoutBounds();
clientX = rect.getMinX();
clientY = rect.getMinY();
}
double[] wraps = null;
boolean wrapped = false;
Bounds[] bounds = null;
if (move && (isJustify() || isFill() || isCenter())) {
bounds = new Bounds[count];
wraps = new double[count];
}
double maxX = 0, x = getInnerMarginLeft(), y = getInnerMarginTop();
for (int i = 0; i < count; i++) {
Node child = children[i];
if (isPack()) {
Size size = computeSize(child, flushCache);
childWidth = size.width;
childHeight = size.height;
}
if (wrap && (i != 0) && (x + childWidth > width)) {
wrapped = true;
if (move && (isJustify() || isFill() || isCenter())) {
if (wraps != null) {
wraps[i - 1] = maxHeight;
}
}
x = getInnerMarginLeft();
y += getSpacing() + maxHeight;
if (isPack())
maxHeight = 0;
}
if (isPack() || isFill() || isCenter()) {
maxHeight = Math.max(maxHeight, childHeight);
}
if (move) {
double childX = x + clientX, childY = y + clientY;
if (isJustify() || isFill() || isCenter()) {
if (bounds != null) {
bounds[i] = new BoundingBox(childX, childY, childWidth, childHeight);
}
} else {
child.resizeRelocate(childX, childY, childWidth, childHeight);
}
}
x += getSpacing() + childWidth;
maxX = Math.max(maxX, x);
}
maxX = Math.max(clientX + getInnerMarginLeft(), maxX - getSpacing());
if (!wrapped)
maxX += getInnerMarginRight();
if (move && (isJustify() || isFill() || isCenter())) {
double space = 0, margin = 0;
if (!wrapped) {
space = Math.max(0, (width - maxX) / (count + 1));
margin = Math.max(0, ((width - maxX) % (count + 1)) / 2);
} else {
if (isFill() || isJustify() || isCenter()) {
int last = 0;
if (count > 0) {
if (wraps != null) {
wraps[count - 1] = maxHeight;
}
}
for (int i = 0; i < count; i++) {
if (wraps != null && wraps[i] != 0) {
int wrapCount = i - last + 1;
if (isJustify()) {
int wrapX = 0;
for (int j = last; j <= i; j++) {
if (bounds != null) {
wrapX += bounds[j].getWidth() + getSpacing();
}
}
space = Math.max(0, (width - wrapX) / (wrapCount + 1));
margin = Math.max(0, ((width - wrapX) % (wrapCount + 1)) / 2);
}
for (int j = last; j <= i; j++) {
if (isJustify()) {
if (bounds != null) {
bounds[j] = changeX(bounds[j], (space * (j - last + 1)) + margin);
}
}
if (isFill()) {
if (bounds != null) {
bounds[j] = changeHeight(bounds[j], wraps[i]);
}
} else {
if (isCenter()) {
if (bounds != null) {
bounds[j] = changeY(bounds[j], Math.max(0, (wraps[i] - bounds[j].getHeight()) / 2));
}
}
}
}
last = i + 1;
}
}
}
}
for (int i = 0; i < count; i++) {
if (!wrapped) {
if (isJustify()) {
if (bounds != null) {
bounds[i] = changeX(bounds[i], (space * (i + 1)) + margin);
}
}
if (isFill()) {
if (bounds != null) {
bounds[i] = changeHeight(bounds[i], maxHeight);
}
} else {
if (isCenter()) {
if (bounds != null) {
bounds[i] = changeY(bounds[i], Math.max(0, (maxHeight - bounds[i].getHeight()) / 2));
}
}
}
}
if (bounds != null) {
children[i].resizeRelocate(bounds[i].getMinX(), bounds[i].getMinY(), bounds[i].getWidth(), bounds[i].getHeight());
}
}
}
return new Size(maxX, y + maxHeight + getInnerMarginBottom());
}
Size layoutVertical(boolean move, boolean wrap, double height, boolean flushCache) {
@SuppressWarnings("null")
@NonNull
Node[] children = getChildren().toArray(new Node[0]);
int count = 0;
for (int i = 0; i < children.length; i++) {
Node control = children[i];
RowData data = getConstraint(control);
if (data == null || !data.isExclude()) {
children[count++] = children[i];
}
}
if (count == 0) {
return new Size(getInnerMarginLeft() + getInnerMarginRight(), getInnerMarginTop() + getInnerMarginBottom());
}
double childWidth = 0, childHeight = 0, maxWidth = 0;
if (!isPack()) {
for (int i = 0; i < count; i++) {
Node child = children[i];
Size size = computeSize(child, flushCache);
childWidth = Math.max(childWidth, size.width);
childHeight = Math.max(childHeight, size.height);
}
maxWidth = childWidth;
}
double clientX = 0, clientY = 0;
if (move) {
Bounds rect = getLayoutBounds();
clientX = rect.getMinX();
clientY = rect.getMinY();
}
double[] wraps = null;
boolean wrapped = false;
Bounds[] bounds = null;
if (move && (isJustify() || isFill() || isCenter())) {
bounds = new Bounds[count];
wraps = new double[count];
}
double maxY = 0, x = getInnerMarginLeft(), y = getInnerMarginBottom();
for (int i = 0; i < count; i++) {
Node child = children[i];
if (isPack()) {
Size size = computeSize(child, flushCache);
childWidth = size.width;
childHeight = size.height;
}
if (wrap && (i != 0) && (y + childHeight > height)) {
wrapped = true;
if (move && (isJustify() || isFill() || isCenter())) {
if (wraps != null) {
wraps[i - 1] = maxWidth;
}
}
x += getSpacing() + maxWidth;
y = getInnerMarginTop();
if (isPack())
maxWidth = 0;
}
if (isPack() || isFill() || isCenter()) {
maxWidth = Math.max(maxWidth, childWidth);
}
if (move) {
double childX = x + clientX, childY = y + clientY;
if (isJustify() || isFill() || isCenter()) {
if (bounds != null) {
bounds[i] = new BoundingBox(childX, childY, childWidth, childHeight);
}
} else {
child.resizeRelocate(childX, childY, childWidth, childHeight);
}
}
y += getSpacing() + childHeight;
maxY = Math.max(maxY, y);
}
maxY = Math.max(clientY + getInnerMarginTop(), maxY - getSpacing());
if (!wrapped)
maxY += getInnerMarginBottom();
if (move && (isJustify() || isFill() || isCenter())) {
double space = 0, margin = 0;
if (!wrapped) {
space = Math.max(0, (height - maxY) / (count + 1));
margin = Math.max(0, ((height - maxY) % (count + 1)) / 2);
} else {
if (isFill() || isJustify() || isCenter()) {
int last = 0;
if (count > 0) {
if (wraps != null) {
wraps[count - 1] = maxWidth;
}
}
for (int i = 0; i < count; i++) {
if (wraps != null && wraps[i] != 0) {
int wrapCount = i - last + 1;
if (isJustify()) {
int wrapY = 0;
for (int j = last; j <= i; j++) {
if (bounds != null) {
wrapY += bounds[j].getHeight() + getSpacing();
}
}
space = Math.max(0, (height - wrapY) / (wrapCount + 1));
margin = Math.max(0, ((height - wrapY) % (wrapCount + 1)) / 2);
}
for (int j = last; j <= i; j++) {
if (isJustify()) {
if (bounds != null) {
bounds[j] = changeY(bounds[j], (space * (j - last + 1)) + margin);
}
}
if (isFill()) {
if (bounds != null) {
bounds[j] = changeWidth(bounds[j], wraps[i]);
}
} else {
if (isCenter()) {
if (bounds != null) {
bounds[j] = changeX(bounds[j], Math.max(0, (wraps[i] - bounds[j].getWidth()) / 2));
}
}
}
}
last = i + 1;
}
}
}
}
for (int i = 0; i < count; i++) {
if (!wrapped) {
if (isJustify()) {
if (bounds != null) {
bounds[i] = changeY(bounds[i], (space * (i + 1)) + margin);
}
}
if (isFill()) {
if (bounds != null) {
bounds[i] = changeWidth(bounds[i], maxWidth);
}
} else {
if (isCenter()) {
if (bounds != null) {
bounds[i] = changeX(bounds[i], Math.max(0, (maxWidth - bounds[i].getWidth()) / 2));
}
}
}
}
if (bounds != null) {
children[i].resizeRelocate(bounds[i].getMinX(), bounds[i].getMinY(), bounds[i].getWidth(), bounds[i].getHeight());
}
}
}
return new Size(x + maxWidth + getInnerMarginRight(), maxY);
}
private static Bounds changeX(Bounds original, double amount) {
return new BoundingBox(original.getMinX() + amount, original.getMinY(), original.getWidth(), original.getHeight());
}
private static Bounds changeY(Bounds original, double amount) {
return new BoundingBox(original.getMinX(), original.getMinY() + amount, original.getWidth(), original.getHeight());
}
private static Bounds changeHeight(Bounds original, double amount) {
return new BoundingBox(original.getMinX(), original.getMinY(), original.getWidth(), original.getHeight() + amount);
}
private static Bounds changeWidth(Bounds original, double amount) {
return new BoundingBox(original.getMinX(), original.getMinY(), original.getWidth() + amount, original.getHeight());
}
/**
* center specifies whether the controls in a row should be centered
* vertically in each cell for horizontal layouts, or centered horizontally
* in each cell for vertical layouts.
*
* <p>
* The default is <code>false</code>
* </p>
*
* @param value
* the new value
*/
public void setCenter(boolean value) {
centerProperty().set(value);
}
/**
* center specifies whether the controls in a row should be centered
* vertically in each cell for horizontal layouts, or centered horizontally
* in each cell for vertical layouts.
*
* <p>
* The default is <code>false</code>
* </p>
*
* @return the current value
*/
public boolean isCenter() {
return centerProperty().get();
}
/**
* center specifies whether the controls in a row should be centered
* vertically in each cell for horizontal layouts, or centered horizontally
* in each cell for vertical layouts.
*
* <p>
* The default is <code>false</code>
* </p>
*
* @return the property
*/
public @NonNull BooleanProperty centerProperty() {
return this.center;
}
/**
* fill specifies whether the controls in a row should be all the same
* height for horizontal layouts, or the same width for vertical layouts.
*
* <p>
* The default is <code>false</code>
* </p>
*
* @param value
* the new value
*/
public void setFill(boolean value) {
fillProperty().set(value);
}
/**
* fill specifies whether the controls in a row should be all the same
* height for horizontal layouts, or the same width for vertical layouts.
*
* <p>
* The default is <code>false</code>
* </p>
*
* @return the current value
*/
public boolean isFill() {
return fillProperty().get();
}
/**
* fill specifies whether the controls in a row should be all the same
* height for horizontal layouts, or the same width for vertical layouts.
*
* <p>
* The default is <code>false</code>
* </p>
*
* @return the property
*/
public @NonNull BooleanProperty fillProperty() {
return this.fill;
}
/**
* justify specifies whether the controls in a row should be fully
* justified, with any extra space placed between the controls.
* <p>
* The default is <code>false</code>
* </p>
*
* @param value
* the new value
*/
public void setJustify(boolean value) {
justifyProperty().set(value);
}
/**
* justify specifies whether the controls in a row should be fully
* justified, with any extra space placed between the controls.
* <p>
* The default is <code>false</code>
* </p>
*
* @return the current value
*/
public boolean isJustify() {
return justifyProperty().get();
}
/**
* justify specifies whether the controls in a row should be fully
* justified, with any extra space placed between the controls.
* <p>
* The default is <code>false</code>
* </p>
*
* @return the property
*/
public @NonNull BooleanProperty justifyProperty() {
return this.justify;
}
/**
* marginBottom specifies the number of pixels of vertical margin that will
* be placed along the bottom edge of the layout.
* <p>
* The default is 0
* </p>
*
* @param value the value
*/
public void setMarginBottom(int value) {
marginBottomProperty().set(value);
}
/**
* marginBottom specifies the number of pixels of vertical margin that will
* be placed along the bottom edge of the layout.
* <p>
* The default is 0
* </p>
*
* @return the current value
*/
public int getMarginBottom() {
return marginBottomProperty().get();
}
/**
* marginBottom specifies the number of pixels of vertical margin that will
* be placed along the bottom edge of the layout.
* <p>
* The default is 0
* </p>
*
* @return the property
*/
public @NonNull IntegerProperty marginBottomProperty() {
return this.marginBottom;
}
/**
* marginHeight specifies the number of pixels of vertical margin that will
* be placed along the top and bottom edges of the layout.
*
* <p>
* The default is 0
* </p>
*
* @param value
* the new value
*/
public void setMarginHeight(int value) {
marginHeightProperty().set(value);
}
/**
* marginHeight specifies the number of pixels of vertical margin that will
* be placed along the top and bottom edges of the layout.
*
* <p>
* The default is 0
* </p>
*
* @return the current value
*/
public int getMarginHeight() {
return marginHeightProperty().get();
}
/**
* marginHeight specifies the number of pixels of vertical margin that will
* be placed along the top and bottom edges of the layout.
*
* <p>
* The default is 0
* </p>
*
* @return the property
*/
public @NonNull IntegerProperty marginHeightProperty() {
return this.marginHeight;
}
/**
* marginLeft specifies the number of pixels of horizontal margin that will
* be placed along the left edge of the layout.
* <p>
* The default is 0
* </p>
*
* @param value
* the new value
*/
public void setMarginLeft(int value) {
marginLeftProperty().set(value);
}
/**
* marginLeft specifies the number of pixels of horizontal margin that will
* be placed along the left edge of the layout.
* <p>
* The default is 0
* </p>
*
* @return the current value
*/
public int getMarginLeft() {
return marginLeftProperty().get();
}
/**
* marginLeft specifies the number of pixels of horizontal margin that will
* be placed along the left edge of the layout.
* <p>
* The default is 0
* </p>
*
* @return the property
*/
public @NonNull IntegerProperty marginLeftProperty() {
return this.marginLeft;
}
/**
* marginRight specifies the number of pixels of horizontal margin that will
* be placed along the right edge of the layout.
* <p>
* The default is 0
* </p>
*
* @param value
* the new value
*/
public void setMarginRight(int value) {
marginRightProperty().set(value);
}
/**
* marginRight specifies the number of pixels of horizontal margin that will
* be placed along the right edge of the layout.
* <p>
* The default is 0
* </p>
*
* @return the current value
*/
public int getMarginRight() {
return marginRightProperty().get();
}
/**
* marginRight specifies the number of pixels of horizontal margin that will
* be placed along the right edge of the layout.
* <p>
* The default is 0
* </p>
*
* @return the property
*/
public @NonNull IntegerProperty marginRightProperty() {
return this.marginRight;
}
/**
* marginTop specifies the number of pixels of vertical margin that will be
* placed along the top edge of the layout.
* <p>
* The default is 0
* </p>
*
* @param value
* the new value
*/
public void setMarginTop(int value) {
marginTopProperty().set(value);
}
/**
* marginTop specifies the number of pixels of vertical margin that will be
* placed along the top edge of the layout.
* <p>
* The default is 0
* </p>
*
* @return the current value
*/
public int getMarginTop() {
return marginTopProperty().get();
}
/**
* marginTop specifies the number of pixels of vertical margin that will be
* placed along the top edge of the layout.
* <p>
* The default is 0
* </p>
*
* @return the property
*/
public @NonNull IntegerProperty marginTopProperty() {
return this.marginTop;
}
/**
* marginWidth specifies the number of pixels of horizontal margin that will
* be placed along the left and right edges of the layout.
* <p>
* The default is 0
* </p>
*
* @param value
* the new value
*/
public void setMarginWidth(int value) {
this.marginWidth.set(value);
}
/**
* marginWidth specifies the number of pixels of horizontal margin that will
* be placed along the left and right edges of the layout.
* <p>
* The default is 0
* </p>
*
* @return the current value
*/
public int getMarginWidth() {
return marginWidthProperty().get();
}
/**
* marginWidth specifies the number of pixels of horizontal margin that will
* be placed along the left and right edges of the layout.
* <p>
* The default is 0
* </p>
*
* @return the property
*/
public @NonNull IntegerProperty marginWidthProperty() {
return this.marginWidth;
}
/**
* pack specifies whether all controls in the layout take their preferred
* size. If pack is false, all controls will have the same size which is the
* size required to accommodate the largest preferred height and the largest
* preferred width of all the controls in the layout.
* <p>
* The default is <code>true</code>
* </p>
*
* @param value
* the new value
*/
public void setPack(boolean value) {
packProperty().set(value);
}
/**
* pack specifies whether all controls in the layout take their preferred
* size. If pack is false, all controls will have the same size which is the
* size required to accommodate the largest preferred height and the largest
* preferred width of all the controls in the layout.
* <p>
* The default is <code>true</code>
* </p>
*
* @return the current value
*/
public boolean isPack() {
return packProperty().get();
}
/**
* pack specifies whether all controls in the layout take their preferred
* size. If pack is false, all controls will have the same size which is the
* size required to accommodate the largest preferred height and the largest
* preferred width of all the controls in the layout.
* <p>
* The default is <code>true</code>
* </p>
*
* @return the property
*/
public @NonNull BooleanProperty packProperty() {
return this.pack;
}
/**
* spacing specifies the number of pixels between the edge of one cell and
* the edge of its neighboring cell.
*
* <p>
* The default is 3
* </p>
*
* @param value
* the spacing
*/
public void setSpacing(int value) {
spacingProperty().set(value);
}
/**
* spacing specifies the number of pixels between the edge of one cell and
* the edge of its neighboring cell.
*
* <p>
* The default is 3
* </p>
*
* @return the current value
*/
public int getSpacing() {
return spacingProperty().get();
}
/**
* spacing specifies the number of pixels between the edge of one cell and
* the edge of its neighboring cell.
*
* <p>
* The default is 3
* </p>
*
* @return the property
*/
public @NonNull IntegerProperty spacingProperty() {
return this.spacing;
}
/**
* wrap specifies whether a control will be wrapped to the next row if there
* is insufficient space on the current row.
* <p>
* The default is <code>true</code>
* </p>
*
* @param value
* the new value
*/
public void setWrap(boolean value) {
wrapProperty().set(value);
}
/**
* wrap specifies whether a control will be wrapped to the next row if there
* is insufficient space on the current row.
* <p>
* The default is <code>true</code>
* </p>
*
* @return the current value
*/
public boolean isWrap() {
return wrapProperty().get();
}
/**
* wrap specifies whether a control will be wrapped to the next row if there
* is insufficient space on the current row.
* <p>
* The default is <code>true</code>
* </p>
*
* @return the property
*/
public @NonNull BooleanProperty wrapProperty() {
return this.wrap;
}
/**
* horizontal specifies if controls are layouted horizontally or vertically
* <p>
* The default is <code>true</code>
* </p>
*
* @param value
* the new value
*/
public void setHorizontal(boolean value) {
horizontalProperty().set(value);
}
/**
* horizontal specifies if controls are layouted horizontally or vertically
* <p>
* The default is <code>true</code>
* </p>
*
* @return the current value
*/
public boolean isHorizontal() {
return horizontalProperty().get();
}
/**
* horizontal specifies if controls are layouted horizontally or vertically
* <p>
* The default is <code>true</code>
* </p>
*
* @return the property
*/
public @NonNull BooleanProperty horizontalProperty() {
return this.horizontal;
}
}
|