|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
XMLTableModel.java | 75% | 55,6% | 53,3% | 56,5% |
|
1 | /* | |
2 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
3 | * | |
4 | * This software is open source. | |
5 | * See the bottom of this file for the licence. | |
6 | * | |
7 | * $Id: XMLTableModel.java,v 1.6 2004/06/25 08:03:40 maartenc Exp $ | |
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 | 0 | public XMLTableModel(Element tableDefinition, Object source) { |
41 | 0 | this( XMLTableDefinition.load( tableDefinition ), source ); |
42 | } | |
43 | ||
44 | /** Creates a TableModel from an XML table definition document | |
45 | * and an XML source | |
46 | */ | |
47 | 2 | public XMLTableModel(Document tableDefinition, Object source) { |
48 | 2 | this( XMLTableDefinition.load( tableDefinition ), source ); |
49 | } | |
50 | ||
51 | 4 | public XMLTableModel(XMLTableDefinition definition, Object source) { |
52 | 4 | this.definition = definition; |
53 | 4 | this.source = source; |
54 | } | |
55 | ||
56 | 24 | public Object getRowValue(int rowIndex) { |
57 | 24 | return getRows().get(rowIndex); |
58 | } | |
59 | ||
60 | 28 | public List getRows() { |
61 | 28 | if ( rows == null ) { |
62 | 4 | rows = definition.getRowXPath().selectNodes( source ); |
63 | } | |
64 | 28 | return rows; |
65 | } | |
66 | ||
67 | ||
68 | // TableModel interface | |
69 | //------------------------------------------------------------------------- | |
70 | 0 | public Class getColumnClass(int columnIndex) { |
71 | 0 | return definition.getColumnClass(columnIndex); |
72 | } | |
73 | ||
74 | 4 | public int getColumnCount() { |
75 | 4 | return definition.getColumnCount(); |
76 | } | |
77 | ||
78 | 12 | public String getColumnName(int columnIndex) { |
79 | 12 | XPath xpath = definition.getColumnNameXPath(columnIndex); |
80 | 12 | if ( xpath != null ) { |
81 | 0 | System.out.println("Evaluating column xpath: " + xpath + " value: " + xpath.valueOf(source) ); |
82 | 0 | return xpath.valueOf( source ); |
83 | } | |
84 | 12 | return definition.getColumnName(columnIndex); |
85 | } | |
86 | ||
87 | 24 | public Object getValueAt(int rowIndex, int columnIndex) { |
88 | 24 | try { |
89 | 24 | Object row = getRowValue(rowIndex); |
90 | 24 | return definition.getValueAt(row, columnIndex); |
91 | } | |
92 | catch (Exception e) { | |
93 | 0 | handleException(e); |
94 | 0 | return null; |
95 | } | |
96 | } | |
97 | ||
98 | 4 | public int getRowCount() { |
99 | 4 | return getRows().size(); |
100 | } | |
101 | ||
102 | // Properties | |
103 | //------------------------------------------------------------------------- | |
104 | ||
105 | /** Getter for property definition. | |
106 | * @return Value of property definition. | |
107 | */ | |
108 | 0 | public XMLTableDefinition getDefinition() { |
109 | 0 | return definition; |
110 | } | |
111 | ||
112 | /** Setter for property definition. | |
113 | * @param definition New value of property definition. | |
114 | */ | |
115 | 0 | public void setDefinition(XMLTableDefinition definition) { |
116 | 0 | 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 | 0 | public Object getSource() { |
123 | 0 | 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 | 0 | public void setSource(Object source) { |
130 | 0 | this.source = source; |
131 | 0 | this.rows = null; |
132 | } | |
133 | ||
134 | ||
135 | // Implementation methods | |
136 | //------------------------------------------------------------------------- | |
137 | ||
138 | 0 | protected void handleException(Exception e) { |
139 | // #### should use jakarta commons-logging | |
140 | 0 | System.out.println( "Caught: " + e ); |
141 | } | |
142 | } | |
143 | ||
144 | ||
145 | ||
146 | ||
147 | /* | |
148 | * Redistribution and use of this software and associated documentation | |
149 | * ("Software"), with or without modification, are permitted provided | |
150 | * that the following conditions are met: | |
151 | * | |
152 | * 1. Redistributions of source code must retain copyright | |
153 | * statements and notices. Redistributions must also contain a | |
154 | * copy of this document. | |
155 | * | |
156 | * 2. Redistributions in binary form must reproduce the | |
157 | * above copyright notice, this list of conditions and the | |
158 | * following disclaimer in the documentation and/or other | |
159 | * materials provided with the distribution. | |
160 | * | |
161 | * 3. The name "DOM4J" must not be used to endorse or promote | |
162 | * products derived from this Software without prior written | |
163 | * permission of MetaStuff, Ltd. For written permission, | |
164 | * please contact dom4j-info@metastuff.com. | |
165 | * | |
166 | * 4. Products derived from this Software may not be called "DOM4J" | |
167 | * nor may "DOM4J" appear in their names without prior written | |
168 | * permission of MetaStuff, Ltd. DOM4J is a registered | |
169 | * trademark of MetaStuff, Ltd. | |
170 | * | |
171 | * 5. Due credit should be given to the DOM4J Project - | |
172 | * http://www.dom4j.org | |
173 | * | |
174 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS | |
175 | * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT | |
176 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
177 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
178 | * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
179 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
180 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
181 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
182 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
183 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
184 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
185 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
186 | * | |
187 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
188 | * | |
189 | * $Id: XMLTableModel.java,v 1.6 2004/06/25 08:03:40 maartenc Exp $ | |
190 | */ |
|