KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > swing > XMLTableModel


1 /*
2  * Copyright 2001-2005 (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
8 package org.dom4j.swing;
9
10 import java.util.List JavaDoc;
11
12 import javax.swing.table.AbstractTableModel JavaDoc;
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 JavaDoc {
27     /** Holds value of property definition. */
28     private XMLTableDefinition definition;
29
30     /** Holds value of property source. */
31     private Object JavaDoc source;
32
33     /** The rows evaluated from the row XPath expression */
34     private List JavaDoc 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 JavaDoc 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 JavaDoc source) {
59         this(XMLTableDefinition.load(tableDefinition), source);
60     }
61
62     public XMLTableModel(XMLTableDefinition definition, Object JavaDoc source) {
63         this.definition = definition;
64         this.source = source;
65     }
66
67     public Object JavaDoc getRowValue(int rowIndex) {
68         return getRows().get(rowIndex);
69     }
70
71     public List JavaDoc getRows() {
72         if (rows == null) {
73             rows = definition.getRowXPath().selectNodes(source);
74         }
75
76         return rows;
77     }
78
79     // TableModel interface
80
// -------------------------------------------------------------------------
81
public Class JavaDoc getColumnClass(int columnIndex) {
82         return definition.getColumnClass(columnIndex);
83     }
84
85     public int getColumnCount() {
86         return definition.getColumnCount();
87     }
88
89     public String JavaDoc 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 JavaDoc getValueAt(int rowIndex, int columnIndex) {
103         try {
104             Object JavaDoc row = getRowValue(rowIndex);
105
106             return definition.getValueAt(row, columnIndex);
107         } catch (Exception JavaDoc e) {
108             handleException(e);
109
110             return null;
111         }
112     }
113
114     public int getRowCount() {
115         return getRows().size();
116     }
117
118     // Properties
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 JavaDoc 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 JavaDoc source) {
156         this.source = source;
157         this.rows = null;
158     }
159
160     // Implementation methods
161
// -------------------------------------------------------------------------
162
protected void handleException(Exception JavaDoc e) {
163         // #### should use jakarta commons-logging
164
System.out.println("Caught: " + e);
165     }
166 }
167
168 /*
169  * Redistribution and use of this software and associated documentation
170  * ("Software"), with or without modification, are permitted provided that the
171  * following conditions are met:
172  *
173  * 1. Redistributions of source code must retain copyright statements and
174  * notices. Redistributions must also contain a copy of this document.
175  *
176  * 2. Redistributions in binary form must reproduce the above copyright notice,
177  * this list of conditions and the following disclaimer in the documentation
178  * and/or other materials provided with the distribution.
179  *
180  * 3. The name "DOM4J" must not be used to endorse or promote products derived
181  * from this Software without prior written permission of MetaStuff, Ltd. For
182  * written permission, please contact dom4j-info@metastuff.com.
183  *
184  * 4. Products derived from this Software may not be called "DOM4J" nor may
185  * "DOM4J" appear in their names without prior written permission of MetaStuff,
186  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
187  *
188  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
189  *
190  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
191  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
194  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
195  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
196  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
197  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
198  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
199  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
200  * POSSIBILITY OF SUCH DAMAGE.
201  *
202  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
203  */

204
Popular Tags