1 package snow.sortabletable; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.event.*; 7 import javax.swing.table.*; 8 9 import java.util.*; 10 11 12 public final class TestModel extends FineGrainTableModel 13 { 14 final Vector<TestModelItem> items = new Vector<TestModelItem>(); 15 String [] COLUMN_NAMES = new String []{"Size", "Name", "Index", "Index2", "Index3", "check"}; 16 17 18 public TestModel() 19 { 20 for (int i=0; i<58; i++) 21 { 22 addRandom(); 23 } 24 } 25 26 int[] COLUMN_PREFERED_SIZES = new int[]{6,4,4,12,12,12}; 27 public int getPreferredColumnWidth(int column) 28 { 29 if(column>=0 && column<COLUMN_PREFERED_SIZES.length) return COLUMN_PREFERED_SIZES[column]; 30 return -1; 31 } 32 33 34 35 public void add(TestModelItem titm) 36 { 37 fireTableModelWillChange(); 38 39 items.addElement(titm); 40 41 fireTableDataChanged(); 42 fireTableModelHasChanged(); 43 } 44 45 public void remove(TestModelItem titm) 46 { 47 fireTableModelWillChange(); 48 items.removeElement(titm); 49 fireTableDataChanged(); 50 fireTableModelHasChanged(); 51 } 52 53 54 int n=0; 55 56 public void addRandom() 57 { 58 n++; 59 60 TestModelItem titm = new TestModelItem(); 61 titm.col1 = Math.random()*1e6; 62 if(Math.random()<0.1) titm.col1 = Math.random()*1e3; 63 titm.col2 = ""+((char) ('a'+(n%26))); 64 titm.col3 = n; 65 titm.col4 = (int) (Math.random()*1000); 66 titm.col5 = (int) (Math.random()*1000000); 67 add(titm); 68 } 69 70 71 public void removeLast() 72 { 73 if(items.size()==0) return; 74 75 fireTableModelWillChange(); 76 items.removeElementAt(items.size()-1); 77 fireTableDataChanged(); 78 fireTableModelHasChanged(); 79 } 80 81 82 public int getRowCount() {return items.size();} 83 public int getColumnCount() {return 6;} 84 85 public TestModelItem getItemAt(int pos) 86 { 87 return items.elementAt(pos); 88 } 89 90 public Object getValueAt(int row, int column) 91 { 92 TestModelItem item = getItemAt(row); 93 if(column==0) return item.col1; 94 if(column==1) return item.col2; 95 if(column==2) return item.col3; 96 if(column==3) return item.col4; 97 if(column==4) return item.col5; 98 if(column==5) return item.checked; 99 return "??"; 100 } 101 102 public boolean hitForTextSearch(int row, String str) 103 { 104 return super.hitForTextSearch(row, str, null); 105 119 } 120 121 public boolean isCellEditable(int row, int col) 122 { 123 return col<=2 || col==5; 124 } 125 126 public void setValueAt(Object val, int row, int col) 127 { 128 if(val==null) return; 129 130 fireTableModelWillChange(); 131 TestModelItem item = getItemAt(row); 132 if(col==0) 133 { 134 try 135 { 136 double vd = Double.parseDouble(""+val); 137 item.col1 = vd; 138 } 139 catch(Exception e){} 140 } 141 else if(col==1) 142 { 143 item.col2 = (String ) val; 144 } 145 else if(col==2) 146 { 147 item.col3 = (Integer ) val; 148 } 149 else if(col==5) 150 { 151 item.checked = (Boolean ) val; 152 } 153 154 155 fireTableDataChanged(); 157 fireTableModelHasChanged(); 158 } 159 160 161 162 public String getColumnName(int column) 163 { 164 if(column>=0 && column<COLUMN_NAMES.length) return COLUMN_NAMES[column]; 165 return "Bad column "+column; 166 } 167 168 188 189 192 public void setRowSelection(int row, boolean isSelected) 193 { 194 TestModelItem item = getItemAt(row); 195 item.selected = isSelected; 196 } 197 198 public boolean isRowSelected(int row) 199 { 200 TestModelItem item = getItemAt(row); 201 return item.selected; 202 } 203 204 public void clearRowSelection() 205 { 206 synchronized(items) 207 { 208 for(int i=0; i<getRowCount(); i++) 209 { 210 TestModelItem item = getItemAt(i); 211 item.selected = false; 212 } 213 } 214 } 215 216 } | Popular Tags |