01: package com.xoetrope.export.xl;
02:
03: import com.xoetrope.export.Block;
04: import com.xoetrope.export.RowBlock;
05: import com.xoetrope.export.TableBlock;
06: import com.xoetrope.export.TableModelBlock;
07: import javax.swing.JLabel;
08: import javax.swing.JTable;
09: import javax.swing.table.TableModel;
10: import jxl.write.WritableSheet;
11: import net.xoetrope.xui.helper.XTranslator;
12:
13: /**
14: *
15: * <p> Copyright (c) Xoetrope Ltd., 2002-2006, This software is licensed under
16: * the GNU Public License (GPL), please see license.txt for more details</p>
17: * <p>$Revision: 1.5 $</p>
18: */
19: public class XLTableModelBlock extends TableModelBlock {
20: protected XLExporter xlExporter;
21: protected WritableSheet sheet;
22: protected XTranslator translator;
23:
24: public XLTableModelBlock(XLExporter exporter) {
25: xlExporter = exporter;
26: translator = exporter.getCurrentProject().getTranslator();
27: }
28:
29: public void setWorksheet(WritableSheet worksheet) {
30: sheet = worksheet;
31: }
32:
33: protected String exportModel(JTable table, TableModel tableModel,
34: boolean includeHeader) {
35: TableModel oldModel = null;
36: if (tableModel == null)
37: tableModel = table.getModel();
38: else {
39: oldModel = table.getModel();
40: table.setModel(tableModel);
41: }
42: TableBlock tableExport = xlExporter.getTableBlock();
43: if (includeHeader) {
44: RowBlock rowBlock = tableExport.addRow();
45: for (int col = 0; col < tableModel.getColumnCount(); col++) {
46: //String value = tableModel.getColumnName( col );
47: String value = table.getTableHeader().getColumnModel()
48: .getColumn(col).getHeaderValue().toString();
49: if (translator != null)
50: value = translator.translate(value);
51: String style = getHeaderStyle();
52: Block cellBlockItem = rowBlock.addCell(value, style, 1,
53: 1);
54: }
55: blocks.add(rowBlock);
56: }
57: for (int row = 0; row < tableModel.getRowCount(); row++) {
58: RowBlock rowBlock = tableExport.addRow();
59: for (int col = 0; col < tableModel.getColumnCount(); col++) {
60: String value = (String) tableModel.getValueAt(row, col);
61: if (translator != null)
62: value = translator.translate(value);
63: Object comp = table.getCellRenderer(row, col)
64: .getTableCellRendererComponent(table, value,
65: false, false, row, col);
66: if (comp instanceof JLabel) {
67: value = ((JLabel) comp).getText();
68: }
69: String style = getHeaderStyle();
70: Block cellBlockItem = rowBlock.addCell(value, style, 1,
71: 1);
72: }
73: blocks.add(rowBlock);
74: }
75:
76: if (oldModel != null)
77: table.setModel(oldModel);
78:
79: return content.toString();
80: }
81: }
|