KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TableModelExtAdapter.java,v 1.1 2005/01/27 14:40:25 kleopatra 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 javax.swing.event.TableModelEvent JavaDoc;
11 import javax.swing.event.TableModelListener JavaDoc;
12
13 /**
14  * Adapts a DefaultTableModelExt object to the DataModel interface so that
15  * user-interface components other than a table can easily be bound to
16  * columns in the model.
17  *
18  * @author Amy Fowler
19  * @version 1.0
20  */

21 public class TableModelExtAdapter extends AbstractDataModel {
22
23     private DefaultTableModelExt model;
24     private int rowIndex = -1;
25     private boolean updatingModel = false;
26
27     public TableModelExtAdapter(DefaultTableModelExt model) {
28         this.model = model;
29         model.addTableModelListener(new TableModelListener JavaDoc() {
30             public void tableChanged(TableModelEvent JavaDoc e) {
31                 if (updatingModel) {
32                     return;
33                 }
34                 if (e.getType() == TableModelEvent.UPDATE) {
35                     if (rowIndex >= e.getFirstRow() && rowIndex <= e.getLastRow()) {
36                         int columnIndex = e.getColumn();
37                         final DefaultTableModelExt model = TableModelExtAdapter.this.model;
38 // cvs version (1.3) does only fire for one column
39
fireColumnsChanged(model, columnIndex, columnIndex);
40                     }
41                 }
42             }
43             private void fireColumnsChanged(DefaultTableModelExt model, int firstColumn, int lastColumn) {
44               if (firstColumn < 0) {
45                 firstColumn = 0;
46                 lastColumn = model.getColumnCount()-1;
47               }
48               for (int col = firstColumn; col <= lastColumn; col++) {
49                 fireValueChanged(model.getColumnMetaData(col).getName());
50               }
51               
52             }
53         });
54     }
55
56     public String JavaDoc[] getFieldNames() {
57         int columnCount = model.getColumnCount();
58         String JavaDoc columnNames[] = new String JavaDoc[columnCount];
59         MetaData metaData[] = model.getMetaData();
60         for(int i = 0; i < columnCount; i++) {
61             columnNames[i] = metaData[i].getName();
62         }
63         return columnNames;
64     }
65
66     public MetaData getMetaData(String JavaDoc fieldName) {
67         return model.getColumnMetaData(model.getColumnIndex(fieldName));
68     }
69
70     public int getFieldCount() {
71         return model.getColumnCount();
72     }
73
74     public Object JavaDoc getValue(String JavaDoc fieldName) {
75         return getRecordIndex() >= 0 ?
76             model.getValueAt(getRecordIndex(), model.getColumnIndex(fieldName)) : null;
77     }
78
79     protected void setValueImpl(String JavaDoc fieldName, Object JavaDoc value) {
80         if (getRecordIndex() < 0) return;
81          updatingModel = true;
82          model.setValueAt(value, getRecordIndex(), model.getColumnIndex(fieldName));
83          updatingModel = false;
84      }
85
86     /**
87      * Sets the current record index such that the data field values
88      * in this value map represent the values contained at the specified
89      * row in the tabular data model.
90      * @param rowIndex integer representing the current row index
91      * @throws IndexOutOfBoundsException if index >= recordCount
92      */

93     public void setRecordIndex(int rowIndex) {
94         if (rowIndex >= model.getRowCount()) {
95             throw new IndexOutOfBoundsException JavaDoc("row index " + rowIndex +
96                                                 "exceeds row count " +
97                                                 model.getRowCount());
98         }
99         if (this.rowIndex != rowIndex) {
100             this.rowIndex = rowIndex;
101             String JavaDoc fieldNames[] = getFieldNames();
102             for(int i = 0; i < fieldNames.length; i++) {
103                 fireValueChanged(fieldNames[i]);
104             }
105         }
106     }
107
108     /**
109      *
110      * @return integer representing the current row index
111      */

112     public int getRecordIndex() {
113         return rowIndex;
114     }
115
116     /**
117      *
118      * @return integer representing the number of rows currently in
119      * the tabular data model
120      */

121     public int getRecordCount() {
122         return model.getRowCount();
123     }
124 }
125
Popular Tags