JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
Alignment.java
Go to the documentation of this file.
1/**
2 * Copyright 2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.graph.ui.layout;
29
30import java.util.List;
31
32/**
33 * Immutable layout alignment options, including {@link Bit#Fill}.
34 */
35public final class Alignment {
36 /** No alignment constant. */
37 public static final Alignment None = new Alignment();
38 /** {@link Bit#CenterHoriz} and {@link Bit#CenterVert} alignment constant. */
39 public static final Alignment Center = new Alignment(Alignment.Bit.CenterHoriz.value | Alignment.Bit.CenterVert.value );
40 /** {@link Bit#CenterHoriz} alignment constant. */
41 public static final Alignment CenterHoriz = new Alignment(Alignment.Bit.CenterHoriz.value );
42 /** {@link Bit#Fill} alignment constant. */
43 public static final Alignment Fill = new Alignment(Alignment.Bit.Fill.value);
44 /** {@link Bit#Fill}, {@link Bit#CenterHoriz} and {@link Bit#CenterVert} alignment constant. */
45 public static final Alignment FillCenter = new Alignment(Alignment.Bit.Fill.value | Alignment.Bit.CenterHoriz.value | Alignment.Bit.CenterVert.value);
46
47 public enum Bit {
48 /** Left alignment. */
49 Left ( ( 1 << 0 ) ),
50
51 /** Right alignment. */
52 Right ( ( 1 << 1 ) ),
53
54 /** Bottom alignment. */
55 Bottom ( ( 1 << 2 ) ),
56
57 /** Top alignment. */
58 Top ( ( 1 << 3 ) ),
59
60 /** Scale object to parent size, e.g. fill {@link GridLayout} or {@link BoxLayout} cell size. */
61 Fill ( ( 1 << 4 ) ),
62
63 /** Horizontal center alignment. */
64 CenterHoriz ( ( 1 << 5 ) ),
65
66 /** Vertical center alignment. */
67 CenterVert ( ( 1 << 6 ) );
68
69 Bit(final int v) { value = v; }
70 public final int value;
71 }
72 public final int mask;
73
74 public static int getBits(final List<Bit> v) {
75 int res = 0;
76 for(final Bit b : v) {
77 res |= b.value;
78 }
79 return res;
80 }
81 public Alignment(final List<Bit> v) {
82 mask = getBits(v);
83 }
84 public Alignment(final Bit v) {
85 mask = v.value;
86 }
87 public Alignment(final int v) {
88 mask = v;
89 }
90 private Alignment() {
91 mask = 0;
92 }
93
94 public boolean isSet(final Bit bit) { return bit.value == ( mask & bit.value ); }
95 public boolean isSet(final List<Bit> bits) { final int bits_i = getBits(bits); return bits_i == ( mask & bits_i ); }
96 public boolean isSet(final int bits) { return bits == ( mask & bits ); }
97
98 @Override
99 public String toString() {
100 int count = 0;
101 final StringBuilder out = new StringBuilder();
102 for (final Bit dt : Bit.values()) {
103 if( isSet(dt) ) {
104 if( 0 < count ) { out.append(", "); }
105 out.append(dt.name()); count++;
106 }
107 }
108 if( 0 == count ) {
109 out.append("None");
110 } else if( 1 < count ) {
111 out.insert(0, "[");
112 out.append("]");
113 }
114 return out.toString();
115 }
116
117 @Override
118 public boolean equals(final Object other) {
119 if (this == other) {
120 return true;
121 }
122 return (other instanceof Alignment) &&
123 this.mask == ((Alignment)other).mask;
124 }
125}
Immutable layout alignment options, including Bit#Fill.
Definition: Alignment.java:35
static final Alignment Fill
Bit#Fill alignment constant.
Definition: Alignment.java:43
static final Alignment CenterHoriz
Bit#CenterHoriz alignment constant.
Definition: Alignment.java:41
static final Alignment Center
Bit#CenterHoriz and Bit#CenterVert alignment constant.
Definition: Alignment.java:39
boolean isSet(final List< Bit > bits)
Definition: Alignment.java:95
static int getBits(final List< Bit > v)
Definition: Alignment.java:74
boolean equals(final Object other)
Definition: Alignment.java:118
boolean isSet(final Bit bit)
Definition: Alignment.java:94
static final Alignment None
No alignment constant.
Definition: Alignment.java:37
static final Alignment FillCenter
Bit#Fill, Bit#CenterHoriz and Bit#CenterVert alignment constant.
Definition: Alignment.java:45
Alignment(final List< Bit > v)
Definition: Alignment.java:81
boolean isSet(final int bits)
Definition: Alignment.java:96
CenterHoriz
Horizontal center alignment.
Definition: Alignment.java:64
CenterVert
Vertical center alignment.
Definition: Alignment.java:67
Fill
Scale object to parent size, e.g.
Definition: Alignment.java:61