1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.gui.swing.util;
18
19 import java.awt.GridBagConstraints;
20 import java.awt.Insets;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26 * Utility to make {@link GridBagConstraints} code nicer.
27 *
28 * @svnLink $Revision: 4093 $;$Date: 2010-01-15 14:26:34 +0100 (Fr, 15. Jan 2010) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/main/java/ch/elca/el4j/services/gui/swing/util/GridConstraintsUtil.java $
29 *
30 * @author David Bernhard (DBD)
31 */
32 public class GridConstraintsUtil {
33
34 /** A logger for debugging purposes. */
35 private static final Logger s_log
36 = LoggerFactory.getLogger(GridConstraintsUtil.class);
37
38 /** The contained GridBagConstraints element. */
39 private GridBagConstraints m_g;
40
41 /**
42 * The contained persistent element. This is used to reset "once" changes.
43 */
44 private GridBagConstraints m_persistent;
45
46 /**
47 * Constructor. Fields to zero.
48 */
49 public GridConstraintsUtil() {
50 m_g = new GridBagConstraints();
51 m_persistent = new GridBagConstraints();
52 m_g.gridx = 0;
53 m_g.gridy = 0;
54 }
55
56 /**
57 * Constructor that allows an initial row to be specified.
58 * In particular, row -1 is allowed so that a loop can begin with newLine().
59 * @param initialRow The initial row.
60 */
61 public GridConstraintsUtil(int initialRow) {
62 this();
63 m_g.gridy = initialRow;
64 }
65
66 /**
67 * Clone the constraints, reset the internal one then return the clone.
68 * @return A GridBagConstraints with the current values.
69 */
70 private GridBagConstraints returnConstraints() {
71 GridBagConstraints current = m_g;
72 m_g = (GridBagConstraints) m_persistent.clone();
73 m_g.gridx = current.gridx;
74 m_g.gridy = current.gridy;
75
76 if (s_log.isDebugEnabled()) {
77 s_log.debug("x " + current.gridx + " y " + current.gridy + " w "
78 + current.gridwidth + " wx " + current.weightx + " wy "
79 + current.weighty + " f " + current.fill + " i t"
80 + current.insets.top + ",l" + current.insets.left + ",b"
81 + current.insets.bottom + ",r" + current.insets.right);
82 }
83
84 return current;
85 }
86
87 /**
88 * Set a new anchor for the next object. This is applied once then reset.
89 * @param anchor The anchor to set.
90 * @return <code>this</code> with the modification made.
91 */
92 public GridConstraintsUtil anchor(int anchor) {
93 m_g.anchor = anchor;
94 return this;
95 }
96
97
98 /**
99 * Set a new width for the next object. This is applied once then reset.
100 * @param width The width to set.
101 * @return <code>this</code> with the modification made.
102 */
103 public GridConstraintsUtil width(int width) {
104 m_g.gridwidth = width;
105 return this;
106 }
107
108 /**
109 * Set a new height for the next object. This is applied once then reset.
110 * @param height The height to set.
111 * @return <code>this</code> with the modification made.
112 */
113 public GridConstraintsUtil height(int height) {
114 m_g.gridheight = height;
115 return this;
116 }
117
118 /**
119 * Set the column weight for the next object.
120 * @param weight The weight to set.
121 * @return <code>this</code> with the modification.
122 */
123 public GridConstraintsUtil weight(double weight) {
124 m_g.weightx = weight;
125 return this;
126 }
127
128 /**
129 * Set the row weight for the next object.
130 * @param weightY The weight to set.
131 * @return <code>this</code> with the modification.
132 */
133 public GridConstraintsUtil weightY(double weightY) {
134 m_g.weighty = weightY;
135 return this;
136 }
137
138 /**
139 * @return A GridBagConstraints pointing to the current row/column.
140 */
141 public GridBagConstraints current() {
142 return returnConstraints();
143 }
144
145 /**
146 * @return A GridBagConstraints for the next cell.
147 */
148 public GridBagConstraints next() {
149 m_g.gridx++;
150 return returnConstraints();
151 }
152
153 /**
154 * @return A GridBagConstraints for the first cell on a new line.
155 */
156 public GridBagConstraints newLine() {
157 m_g.gridx = 0;
158 m_g.gridy++;
159 return returnConstraints();
160 }
161
162 /**
163 * Persistently set insets.
164 * @param i The insets.
165 */
166 public void setPersistentInsets(Insets i) {
167 m_persistent.insets = i;
168 m_g.insets = i;
169 }
170
171 /**
172 * Persistently set the fill parameter.
173 * @param fill The fill parameter to set.
174 */
175 public void setPeristentFill(int fill) {
176 m_persistent.fill = fill;
177 m_g.fill = fill;
178 }
179
180 /**
181 * Set insets for the next element.
182 * @param t Top.
183 * @param l Left.
184 * @param b Bottom.
185 * @param r Right.
186 * @return <code>this</code> with the modification.
187 */
188 public GridConstraintsUtil insets(int t, int l, int b, int r) {
189 m_g.insets = new Insets(t, l, b, r);
190 return this;
191 }
192 }