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 package groovy.swing.impl;
47
48 import java.awt.Component;
49 import java.awt.GridBagConstraints;
50 import java.util.logging.Level;
51 import java.util.logging.Logger;
52
53 /***
54 * Represents a cell in a table layout
55 *
56 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
57 * @version $Revision: 1.2 $
58 */
59 public class TableLayoutCell implements ContainerFacade {
60
61 protected static final Logger log = Logger.getLogger(TableLayoutCell.class.getName());
62
63 private TableLayoutRow parent;
64 private Component component;
65 private GridBagConstraints constraints;
66 private String align;
67 private String valign;
68 private int colspan = 1;
69 private int rowspan = 1;
70 private boolean colfill = false;
71 private boolean rowfill = false;
72
73
74 public TableLayoutCell(TableLayoutRow parent) {
75 this.parent = parent;
76 }
77
78 public void addComponent(Component component) {
79 if (this.component != null) {
80 log.log(Level.WARNING, "This td cell already has a component: " + component);
81 }
82 this.component = component;
83 parent.addCell(this);
84 }
85
86 public Component getComponent() {
87 return component;
88 }
89
90 /***
91 * Sets the horizontal alignment to a case insensitive value of {LEFT, CENTER, RIGHT}
92 */
93 public void setAlign(String align) {
94 this.align = align;
95 }
96
97 /***
98 * Sets the vertical alignment to a case insensitive value of {TOP, MIDDLE, BOTTOM}
99 */
100 public void setValign(String valign) {
101 this.valign = valign;
102 }
103
104
105 /***
106 * Sets the number of columns that this cell should span. The default value is 1
107 */
108 public void setColspan(int colspan) {
109 this.colspan = colspan;
110 }
111
112 /***
113 * Sets the number of rows that this cell should span. The default value is 1
114 */
115 public void setRowspan(int rowspan) {
116 this.rowspan = rowspan;
117 }
118
119 /***
120 * Returns the colfill.
121 * @return boolean
122 */
123 public boolean isColfill() {
124 return colfill;
125 }
126
127 /***
128 * Returns the rowfill.
129 * @return boolean
130 */
131 public boolean isRowfill() {
132 return rowfill;
133 }
134
135 /***
136 * Sets whether or not this column should allow its component to stretch to fill the space available
137 */
138 public void setColfill(boolean colfill) {
139 this.colfill = colfill;
140 }
141
142 /***
143 * Sets whether or not this row should allow its component to stretch to fill the space available
144 */
145 public void setRowfill(boolean rowfill) {
146 this.rowfill = rowfill;
147 }
148
149
150 /***
151 * @return the constraints of this cell
152 */
153 public GridBagConstraints getConstraints() {
154 if (constraints == null) {
155 constraints = createConstraints();
156 }
157 return constraints;
158 }
159
160
161
162
163 protected GridBagConstraints createConstraints() {
164 GridBagConstraints answer = new GridBagConstraints();
165 answer.anchor = getAnchor();
166 if (colspan < 1) {
167 colspan = 1;
168 }
169 if (rowspan < 1) {
170 rowspan = 1;
171 }
172 if (isColfill()) {
173 answer.fill = isRowfill()
174 ? GridBagConstraints.BOTH
175 : GridBagConstraints.HORIZONTAL;
176 }
177 else {
178 answer.fill = isRowfill()
179 ? GridBagConstraints.VERTICAL
180 : GridBagConstraints.NONE;
181 }
182 answer.weightx = 0.2;
183 answer.weighty = 0;
184 answer.gridwidth = colspan;
185 answer.gridheight = rowspan;
186 return answer;
187 }
188
189 /***
190 * @return the GridBagConstraints enumeration for achor
191 */
192 protected int getAnchor() {
193 boolean isTop = "top".equalsIgnoreCase(valign);
194 boolean isBottom = "bottom".equalsIgnoreCase(valign);
195
196 if ("center".equalsIgnoreCase(align)) {
197 if (isTop) {
198 return GridBagConstraints.NORTH;
199 }
200 else if (isBottom) {
201 return GridBagConstraints.SOUTH;
202 }
203 else {
204 return GridBagConstraints.CENTER;
205 }
206 }
207 else if ("right".equalsIgnoreCase(align)) {
208 if (isTop) {
209 return GridBagConstraints.NORTHEAST;
210 }
211 else if (isBottom) {
212 return GridBagConstraints.SOUTHEAST;
213 }
214 else {
215 return GridBagConstraints.EAST;
216 }
217 }
218 else {
219
220 if (isTop) {
221 return GridBagConstraints.NORTHWEST;
222 }
223 else if (isBottom) {
224 return GridBagConstraints.SOUTHWEST;
225 }
226 else {
227 return GridBagConstraints.WEST;
228 }
229 }
230 }
231 }