1
2
3
4
5
6
7
8
9
10 package org.dom4j.swing;
11
12 import java.util.List;
13
14 import javax.swing.table.AbstractTableModel;
15
16 import org.dom4j.Document;
17 import org.dom4j.Element;
18 import org.dom4j.XPath;
19
20 /*** <p><code>XMLTableDefinition</code> repro.</p>
21 *
22 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
23 * @version $Revision: 1.6 $
24 */
25 public class XMLTableModel extends AbstractTableModel {
26
27
28 /*** Holds value of property definition. */
29 private XMLTableDefinition definition;
30
31 /*** Holds value of property source. */
32 private Object source;
33
34 /*** The rows evaluated from the row XPath expression */
35 private List rows;
36
37 /*** Creates a TableModel from an XML table definition document
38 * and an XML source
39 */
40 public XMLTableModel(Element tableDefinition, Object source) {
41 this( XMLTableDefinition.load( tableDefinition ), source );
42 }
43
44 /*** Creates a TableModel from an XML table definition document
45 * and an XML source
46 */
47 public XMLTableModel(Document tableDefinition, Object source) {
48 this( XMLTableDefinition.load( tableDefinition ), source );
49 }
50
51 public XMLTableModel(XMLTableDefinition definition, Object source) {
52 this.definition = definition;
53 this.source = source;
54 }
55
56 public Object getRowValue(int rowIndex) {
57 return getRows().get(rowIndex);
58 }
59
60 public List getRows() {
61 if ( rows == null ) {
62 rows = definition.getRowXPath().selectNodes( source );
63 }
64 return rows;
65 }
66
67
68
69
70 public Class getColumnClass(int columnIndex) {
71 return definition.getColumnClass(columnIndex);
72 }
73
74 public int getColumnCount() {
75 return definition.getColumnCount();
76 }
77
78 public String getColumnName(int columnIndex) {
79 XPath xpath = definition.getColumnNameXPath(columnIndex);
80 if ( xpath != null ) {
81 System.out.println("Evaluating column xpath: " + xpath + " value: " + xpath.valueOf(source) );
82 return xpath.valueOf( source );
83 }
84 return definition.getColumnName(columnIndex);
85 }
86
87 public Object getValueAt(int rowIndex, int columnIndex) {
88 try {
89 Object row = getRowValue(rowIndex);
90 return definition.getValueAt(row, columnIndex);
91 }
92 catch (Exception e) {
93 handleException(e);
94 return null;
95 }
96 }
97
98 public int getRowCount() {
99 return getRows().size();
100 }
101
102
103
104
105 /*** Getter for property definition.
106 * @return Value of property definition.
107 */
108 public XMLTableDefinition getDefinition() {
109 return definition;
110 }
111
112 /*** Setter for property definition.
113 * @param definition New value of property definition.
114 */
115 public void setDefinition(XMLTableDefinition definition) {
116 this.definition = definition;
117 }
118
119 /*** Getter for the XML source, which is usually a Node or List of nodes.
120 * @return Value of property source.
121 */
122 public Object getSource() {
123 return source;
124 }
125
126 /*** Setter for the XML source, which is usually a Node or List of nodes.
127 * @param source New value of property source.
128 */
129 public void setSource(Object source) {
130 this.source = source;
131 this.rows = null;
132 }
133
134
135
136
137
138 protected void handleException(Exception e) {
139
140 System.out.println( "Caught: " + e );
141 }
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190