001: /*
002: $Id: TableLayoutCell.java 455 2003-12-23 13:43:06Z jstrachan $
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: 455 $
058: */
059: public class TableLayoutCell implements ContainerFacade {
060:
061: protected static final Logger log = Logger
062: .getLogger(TableLayoutCell.class.getName());
063:
064: private TableLayoutRow parent;
065: private Component component;
066: private GridBagConstraints constraints;
067: private String align;
068: private String valign;
069: private int colspan = 1;
070: private int rowspan = 1;
071: private boolean colfill = false;
072: private boolean rowfill = false;
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,
081: "This td cell already has a component: "
082: + component);
083: }
084: this .component = component;
085: parent.addCell(this );
086: }
087:
088: public Component getComponent() {
089: return component;
090: }
091:
092: /**
093: * Sets the horizontal alignment to a case insensitive value of {LEFT, CENTER, RIGHT}
094: */
095: public void setAlign(String align) {
096: this .align = align;
097: }
098:
099: /**
100: * Sets the vertical alignment to a case insensitive value of {TOP, MIDDLE, BOTTOM}
101: */
102: public void setValign(String valign) {
103: this .valign = valign;
104: }
105:
106: /**
107: * Sets the number of columns that this cell should span. The default value is 1
108: */
109: public void setColspan(int colspan) {
110: this .colspan = colspan;
111: }
112:
113: /**
114: * Sets the number of rows that this cell should span. The default value is 1
115: */
116: public void setRowspan(int rowspan) {
117: this .rowspan = rowspan;
118: }
119:
120: /**
121: * Returns the colfill.
122: * @return boolean
123: */
124: public boolean isColfill() {
125: return colfill;
126: }
127:
128: /**
129: * Returns the rowfill.
130: * @return boolean
131: */
132: public boolean isRowfill() {
133: return rowfill;
134: }
135:
136: /**
137: * Sets whether or not this column should allow its component to stretch to fill the space available
138: */
139: public void setColfill(boolean colfill) {
140: this .colfill = colfill;
141: }
142:
143: /**
144: * Sets whether or not this row should allow its component to stretch to fill the space available
145: */
146: public void setRowfill(boolean rowfill) {
147: this .rowfill = rowfill;
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() ? GridBagConstraints.BOTH
174: : GridBagConstraints.HORIZONTAL;
175: } else {
176: answer.fill = isRowfill() ? GridBagConstraints.VERTICAL
177: : GridBagConstraints.NONE;
178: }
179: answer.weightx = 0.2;
180: answer.weighty = 0;
181: answer.gridwidth = colspan;
182: answer.gridheight = rowspan;
183: return answer;
184: }
185:
186: /**
187: * @return the GridBagConstraints enumeration for achor
188: */
189: protected int getAnchor() {
190: boolean isTop = "top".equalsIgnoreCase(valign);
191: boolean isBottom = "bottom".equalsIgnoreCase(valign);
192:
193: if ("center".equalsIgnoreCase(align)) {
194: if (isTop) {
195: return GridBagConstraints.NORTH;
196: } else if (isBottom) {
197: return GridBagConstraints.SOUTH;
198: } else {
199: return GridBagConstraints.CENTER;
200: }
201: } else if ("right".equalsIgnoreCase(align)) {
202: if (isTop) {
203: return GridBagConstraints.NORTHEAST;
204: } else if (isBottom) {
205: return GridBagConstraints.SOUTHEAST;
206: } else {
207: return GridBagConstraints.EAST;
208: }
209: } else {
210: // defaults to left
211: if (isTop) {
212: return GridBagConstraints.NORTHWEST;
213: } else if (isBottom) {
214: return GridBagConstraints.SOUTHWEST;
215: } else {
216: return GridBagConstraints.WEST;
217: }
218: }
219: }
220: }
|