001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: XMLTableModel.java,v 1.1 2001/12/14 11:32:09 jstrachan Exp $ 008 */ 009 010package org.dom4j.swing; 011 012import java.util.List; 013import javax.swing.table.AbstractTableModel; 014 015import org.dom4j.Document; 016import org.dom4j.DocumentHelper; 017import org.dom4j.Element; 018import org.dom4j.XPath; 019 020/** <p><code>XMLTableDefinition</code> repro.</p> 021 * 022 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 023 * @version $Revision: 1.1 $ 024 */ 025public class XMLTableModel extends AbstractTableModel { 026 027 028 /** Holds value of property definition. */ 029 private XMLTableDefinition definition; 030 031 /** Holds value of property source. */ 032 private Object source; 033 034 /** The rows evaluated from the row XPath expression */ 035 private List rows; 036 037 /** Creates a TableModel from an XML table definition document 038 * and an XML source 039 */ 040 public XMLTableModel(Element tableDefinition, Object source) { 041 this( XMLTableDefinition.load( tableDefinition ), source ); 042 } 043 044 /** Creates a TableModel from an XML table definition document 045 * and an XML source 046 */ 047 public XMLTableModel(Document tableDefinition, Object source) { 048 this( XMLTableDefinition.load( tableDefinition ), source ); 049 } 050 051 public XMLTableModel(XMLTableDefinition definition, Object source) { 052 this.definition = definition; 053 this.source = source; 054 } 055 056 public Object getRowValue(int rowIndex) { 057 return getRows().get(rowIndex); 058 } 059 060 public List getRows() { 061 if ( rows == null ) { 062 rows = definition.getRowXPath().selectNodes( source ); 063 } 064 return rows; 065 } 066 067 068 // TableModel interface 069 //------------------------------------------------------------------------- 070 public Class getColumnClass(int columnIndex) { 071 return definition.getColumnClass(columnIndex); 072 } 073 074 public int getColumnCount() { 075 return definition.getColumnCount(); 076 } 077 078 public String getColumnName(int columnIndex) { 079 return definition.getColumnName(columnIndex); 080 } 081 082 public Object getValueAt(int rowIndex, int columnIndex) { 083 try { 084 Object row = getRowValue(rowIndex); 085 return definition.getValueAt(row, columnIndex); 086 } 087 catch (Exception e) { 088 handleException(e); 089 return null; 090 } 091 } 092 093 public int getRowCount() { 094 return getRows().size(); 095 } 096 097 // Properties 098 //------------------------------------------------------------------------- 099 100 /** Getter for property definition. 101 * @return Value of property definition. 102 */ 103 public XMLTableDefinition getDefinition() { 104 return definition; 105 } 106 107 /** Setter for property definition. 108 * @param definition New value of property definition. 109 */ 110 public void setDefinition(XMLTableDefinition definition) { 111 this.definition = definition; 112 } 113 114 /** Getter for the XML source, which is usually a Node or List of nodes. 115 * @return Value of property source. 116 */ 117 public Object getSource() { 118 return source; 119 } 120 121 /** Setter for the XML source, which is usually a Node or List of nodes. 122 * @param source New value of property source. 123 */ 124 public void setSource(Object source) { 125 this.source = source; 126 this.rows = null; 127 } 128 129 130 // Implementation methods 131 //------------------------------------------------------------------------- 132 133 protected void handleException(Exception e) { 134 // #### should use jakarta commons-logging 135 System.out.println( "Caught: " + e ); 136 } 137} 138 139 140 141 142/* 143 * Redistribution and use of this software and associated documentation 144 * ("Software"), with or without modification, are permitted provided 145 * that the following conditions are met: 146 * 147 * 1. Redistributions of source code must retain copyright 148 * statements and notices. Redistributions must also contain a 149 * copy of this document. 150 * 151 * 2. Redistributions in binary form must reproduce the 152 * above copyright notice, this list of conditions and the 153 * following disclaimer in the documentation and/or other 154 * materials provided with the distribution. 155 * 156 * 3. The name "DOM4J" must not be used to endorse or promote 157 * products derived from this Software without prior written 158 * permission of MetaStuff, Ltd. For written permission, 159 * please contact dom4j-info@metastuff.com. 160 * 161 * 4. Products derived from this Software may not be called "DOM4J" 162 * nor may "DOM4J" appear in their names without prior written 163 * permission of MetaStuff, Ltd. DOM4J is a registered 164 * trademark of MetaStuff, Ltd. 165 * 166 * 5. Due credit should be given to the DOM4J Project 167 * (http://dom4j.org/). 168 * 169 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 170 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 171 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 172 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 173 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 174 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 175 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 176 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 177 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 178 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 179 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 180 * OF THE POSSIBILITY OF SUCH DAMAGE. 181 * 182 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 183 * 184 * $Id: XMLTableModel.java,v 1.1 2001/12/14 11:32:09 jstrachan Exp $ 185 */