source: josm/trunk/src/org/openstreetmap/josm/tools/GBC.java@ 9190

Last change on this file since 9190 was 8911, checked in by simon04, 9 years ago

GBC: improve Javadoc

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.Component;
5import java.awt.Dimension;
6import java.awt.GridBagConstraints;
7import java.awt.Insets;
8
9import 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 */
17public 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 * Specifies how to distribute extra horizontal space.
100 * @param weightx Weight in horizontal direction
101 * @param weighty Weight in vertical direction
102 * @return This constraint for chaining.
103 */
104 public GBC weight(double weightx, double weighty) {
105 this.weightx = weightx;
106 this.weighty = weighty;
107 return this;
108 }
109
110 /**
111 * This is a helper to easily create a glue with a minimum default value.
112 * @param x If higher than 0, this will be a horizontal glue with x as minimum
113 * horizontal strut.
114 * @param y If higher than 0, this will be a vertical glue with y as minimum
115 * vertical strut.
116 * @return the glue component
117 */
118 public static Component glue(int x, int y) {
119 short maxx = x > 0 ? Short.MAX_VALUE : 0;
120 short maxy = y > 0 ? Short.MAX_VALUE : 0;
121 return new Box.Filler(new Dimension(x, y), new Dimension(x, y), new Dimension(maxx, maxy));
122 }
123
124 /**
125 * Sets the constraint's {@code gridx}, {@code gridy}.
126 * @return This constraint for chaining.
127 * @see #gridx
128 * @see #gridy
129 */
130 public GBC grid(int gridx, int gridy) {
131 this.gridx = gridx;
132 this.gridy = gridy;
133 return this;
134 }
135
136 /**
137 * Sets the constraint's {@code gridwidth}, {@code gridheight}.
138 * @return This constraint for chaining.
139 * @see #gridwidth
140 * @see #gridheight
141 */
142 public GBC span(int gridwidth, int gridheight) {
143 this.gridwidth = gridwidth;
144 this.gridheight = gridheight;
145 return this;
146 }
147
148 /**
149 * Sets the constraint's {@code gridwidth}.
150 * @return This constraint for chaining.
151 * @see #gridwidth
152 */
153 public GBC span(int gridwidth) {
154 this.gridwidth = gridwidth;
155 return this;
156 }
157
158 /**
159 * Create a standard constraint with the {@code gridx}, {@code gridy} set.
160 *
161 * Is equivalent to {@code std().grid(gridx, gridy)}
162 * @return A standard constraint.
163 * @see #std()
164 * @see #grid(int, int)
165 * @see #gridx
166 * @see #gridy
167 */
168 public static GBC std(int gridx, int gridy) {
169 return std().grid(gridx, gridy);
170 }
171
172}
Note: See TracBrowser for help on using the repository browser.