1   
2   
3   
4   
5   
6   
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  /***
19   * <p>
20   * <code>XMLTableDefinition</code> repro.
21   * </p>
22   * 
23   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
24   * @version $Revision: 1.8 $
25   */
26  public class XMLTableModel extends AbstractTableModel {
27      /*** Holds value of property definition. */
28      private XMLTableDefinition definition;
29  
30      /*** Holds value of property source. */
31      private Object source;
32  
33      /*** The rows evaluated from the row XPath expression */
34      private List rows;
35  
36      /***
37       * Creates a TableModel from an XML table definition document and an XML
38       * source
39       * 
40       * @param tableDefinition
41       *            DOCUMENT ME!
42       * @param source
43       *            DOCUMENT ME!
44       */
45      public XMLTableModel(Element tableDefinition, Object source) {
46          this(XMLTableDefinition.load(tableDefinition), source);
47      }
48  
49      /***
50       * Creates a TableModel from an XML table definition document and an XML
51       * source
52       * 
53       * @param tableDefinition
54       *            DOCUMENT ME!
55       * @param source
56       *            DOCUMENT ME!
57       */
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      
80      
81      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     
119     
120 
121     /***
122      * Getter for property definition.
123      * 
124      * @return Value of property definition.
125      */
126     public XMLTableDefinition getDefinition() {
127         return definition;
128     }
129 
130     /***
131      * Setter for property definition.
132      * 
133      * @param definition
134      *            New value of property definition.
135      */
136     public void setDefinition(XMLTableDefinition definition) {
137         this.definition = definition;
138     }
139 
140     /***
141      * Getter for the XML source, which is usually a Node or List of nodes.
142      * 
143      * @return Value of property source.
144      */
145     public Object getSource() {
146         return source;
147     }
148 
149     /***
150      * Setter for the XML source, which is usually a Node or List of nodes.
151      * 
152      * @param source
153      *            New value of property source.
154      */
155     public void setSource(Object source) {
156         this.source = source;
157         this.rows = null;
158     }
159 
160     
161     
162     protected void handleException(Exception e) {
163         
164         System.out.println("Caught: " + e);
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 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203