1 7 8 package org.dom4j.swing; 9 10 import java.util.List ; 11 12 import javax.swing.table.AbstractTableModel ; 13 14 import org.dom4j.Document; 15 import org.dom4j.Element; 16 import org.dom4j.XPath; 17 18 26 public class XMLTableModel extends AbstractTableModel { 27 28 private XMLTableDefinition definition; 29 30 31 private Object source; 32 33 34 private List rows; 35 36 45 public XMLTableModel(Element tableDefinition, Object source) { 46 this(XMLTableDefinition.load(tableDefinition), source); 47 } 48 49 58 public XMLTableModel(Document tableDefinition, Object source) { 59 this(XMLTableDefinition.load(tableDefinition), source); 60 } 61 62 public XMLTableModel(XMLTableDefinition definition, Object source) { 63 this.definition = definition; 64 this.source = source; 65 } 66 67 public Object getRowValue(int rowIndex) { 68 return getRows().get(rowIndex); 69 } 70 71 public List getRows() { 72 if (rows == null) { 73 rows = definition.getRowXPath().selectNodes(source); 74 } 75 76 return rows; 77 } 78 79 public Class getColumnClass(int columnIndex) { 82 return definition.getColumnClass(columnIndex); 83 } 84 85 public int getColumnCount() { 86 return definition.getColumnCount(); 87 } 88 89 public String getColumnName(int columnIndex) { 90 XPath xpath = definition.getColumnNameXPath(columnIndex); 91 92 if (xpath != null) { 93 System.out.println("Evaluating column xpath: " + xpath + " value: " 94 + xpath.valueOf(source)); 95 96 return xpath.valueOf(source); 97 } 98 99 return definition.getColumnName(columnIndex); 100 } 101 102 public Object getValueAt(int rowIndex, int columnIndex) { 103 try { 104 Object row = getRowValue(rowIndex); 105 106 return definition.getValueAt(row, columnIndex); 107 } catch (Exception e) { 108 handleException(e); 109 110 return null; 111 } 112 } 113 114 public int getRowCount() { 115 return getRows().size(); 116 } 117 118 121 126 public XMLTableDefinition getDefinition() { 127 return definition; 128 } 129 130 136 public void setDefinition(XMLTableDefinition definition) { 137 this.definition = definition; 138 } 139 140 145 public Object getSource() { 146 return source; 147 } 148 149 155 public void setSource(Object source) { 156 this.source = source; 157 this.rows = null; 158 } 159 160 protected void handleException(Exception e) { 163 System.out.println("Caught: " + e); 165 } 166 } 167 168 204 | Popular Tags |