KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > data > RowSetAdapter


1 /*
2  * $Id: RowSetAdapter.java,v 1.2 2004/09/07 18:16:01 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.data;
9
10 import java.sql.ResultSetMetaData JavaDoc;
11
12 import java.util.ArrayList JavaDoc;
13
14 import javax.sql.RowSet JavaDoc;
15
16 import javax.swing.table.AbstractTableModel JavaDoc;
17 import javax.swing.table.TableModel JavaDoc;
18 import javax.swing.event.TableModelListener JavaDoc;
19 import javax.swing.event.TableModelEvent JavaDoc;
20
21 /**
22  * Placeholder for future RowSet TableModel adapter to enable
23  * easy connectivity to JDBC RowSet functionality. For more details see the
24  * <a HREF="http://www.jcp.org/en/jsr/detail?id=114">JSR 114</a>.
25  * Note: This class is not yet fully functional.
26  *
27  * @author Amy Fowler
28  * @version 1.0
29  */

30 public class RowSetAdapter extends AbstractTableModel JavaDoc {
31     private RowSet JavaDoc rowset;
32     private ResultSetMetaData JavaDoc metaData;
33
34     /**
35      * Note: This class is not yet functional, but will be completed
36      * in JDNC Milestone5.
37      *
38      * Creates a table model adapter which binds to the specified tabular
39      * data model.
40      *
41      * @param rowset RowSet object containing the tabular data
42      * @throws NullPointerException if rowset is null
43      * @throws SQLException
44      */

45     private RowSetAdapter(RowSet JavaDoc rowset) throws java.sql.SQLException JavaDoc{
46         if (rowset == null) {
47             throw new NullPointerException JavaDoc("rowset cannot be null");
48         }
49         this.rowset = rowset;
50         this.metaData = rowset.getMetaData();
51     }
52
53     public Class JavaDoc getColumnClass(int columnIndex) {
54         Class JavaDoc klass = null;
55         try {
56             klass = Class.forName(metaData.getColumnClassName(
57                 translateAdapterColumn(columnIndex)));
58         } catch (Exception JavaDoc e) {
59
60         }
61         return klass;
62     }
63
64     public int getRowCount() {
65         //REMIND(aim): awk! size() doesn't exist in 1.4.2....
66
//return rowset.size();
67
return 0;
68     }
69
70     public int getColumnCount() {
71         int columnCount = 0;
72         try {
73             columnCount = metaData.getColumnCount();
74         } catch (Exception JavaDoc e) {
75             e.printStackTrace();
76         }
77         return columnCount;
78     }
79
80     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
81         Object JavaDoc value = null;
82         try {
83             synchronized(rowset) {
84                 rowset.absolute(translateAdapterRow(rowIndex));
85                 value = rowset.getObject(translateAdapterColumn(columnIndex));
86             }
87         } catch (Exception JavaDoc e) {
88
89         }
90         return value;
91     }
92
93     public boolean isCellEditable(int rowIndex, int columnIndex) {
94         //REMIND(aim): investigate isReadOnly vs isWritable vs isDefinitelyWritable
95
boolean editable = false;
96         try {
97             editable = metaData.isWritable(translateAdapterColumn(columnIndex));
98         } catch (Exception JavaDoc e) {
99
100         }
101         return editable;
102     }
103
104     protected int translateAdapterColumn(int columnIndex) {
105         return columnIndex + 1;
106     }
107
108     protected int translateDataColumn(int dataColumnIndex) {
109         return dataColumnIndex - 1;
110     }
111
112     protected int translateAdapterRow(int rowIndex) {
113         return rowIndex + 1;
114     }
115
116     protected int translateDataRow(int dataRowIndex) {
117         return dataRowIndex - 1;
118     }
119
120 }
121
Popular Tags