001: package snow.sortabletable;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import javax.swing.event.*;
007: import javax.swing.table.*;
008: import java.util.*;
009:
010: /** wraps around a table model to extract first column of rest
011: USE instanciate twice, one time with first=true for the headers
012: one time with first=false for the rest
013: */
014: public class RowHeaderExtractorWrapperSortableTableModel extends
015: AbstractTableModel {
016: boolean first;
017: SortableTableModel baseModel;
018: int numberOfColumnHeaders;
019:
020: public RowHeaderExtractorWrapperSortableTableModel(boolean _first,
021: SortableTableModel _baseModel, int _numberOfColumnHeaders) {
022: super ();
023: this .first = _first;
024: this .baseModel = _baseModel;
025: this .numberOfColumnHeaders = _numberOfColumnHeaders;
026:
027: // give the events of the bassModel to this
028: baseModel.addTableModelListener(new TableModelListener() {
029: public void tableChanged(TableModelEvent e) {
030: if (e.getFirstRow() == TableModelEvent.HEADER_ROW) {
031: fireTableStructureChanged();
032: } else {
033: fireTableDataChanged();
034: }
035: }
036: });
037:
038: }
039:
040: private int getColumn(int col) {
041: if (first)
042: return col;
043: return col + numberOfColumnHeaders;
044: }
045:
046: // AbstractTableModel impl
047: //
048:
049: public int getRowCount() {
050: return baseModel.getRowCount();
051: }
052:
053: public int getColumnCount() {
054: return (first ? numberOfColumnHeaders : baseModel
055: .getColumnCount()
056: - numberOfColumnHeaders);
057: }
058:
059: @Override
060: public Object getValueAt(int row, int column) {
061: return baseModel.getValueAt(row, getColumn(column));
062: }
063:
064: @Override
065: public boolean isCellEditable(int row, int col) {
066: return baseModel.isCellEditable(row, getColumn(col));
067: }
068:
069: @Override
070: public void setValueAt(Object val, int row, int col) {
071: baseModel.setValueAt(val, row, getColumn(col));
072: }
073:
074: @Override
075: public String getColumnName(int column) {
076: return baseModel.getColumnName(getColumn(column));
077:
078: }
079:
080: @Override
081: public Class getColumnClass(int col) {
082: return baseModel.getColumnClass(getColumn(col));
083: }
084:
085: /* public void setPreferredColumnSizesFromModel()
086: {
087: if(tableReference==null) return;
088:
089: int fontSize = UIManager.getFont("Label.font").getSize();
090: for(int i=0; i<tableReference.getColumnCount(); i++)
091: {
092: int pos = this.getColumnForViewIndex(i);
093: int w = this.basicTableModel.getPreferredColumnWidth(pos);
094: if(w>0)
095: {
096: tableReference.getColumnModel().getColumn(i).setPreferredWidth(w*fontSize);
097: }
098: }
099:
100: } */
101:
102: }
|