001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020:
021: package com.salmonllc.swing.table;
022:
023: import com.salmonllc.sql.*;
024: import com.salmonllc.swing.SOption;
025: import com.salmonllc.swing.STable;
026: import com.salmonllc.swing.events.ValueChangedEvent;
027:
028: import javax.swing.event.ListSelectionEvent;
029: import javax.swing.event.ListSelectionListener;
030: import javax.swing.table.AbstractTableModel;
031:
032: /**
033: * A Table Model for a JTable that allows for binding to a DataStore
034: */
035: public class DataStoreTableModel extends AbstractTableModel implements
036: ModelChangedListener, ListSelectionListener {
037:
038: DataStoreBuffer _ds;
039: String _colNames[];
040: boolean _isEditable[];
041: STable _tab;
042: boolean _clickSort = true;
043:
044: /**
045: * Constructor for DataStoreTableModel.
046: */
047: public DataStoreTableModel(DataStoreBuffer ds, String colNames[]) {
048: super ();
049: _ds = ds;
050: _colNames = colNames;
051: _isEditable = new boolean[ds.getColumnCount()];
052: _ds.addModelChangedListener(this );
053: }
054:
055: /**
056: * Constructor for DataStoreTableModel.
057: */
058: public DataStoreTableModel(DataStoreBuffer ds) {
059: super ();
060: _ds = ds;
061: _colNames = new String[ds.getColumnCount()];
062: _isEditable = new boolean[ds.getColumnCount()];
063: try {
064: for (int i = 0; i < ds.getColumnCount(); i++) {
065: _colNames[i] = ds.getColumnName(i);
066: }
067: } catch (Exception e) {
068: }
069: _ds.addModelChangedListener(this );
070: }
071:
072: /**
073: * @see javax.swing.table.TableModel#getRowCount()
074: */
075: public int getRowCount() {
076: return _ds.getRowCount();
077: }
078:
079: /**
080: * @see javax.swing.table.TableModel#getColumnCount()
081: */
082: public int getColumnCount() {
083: return _ds.getColumnCount();
084: }
085:
086: /**
087: * @see javax.swing.table.TableModel#getValueAt(int, int)
088: */
089: public Object getValueAt(int rowIndex, int columnIndex) {
090: try {
091: if (columnIndex == -1)
092: return null;
093: else
094: return _ds.getFormattedString(rowIndex, columnIndex);
095: } catch (Exception e) {
096: return null;
097: }
098: }
099:
100: /**
101: * @see javax.swing.table.TableModel#getColumnName(int)
102: */
103: public String getColumnName(int columnIndex) {
104: return _colNames[columnIndex];
105: }
106:
107: /**
108: * @see javax.swing.table.TableModel#setValueAt(Object, int, int)
109: */
110: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
111: ValueChangedEvent e = generateValueChangedEvent(aValue,
112: rowIndex, columnIndex);
113: _tab.getHelper().notifyValueChangedListeners(e);
114: }
115:
116: /**
117: * Part of the model changed listener interface, don't call this method directly
118: */
119: public void modelChanged(ModelChangedEvent evt) {
120: if (evt.getType() != ModelChangedEvent.TYPE_ROW_FOCUS_CHANGED) {
121: if (evt.isAllDataChanged()) {
122: fireTableDataChanged();
123: if (_tab.getAutoSelectCurrentRow()
124: && _ds.getRow() != -1)
125: _tab.selectRow(_ds.getRow());
126: } else {
127: fireTableRowsUpdated(evt.getNewRow(), evt.getNewRow());
128: _tab.revalidate();
129: _tab.repaint();
130: }
131: } else if (_tab.getAutoSelectCurrentRow()) {
132: _tab.selectRow(evt.getNewRow());
133: }
134: }
135:
136: /**
137: * Returns false. This is the default implementation for all cells.
138: *
139: * @param rowIndex the row being queried
140: * @param columnIndex the column being queried
141: * @return false
142: */
143: public boolean isCellEditable(int rowIndex, int columnIndex) {
144: if (columnIndex == -1)
145: return false;
146: else
147: return _isEditable[columnIndex];
148: }
149:
150: /**
151: * Indicates whether or not a column is editable
152: */
153: public void setColumnEditable(int columnIndex, boolean editable) {
154: _isEditable[columnIndex] = editable;
155: }
156:
157: /**
158: * Indicates whether or not a column is editable
159: */
160: public boolean isColumnEditable(int columnIndex) {
161: if (columnIndex == -1 || columnIndex >= _isEditable.length)
162: return false;
163: return _isEditable[columnIndex];
164: }
165:
166: /**
167: * Sets the STable this model is for
168: */
169: public void setSTable(STable tab) {
170: _tab = tab;
171: _tab.getSelectionModel().addListSelectionListener(this );
172: }
173:
174: ValueChangedEvent generateValueChangedEvent(Object val, int row,
175: int col) {
176: try {
177: String value = null;
178: if (val != null) {
179: if (val instanceof SOption)
180: value = ((SOption) val).getKey();
181: else
182: value = val.toString();
183: }
184: return new ValueChangedEvent(_tab, _ds.getFormattedString(
185: row, col), value, _ds, row, col);
186: } catch (Exception e) {
187: e.printStackTrace();
188: return null;
189: }
190: }
191:
192: /**
193: * Gets the DataStore
194: */
195: public DataStoreBuffer getDataStore() {
196: return _ds;
197: }
198:
199: /**
200: * Part of the list selection interface
201: */
202: public void valueChanged(ListSelectionEvent e) {
203: if (_tab.getAutoSelectCurrentRow())
204: _ds.gotoRow(_tab.getSelectedRow());
205: }
206:
207: }
|