Source of Table.java


  1: 
  2: package adapter;
  3: 
  4: import java.awt.Color;
  5: import java.awt.Point;
  6: import java.awt.event.*;
  7: import javax.swing.*;
  8: import javax.swing.table.*;
  9: import javax.swing.event.*;
 10: import java.util.*;
 11: 
 12: public class Table extends JTable { 
 13: 
 14:   public Table() { 
 15:     this(null); 
 16:   }
 17: 
 18:   public Table(List entries) { 
 19:     super(new ListTableModel(entries));
 20:     model = (ListTableModel) dataModel;     
 21: 
 22:     getTableHeader().addMouseListener(new MouseAdapter() { 
 23:         public void mousePressed(MouseEvent e) { 
 24:           Point p = e.getPoint(); 
 25:           JTableHeader header = (JTableHeader) e.getSource(); 
 26:           int column = header.columnAtPoint(p); 
 27:           if (model.sort(column)) { 
 28:             clearSelection();
 29:             updateUI();
 30:           }
 31:         }
 32:       }); 
 33: 
 34:     setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 35: 
 36:     int columnCount = model.getColumnCount(); 
 37:     for (int i = 0; i < columnCount; i++) {
 38:       TableColumn column = getColumnModel().getColumn(i);
 39:       DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
 40:       String tip = model.getColumnTip(i);
 41:       renderer.setToolTipText(tip);
 42:       column.setCellRenderer(renderer);
 43:       int w = model.getColumnWidth(i);
 44:       if (w > 0) { 
 45:         column.setPreferredWidth(w); 
 46:       }
 47:     }
 48:   }
 49: 
 50:   protected ListTableModel model; 
 51: 
 52:   static class ListTableModel extends AbstractTableModel {
 53: 
 54:     public ListTableModel(List entries) {
 55:       if (entries != null && 
 56:           entries.size() > 0) {
 57:         Object obj = entries.get(0);
 58:         if (obj != null && 
 59:             obj instanceof TableEntry) { 
 60:           this.prototype = (TableEntry) obj;
 61:           setData(entries);
 62:         }
 63:       }
 64:     }
 65:     
 66:     public ListTableModel(TableEntry prototype) {
 67:       this.prototype = prototype;
 68:     }
 69:         
 70:     public int getColumnCount() {
 71:       if (prototype != null) { 
 72:         return prototype.getColumnCount();
 73:       }
 74:       return 0;
 75:     }
 76:     
 77:     public int getRowCount() { 
 78:       if (entries != null) { 
 79:         return entries.size();
 80:       } else { 
 81:         return 0;
 82:       }
 83:     }
 84:     
 85:     public String getColumnName(int col) {
 86:       if (prototype != null) { 
 87:         return prototype.getColumnName(col); 
 88:       }
 89:       return null;
 90:     }
 91:     
 92:     public Object getValueAt(int row, int col) {
 93:       if (entries != null) { 
 94:         TableEntry entry = getTableEntry(row);
 95:         if (entry != null) { 
 96:           return entry.getColumnValue(col);
 97:         }
 98:       }
 99:       return null;
100:     }
101:     
102:     public Class getColumnClass(int col) {
103:       if (prototype != null) { 
104:         return prototype.getColumnClass(col);
105:       }
106:       return String.class;
107:     }
108:     
109:     public String getColumnTip(int col) {
110:       if (prototype != null) { 
111:         return prototype.getColumnTip(col);
112:       }
113:       return null;
114:     }
115:     
116:     public Comparator getColumnComparator(int col) {
117:       if (prototype != null) { 
118:         return prototype.getColumnComparator(col);
119:       }
120:       return null;
121:     }
122:     
123:     public int getColumnWidth(int col) {
124:       if (prototype != null) { 
125:         return prototype.getColumnWidth(col);
126:       }
127:       return -1;
128:     }
129: 
130:     public boolean isCellEditable(int row, int col) {
131:       return false;
132:     }
133:     
134:     public void setValueAt(Object value, int row, int col) {
135:     }
136:     
137:     public void clearData() {
138:       entries = null;
139:     }
140:   
141:     public void setData(List entries) {
142:       this.entries = entries;
143:     }
144:     
145:     public boolean sort(int col) { 
146:       if (entries != null && 
147:           col >=0 && 
148:           col < getColumnCount()) { 
149:         Comparator c = getColumnComparator(col);
150:         if (c != null) { 
151:           Collections.sort(entries, c);
152:           return true; 
153:         }
154:       }
155:       return false; 
156:     }
157:     
158:     public TableEntry getTableEntry(int i) { 
159:       if (entries != null && 
160:           i >= 0 && 
161:           i < entries.size()) { 
162:         return (TableEntry) entries.get(i); 
163:       }
164:       return null; 
165:     }
166:     
167:     protected TableEntry prototype; 
168:     protected List entries; // elements are instance of TableEntry
169:     
170:   }
171: 
172: }