| 1 package snow.sortabletable; 2 3 import javax.swing.table.*; 4 import java.util.*; 5 import javax.swing.event.*; 6 import javax.swing.*; 7 import java.awt.event.*; 8 import java.awt.*; 9 import java.util.regex.*; 10 11 12 13 22 public abstract class FineGrainTableModel extends AbstractTableModel 23 { 24 final Vector<TableModelChangeListener> modelListeners = new Vector<TableModelChangeListener>(); 25 26 27 public FineGrainTableModel() 28 { 29 30 } 32 33 36 public Class getColumnClass ( int column ) 37 { 38 if(this.getColumnCount()==0 || this.getRowCount()==0) 39 { 40 return Object .class.getClass(); 41 } 42 43 Object val = this.getValueAt(0,column); 44 if(val==null) return String .class; 45 return val.getClass(); 46 } 47 48 49 51 @SuppressWarnings ("unchecked") 52 public int compareForColumnSort(int pos1, int pos2, int col) 53 { 54 Object val1 = this.getValueAt(pos1,col); 55 Object val2 = this.getValueAt(pos2,col); 56 57 if(val1 instanceof Comparable ) 58 { 59 return ((Comparable ) val1).compareTo(val2); 60 } 61 else if(val1 instanceof Boolean ) 62 { 63 return compareBooleans((Boolean ) val1, (Boolean ) val2); 64 } 65 else 66 { 67 return (""+val1.toString()).compareTo(this.getValueAt(pos2,col).toString()); 69 } 70 } 71 72 73 77 public boolean hitForTextSearch(int row, String txt, Pattern p) 78 { 79 if(txt==null || txt.equals("")) return true; 80 String search = txt.toUpperCase(); 81 82 for(int col=0; col<this.getColumnCount(); col++) 83 { 84 String val = this.getValueAt(row,col).toString(); 85 if(p!=null) 86 { 87 Matcher m = p.matcher(val); 88 return m.matches(); 89 } 90 else 91 { 92 if(val.toUpperCase().indexOf(search)>=0) return true; 93 } 94 } 95 return false; 96 } 97 98 103 public boolean hitForTextSearch(int row, int column, String txt, Pattern p) 104 { 105 if(column==-1) return hitForTextSearch(row, txt, p); 107 108 if(txt==null || txt.equals("")) return true; 109 String search = txt.toUpperCase(); 110 String val = this.getValueAt(row,column).toString(); 111 if(val.toUpperCase().indexOf(search)>=0) return true; 112 113 return false; 114 } 115 116 123 public int getPreferredColumnWidth(int column) 124 { 125 return -1; 126 } 127 128 public int getColumnAlignment(int column) 129 { 130 return JLabel.LEFT; 131 } 132 133 137 138 151 private HashSet<Integer > selectedRows = new HashSet<Integer >(); 152 153 154 167 public void setRowSelection(int row, boolean isSelected) 168 { 169 if(isSelected) 170 { 171 selectedRows.add( row ); 172 } 173 else 174 { 175 selectedRows.remove( row ); 176 } 177 } 178 179 181 public boolean isRowSelected(int row) 182 { 183 return selectedRows.contains( row ); 184 } 185 186 public void clearRowSelection() 187 { 188 for(int i=0; i<this.getRowCount(); i++) 189 { 190 this.setRowSelection(i, false); 191 } 192 } 193 194 197 public int[] getSelectedRows() 198 { 199 Vector<Integer > sel = new Vector<Integer >(); 202 synchronized(this) 203 { 204 for(int i=0; i<getRowCount(); i++) 205 { 206 if(isRowSelected(i)) sel.add(i); 207 } 208 int[] rep = new int[sel.size()]; 209 210 for(int i=0; i<rep.length; i++) 211 { 212 rep[i] = sel.elementAt(i); 213 } 214 return rep; 215 } 216 217 } 218 219 220 221 222 public void addModelChangeListener(TableModelChangeListener listener) 225 { 226 modelListeners.addElement(listener); 227 } 228 229 public void removeModelChangeListener(TableModelChangeListener listener) 230 { 231 modelListeners.removeElement(listener); 232 } 233 234 public void fireTableModelWillChange() 235 { 236 TableModelChangeListener[] tml = null; 237 synchronized(modelListeners) 238 { 239 tml = modelListeners.toArray(new TableModelChangeListener[modelListeners.size()]); 243 } 244 for(int i=tml.length-1; i>=0; i--) { 246 tml[i].tableModelWillChange(new ChangeEvent(this)); 247 } 248 } 249 250 public void fireTableModelHasChanged() 251 { 252 TableModelChangeListener[] tml = null; 253 synchronized(modelListeners) 254 { 255 tml = modelListeners.toArray(new TableModelChangeListener[modelListeners.size()]); 259 } 260 for(int i=tml.length-1; i>=0; i--) { 262 tml[i].tableModelHasChanged(new ChangeEvent(this)); 263 } 264 } 265 266 267 270 public static int compareInts(int i1, int i2) 271 { 272 if(i1==i2) return 0; 273 if(i1>i2) return 1; 274 return -1; 275 } 276 277 278 public static int compareDoubles(double i1, double i2) 279 { 280 if(i1==i2) return 0; 281 if(i1>i2) return 1; 282 return -1; 283 } 284 285 public static int compareBooleans(boolean b1, boolean b2) 286 { 287 if(b1==b2) return 0; 288 if(b1) return 1; 289 return -1; 290 } 291 292 293 } | Popular Tags |