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  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.commons.jelly.JellyTagException;
25  import org.apache.commons.jelly.TagSupport;
26  import org.apache.commons.jelly.XMLOutput;
27  import org.apache.commons.jelly.tags.swing.impl.Cell;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /***
32   * Represents a tabular row inside a <tableLayout> tag which mimicks the
33   * <tr> HTML tag.
34   *
35   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
36   * @version $Revision: 155420 $
37   */
38  public class TrTag extends TagSupport {
39  
40      /*** The Log to which logging calls will be made. */
41      private static final Log log = LogFactory.getLog(TrTag.class);
42  
43      private TableLayoutTag tableLayoutTag;
44      private List cells = new ArrayList();
45      private int rowIndex;
46  
47      public TrTag() {
48      }
49  
50      /***
51       * Adds a new cell to this row
52       */
53      public void addCell(Component component, GridBagConstraints constraints) throws JellyTagException {
54          constraints.gridx = cells.size();
55          cells.add(new Cell(constraints, component));
56      }
57  
58  
59      // Tag interface
60      //-------------------------------------------------------------------------
61      public void doTag(final XMLOutput output) throws JellyTagException {
62          tableLayoutTag = (TableLayoutTag) findAncestorWithClass( TableLayoutTag.class );
63          if (tableLayoutTag == null) {
64              throw new JellyTagException( "this tag must be nested within a <tableLayout> tag" );
65          }
66          rowIndex = tableLayoutTag.nextRowIndex();
67          cells.clear();
68  
69          invokeBody(output);
70  
71          // now iterate through the rows and add each one to the layout...
72          int colIndex = 0;
73          for (Iterator iter = cells.iterator(); iter.hasNext(); ) {
74              Cell cell = (Cell) iter.next();
75              GridBagConstraints c = cell.getConstraints();
76  
77              // are we the last cell in the row
78              if ( iter.hasNext() ) {
79                  // not last in row
80                  c.gridwidth = 1;
81                  c.gridx = colIndex++;
82              }
83              else {
84                  // end of row
85                  c.gridwidth = GridBagConstraints.REMAINDER;
86              }
87              c.gridy = rowIndex;
88  
89              // now lets add the cell to the table
90              tableLayoutTag.addCell(cell);
91          }
92          cells.clear();
93      }
94  
95      // Properties
96      //-------------------------------------------------------------------------
97  
98      /***
99       * @return the row index of this row
100      */
101     public int getRowIndex() {
102         return rowIndex;
103     }
104 
105 }