View Javadoc

1   /*
2    $Id: TableLayoutCell.java,v 1.2 2003/12/23 13:41:57 jstrachan Exp $
3   
4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5   
6    Redistribution and use of this software and associated documentation
7    ("Software"), with or without modification, are permitted provided
8    that the following conditions are met:
9   
10   1. Redistributions of source code must retain copyright
11      statements and notices.  Redistributions must also contain a
12      copy of this document.
13  
14   2. Redistributions in binary form must reproduce the
15      above copyright notice, this list of conditions and the
16      following disclaimer in the documentation and/or other
17      materials provided with the distribution.
18  
19   3. The name "groovy" must not be used to endorse or promote
20      products derived from this Software without prior written
21      permission of The Codehaus.  For written permission,
22      please contact info@codehaus.org.
23  
24   4. Products derived from this Software may not be called "groovy"
25      nor may "groovy" appear in their names without prior written
26      permission of The Codehaus. "groovy" is a registered
27      trademark of The Codehaus.
28  
29   5. Due credit should be given to The Codehaus -
30      http://groovy.codehaus.org/
31  
32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43   OF THE POSSIBILITY OF SUCH DAMAGE.
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     // 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 }