File: GBC.java

package info (click to toggle)
mobile-atlas-creator 2.1.0-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 4,084 kB
  • sloc: java: 36,206; xml: 404; sh: 20; makefile: 19
file content (183 lines) | stat: -rw-r--r-- 4,646 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
 * Copyright (c) MOBAC developers
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
// License: GPL. Copyright 2007 by Immanuel Scholz and others
package mobac.utilities;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.Box;

/**
 * A wrapper for GridBagConstraints which has sane default static creators and
 * member functions to chain calling.
 * 
 * @author imi
 */
public class GBC extends GridBagConstraints {

	private static final long serialVersionUID = 1L;

	/**
	 * Use public static creator functions to create an GBC.
	 */
	private GBC() {
	}

	/**
	 * Create a standard constraint (which is not the last).
	 * 
	 * @return A standard constraint with no filling.
	 */
	public static GBC std() {
		GBC c = new GBC();
		c.anchor = WEST;
		return c;
	}

	/**
	 * Create the constraint for the last elements on a line.
	 * 
	 * @return A constraint which indicates the last item on a line.
	 */
	public static GBC eol() {
		GBC c = std();
		c.gridwidth = REMAINDER;
		return c;
	}

	/**
	 * Create the constraint for the last elements on a line and on a paragraph.
	 * This is merely a shortcut for eol().insets(0,0,0,10)
	 * 
	 * @return A constraint which indicates the last item on a line.
	 */
	public static GBC eop() {
		return eol().insets(0, 0, 0, 10);
	}

	/**
	 * Try to fill both, horizontal and vertical
	 * 
	 * @return This constraint for chaining.
	 */
	public GBC fill() {
		return fill(BOTH);
	}

	public GBC toggleEol() {
		if (gridwidth == GBC.REMAINDER)
			gridwidth = 1;
		else
			gridwidth = GBC.REMAINDER;
		return this;
	}

	public GBC gridwidth(int value) {
		gridwidth = value;
		return this;
	}

	public GBC gridheight(int value) {
		gridheight = value;
		return this;
	}

	public GBC gridx(int value) {
		gridx = value;
		return this;
	}

	public GBC gridxy(int x, int y) {
		gridx = x;
		gridy = y;
		return this;
	}

	public GBC gridy(int value) {
		gridy = value;
		return this;
	}

	/**
	 * Set fill to the given value
	 * 
	 * @param value
	 *            The filling value, either NONE, HORIZONTAL, VERTICAL or BOTH
	 * @return This constraint for chaining.
	 */
	public GBC fill(int value) {
		fill = value;
		if (value == HORIZONTAL || value == BOTH)
			weightx = 1.0;
		if (value == VERTICAL || value == BOTH)
			weighty = 1.0;
		return this;
	}

	public GBC fillH() {
		return fill(GBC.HORIZONTAL);
	}

	/**
	 * Set the anchor of this GBC to a.
	 * 
	 * @param a
	 *            The new anchor, e.g. GBC.CENTER or GBC.EAST.
	 * @return This constraint for chaining.
	 */
	public GBC anchor(int a) {
		anchor = a;
		return this;
	}

	/**
	 * Adds insets to this GBC.
	 * 
	 * @param left
	 *            The left space of the insets
	 * @param top
	 *            The top space of the insets
	 * @param right
	 *            The right space of the insets
	 * @param bottom
	 *            The bottom space of the insets
	 * @return This constraint for chaining.
	 */
	public GBC insets(int left, int top, int right, int bottom) {
		insets = new Insets(top, left, bottom, right);
		return this;
	}

	/**
	 * This is a helper to easily create a glue with a minimum default value.
	 * 
	 * @param x
	 *            If higher than 0, this will be a horizontal glue with x as
	 *            minimum horizontal strut.
	 * @param y
	 *            If higher than 0, this will be a vertical glue with y as
	 *            minimum vertical strut.
	 */
	public static Component glue(int x, int y) {
		short maxx = x > 0 ? Short.MAX_VALUE : 0;
		short maxy = y > 0 ? Short.MAX_VALUE : 0;
		return new Box.Filler(new Dimension(x, y), new Dimension(x, y), new Dimension(maxx, maxy));
	}
}