001 /*
002 $Id: TableLayoutCell.java,v 1.2 2003/12/23 13:41:57 jstrachan Exp $
003
004 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005
006 Redistribution and use of this software and associated documentation
007 ("Software"), with or without modification, are permitted provided
008 that the following conditions are met:
009
010 1. Redistributions of source code must retain copyright
011 statements and notices. Redistributions must also contain a
012 copy of this document.
013
014 2. Redistributions in binary form must reproduce the
015 above copyright notice, this list of conditions and the
016 following disclaimer in the documentation and/or other
017 materials provided with the distribution.
018
019 3. The name "groovy" must not be used to endorse or promote
020 products derived from this Software without prior written
021 permission of The Codehaus. For written permission,
022 please contact info@codehaus.org.
023
024 4. Products derived from this Software may not be called "groovy"
025 nor may "groovy" appear in their names without prior written
026 permission of The Codehaus. "groovy" is a registered
027 trademark of The Codehaus.
028
029 5. Due credit should be given to The Codehaus -
030 http://groovy.codehaus.org/
031
032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043 OF THE POSSIBILITY OF SUCH DAMAGE.
044
045 */
046 package groovy.swing.impl;
047
048 import java.awt.Component;
049 import java.awt.GridBagConstraints;
050 import java.util.logging.Level;
051 import java.util.logging.Logger;
052
053 /**
054 * Represents a cell in a table layout
055 *
056 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
057 * @version $Revision: 1.2 $
058 */
059 public class TableLayoutCell implements ContainerFacade {
060
061 protected static final Logger log = Logger.getLogger(TableLayoutCell.class.getName());
062
063 private TableLayoutRow parent;
064 private Component component;
065 private GridBagConstraints constraints;
066 private String align;
067 private String valign;
068 private int colspan = 1;
069 private int rowspan = 1;
070 private boolean colfill = false;
071 private boolean rowfill = false;
072
073
074 public TableLayoutCell(TableLayoutRow parent) {
075 this.parent = parent;
076 }
077
078 public void addComponent(Component component) {
079 if (this.component != null) {
080 log.log(Level.WARNING, "This td cell already has a component: " + component);
081 }
082 this.component = component;
083 parent.addCell(this);
084 }
085
086 public Component getComponent() {
087 return component;
088 }
089
090 /**
091 * Sets the horizontal alignment to a case insensitive value of {LEFT, CENTER, RIGHT}
092 */
093 public void setAlign(String align) {
094 this.align = align;
095 }
096
097 /**
098 * Sets the vertical alignment to a case insensitive value of {TOP, MIDDLE, BOTTOM}
099 */
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 // Implementation methods
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 // defaults to left
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 }