1 36 37 40 41 58 59 import java.util.*; 60 61 import javax.swing.table.TableModel ; 62 import javax.swing.event.TableModelEvent ; 63 64 66 import java.awt.event.MouseAdapter ; 67 import java.awt.event.MouseEvent ; 68 import java.awt.event.InputEvent ; 69 import javax.swing.JTable ; 70 import javax.swing.table.JTableHeader ; 71 import javax.swing.table.TableColumn ; 72 import javax.swing.table.TableColumnModel ; 73 74 public class TableSorter extends TableMap 75 { 76 int indexes[]; 77 Vector sortingColumns = new Vector(); 78 boolean ascending = true; 79 int compares; 80 81 public TableSorter() 82 { 83 indexes = new int[0]; } 85 86 public TableSorter(TableModel model) 87 { 88 setModel(model); 89 } 90 91 public void setModel(TableModel model) { 92 super.setModel(model); 93 reallocateIndexes(); 94 } 95 96 public int compareRowsByColumn(int row1, int row2, int column) 97 { 98 Class type = model.getColumnClass(column); 99 TableModel data = model; 100 101 103 Object o1 = data.getValueAt(row1, column); 104 Object o2 = data.getValueAt(row2, column); 105 106 if (o1 == null && o2 == null) { 108 return 0; 109 } 110 else if (o1 == null) { return -1; 112 } 113 else if (o2 == null) { 114 return 1; 115 } 116 117 123 if (type.getSuperclass() == java.lang.Number .class) 124 { 125 Number n1 = (Number )data.getValueAt(row1, column); 126 double d1 = n1.doubleValue(); 127 Number n2 = (Number )data.getValueAt(row2, column); 128 double d2 = n2.doubleValue(); 129 130 if (d1 < d2) 131 return -1; 132 else if (d1 > d2) 133 return 1; 134 else 135 return 0; 136 } 137 else if (type == java.util.Date .class) 138 { 139 Date d1 = (Date)data.getValueAt(row1, column); 140 long n1 = d1.getTime(); 141 Date d2 = (Date)data.getValueAt(row2, column); 142 long n2 = d2.getTime(); 143 144 if (n1 < n2) 145 return -1; 146 else if (n1 > n2) 147 return 1; 148 else return 0; 149 } 150 else if (type == String .class) 151 { 152 String s1 = (String )data.getValueAt(row1, column); 153 String s2 = (String )data.getValueAt(row2, column); 154 int result = s1.compareTo(s2); 155 156 if (result < 0) 157 return -1; 158 else if (result > 0) 159 return 1; 160 else return 0; 161 } 162 else if (type == Boolean .class) 163 { 164 Boolean bool1 = (Boolean )data.getValueAt(row1, column); 165 boolean b1 = bool1.booleanValue(); 166 Boolean bool2 = (Boolean )data.getValueAt(row2, column); 167 boolean b2 = bool2.booleanValue(); 168 169 if (b1 == b2) 170 return 0; 171 else if (b1) return 1; 173 else 174 return -1; 175 } 176 else 177 { 178 Object v1 = data.getValueAt(row1, column); 179 String s1 = v1.toString(); 180 Object v2 = data.getValueAt(row2, column); 181 String s2 = v2.toString(); 182 int result = s1.compareTo(s2); 183 184 if (result < 0) 185 return -1; 186 else if (result > 0) 187 return 1; 188 else return 0; 189 } 190 } 191 192 public int compare(int row1, int row2) 193 { 194 compares++; 195 for(int level = 0; level < sortingColumns.size(); level++) 196 { 197 Integer column = (Integer )sortingColumns.elementAt(level); 198 int result = compareRowsByColumn(row1, row2, column.intValue()); 199 if (result != 0) 200 return ascending ? result : -result; 201 } 202 return 0; 203 } 204 205 public void reallocateIndexes() 206 { 207 int rowCount = model.getRowCount(); 208 209 indexes = new int[rowCount]; 212 213 for(int row = 0; row < rowCount; row++) 215 indexes[row] = row; 216 } 217 218 public void tableChanged(TableModelEvent e) 219 { 220 System.out.println("Sorter: tableChanged"); 221 reallocateIndexes(); 222 223 super.tableChanged(e); 224 } 225 226 public void checkModel() 227 { 228 if (indexes.length != model.getRowCount()) { 229 System.err.println("Sorter not informed of a change in model."); 230 } 231 } 232 233 public void sort(Object sender) 234 { 235 checkModel(); 236 237 compares = 0; 238 shuttlesort((int[])indexes.clone(), indexes, 0, indexes.length); 241 System.out.println("Compares: "+compares); 242 } 243 244 public void n2sort() { 245 for(int i = 0; i < getRowCount(); i++) { 246 for(int j = i+1; j < getRowCount(); j++) { 247 if (compare(indexes[i], indexes[j]) == -1) { 248 swap(i, j); 249 } 250 } 251 } 252 } 253 254 public void shuttlesort(int from[], int to[], int low, int high) { 262 if (high - low < 2) { 263 return; 264 } 265 int middle = (low + high)/2; 266 shuttlesort(to, from, low, middle); 267 shuttlesort(to, from, middle, high); 268 269 int p = low; 270 int q = middle; 271 272 286 287 if (high - low >= 4 && compare(from[middle-1], from[middle]) <= 0) { 288 for (int i = low; i < high; i++) { 289 to[i] = from[i]; 290 } 291 return; 292 } 293 294 296 for(int i = low; i < high; i++) { 297 if (q >= high || (p < middle && compare(from[p], from[q]) <= 0)) { 298 to[i] = from[p++]; 299 } 300 else { 301 to[i] = from[q++]; 302 } 303 } 304 } 305 306 public void swap(int i, int j) { 307 int tmp = indexes[i]; 308 indexes[i] = indexes[j]; 309 indexes[j] = tmp; 310 } 311 312 315 public Object getValueAt(int aRow, int aColumn) 316 { 317 checkModel(); 318 return model.getValueAt(indexes[aRow], aColumn); 319 } 320 321 public void setValueAt(Object aValue, int aRow, int aColumn) 322 { 323 checkModel(); 324 model.setValueAt(aValue, indexes[aRow], aColumn); 325 } 326 327 public void sortByColumn(int column) { 328 sortByColumn(column, true); 329 } 330 331 public void sortByColumn(int column, boolean ascending) { 332 this.ascending = ascending; 333 sortingColumns.removeAllElements(); 334 sortingColumns.addElement(new Integer (column)); 335 sort(this); 336 super.tableChanged(new TableModelEvent (this)); 337 } 338 339 public void addMouseListenerToHeaderInTable(JTable table) { 343 final TableSorter sorter = this; 344 final JTable tableView = table; 345 tableView.setColumnSelectionAllowed(false); 346 MouseAdapter listMouseListener = new MouseAdapter () { 347 public void mouseClicked(MouseEvent e) { 348 TableColumnModel columnModel = tableView.getColumnModel(); 349 int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 350 int column = tableView.convertColumnIndexToModel(viewColumn); 351 if(e.getClickCount() == 1 && column != -1) { 352 System.out.println("Sorting ..."); 353 int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK; 354 boolean ascending = (shiftPressed == 0); 355 sorter.sortByColumn(column, ascending); 356 } 357 } 358 }; 359 JTableHeader th = tableView.getTableHeader(); 360 th.addMouseListener(listMouseListener); 361 } 362 363 364 365 } 366 | Popular Tags |