1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.GridBagConstraints;
|
---|
7 | import java.awt.Insets;
|
---|
8 |
|
---|
9 | import javax.swing.Box;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * A wrapper for GridBagConstraints which has sane default static creators and
|
---|
13 | * member functions to chain calling.
|
---|
14 | *
|
---|
15 | * @author imi
|
---|
16 | */
|
---|
17 | public final class GBC extends GridBagConstraints {
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Use public static creator functions to create an GBC.
|
---|
21 | */
|
---|
22 | private GBC() {}
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Create a standard constraint (which is not the last).
|
---|
26 | * @return A standard constraint with no filling.
|
---|
27 | */
|
---|
28 | public static GBC std() {
|
---|
29 | GBC c = new GBC();
|
---|
30 | c.anchor = WEST;
|
---|
31 | return c;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Create the constraint for the last elements on a line.
|
---|
36 | * @return A constraint which indicates the last item on a line.
|
---|
37 | */
|
---|
38 | public static GBC eol() {
|
---|
39 | GBC c = std();
|
---|
40 | c.gridwidth = REMAINDER;
|
---|
41 | return c;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Create the constraint for the last elements on a line and on a paragraph.
|
---|
46 | * This is merely a shortcut for eol().insets(0,0,0,10)
|
---|
47 | * @return A constraint which indicates the last item on a line.
|
---|
48 | */
|
---|
49 | public static GBC eop() {
|
---|
50 | return eol().insets(0, 0, 0, 10);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Try to fill both, horizontal and vertical
|
---|
55 | * @return This constraint for chaining.
|
---|
56 | */
|
---|
57 | public GBC fill() {
|
---|
58 | return fill(BOTH);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Set fill to the given value
|
---|
63 | * @param value The filling value, either NONE, HORIZONTAL, VERTICAL or BOTH
|
---|
64 | * @return This constraint for chaining.
|
---|
65 | */
|
---|
66 | public GBC fill(int value) {
|
---|
67 | fill = value;
|
---|
68 | if (value == HORIZONTAL || value == BOTH)
|
---|
69 | weightx = 1.0;
|
---|
70 | if (value == VERTICAL || value == BOTH)
|
---|
71 | weighty = 1.0;
|
---|
72 | return this;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Set the anchor of this GBC to a.
|
---|
77 | * @param a The new anchor, e.g. GBC.CENTER or GBC.EAST.
|
---|
78 | * @return This constraint for chaining.
|
---|
79 | */
|
---|
80 | public GBC anchor(int a) {
|
---|
81 | anchor = a;
|
---|
82 | return this;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Adds insets to this GBC.
|
---|
87 | * @param left The left space of the insets
|
---|
88 | * @param top The top space of the insets
|
---|
89 | * @param right The right space of the insets
|
---|
90 | * @param bottom The bottom space of the insets
|
---|
91 | * @return This constraint for chaining.
|
---|
92 | */
|
---|
93 | public GBC insets(int left, int top, int right, int bottom) {
|
---|
94 | insets = new Insets(top, left, bottom, right);
|
---|
95 | return this;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Adds insets to this GBC.
|
---|
100 | * @param insets The insets in all directions.
|
---|
101 | * @return This constraint for chaining.
|
---|
102 | * @since 10649
|
---|
103 | */
|
---|
104 | public GBC insets(int insets) {
|
---|
105 | insets(insets, insets, insets, insets);
|
---|
106 | return this;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Specifies how to distribute extra horizontal space.
|
---|
111 | * @param weightx Weight in horizontal direction
|
---|
112 | * @param weighty Weight in vertical direction
|
---|
113 | * @return This constraint for chaining.
|
---|
114 | */
|
---|
115 | public GBC weight(double weightx, double weighty) {
|
---|
116 | this.weightx = weightx;
|
---|
117 | this.weighty = weighty;
|
---|
118 | return this;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * This is a helper to easily create a glue with a minimum default value.
|
---|
123 | * @param x If higher than 0, this will be a horizontal glue with x as minimum
|
---|
124 | * horizontal strut.
|
---|
125 | * @param y If higher than 0, this will be a vertical glue with y as minimum
|
---|
126 | * vertical strut.
|
---|
127 | * @return the glue component
|
---|
128 | */
|
---|
129 | public static Component glue(int x, int y) {
|
---|
130 | short maxx = x > 0 ? Short.MAX_VALUE : 0;
|
---|
131 | short maxy = y > 0 ? Short.MAX_VALUE : 0;
|
---|
132 | return new Box.Filler(new Dimension(x, y), new Dimension(x, y), new Dimension(maxx, maxy));
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Sets the constraint's {@code gridx}, {@code gridy}.
|
---|
137 | * @param gridx cell containing the leading edge of the component's display area
|
---|
138 | * @param gridy cell at the top of the component's display area
|
---|
139 | * @return This constraint for chaining.
|
---|
140 | * @see #gridx
|
---|
141 | * @see #gridy
|
---|
142 | */
|
---|
143 | public GBC grid(int gridx, int gridy) {
|
---|
144 | this.gridx = gridx;
|
---|
145 | this.gridy = gridy;
|
---|
146 | return this;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Sets the constraint's {@code gridwidth}, {@code gridheight}.
|
---|
151 | * @param gridwidth number of cells in a row for the component's display area
|
---|
152 | * @param gridheight number of cells in a column for the component's display area
|
---|
153 | * @return This constraint for chaining.
|
---|
154 | * @see #gridwidth
|
---|
155 | * @see #gridheight
|
---|
156 | */
|
---|
157 | public GBC span(int gridwidth, int gridheight) {
|
---|
158 | this.gridwidth = gridwidth;
|
---|
159 | this.gridheight = gridheight;
|
---|
160 | return this;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Sets the constraint's {@code gridwidth}.
|
---|
165 | * @param gridwidth number of cells in a row for the component's display area
|
---|
166 | * @return This constraint for chaining.
|
---|
167 | * @see #gridwidth
|
---|
168 | */
|
---|
169 | public GBC span(int gridwidth) {
|
---|
170 | this.gridwidth = gridwidth;
|
---|
171 | return this;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Create a standard constraint with the {@code gridx}, {@code gridy} set.
|
---|
176 | *
|
---|
177 | * Is equivalent to {@code std().grid(gridx, gridy)}
|
---|
178 | * @param gridx cell containing the leading edge of the component's display area
|
---|
179 | * @param gridy cell at the top of the component's display area
|
---|
180 | * @return A standard constraint.
|
---|
181 | * @see #std()
|
---|
182 | * @see #grid(int, int)
|
---|
183 | * @see #gridx
|
---|
184 | * @see #gridy
|
---|
185 | */
|
---|
186 | public static GBC std(int gridx, int gridy) {
|
---|
187 | return std().grid(gridx, gridy);
|
---|
188 | }
|
---|
189 | }
|
---|