RowSetAdapter.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: RowSetAdapter.java,v 1.2 2005/10/10 17:01:10 rbair Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.binding.swingx.adapter;
  22. import java.sql.ResultSetMetaData;
  23. import java.sql.SQLException;
  24. import javax.sql.RowSet;
  25. import javax.swing.table.AbstractTableModel;
  26. /**
  27.  * Placeholder for future RowSet TableModel adapter to enable
  28.  * easy connectivity to JDBC RowSet functionality. For more details see the
  29.  * <a href="http://www.jcp.org/en/jsr/detail?id=114">JSR 114</a>.
  30.  * Note: This class is not yet fully functional.
  31.  *
  32.  * @author Amy Fowler
  33.  * @version 1.0
  34.  */
  35. public class RowSetAdapter extends AbstractTableModel {
  36.     private RowSet rowset;
  37.     private ResultSetMetaData metaData;
  38.     /**
  39.      * Note: This class is not yet functional, but will be completed
  40.      * in JDNC Milestone5.
  41.      *
  42.      * Creates a table model adapter which binds to the specified tabular
  43.      * data model.
  44.      *
  45.      * @param rowset RowSet object containing the tabular data
  46.      * @throws NullPointerException if rowset is null
  47.      * @throws SQLException
  48.      */
  49.     private RowSetAdapter(RowSet rowset) throws java.sql.SQLException{
  50.         if (rowset == null) {
  51.             throw new NullPointerException("rowset cannot be null");
  52.         }
  53.         this.rowset = rowset;
  54.         this.metaData = rowset.getMetaData();
  55.     }
  56.     public Class getColumnClass(int columnIndex) {
  57.         Class klass = null;
  58.         try {
  59.             klass = Class.forName(metaData.getColumnClassName(
  60.                 translateAdapterColumn(columnIndex)));
  61.         } catch (Exception e) {
  62.         }
  63.         return klass;
  64.     }
  65.     public int getRowCount() {
  66.         //REMIND(aim): awk! size() doesn't exist in 1.4.2....
  67.         //return rowset.size();
  68.         return 0;
  69.     }
  70.     public int getColumnCount() {
  71.         int columnCount = 0;
  72.         try {
  73.             columnCount = metaData.getColumnCount();
  74.         } catch (Exception e) {
  75.             e.printStackTrace();
  76.         }
  77.         return columnCount;
  78.     }
  79.     public Object getValueAt(int rowIndex, int columnIndex) {
  80.         Object value = null;
  81.         try {
  82.             synchronized(rowset) {
  83.                 rowset.absolute(translateAdapterRow(rowIndex));
  84.                 value = rowset.getObject(translateAdapterColumn(columnIndex));
  85.             }
  86.         } catch (Exception e) {
  87.         }
  88.         return value;
  89.     }
  90.     public boolean isCellEditable(int rowIndex, int columnIndex) {
  91.         //REMIND(aim): investigate isReadOnly vs isWritable vs isDefinitelyWritable
  92.         boolean editable = false;
  93.         try {
  94.             editable = metaData.isWritable(translateAdapterColumn(columnIndex));
  95.         } catch (Exception e) {
  96.         }
  97.         return editable;
  98.     }
  99.     protected int translateAdapterColumn(int columnIndex) {
  100.         return columnIndex + 1;
  101.     }
  102.     protected int translateDataColumn(int dataColumnIndex) {
  103.         return dataColumnIndex - 1;
  104.     }
  105.     protected int translateAdapterRow(int rowIndex) {
  106.         return rowIndex + 1;
  107.     }
  108.     protected int translateDataRow(int dataRowIndex) {
  109.         return dataRowIndex - 1;
  110.     }
  111. }