View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.swing;
17  
18  import java.awt.Component;
19  import java.awt.GridBagConstraints;
20  
21  import org.apache.commons.jelly.JellyTagException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /***
28   * Represents a tabular cell inside a <tl> tag inside a <tableLayout>
29   * tag which mimicks the <td> HTML tag.
30   *
31   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32   * @version $Revision: 155420 $
33   */
34  public class TdTag extends TagSupport implements ContainerTag {
35  
36      /*** The Log to which logging calls will be made. */
37      private static final Log log = LogFactory.getLog(TdTag.class);
38  
39      private String align;
40      private String valign;
41      private int colspan = 1;
42      private int rowspan = 1;
43      private boolean colfill = false;
44      private boolean rowfill = false;
45  
46      public TdTag() {
47      }
48  
49      // ContainerTag interface
50      //-------------------------------------------------------------------------
51  
52      /***
53       * Adds a child component to this parent
54       */
55      public void addChild(Component component, Object constraints) throws JellyTagException {
56          // add my child component to the layout manager
57          TrTag tag = (TrTag) findAncestorWithClass( TrTag.class );
58          if (tag == null) {
59              throw new JellyTagException( "this tag must be nested within a <tr> tag" );
60          }
61          tag.addCell(component, createConstraints());
62      }
63  
64  
65      // Tag interface
66      //-------------------------------------------------------------------------
67      public void doTag(final XMLOutput output) throws JellyTagException {
68          invokeBody(output);
69      }
70  
71  
72      // Properties
73      //-------------------------------------------------------------------------
74  
75      /***
76       * Sets the horizontal alignment to a case insensitive value of {LEFT, CENTER, RIGHT}
77       */
78      public void setAlign(String align) {
79          this.align = align;
80      }
81  
82      /***
83       * Sets the vertical alignment to a case insensitive value of {TOP, MIDDLE, BOTTOM}
84       */
85      public void setValign(String valign) {
86          this.valign = valign;
87      }
88  
89  
90      /***
91       * Sets the number of columns that this cell should span. The default value is 1
92       */
93      public void setColspan(int colspan) {
94          this.colspan = colspan;
95      }
96  
97      /***
98       * Sets the number of rows that this cell should span. The default value is 1
99       */
100     public void setRowspan(int rowspan) {
101         this.rowspan = rowspan;
102     }
103 
104     /***
105      * Returns the colfill.
106      * @return boolean
107      */
108     public boolean isColfill() {
109         return colfill;
110     }
111 
112     /***
113      * Returns the rowfill.
114      * @return boolean
115      */
116     public boolean isRowfill() {
117         return rowfill;
118     }
119 
120     /***
121      * Sets whether or not this column should allow its component to stretch to fill the space available
122      */
123     public void setColfill(boolean colfill) {
124         this.colfill = colfill;
125     }
126 
127     /***
128      * Sets whether or not this row should allow its component to stretch to fill the space available
129      */
130     public void setRowfill(boolean rowfill) {
131         this.rowfill = rowfill;
132     }
133 
134 
135     // Implementation methods
136     //-------------------------------------------------------------------------
137 
138     /***
139      * Factory method to create a new constraints object
140      */
141     protected GridBagConstraints createConstraints() {
142         GridBagConstraints answer = new GridBagConstraints();
143         answer.anchor = getAnchor();
144         if (colspan < 1) {
145             colspan = 1;
146         }
147         if (rowspan < 1) {
148             rowspan = 1;
149         }
150         if (isColfill())  {
151             answer.fill = isRowfill()
152                 ? GridBagConstraints.BOTH
153                 : GridBagConstraints.HORIZONTAL;
154         }
155         else {
156             answer.fill = isRowfill()
157                 ? GridBagConstraints.VERTICAL
158                 : GridBagConstraints.NONE;
159         }
160         answer.weightx = 0.2;
161         answer.weighty = 0;
162         answer.gridwidth = colspan;
163         answer.gridheight = rowspan;
164         return answer;
165     }
166 
167     /***
168      * @return the GridBagConstraints enumeration for achor
169      */
170     protected int getAnchor() {
171         boolean isTop = "top".equalsIgnoreCase(valign);
172         boolean isBottom = "bottom".equalsIgnoreCase(valign);
173 
174         if ("center".equalsIgnoreCase(align)) {
175             if (isTop) {
176                 return GridBagConstraints.NORTH;
177             }
178             else if (isBottom) {
179                 return GridBagConstraints.SOUTH;
180             }
181             else {
182                 return GridBagConstraints.CENTER;
183             }
184         }
185         else if ("right".equalsIgnoreCase(align)) {
186             if (isTop) {
187                 return GridBagConstraints.NORTHEAST;
188             }
189             else if (isBottom) {
190                 return GridBagConstraints.SOUTHEAST;
191             }
192             else {
193                 return GridBagConstraints.EAST;
194             }
195         }
196         else {
197             // defaults to left
198             if (isTop) {
199                 return GridBagConstraints.NORTHWEST;
200             }
201             else if (isBottom) {
202                 return GridBagConstraints.SOUTHWEST;
203             }
204             else {
205                 return GridBagConstraints.WEST;
206             }
207         }
208     }
209 }