1 16 17 package org.mc4j.console.swing.table; 18 19 20 import java.awt.event.MouseAdapter ; 21 import java.awt.event.MouseEvent ; 22 import java.util.Date ; 23 import java.util.Vector ; 24 25 import javax.swing.JTable ; 26 import javax.swing.event.TableModelEvent ; 27 import javax.swing.table.JTableHeader ; 28 import javax.swing.table.TableColumnModel ; 29 import javax.swing.table.TableModel ; 30 31 48 public class TableSorter extends TableMap { 49 int indexes[]; 50 Vector sortingColumns = new Vector (); 51 boolean ascending = true; 52 int compares; 53 54 int currentSortColumn; 55 56 public TableSorter() { 57 indexes = new int[0]; } 59 60 public TableSorter(TableModel model) { 61 setModel(model); 62 } 63 64 public void setModel(TableModel model) { 65 super.setModel(model); 66 reallocateIndexes(); 67 } 68 69 public int getCurrentSortColumn() { 70 return currentSortColumn; 71 } 72 73 public void setCurrentSortColumn(int currentSortColumn) { 74 this.currentSortColumn = currentSortColumn; 75 } 76 77 public boolean isAscending() { 78 return ascending; 79 } 80 81 public int compareRowsByColumn(int row1, int row2, int column) { 82 Class type = model.getColumnClass(column); 83 TableModel data = model; 84 85 87 Object o1 = data.getValueAt(row1, column); 88 Object o2 = data.getValueAt(row2, column); 89 90 if (o1 == null && o2 == null) { 92 return 0; 93 } else if (o1 == null) { return -1; 95 } else if (o2 == null) { 96 return 1; 97 } 98 99 107 108 if (type.getSuperclass() == java.lang.Number .class) { 109 Number n1 = (Number )data.getValueAt(row1, column); 110 double d1 = n1.doubleValue(); 111 Number n2 = (Number )data.getValueAt(row2, column); 112 double d2 = n2.doubleValue(); 113 114 if (d1 < d2) { 115 return -1; 116 } else if (d1 > d2) { 117 return 1; 118 } else { 119 return 0; 120 } 121 } else if (type == java.util.Date .class) { 122 Date d1 = (Date )data.getValueAt(row1, column); 123 long n1 = d1.getTime(); 124 Date d2 = (Date )data.getValueAt(row2, column); 125 long n2 = d2.getTime(); 126 127 if (n1 < n2) { 128 return -1; 129 } else if (n1 > n2) { 130 return 1; 131 } else { 132 return 0; 133 } 134 } else if (type == String .class) { 135 String s1 = (String )data.getValueAt(row1, column); 136 String s2 = (String )data.getValueAt(row2, column); 137 int result = s1.compareTo(s2); 138 139 if (result < 0) { 140 return -1; 141 } else if (result > 0) { 142 return 1; 143 } else { 144 return 0; 145 } 146 } else if (type == Boolean .class) { 147 Boolean bool1 = (Boolean )data.getValueAt(row1, column); 148 boolean b1 = bool1.booleanValue(); 149 Boolean bool2 = (Boolean )data.getValueAt(row2, column); 150 boolean b2 = bool2.booleanValue(); 151 152 if (b1 == b2) { 153 return 0; 154 } else if (b1) { return 1; 156 } else { 157 return -1; 158 } 159 } else { 160 Object v1 = data.getValueAt(row1, column); 161 String s1 = v1.toString(); 162 Object v2 = data.getValueAt(row2, column); 163 String s2 = v2.toString(); 164 int result = s1.compareTo(s2); 165 166 if (result < 0) { 167 return -1; 168 } else if (result > 0) { 169 return 1; 170 } else { 171 return 0; 172 } 173 } 174 } 175 176 public int compare(int row1, int row2) { 177 compares++; 178 for (int level = 0; level < sortingColumns.size(); level++) { 179 Integer column = (Integer )sortingColumns.elementAt(level); 180 int result = compareRowsByColumn(row1, row2, column.intValue()); 181 if (result != 0) { 182 return ascending ? result : -result; 183 } 184 } 185 return 0; 186 } 187 188 public void reallocateIndexes() { 189 int rowCount = model.getRowCount(); 190 191 indexes = new int[rowCount]; 194 195 for (int row = 0; row < rowCount; row++) { 197 indexes[row] = row; 198 } 199 } 200 201 public void tableChanged(TableModelEvent e) { 202 reallocateIndexes(); 204 sort(this); 205 206 super.tableChanged(e); 207 } 208 209 public void checkModel() { 210 if (indexes.length != model.getRowCount()) { 211 System.err.println("Sorter not informed of a change in model."); 212 } 213 } 214 215 public void sort(Object sender) { 216 checkModel(); 217 218 compares = 0; 219 shuttlesort((int[])indexes.clone(), indexes, 0, indexes.length); 222 } 224 225 public void n2sort() { 226 for (int i = 0; i < getRowCount(); i++) { 227 for (int j = i+1; j < getRowCount(); j++) { 228 if (compare(indexes[i], indexes[j]) == -1) { 229 swap(i, j); 230 } 231 } 232 } 233 } 234 235 public void shuttlesort(int from[], int to[], int low, int high) { 243 if (high - low < 2) { 244 return; 245 } 246 int middle = (low + high)/2; 247 shuttlesort(to, from, low, middle); 248 shuttlesort(to, from, middle, high); 249 250 int p = low; 251 int q = middle; 252 253 267 268 if (high - low >= 4 && compare(from[middle-1], from[middle]) <= 0) { 269 for (int i = low; i < high; i++) { 270 to[i] = from[i]; 271 } 272 return; 273 } 274 275 277 for (int i = low; i < high; i++) { 278 if (q >= high || (p < middle && compare(from[p], from[q]) <= 0)) { 279 to[i] = from[p++]; 280 } 281 else { 282 to[i] = from[q++]; 283 } 284 } 285 } 286 287 public void swap(int i, int j) { 288 int tmp = indexes[i]; 289 indexes[i] = indexes[j]; 290 indexes[j] = tmp; 291 } 292 293 296 public Object getValueAt(int aRow, int aColumn) { 297 checkModel(); 298 return model.getValueAt(indexes[aRow], aColumn); 299 } 300 301 public int translateRow(int row) { 302 return indexes[row]; 303 } 304 305 public void setValueAt(Object aValue, int aRow, int aColumn) { 306 checkModel(); 307 model.setValueAt(aValue, indexes[aRow], aColumn); 308 } 309 310 public void sortByColumn(int column) { 311 sortByColumn(column, true); 312 } 313 314 public void sortByColumn(int column, boolean ascending) { 315 316 setCurrentSortColumn(column); 317 this.ascending = ascending; 318 sortingColumns.removeAllElements(); 319 sortingColumns.addElement(new Integer (column)); 320 sort(this); 321 super.tableChanged(new TableModelEvent (this)); 322 323 } 324 325 public void addMouseListenerToHeaderInTable(JTable table) { 329 final TableSorter sorter = this; 330 final JTable tableView = table; 331 tableView.setColumnSelectionAllowed(false); 332 MouseAdapter listMouseListener = new MouseAdapter () { 333 public void mouseClicked(MouseEvent e) { 334 TableColumnModel columnModel = tableView.getColumnModel(); 335 int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 336 int column = tableView.convertColumnIndexToModel(viewColumn); 337 if (e.getClickCount() == 1 && column != -1) { 338 boolean ascending = sorter.isAscending(); 341 if (currentSortColumn == column) { 342 ascending = !ascending; 343 } 344 345 sorter.sortByColumn(column, ascending); 346 tableView.getTableHeader().repaint(); 348 } 349 } 350 }; 351 JTableHeader th = tableView.getTableHeader(); 352 th.addMouseListener(listMouseListener); 353 } 354 } 355 | Popular Tags |