1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
78 if ( iter.hasNext() ) {
79
80 c.gridwidth = 1;
81 c.gridx = colIndex++;
82 }
83 else {
84
85 c.gridwidth = GridBagConstraints.REMAINDER;
86 }
87 c.gridy = rowIndex;
88
89
90 tableLayoutTag.addCell(cell);
91 }
92 cells.clear();
93 }
94
95
96
97
98 /***
99 * @return the row index of this row
100 */
101 public int getRowIndex() {
102 return rowIndex;
103 }
104
105 }