- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathBoxView.java
1189 lines (1105 loc) · 44.5 KB
/
BoxView.java
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
/*
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing.text;
importjava.io.PrintStream;
importjava.util.Vector;
importjava.awt.*;
importjavax.swing.event.DocumentEvent;
importjavax.swing.SizeRequirements;
/**
* A view that arranges its children into a box shape by tiling
* its children along an axis. The box is somewhat like that
* found in TeX where there is alignment of the
* children, flexibility of the children is considered, etc.
* This is a building block that might be useful to represent
* things like a collection of lines, paragraphs,
* lists, columns, pages, etc. The axis along which the children are tiled is
* considered the major axis. The orthogonal axis is the minor axis.
* <p>
* Layout for each axis is handled separately by the methods
* <code>layoutMajorAxis</code> and <code>layoutMinorAxis</code>.
* Subclasses can change the layout algorithm by
* reimplementing these methods. These methods will be called
* as necessary depending upon whether or not there is cached
* layout information and the cache is considered
* valid. These methods are typically called if the given size
* along the axis changes, or if <code>layoutChanged</code> is
* called to force an updated layout. The <code>layoutChanged</code>
* method invalidates cached layout information, if there is any.
* The requirements published to the parent view are calculated by
* the methods <code>calculateMajorAxisRequirements</code>
* and <code>calculateMinorAxisRequirements</code>.
* If the layout algorithm is changed, these methods will
* likely need to be reimplemented.
*
* @author Timothy Prinzing
*/
publicclassBoxViewextendsCompositeView {
/**
* Constructs a <code>BoxView</code>.
*
* @param elem the element this view is responsible for
* @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
*/
publicBoxView(Elementelem, intaxis) {
super(elem);
tempRect = newRectangle();
this.majorAxis = axis;
majorOffsets = newint[0];
majorSpans = newint[0];
majorReqValid = false;
majorAllocValid = false;
minorOffsets = newint[0];
minorSpans = newint[0];
minorReqValid = false;
minorAllocValid = false;
}
/**
* Fetches the tile axis property. This is the axis along which
* the child views are tiled.
*
* @return the major axis of the box, either
* <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
*
* @since 1.3
*/
publicintgetAxis() {
returnmajorAxis;
}
/**
* Sets the tile axis property. This is the axis along which
* the child views are tiled.
*
* @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
*
* @since 1.3
*/
publicvoidsetAxis(intaxis) {
booleanaxisChanged = (axis != majorAxis);
majorAxis = axis;
if (axisChanged) {
preferenceChanged(null, true, true);
}
}
/**
* Invalidates the layout along an axis. This happens
* automatically if the preferences have changed for
* any of the child views. In some cases the layout
* may need to be recalculated when the preferences
* have not changed. The layout can be marked as
* invalid by calling this method. The layout will
* be updated the next time the <code>setSize</code> method
* is called on this view (typically in paint).
*
* @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
*
* @since 1.3
*/
publicvoidlayoutChanged(intaxis) {
if (axis == majorAxis) {
majorAllocValid = false;
} else {
minorAllocValid = false;
}
}
/**
* Determines if the layout is valid along the given axis.
*
* @param axis either {@code View.X_AXIS} or {@code View.Y_AXIS}
* @return {@code true} if the layout is valid along the given axis,
* otherwise {@code false}
* @since 1.4
*/
protectedbooleanisLayoutValid(intaxis) {
if (axis == majorAxis) {
returnmajorAllocValid;
} else {
returnminorAllocValid;
}
}
/**
* Paints a child. By default
* that is all it does, but a subclass can use this to paint
* things relative to the child.
*
* @param g the graphics context
* @param alloc the allocated region to paint into
* @param index the child index, >= 0 && < getViewCount()
*/
protectedvoidpaintChild(Graphicsg, Rectanglealloc, intindex) {
Viewchild = getView(index);
child.paint(g, alloc);
}
// --- View methods ---------------------------------------------
/**
* Invalidates the layout and resizes the cache of
* requests/allocations. The child allocations can still
* be accessed for the old layout, but the new children
* will have an offset and span of 0.
*
* @param index the starting index into the child views to insert
* the new views; this should be a value >= 0 and <= getViewCount
* @param length the number of existing child views to remove;
* This should be a value >= 0 and <= (getViewCount() - offset)
* @param elems the child views to add; this value can be
* <code>null</code>to indicate no children are being added
* (useful to remove)
*/
publicvoidreplace(intindex, intlength, View[] elems) {
super.replace(index, length, elems);
// invalidate cache
intnInserted = (elems != null) ? elems.length : 0;
majorOffsets = updateLayoutArray(majorOffsets, index, nInserted);
majorSpans = updateLayoutArray(majorSpans, index, nInserted);
majorReqValid = false;
majorAllocValid = false;
minorOffsets = updateLayoutArray(minorOffsets, index, nInserted);
minorSpans = updateLayoutArray(minorSpans, index, nInserted);
minorReqValid = false;
minorAllocValid = false;
}
/**
* Resizes the given layout array to match the new number of
* child views. The current number of child views are used to
* produce the new array. The contents of the old array are
* inserted into the new array at the appropriate places so that
* the old layout information is transferred to the new array.
*
* @param oldArray the original layout array
* @param offset location where new views will be inserted
* @param nInserted the number of child views being inserted;
* therefore the number of blank spaces to leave in the
* new array at location <code>offset</code>
* @return the new layout array
*/
int[] updateLayoutArray(int[] oldArray, intoffset, intnInserted) {
intn = getViewCount();
int[] newArray = newint[n];
System.arraycopy(oldArray, 0, newArray, 0, offset);
System.arraycopy(oldArray, offset,
newArray, offset + nInserted, n - nInserted - offset);
returnnewArray;
}
/**
* Forwards the given <code>DocumentEvent</code> to the child views
* that need to be notified of the change to the model.
* If a child changed its requirements and the allocation
* was valid prior to forwarding the portion of the box
* from the starting child to the end of the box will
* be repainted.
*
* @param ec changes to the element this view is responsible
* for (may be <code>null</code> if there were no changes)
* @param e the change information from the associated document
* @param a the current allocation of the view
* @param f the factory to use to rebuild if the view has children
* @see #insertUpdate
* @see #removeUpdate
* @see #changedUpdate
* @since 1.3
*/
protectedvoidforwardUpdate(DocumentEvent.ElementChangeec,
DocumentEvente, Shapea, ViewFactoryf) {
booleanwasValid = isLayoutValid(majorAxis);
super.forwardUpdate(ec, e, a, f);
// determine if a repaint is needed
if (wasValid && (! isLayoutValid(majorAxis))) {
// Repaint is needed because one of the tiled children
// have changed their span along the major axis. If there
// is a hosting component and an allocated shape we repaint.
Componentc = getContainer();
if ((a != null) && (c != null)) {
intpos = e.getOffset();
intindex = getViewIndexAtPosition(pos);
Rectanglealloc = getInsideAllocation(a);
if (majorAxis == X_AXIS) {
alloc.x += majorOffsets[index];
alloc.width -= majorOffsets[index];
} else {
alloc.y += minorOffsets[index];
alloc.height -= minorOffsets[index];
}
c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
}
}
}
/**
* This is called by a child to indicate its
* preferred span has changed. This is implemented to
* throw away cached layout information so that new
* calculations will be done the next time the children
* need an allocation.
*
* @param child the child view
* @param width true if the width preference should change
* @param height true if the height preference should change
*/
publicvoidpreferenceChanged(Viewchild, booleanwidth, booleanheight) {
booleanmajorChanged = (majorAxis == X_AXIS) ? width : height;
booleanminorChanged = (majorAxis == X_AXIS) ? height : width;
if (majorChanged) {
majorReqValid = false;
majorAllocValid = false;
}
if (minorChanged) {
minorReqValid = false;
minorAllocValid = false;
}
super.preferenceChanged(child, width, height);
}
/**
* Gets the resize weight. A value of 0 or less is not resizable.
*
* @param axis may be either <code>View.X_AXIS</code> or
* <code>View.Y_AXIS</code>
* @return the weight
* @throws IllegalArgumentException for an invalid axis
*/
publicintgetResizeWeight(intaxis) {
checkRequests(axis);
if (axis == majorAxis) {
if ((majorRequest.preferred != majorRequest.minimum) ||
(majorRequest.preferred != majorRequest.maximum)) {
return1;
}
} else {
if ((minorRequest.preferred != minorRequest.minimum) ||
(minorRequest.preferred != minorRequest.maximum)) {
return1;
}
}
return0;
}
/**
* Sets the size of the view along an axis. This should cause
* layout of the view along the given axis.
*
* @param axis may be either <code>View.X_AXIS</code> or
* <code>View.Y_AXIS</code>
* @param span the span to layout to >= 0
*/
voidsetSpanOnAxis(intaxis, floatspan) {
if (axis == majorAxis) {
if (majorSpan != (int) span) {
majorAllocValid = false;
}
if (! majorAllocValid) {
// layout the major axis
majorSpan = (int) span;
checkRequests(majorAxis);
layoutMajorAxis(majorSpan, axis, majorOffsets, majorSpans);
majorAllocValid = true;
// flush changes to the children
updateChildSizes();
}
} else {
if (((int) span) != minorSpan) {
minorAllocValid = false;
}
if (! minorAllocValid) {
// layout the minor axis
minorSpan = (int) span;
checkRequests(axis);
layoutMinorAxis(minorSpan, axis, minorOffsets, minorSpans);
minorAllocValid = true;
// flush changes to the children
updateChildSizes();
}
}
}
/**
* Propagates the current allocations to the child views.
*/
voidupdateChildSizes() {
intn = getViewCount();
if (majorAxis == X_AXIS) {
for (inti = 0; i < n; i++) {
Viewv = getView(i);
v.setSize((float) majorSpans[i], (float) minorSpans[i]);
}
} else {
for (inti = 0; i < n; i++) {
Viewv = getView(i);
v.setSize((float) minorSpans[i], (float) majorSpans[i]);
}
}
}
/**
* Returns the size of the view along an axis. This is implemented
* to return zero.
*
* @param axis may be either <code>View.X_AXIS</code> or
* <code>View.Y_AXIS</code>
* @return the current span of the view along the given axis, >= 0
*/
floatgetSpanOnAxis(intaxis) {
if (axis == majorAxis) {
returnmajorSpan;
} else {
returnminorSpan;
}
}
/**
* Sets the size of the view. This should cause
* layout of the view if the view caches any layout
* information. This is implemented to call the
* layout method with the sizes inside of the insets.
*
* @param width the width >= 0
* @param height the height >= 0
*/
publicvoidsetSize(floatwidth, floatheight) {
layout(Math.max(0, (int)(width - getLeftInset() - getRightInset())),
Math.max(0, (int)(height - getTopInset() - getBottomInset())));
}
/**
* Renders the <code>BoxView</code> using the given
* rendering surface and area
* on that surface. Only the children that intersect
* the clip bounds of the given <code>Graphics</code>
* will be rendered.
*
* @param g the rendering surface to use
* @param allocation the allocated region to render into
* @see View#paint
*/
publicvoidpaint(Graphicsg, Shapeallocation) {
Rectanglealloc = (allocationinstanceofRectangle) ?
(Rectangle)allocation : allocation.getBounds();
intn = getViewCount();
intx = alloc.x + getLeftInset();
inty = alloc.y + getTopInset();
Rectangleclip = g.getClipBounds();
for (inti = 0; i < n; i++) {
tempRect.x = x + getOffset(X_AXIS, i);
tempRect.y = y + getOffset(Y_AXIS, i);
tempRect.width = getSpan(X_AXIS, i);
tempRect.height = getSpan(Y_AXIS, i);
inttrx0 = tempRect.x, trx1 = trx0 + tempRect.width;
inttry0 = tempRect.y, try1 = try0 + tempRect.height;
intcrx0 = clip.x, crx1 = crx0 + clip.width;
intcry0 = clip.y, cry1 = cry0 + clip.height;
// We should paint views that intersect with clipping region
// even if the intersection has no inside points (is a line).
// This is needed for supporting views that have zero width, like
// views that contain only combining marks.
if ((trx1 >= crx0) && (try1 >= cry0) && (crx1 >= trx0) && (cry1 >= try0)) {
paintChild(g, tempRect, i);
}
}
}
/**
* Fetches the allocation for the given child view.
* This enables finding out where various views
* are located. This is implemented to return
* <code>null</code> if the layout is invalid,
* otherwise the superclass behavior is executed.
*
* @param index the index of the child, >= 0 && > getViewCount()
* @param a the allocation to this view
* @return the allocation to the child; or <code>null</code>
* if <code>a</code> is <code>null</code>;
* or <code>null</code> if the layout is invalid
*/
publicShapegetChildAllocation(intindex, Shapea) {
if (a != null) {
Shapeca = super.getChildAllocation(index, a);
if ((ca != null) && (! isAllocationValid())) {
// The child allocation may not have been set yet.
Rectangler = (cainstanceofRectangle) ?
(Rectangle) ca : ca.getBounds();
if ((r.width == 0) && (r.height == 0)) {
returnnull;
}
}
returnca;
}
returnnull;
}
/**
* Provides a mapping from the document model coordinate space
* to the coordinate space of the view mapped to it. This makes
* sure the allocation is valid before calling the superclass.
*
* @param pos the position to convert >= 0
* @param a the allocated region to render into
* @return the bounding box of the given position
* @throws BadLocationException if the given position does
* not represent a valid location in the associated document
* @see View#modelToView
*/
publicShapemodelToView(intpos, Shapea, Position.Biasb) throwsBadLocationException {
if (! isAllocationValid()) {
Rectanglealloc = a.getBounds();
setSize(alloc.width, alloc.height);
}
returnsuper.modelToView(pos, a, b);
}
/**
* Provides a mapping from the view coordinate space to the logical
* coordinate space of the model.
*
* @param x x coordinate of the view location to convert >= 0
* @param y y coordinate of the view location to convert >= 0
* @param a the allocated region to render into
* @return the location within the model that best represents the
* given point in the view >= 0
* @see View#viewToModel
*/
publicintviewToModel(floatx, floaty, Shapea, Position.Bias[] bias) {
if (! isAllocationValid()) {
Rectanglealloc = a.getBounds();
setSize(alloc.width, alloc.height);
}
returnsuper.viewToModel(x, y, a, bias);
}
/**
* Determines the desired alignment for this view along an
* axis. This is implemented to give the total alignment
* needed to position the children with the alignment points
* lined up along the axis orthogonal to the axis that is
* being tiled. The axis being tiled will request to be
* centered (i.e. 0.5f).
*
* @param axis may be either <code>View.X_AXIS</code>
* or <code>View.Y_AXIS</code>
* @return the desired alignment >= 0.0f && <= 1.0f; this should
* be a value between 0.0 and 1.0 where 0 indicates alignment at the
* origin and 1.0 indicates alignment to the full span
* away from the origin; an alignment of 0.5 would be the
* center of the view
* @throws IllegalArgumentException for an invalid axis
*/
publicfloatgetAlignment(intaxis) {
checkRequests(axis);
if (axis == majorAxis) {
returnmajorRequest.alignment;
} else {
returnminorRequest.alignment;
}
}
/**
* Determines the preferred span for this view along an
* axis.
*
* @param axis may be either <code>View.X_AXIS</code>
* or <code>View.Y_AXIS</code>
* @return the span the view would like to be rendered into >= 0;
* typically the view is told to render into the span
* that is returned, although there is no guarantee;
* the parent may choose to resize or break the view
* @throws IllegalArgumentException for an invalid axis type
*/
publicfloatgetPreferredSpan(intaxis) {
checkRequests(axis);
floatmarginSpan = (axis == X_AXIS) ? getLeftInset() + getRightInset() :
getTopInset() + getBottomInset();
if (axis == majorAxis) {
return ((float)majorRequest.preferred) + marginSpan;
} else {
return ((float)minorRequest.preferred) + marginSpan;
}
}
/**
* Determines the minimum span for this view along an
* axis.
*
* @param axis may be either <code>View.X_AXIS</code>
* or <code>View.Y_AXIS</code>
* @return the span the view would like to be rendered into >= 0;
* typically the view is told to render into the span
* that is returned, although there is no guarantee;
* the parent may choose to resize or break the view
* @throws IllegalArgumentException for an invalid axis type
*/
publicfloatgetMinimumSpan(intaxis) {
checkRequests(axis);
floatmarginSpan = (axis == X_AXIS) ? getLeftInset() + getRightInset() :
getTopInset() + getBottomInset();
if (axis == majorAxis) {
return ((float)majorRequest.minimum) + marginSpan;
} else {
return ((float)minorRequest.minimum) + marginSpan;
}
}
/**
* Determines the maximum span for this view along an
* axis.
*
* @param axis may be either <code>View.X_AXIS</code>
* or <code>View.Y_AXIS</code>
* @return the span the view would like to be rendered into >= 0;
* typically the view is told to render into the span
* that is returned, although there is no guarantee;
* the parent may choose to resize or break the view
* @throws IllegalArgumentException for an invalid axis type
*/
publicfloatgetMaximumSpan(intaxis) {
checkRequests(axis);
floatmarginSpan = (axis == X_AXIS) ? getLeftInset() + getRightInset() :
getTopInset() + getBottomInset();
if (axis == majorAxis) {
return ((float)majorRequest.maximum) + marginSpan;
} else {
return ((float)minorRequest.maximum) + marginSpan;
}
}
// --- local methods ----------------------------------------------------
/**
* Are the allocations for the children still
* valid?
*
* @return true if allocations still valid
*/
protectedbooleanisAllocationValid() {
return (majorAllocValid && minorAllocValid);
}
/**
* Determines if a point falls before an allocated region.
*
* @param x the X coordinate >= 0
* @param y the Y coordinate >= 0
* @param innerAlloc the allocated region; this is the area
* inside of the insets
* @return true if the point lies before the region else false
*/
protectedbooleanisBefore(intx, inty, RectangleinnerAlloc) {
if (majorAxis == View.X_AXIS) {
return (x < innerAlloc.x);
} else {
return (y < innerAlloc.y);
}
}
/**
* Determines if a point falls after an allocated region.
*
* @param x the X coordinate >= 0
* @param y the Y coordinate >= 0
* @param innerAlloc the allocated region; this is the area
* inside of the insets
* @return true if the point lies after the region else false
*/
protectedbooleanisAfter(intx, inty, RectangleinnerAlloc) {
if (majorAxis == View.X_AXIS) {
return (x > (innerAlloc.width + innerAlloc.x));
} else {
return (y > (innerAlloc.height + innerAlloc.y));
}
}
/**
* Fetches the child view at the given coordinates.
*
* @param x the X coordinate >= 0
* @param y the Y coordinate >= 0
* @param alloc the parents inner allocation on entry, which should
* be changed to the child's allocation on exit
* @return the view
*/
protectedViewgetViewAtPoint(intx, inty, Rectanglealloc) {
intn = getViewCount();
if (majorAxis == View.X_AXIS) {
if (x < (alloc.x + majorOffsets[0])) {
childAllocation(0, alloc);
returngetView(0);
}
for (inti = 0; i < n; i++) {
if (x < (alloc.x + majorOffsets[i])) {
childAllocation(i - 1, alloc);
returngetView(i - 1);
}
}
childAllocation(n - 1, alloc);
returngetView(n - 1);
} else {
if (y < (alloc.y + majorOffsets[0])) {
childAllocation(0, alloc);
returngetView(0);
}
for (inti = 0; i < n; i++) {
if (y < (alloc.y + majorOffsets[i])) {
childAllocation(i - 1, alloc);
returngetView(i - 1);
}
}
childAllocation(n - 1, alloc);
returngetView(n - 1);
}
}
/**
* Allocates a region for a child view.
*
* @param index the index of the child view to
* allocate, >= 0 && < getViewCount()
* @param alloc the allocated region
*/
protectedvoidchildAllocation(intindex, Rectanglealloc) {
alloc.x += getOffset(X_AXIS, index);
alloc.y += getOffset(Y_AXIS, index);
alloc.width = getSpan(X_AXIS, index);
alloc.height = getSpan(Y_AXIS, index);
}
/**
* Perform layout on the box
*
* @param width the width (inside of the insets) >= 0
* @param height the height (inside of the insets) >= 0
*/
protectedvoidlayout(intwidth, intheight) {
setSpanOnAxis(X_AXIS, width);
setSpanOnAxis(Y_AXIS, height);
}
/**
* Returns the current width of the box. This is the width that
* it was last allocated.
* @return the current width of the box
*/
publicintgetWidth() {
intspan;
if (majorAxis == X_AXIS) {
span = majorSpan;
} else {
span = minorSpan;
}
span += getLeftInset() - getRightInset();
returnspan;
}
/**
* Returns the current height of the box. This is the height that
* it was last allocated.
* @return the current height of the box
*/
publicintgetHeight() {
intspan;
if (majorAxis == Y_AXIS) {
span = majorSpan;
} else {
span = minorSpan;
}
span += getTopInset() - getBottomInset();
returnspan;
}
/**
* Performs layout for the major axis of the box (i.e. the
* axis that it represents). The results of the layout (the
* offset and span for each children) are placed in the given
* arrays which represent the allocations to the children
* along the major axis.
*
* @param targetSpan the total span given to the view, which
* would be used to layout the children
* @param axis the axis being laid out
* @param offsets the offsets from the origin of the view for
* each of the child views; this is a return value and is
* filled in by the implementation of this method
* @param spans the span of each child view; this is a return
* value and is filled in by the implementation of this method
*/
protectedvoidlayoutMajorAxis(inttargetSpan, intaxis, int[] offsets, int[] spans) {
/*
* first pass, calculate the preferred sizes
* and the flexibility to adjust the sizes.
*/
longpreferred = 0;
intn = getViewCount();
for (inti = 0; i < n; i++) {
Viewv = getView(i);
spans[i] = (int) v.getPreferredSpan(axis);
preferred += spans[i];
}
/*
* Second pass, expand or contract by as much as possible to reach
* the target span.
*/
// determine the adjustment to be made
longdesiredAdjustment = targetSpan - preferred;
floatadjustmentFactor = 0.0f;
int[] diffs = null;
if (desiredAdjustment != 0) {
longtotalSpan = 0;
diffs = newint[n];
for (inti = 0; i < n; i++) {
Viewv = getView(i);
inttmp;
if (desiredAdjustment < 0) {
tmp = (int)v.getMinimumSpan(axis);
diffs[i] = spans[i] - tmp;
} else {
tmp = (int)v.getMaximumSpan(axis);
diffs[i] = tmp - spans[i];
}
totalSpan += tmp;
}
floatmaximumAdjustment = Math.abs(totalSpan - preferred);
adjustmentFactor = desiredAdjustment / maximumAdjustment;
adjustmentFactor = Math.min(adjustmentFactor, 1.0f);
adjustmentFactor = Math.max(adjustmentFactor, -1.0f);
}
// make the adjustments
inttotalOffset = 0;
for (inti = 0; i < n; i++) {
offsets[i] = totalOffset;
if (desiredAdjustment != 0) {
floatadjF = adjustmentFactor * diffs[i];
spans[i] += Math.round(adjF);
}
totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE);
}
}
/**
* Performs layout for the minor axis of the box (i.e. the
* axis orthogonal to the axis that it represents). The results
* of the layout (the offset and span for each children) are
* placed in the given arrays which represent the allocations to
* the children along the minor axis.
*
* @param targetSpan the total span given to the view, which
* would be used to layout the children
* @param axis the axis being laid out
* @param offsets the offsets from the origin of the view for
* each of the child views; this is a return value and is
* filled in by the implementation of this method
* @param spans the span of each child view; this is a return
* value and is filled in by the implementation of this method
*/
protectedvoidlayoutMinorAxis(inttargetSpan, intaxis, int[] offsets, int[] spans) {
intn = getViewCount();
for (inti = 0; i < n; i++) {
Viewv = getView(i);
intmax = (int) v.getMaximumSpan(axis);
if (max < targetSpan) {
// can't make the child this wide, align it
floatalign = v.getAlignment(axis);
offsets[i] = (int) ((targetSpan - max) * align);
spans[i] = max;
} else {
// make it the target width, or as small as it can get.
intmin = (int)v.getMinimumSpan(axis);
offsets[i] = 0;
spans[i] = Math.max(min, targetSpan);
}
}
}
/**
* Calculates the size requirements for the major axis
* <code>axis</code>.
*
* @param axis the axis being studied
* @param r the <code>SizeRequirements</code> object;
* if <code>null</code> one will be created
* @return the newly initialized <code>SizeRequirements</code> object
* @see javax.swing.SizeRequirements
*/
protectedSizeRequirementscalculateMajorAxisRequirements(intaxis, SizeRequirementsr) {
// calculate tiled request
floatmin = 0;
floatpref = 0;
floatmax = 0;
intn = getViewCount();
for (inti = 0; i < n; i++) {
Viewv = getView(i);
min += v.getMinimumSpan(axis);
pref += v.getPreferredSpan(axis);
max += v.getMaximumSpan(axis);
}
if (r == null) {
r = newSizeRequirements();
}
r.alignment = 0.5f;
r.minimum = (int) min;
r.preferred = (int) pref;
r.maximum = (int) max;
returnr;
}
/**
* Calculates the size requirements for the minor axis
* <code>axis</code>.
*
* @param axis the axis being studied
* @param r the <code>SizeRequirements</code> object;
* if <code>null</code> one will be created
* @return the newly initialized <code>SizeRequirements</code> object
* @see javax.swing.SizeRequirements
*/
protectedSizeRequirementscalculateMinorAxisRequirements(intaxis, SizeRequirementsr) {
intmin = 0;
longpref = 0;
intmax = Integer.MAX_VALUE;
intn = getViewCount();
for (inti = 0; i < n; i++) {
Viewv = getView(i);
min = Math.max((int) v.getMinimumSpan(axis), min);
pref = Math.max((int) v.getPreferredSpan(axis), pref);
max = Math.max((int) v.getMaximumSpan(axis), max);
}
if (r == null) {
r = newSizeRequirements();
r.alignment = 0.5f;
}
r.preferred = (int) pref;
r.minimum = min;
r.maximum = max;
returnr;
}
/**
* Checks the request cache and update if needed.
* @param axis the axis being studied
* @throws IllegalArgumentException if <code>axis</code> is
* neither <code>View.X_AXIS</code> nor <code>View.Y_AXIS</code>
*/
voidcheckRequests(intaxis) {
if ((axis != X_AXIS) && (axis != Y_AXIS)) {
thrownewIllegalArgumentException("Invalid axis: " + axis);
}
if (axis == majorAxis) {
if (!majorReqValid) {
majorRequest = calculateMajorAxisRequirements(axis,
majorRequest);
majorReqValid = true;
}
} elseif (! minorReqValid) {
minorRequest = calculateMinorAxisRequirements(axis, minorRequest);
minorReqValid = true;
}
}
/**
* Computes the location and extent of each child view
* in this <code>BoxView</code> given the <code>targetSpan</code>,
* which is the width (or height) of the region we have to
* work with.
*
* @param targetSpan the total span given to the view, which
* would be used to layout the children
* @param axis the axis being studied, either
* <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
* @param offsets an empty array filled by this method with
* values specifying the location of each child view
* @param spans an empty array filled by this method with
* values specifying the extent of each child view
*/
protectedvoidbaselineLayout(inttargetSpan, intaxis, int[] offsets, int[] spans) {
inttotalAscent = (int)(targetSpan * getAlignment(axis));
inttotalDescent = targetSpan - totalAscent;
intn = getViewCount();
for (inti = 0; i < n; i++) {
Viewv = getView(i);
floatalign = v.getAlignment(axis);
floatviewSpan;
if (v.getResizeWeight(axis) > 0) {
// if resizable then resize to the best fit
// the smallest span possible
floatminSpan = v.getMinimumSpan(axis);
// the largest span possible
floatmaxSpan = v.getMaximumSpan(axis);
if (align == 0.0f) {
// if the alignment is 0 then we need to fit into the descent
viewSpan = Math.max(Math.min(maxSpan, totalDescent), minSpan);
} elseif (align == 1.0f) {
// if the alignment is 1 then we need to fit into the ascent
viewSpan = Math.max(Math.min(maxSpan, totalAscent), minSpan);
} else {
// figure out the span that we must fit into
floatfitSpan = Math.min(totalAscent / align,
totalDescent / (1.0f - align));
// fit into the calculated span
viewSpan = Math.max(Math.min(maxSpan, fitSpan), minSpan);
}
} else {
// otherwise use the preferred spans
viewSpan = v.getPreferredSpan(axis);
}
offsets[i] = totalAscent - (int)(viewSpan * align);
spans[i] = (int)viewSpan;
}
}
/**
* Calculates the size requirements for this <code>BoxView</code>
* by examining the size of each child view.