Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/awt/FlowLayout.java
41152 views
1
/*
2
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.awt;
27
28
import java.io.IOException;
29
import java.io.ObjectInputStream;
30
import java.io.Serial;
31
32
/**
33
* A flow layout arranges components in a directional flow, much
34
* like lines of text in a paragraph. The flow direction is
35
* determined by the container's {@code componentOrientation}
36
* property and may be one of two values:
37
* <ul>
38
* <li>{@code ComponentOrientation.LEFT_TO_RIGHT}
39
* <li>{@code ComponentOrientation.RIGHT_TO_LEFT}
40
* </ul>
41
* Flow layouts are typically used
42
* to arrange buttons in a panel. It arranges buttons
43
* horizontally until no more buttons fit on the same line.
44
* The line alignment is determined by the {@code align}
45
* property. The possible values are:
46
* <ul>
47
* <li>{@link #LEFT LEFT}
48
* <li>{@link #RIGHT RIGHT}
49
* <li>{@link #CENTER CENTER}
50
* <li>{@link #LEADING LEADING}
51
* <li>{@link #TRAILING TRAILING}
52
* </ul>
53
* <p>
54
* For example, the following picture shows an applet using the flow
55
* layout manager (its default layout manager) to position three buttons:
56
* <p>
57
* <img src="doc-files/FlowLayout-1.gif"
58
* ALT="Graphic of Layout for Three Buttons"
59
* style="margin: 7px 10px;">
60
* <p>
61
* Here is the code for this applet:
62
*
63
* <hr><blockquote><pre>
64
* import java.awt.*;
65
* import java.applet.Applet;
66
*
67
* public class myButtons extends Applet {
68
* Button button1, button2, button3;
69
* public void init() {
70
* button1 = new Button("Ok");
71
* button2 = new Button("Open");
72
* button3 = new Button("Close");
73
* add(button1);
74
* add(button2);
75
* add(button3);
76
* }
77
* }
78
* </pre></blockquote><hr>
79
* <p>
80
* A flow layout lets each component assume its natural (preferred) size.
81
*
82
* @author Arthur van Hoff
83
* @author Sami Shaio
84
* @since 1.0
85
* @see ComponentOrientation
86
*/
87
public class FlowLayout implements LayoutManager, java.io.Serializable {
88
89
/**
90
* This value indicates that each row of components
91
* should be left-justified.
92
*/
93
public static final int LEFT = 0;
94
95
/**
96
* This value indicates that each row of components
97
* should be centered.
98
*/
99
public static final int CENTER = 1;
100
101
/**
102
* This value indicates that each row of components
103
* should be right-justified.
104
*/
105
public static final int RIGHT = 2;
106
107
/**
108
* This value indicates that each row of components
109
* should be justified to the leading edge of the container's
110
* orientation, for example, to the left in left-to-right orientations.
111
*
112
* @see java.awt.Component#getComponentOrientation
113
* @see java.awt.ComponentOrientation
114
* @since 1.2
115
*/
116
public static final int LEADING = 3;
117
118
/**
119
* This value indicates that each row of components
120
* should be justified to the trailing edge of the container's
121
* orientation, for example, to the right in left-to-right orientations.
122
*
123
* @see java.awt.Component#getComponentOrientation
124
* @see java.awt.ComponentOrientation
125
* @since 1.2
126
*/
127
public static final int TRAILING = 4;
128
129
/**
130
* {@code align} is the property that determines
131
* how each row distributes empty space.
132
* It can be one of the following values:
133
* <ul>
134
* <li>{@code LEFT}
135
* <li>{@code RIGHT}
136
* <li>{@code CENTER}
137
* </ul>
138
*
139
* @serial
140
* @see #getAlignment
141
* @see #setAlignment
142
*/
143
int align; // This is for 1.1 serialization compatibility
144
145
/**
146
* {@code newAlign} is the property that determines
147
* how each row distributes empty space for the Java 2 platform,
148
* v1.2 and greater.
149
* It can be one of the following three values:
150
* <ul>
151
* <li>{@code LEFT}
152
* <li>{@code RIGHT}
153
* <li>{@code CENTER}
154
* <li>{@code LEADING}
155
* <li>{@code TRAILING}
156
* </ul>
157
*
158
* @serial
159
* @since 1.2
160
* @see #getAlignment
161
* @see #setAlignment
162
*/
163
int newAlign; // This is the one we actually use
164
165
/**
166
* The flow layout manager allows a separation of
167
* components with gaps. The horizontal gap will
168
* specify the space between components and between
169
* the components and the borders of the
170
* {@code Container}.
171
*
172
* @serial
173
* @see #getHgap()
174
* @see #setHgap(int)
175
*/
176
int hgap;
177
178
/**
179
* The flow layout manager allows a separation of
180
* components with gaps. The vertical gap will
181
* specify the space between rows and between the
182
* the rows and the borders of the {@code Container}.
183
*
184
* @serial
185
* @see #getHgap()
186
* @see #setHgap(int)
187
*/
188
int vgap;
189
190
/**
191
* If true, components will be aligned on their baseline.
192
*/
193
private boolean alignOnBaseline;
194
195
/**
196
* Use serialVersionUID from JDK 1.1 for interoperability.
197
*/
198
@Serial
199
private static final long serialVersionUID = -7262534875583282631L;
200
201
/**
202
* Constructs a new {@code FlowLayout} with a centered alignment and a
203
* default 5-unit horizontal and vertical gap.
204
*/
205
public FlowLayout() {
206
this(CENTER, 5, 5);
207
}
208
209
/**
210
* Constructs a new {@code FlowLayout} with the specified
211
* alignment and a default 5-unit horizontal and vertical gap.
212
* The value of the alignment argument must be one of
213
* {@code FlowLayout.LEFT}, {@code FlowLayout.RIGHT},
214
* {@code FlowLayout.CENTER}, {@code FlowLayout.LEADING},
215
* or {@code FlowLayout.TRAILING}.
216
* @param align the alignment value
217
*/
218
public FlowLayout(int align) {
219
this(align, 5, 5);
220
}
221
222
/**
223
* Creates a new flow layout manager with the indicated alignment
224
* and the indicated horizontal and vertical gaps.
225
* <p>
226
* The value of the alignment argument must be one of
227
* {@code FlowLayout.LEFT}, {@code FlowLayout.RIGHT},
228
* {@code FlowLayout.CENTER}, {@code FlowLayout.LEADING},
229
* or {@code FlowLayout.TRAILING}.
230
* @param align the alignment value
231
* @param hgap the horizontal gap between components
232
* and between the components and the
233
* borders of the {@code Container}
234
* @param vgap the vertical gap between components
235
* and between the components and the
236
* borders of the {@code Container}
237
*/
238
public FlowLayout(int align, int hgap, int vgap) {
239
this.hgap = hgap;
240
this.vgap = vgap;
241
setAlignment(align);
242
}
243
244
/**
245
* Gets the alignment for this layout.
246
* Possible values are {@code FlowLayout.LEFT},
247
* {@code FlowLayout.RIGHT}, {@code FlowLayout.CENTER},
248
* {@code FlowLayout.LEADING},
249
* or {@code FlowLayout.TRAILING}.
250
* @return the alignment value for this layout
251
* @see java.awt.FlowLayout#setAlignment
252
* @since 1.1
253
*/
254
public int getAlignment() {
255
return newAlign;
256
}
257
258
/**
259
* Sets the alignment for this layout.
260
* Possible values are
261
* <ul>
262
* <li>{@code FlowLayout.LEFT}
263
* <li>{@code FlowLayout.RIGHT}
264
* <li>{@code FlowLayout.CENTER}
265
* <li>{@code FlowLayout.LEADING}
266
* <li>{@code FlowLayout.TRAILING}
267
* </ul>
268
* @param align one of the alignment values shown above
269
* @see #getAlignment()
270
* @since 1.1
271
*/
272
public void setAlignment(int align) {
273
this.newAlign = align;
274
275
// this.align is used only for serialization compatibility,
276
// so set it to a value compatible with the 1.1 version
277
// of the class
278
279
switch (align) {
280
case LEADING:
281
this.align = LEFT;
282
break;
283
case TRAILING:
284
this.align = RIGHT;
285
break;
286
default:
287
this.align = align;
288
break;
289
}
290
}
291
292
/**
293
* Gets the horizontal gap between components
294
* and between the components and the borders
295
* of the {@code Container}
296
*
297
* @return the horizontal gap between components
298
* and between the components and the borders
299
* of the {@code Container}
300
* @see java.awt.FlowLayout#setHgap
301
* @since 1.1
302
*/
303
public int getHgap() {
304
return hgap;
305
}
306
307
/**
308
* Sets the horizontal gap between components and
309
* between the components and the borders of the
310
* {@code Container}.
311
*
312
* @param hgap the horizontal gap between components
313
* and between the components and the borders
314
* of the {@code Container}
315
* @see java.awt.FlowLayout#getHgap
316
* @since 1.1
317
*/
318
public void setHgap(int hgap) {
319
this.hgap = hgap;
320
}
321
322
/**
323
* Gets the vertical gap between components and
324
* between the components and the borders of the
325
* {@code Container}.
326
*
327
* @return the vertical gap between components
328
* and between the components and the borders
329
* of the {@code Container}
330
* @see java.awt.FlowLayout#setVgap
331
* @since 1.1
332
*/
333
public int getVgap() {
334
return vgap;
335
}
336
337
/**
338
* Sets the vertical gap between components and between
339
* the components and the borders of the {@code Container}.
340
*
341
* @param vgap the vertical gap between components
342
* and between the components and the borders
343
* of the {@code Container}
344
* @see java.awt.FlowLayout#getVgap
345
* @since 1.1
346
*/
347
public void setVgap(int vgap) {
348
this.vgap = vgap;
349
}
350
351
/**
352
* Sets whether or not components should be vertically aligned along their
353
* baseline. Components that do not have a baseline will be centered.
354
* The default is false.
355
*
356
* @param alignOnBaseline whether or not components should be
357
* vertically aligned on their baseline
358
* @since 1.6
359
*/
360
public void setAlignOnBaseline(boolean alignOnBaseline) {
361
this.alignOnBaseline = alignOnBaseline;
362
}
363
364
/**
365
* Returns true if components are to be vertically aligned along
366
* their baseline. The default is false.
367
*
368
* @return true if components are to be vertically aligned along
369
* their baseline
370
* @since 1.6
371
*/
372
public boolean getAlignOnBaseline() {
373
return alignOnBaseline;
374
}
375
376
/**
377
* Adds the specified component to the layout.
378
* Not used by this class.
379
* @param name the name of the component
380
* @param comp the component to be added
381
*/
382
public void addLayoutComponent(String name, Component comp) {
383
}
384
385
/**
386
* Removes the specified component from the layout.
387
* Not used by this class.
388
* @param comp the component to remove
389
* @see java.awt.Container#removeAll
390
*/
391
public void removeLayoutComponent(Component comp) {
392
}
393
394
/**
395
* Returns the preferred dimensions for this layout given the
396
* <i>visible</i> components in the specified target container.
397
*
398
* @param target the container that needs to be laid out
399
* @return the preferred dimensions to lay out the
400
* subcomponents of the specified container
401
* @see Container
402
* @see #minimumLayoutSize
403
* @see java.awt.Container#getPreferredSize
404
*/
405
public Dimension preferredLayoutSize(Container target) {
406
synchronized (target.getTreeLock()) {
407
Dimension dim = new Dimension(0, 0);
408
int nmembers = target.getComponentCount();
409
boolean firstVisibleComponent = true;
410
boolean useBaseline = getAlignOnBaseline();
411
int maxAscent = 0;
412
int maxDescent = 0;
413
414
for (int i = 0 ; i < nmembers ; i++) {
415
Component m = target.getComponent(i);
416
if (m.isVisible()) {
417
Dimension d = m.getPreferredSize();
418
dim.height = Math.max(dim.height, d.height);
419
if (firstVisibleComponent) {
420
firstVisibleComponent = false;
421
} else {
422
dim.width += hgap;
423
}
424
dim.width += d.width;
425
if (useBaseline) {
426
int baseline = m.getBaseline(d.width, d.height);
427
if (baseline >= 0) {
428
maxAscent = Math.max(maxAscent, baseline);
429
maxDescent = Math.max(maxDescent, d.height - baseline);
430
}
431
}
432
}
433
}
434
if (useBaseline) {
435
dim.height = Math.max(maxAscent + maxDescent, dim.height);
436
}
437
Insets insets = target.getInsets();
438
dim.width += insets.left + insets.right + hgap*2;
439
dim.height += insets.top + insets.bottom + vgap*2;
440
return dim;
441
}
442
}
443
444
/**
445
* Returns the minimum dimensions needed to layout the <i>visible</i>
446
* components contained in the specified target container.
447
* @param target the container that needs to be laid out
448
* @return the minimum dimensions to lay out the
449
* subcomponents of the specified container
450
* @see #preferredLayoutSize
451
* @see java.awt.Container
452
* @see java.awt.Container#doLayout
453
*/
454
public Dimension minimumLayoutSize(Container target) {
455
synchronized (target.getTreeLock()) {
456
boolean useBaseline = getAlignOnBaseline();
457
Dimension dim = new Dimension(0, 0);
458
int nmembers = target.getComponentCount();
459
int maxAscent = 0;
460
int maxDescent = 0;
461
boolean firstVisibleComponent = true;
462
463
for (int i = 0 ; i < nmembers ; i++) {
464
Component m = target.getComponent(i);
465
if (m.visible) {
466
Dimension d = m.getMinimumSize();
467
dim.height = Math.max(dim.height, d.height);
468
if (firstVisibleComponent) {
469
firstVisibleComponent = false;
470
} else {
471
dim.width += hgap;
472
}
473
dim.width += d.width;
474
if (useBaseline) {
475
int baseline = m.getBaseline(d.width, d.height);
476
if (baseline >= 0) {
477
maxAscent = Math.max(maxAscent, baseline);
478
maxDescent = Math.max(maxDescent,
479
dim.height - baseline);
480
}
481
}
482
}
483
}
484
485
if (useBaseline) {
486
dim.height = Math.max(maxAscent + maxDescent, dim.height);
487
}
488
489
Insets insets = target.getInsets();
490
dim.width += insets.left + insets.right + hgap*2;
491
dim.height += insets.top + insets.bottom + vgap*2;
492
return dim;
493
494
495
496
497
498
}
499
}
500
501
/**
502
* Centers the elements in the specified row, if there is any slack.
503
* @param target the component which needs to be moved
504
* @param x the x coordinate
505
* @param y the y coordinate
506
* @param width the width dimensions
507
* @param height the height dimensions
508
* @param rowStart the beginning of the row
509
* @param rowEnd the ending of the row
510
* @param useBaseline Whether or not to align on baseline.
511
* @param ascent Ascent for the components. This is only valid if
512
* useBaseline is true.
513
* @param descent Ascent for the components. This is only valid if
514
* useBaseline is true.
515
* @return actual row height
516
*/
517
private int moveComponents(Container target, int x, int y, int width, int height,
518
int rowStart, int rowEnd, boolean ltr,
519
boolean useBaseline, int[] ascent,
520
int[] descent) {
521
switch (newAlign) {
522
case LEFT:
523
x += ltr ? 0 : width;
524
break;
525
case CENTER:
526
x += width / 2;
527
break;
528
case RIGHT:
529
x += ltr ? width : 0;
530
break;
531
case LEADING:
532
break;
533
case TRAILING:
534
x += width;
535
break;
536
}
537
int maxAscent = 0;
538
int nonbaselineHeight = 0;
539
int baselineOffset = 0;
540
if (useBaseline) {
541
int maxDescent = 0;
542
for (int i = rowStart ; i < rowEnd ; i++) {
543
Component m = target.getComponent(i);
544
if (m.visible) {
545
if (ascent[i] >= 0) {
546
maxAscent = Math.max(maxAscent, ascent[i]);
547
maxDescent = Math.max(maxDescent, descent[i]);
548
}
549
else {
550
nonbaselineHeight = Math.max(m.getHeight(),
551
nonbaselineHeight);
552
}
553
}
554
}
555
height = Math.max(maxAscent + maxDescent, nonbaselineHeight);
556
baselineOffset = (height - maxAscent - maxDescent) / 2;
557
}
558
for (int i = rowStart ; i < rowEnd ; i++) {
559
Component m = target.getComponent(i);
560
if (m.isVisible()) {
561
int cy;
562
if (useBaseline && ascent[i] >= 0) {
563
cy = y + baselineOffset + maxAscent - ascent[i];
564
}
565
else {
566
cy = y + (height - m.height) / 2;
567
}
568
if (ltr) {
569
m.setLocation(x, cy);
570
} else {
571
m.setLocation(target.width - x - m.width, cy);
572
}
573
x += m.width + hgap;
574
}
575
}
576
return height;
577
}
578
579
/**
580
* Lays out the container. This method lets each
581
* <i>visible</i> component take
582
* its preferred size by reshaping the components in the
583
* target container in order to satisfy the alignment of
584
* this {@code FlowLayout} object.
585
*
586
* @param target the specified component being laid out
587
* @see Container
588
* @see java.awt.Container#doLayout
589
*/
590
public void layoutContainer(Container target) {
591
synchronized (target.getTreeLock()) {
592
Insets insets = target.getInsets();
593
int maxwidth = target.width - (insets.left + insets.right + hgap*2);
594
int nmembers = target.getComponentCount();
595
int x = 0, y = insets.top + vgap;
596
int rowh = 0, start = 0;
597
598
boolean ltr = target.getComponentOrientation().isLeftToRight();
599
600
boolean useBaseline = getAlignOnBaseline();
601
int[] ascent = null;
602
int[] descent = null;
603
604
if (useBaseline) {
605
ascent = new int[nmembers];
606
descent = new int[nmembers];
607
}
608
609
for (int i = 0 ; i < nmembers ; i++) {
610
Component m = target.getComponent(i);
611
if (m.isVisible()) {
612
Dimension d = m.getPreferredSize();
613
m.setSize(d.width, d.height);
614
615
if (useBaseline) {
616
int baseline = m.getBaseline(d.width, d.height);
617
if (baseline >= 0) {
618
ascent[i] = baseline;
619
descent[i] = d.height - baseline;
620
}
621
else {
622
ascent[i] = -1;
623
}
624
}
625
if ((x == 0) || ((x + d.width) <= maxwidth)) {
626
if (x > 0) {
627
x += hgap;
628
}
629
x += d.width;
630
rowh = Math.max(rowh, d.height);
631
} else {
632
rowh = moveComponents(target, insets.left + hgap, y,
633
maxwidth - x, rowh, start, i, ltr,
634
useBaseline, ascent, descent);
635
x = d.width;
636
y += vgap + rowh;
637
rowh = d.height;
638
start = i;
639
}
640
}
641
}
642
moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh,
643
start, nmembers, ltr, useBaseline, ascent, descent);
644
}
645
}
646
647
//
648
// the internal serial version which says which version was written
649
// - 0 (default) for versions before the Java 2 platform, v1.2
650
// - 1 for version >= Java 2 platform v1.2, which includes "newAlign" field
651
//
652
private static final int currentSerialVersion = 1;
653
/**
654
* This represent the {@code currentSerialVersion}
655
* which is bein used. It will be one of two values:
656
* {@code 0} versions before Java 2 platform v1.2,
657
* {@code 1} versions after Java 2 platform v1.2.
658
*
659
* @serial
660
* @since 1.2
661
*/
662
private int serialVersionOnStream = currentSerialVersion;
663
664
/**
665
* Reads this object out of a serialization stream, handling
666
* objects written by older versions of the class that didn't contain all
667
* of the fields we use now..
668
*
669
* @param stream the {@code ObjectInputStream} to read
670
* @throws ClassNotFoundException if the class of a serialized object could
671
* not be found
672
* @throws IOException if an I/O error occurs
673
*/
674
@Serial
675
private void readObject(ObjectInputStream stream)
676
throws IOException, ClassNotFoundException
677
{
678
stream.defaultReadObject();
679
680
if (serialVersionOnStream < 1) {
681
// "newAlign" field wasn't present, so use the old "align" field.
682
setAlignment(this.align);
683
}
684
serialVersionOnStream = currentSerialVersion;
685
}
686
687
/**
688
* Returns a string representation of this {@code FlowLayout}
689
* object and its values.
690
* @return a string representation of this layout
691
*/
692
public String toString() {
693
String str = "";
694
switch (align) {
695
case LEFT: str = ",align=left"; break;
696
case CENTER: str = ",align=center"; break;
697
case RIGHT: str = ",align=right"; break;
698
case LEADING: str = ",align=leading"; break;
699
case TRAILING: str = ",align=trailing"; break;
700
}
701
return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]";
702
}
703
704
705
}
706
707