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: public final class TestModel extends FineGrainTableModel {
011: private final Vector<TestModelItem> items = new Vector<TestModelItem>();
012: private final String[] COLUMN_NAMES = new String[] { "Size",
013: "Name", "Index", "Index2", "Index3", "check" };
014:
015: public TestModel() {
016: for (int i = 0; i < 3; i++) {
017: addRandom();
018: }
019: }
020:
021: int[] COLUMN_PREFERED_SIZES = new int[] { 6, 4, 4, 12, 12, 12 };
022:
023: @Override
024: public int getPreferredColumnWidth(int column) {
025: if (column >= 0 && column < COLUMN_PREFERED_SIZES.length)
026: return COLUMN_PREFERED_SIZES[column];
027: return -1;
028: }
029:
030: public void add(TestModelItem titm) {
031: fireTableModelWillChange();
032:
033: items.addElement(titm);
034:
035: fireTableDataChanged();
036: fireTableModelHasChanged();
037: }
038:
039: public void remove(TestModelItem titm) {
040: fireTableModelWillChange();
041: items.removeElement(titm);
042: fireTableDataChanged();
043: fireTableModelHasChanged();
044: }
045:
046: int n = 0;
047:
048: public void addRandom() {
049: n++;
050:
051: TestModelItem titm = new TestModelItem();
052: titm.col1 = Math.random() * 1e6;
053: if (Math.random() < 0.1)
054: titm.col1 = Math.random() * 1e3;
055: titm.col2 = "" + ((char) ('a' + (n % 26)));
056: titm.col3 = n;
057: titm.col4 = (int) (Math.random() * 1000);
058: titm.col5 = (int) (Math.random() * 1000000);
059: add(titm);
060: }
061:
062: public void removeLast() {
063: if (items.isEmpty())
064: return;
065:
066: fireTableModelWillChange();
067: items.removeElementAt(items.size() - 1);
068: fireTableDataChanged();
069: fireTableModelHasChanged();
070: }
071:
072: public int getRowCount() {
073: return items.size();
074: }
075:
076: public int getColumnCount() {
077: return 6;
078: }
079:
080: public TestModelItem getItemAt(int pos) {
081: return items.get(pos);
082: }
083:
084: public Object getValueAt(int row, int column) {
085: TestModelItem item = getItemAt(row);
086: if (column == 0)
087: return item.col1;
088: if (column == 1)
089: return item.col2;
090: if (column == 2)
091: return item.col3;
092: if (column == 3)
093: return item.col4;
094: if (column == 4)
095: return item.col5;
096: if (column == 5)
097: return item.checked;
098: return "??";
099: }
100:
101: @Override
102: public boolean isCellEditable(int row, int col) {
103: return col <= 2 || col == 5;
104: }
105:
106: @Override
107: public void setValueAt(Object val, int row, int col) {
108: if (val == null)
109: return;
110:
111: fireTableModelWillChange();
112: TestModelItem item = getItemAt(row);
113: if (col == 0) {
114: try {
115: double vd = Double.parseDouble("" + val);
116: item.col1 = vd;
117: } catch (Exception e) {
118: }
119: } else if (col == 1) {
120: item.col2 = (String) val;
121: } else if (col == 2) {
122: item.col3 = (Integer) val;
123: } else if (col == 5) {
124: item.checked = (Boolean) val;
125: }
126:
127: // must notify table that data has changed
128: fireTableDataChanged();
129: fireTableModelHasChanged();
130: }
131:
132: @Override
133: public String getColumnName(int column) {
134: if (column >= 0 && column < COLUMN_NAMES.length)
135: return COLUMN_NAMES[column];
136: return "Bad column " + column;
137: }
138:
139: /*
140: public int compareForColumnSort(int pos1, int pos2, int col)
141: {
142: if(col==0 || col>=2)
143: {
144: double v1 = Double.parseDouble(""+getValueAt(pos1, col));
145: double v2 = Double.parseDouble(""+getValueAt(pos2, col));
146: if(v1==v2) return 0;
147: if(v1>v2) return 1;
148: return -1;
149: }
150: else
151: {
152: String v1 = ""+getValueAt(pos1, col);
153: String v2 = ""+getValueAt(pos2, col);
154: return v1.compareTo(v2);
155: }
156: // return 0;
157:
158: }*/
159:
160: // Selection implementation
161: //
162: @Override
163: public void setRowSelection(int row, boolean isSelected) {
164: TestModelItem item = getItemAt(row);
165: item.selected = isSelected;
166: }
167:
168: @Override
169: public boolean isRowSelected(int row) {
170: TestModelItem item = getItemAt(row);
171: return item.selected;
172: }
173:
174: @Override
175: public void clearRowSelection() {
176: synchronized (items) {
177: for (int i = 0; i < getRowCount(); i++) {
178: TestModelItem item = getItemAt(i);
179: item.selected = false;
180: }
181: }
182: }
183:
184: }
|