01: package com.xoetrope.export.html;
02:
03: import com.xoetrope.export.TableModelBlock;
04: import com.xoetrope.export.TagExporter;
05: import javax.swing.JLabel;
06: import javax.swing.JTable;
07: import javax.swing.table.TableCellRenderer;
08: import javax.swing.table.TableModel;
09:
10: /**
11: *
12: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
13: * the GNU Public License (GPL), please see license.txt for more details. If
14: * you make commercial use of this software you must purchase a commercial
15: * license from Xoetrope.</p>
16: * <p> $Revision: 1.2 $</p>
17: */
18: public class HTMLTableModelBlock extends TableModelBlock implements
19: TagExporter {
20: public HTMLTableModelBlock() {
21: }
22:
23: protected String exportModel(JTable table, TableModel tableModel,
24: boolean includeHeader) {
25: TableModel oldModel = null;
26: if (tableModel == null)
27: tableModel = table.getModel();
28: else {
29: oldModel = table.getModel();
30: table.setModel(tableModel);
31: }
32:
33: String widthStr = getWidth();
34: content.append("<TABLE");
35: if ((widthStr != null) && (widthStr.length() > 0))
36: content.append(" width=\"" + widthStr + "\"");
37: content.append(">\n");
38:
39: if (includeHeader) {
40: content.append("<TR>\n");
41: for (int col = 0; col < tableModel.getColumnCount(); col++) {
42: content.append("<TD class='" + getHeaderStyle()
43: + "'>\n");
44: TableCellRenderer renderer = table.getColumnModel()
45: .getColumn(col).getHeaderRenderer();
46: String value = table.getTableHeader().getColumnModel()
47: .getColumn(col).getHeaderValue().toString();
48:
49: content.append(value);
50: content.append("</TD>\n");
51: }
52: }
53:
54: for (int row = 0; row < tableModel.getRowCount(); row++) {
55: content.append("</TR>\n");
56: content.append("<TR>\n");
57: for (int col = 0; col < tableModel.getColumnCount(); col++) {
58: content
59: .append("<TD class='" + getTableStyle()
60: + "'>\n");
61: String value = (String) tableModel.getValueAt(row, col);
62: Object comp = table.getCellRenderer(row, col)
63: .getTableCellRendererComponent(table, value,
64: false, false, row, col);
65: if (comp instanceof JLabel) {
66: value = ((JLabel) comp).getText();
67: }
68: if ((value != null) && (value.compareTo("null") != 0))
69: content.append(value);
70: content.append("</TD>\n");
71: }
72: content.append("</TR>\n");
73: }
74:
75: content.append("</TABLE>\n");
76: if (oldModel != null)
77: table.setModel(oldModel);
78:
79: return content.toString();
80: }
81:
82: public String getStartTag() {
83: return content.toString();
84: }
85:
86: public String getEndTag() {
87: return "";
88: }
89:
90: }
|