1 | package org.openstreetmap.josm.gui;
|
---|
2 |
|
---|
3 | import java.awt.Component;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.GridBagConstraints;
|
---|
6 | import java.awt.Insets;
|
---|
7 |
|
---|
8 | import javax.swing.Box;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A wrapper for GridBagConstraints which has sane default static creators and
|
---|
12 | * member functions to chain calling.
|
---|
13 | *
|
---|
14 | * @author imi
|
---|
15 | */
|
---|
16 | public class GBC extends GridBagConstraints {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Use public static creator functions to create an GBC.
|
---|
20 | */
|
---|
21 | private GBC() {}
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Create a standard constraint (which is not the last).
|
---|
25 | * @return A standard constraint with no filling.
|
---|
26 | */
|
---|
27 | public static GBC std() {
|
---|
28 | GBC c = new GBC();
|
---|
29 | c.anchor = WEST;
|
---|
30 | return c;
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Create the constraint for the last elements on a line.
|
---|
35 | * @return A constraint which indicates the last item on a line.
|
---|
36 | */
|
---|
37 | public static GBC eol() {
|
---|
38 | GBC c = std();
|
---|
39 | c.gridwidth = REMAINDER;
|
---|
40 | return c;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Try to fill both, horizontal and vertical
|
---|
45 | * @return This constraint for chaining.
|
---|
46 | */
|
---|
47 | public GBC fill() {
|
---|
48 | return fill(BOTH);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Set fill to the given value
|
---|
53 | * @param value The filling value, either NONE, HORIZONTAL, VERTICAL or BOTH
|
---|
54 | * @return This constraint for chaining.
|
---|
55 | */
|
---|
56 | public GBC fill(int value) {
|
---|
57 | fill = value;
|
---|
58 | if (value == HORIZONTAL || value == BOTH)
|
---|
59 | weightx = 1.0;
|
---|
60 | if (value == VERTICAL || value == BOTH)
|
---|
61 | weighty = 1.0;
|
---|
62 | return this;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Set the anchor of this GBC to a.
|
---|
67 | * @param a The new anchor, e.g. GBC.CENTER or GBC.EAST.
|
---|
68 | * @return This constraint for chaining.
|
---|
69 | */
|
---|
70 | public GBC anchor(int a) {
|
---|
71 | anchor = a;
|
---|
72 | return this;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Adds insets to this GBC.
|
---|
77 | * @param left The left space of the insets
|
---|
78 | * @param top The top space of the insets
|
---|
79 | * @param right The right space of the insets
|
---|
80 | * @param bottom The bottom space of the insets
|
---|
81 | * @return This constraint for chaining.
|
---|
82 | */
|
---|
83 | public GBC insets(int left, int top, int right, int bottom) {
|
---|
84 | insets = new Insets(top, left, bottom, right);
|
---|
85 | return this;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * This is a helper to easily create a glue with a minimum default value.
|
---|
90 | * @param x If higher than 0, this will be a horizontal glue with x as minimum
|
---|
91 | * horizontal strut.
|
---|
92 | * @param y If higher than 0, this will be a vertical glue with y as minimum
|
---|
93 | * vertical strut.
|
---|
94 | */
|
---|
95 | public static Component glue(int x, int y) {
|
---|
96 | short maxx = x > 0 ? Short.MAX_VALUE : 0;
|
---|
97 | short maxy = y > 0 ? Short.MAX_VALUE : 0;
|
---|
98 | return new Box.Filler(new Dimension(x,y), new Dimension(x,y), new Dimension(maxx,maxy));
|
---|
99 | }
|
---|
100 | }
|
---|