1 28 29 33 package net.sf.jasperreports.engine.data; 34 35 import java.util.HashMap ; 36 37 import javax.swing.table.TableModel ; 38 39 import net.sf.jasperreports.engine.JRException; 40 import net.sf.jasperreports.engine.JRField; 41 import net.sf.jasperreports.engine.JRRewindableDataSource; 42 43 44 48 public class JRTableModelDataSource implements JRRewindableDataSource 49 { 50 51 52 55 private TableModel tableModel = null; 56 private int index = -1; 57 private HashMap columnNames = new HashMap (); 58 59 60 63 public JRTableModelDataSource(TableModel model) 64 { 65 this.tableModel = model; 66 67 if (this.tableModel != null) 68 { 69 for(int i = 0; i < tableModel.getColumnCount(); i++) 70 { 71 this.columnNames.put(tableModel.getColumnName(i), new Integer (i)); 72 } 73 } 74 } 75 76 77 80 public boolean next() 81 { 82 this.index++; 83 84 if (this.tableModel != null) 85 { 86 return (this.index < this.tableModel.getRowCount()); 87 } 88 89 return false; 90 } 91 92 93 96 public Object getFieldValue(JRField jrField) throws JRException 97 { 98 String fieldName = jrField.getName(); 99 100 Integer columnIndex = (Integer )this.columnNames.get(fieldName); 101 102 if (columnIndex != null) 103 { 104 return this.tableModel.getValueAt(index, columnIndex.intValue()); 105 } 106 else if (fieldName.startsWith("COLUMN_")) 107 { 108 return this.tableModel.getValueAt(index, Integer.parseInt(fieldName.substring(7))); 109 } 110 else 111 { 112 throw new JRException("Unknown column name : " + fieldName); 113 } 114 } 115 116 117 120 public void moveFirst() 121 { 122 this.index = -1; 123 } 124 125 126 } 127 | Popular Tags |