1 18 19 package org.objectweb.mobilitools.util.gui; 20 21 import java.util.*; 22 23 import javax.swing.table.TableModel ; 24 import javax.swing.event.TableModelEvent ; 25 26 28 import java.awt.event.MouseAdapter ; 29 import java.awt.event.MouseEvent ; 30 import java.awt.event.InputEvent ; 31 import javax.swing.JTable ; 32 import javax.swing.table.JTableHeader ; 33 import javax.swing.table.TableColumnModel ; 34 35 public class TableSorter extends TableMap { 36 int indexes[]; 37 Vector sortingColumns = new Vector(); 38 boolean ascending = true; 39 int compares; 40 JTable tableView; 41 42 public TableSorter() { 43 indexes = new int[0]; } 45 46 public TableSorter(TableModel model) { 47 setModel(model); 48 } 49 50 public void setModel(TableModel model) { 51 super.setModel(model); 52 reallocateIndexes(); 53 } 54 55 synchronized public int compareRowsByColumn(int row1, int row2, int column) { 56 Class type = model.getColumnClass(column); 57 TableModel data = model; 58 59 61 Object o1 = data.getValueAt(row1, column); 62 Object o2 = data.getValueAt(row2, column); 63 64 if (o1 == null && o2 == null) { 66 return 0; 67 } else if (o1 == null) { return -1; 69 } else if (o2 == null) { 70 return 1; 71 } 72 73 81 82 if (type.getSuperclass() == java.lang.Number .class) { 83 Number n1 = (Number )data.getValueAt(row1, column); 84 double d1 = n1.doubleValue(); 85 Number n2 = (Number )data.getValueAt(row2, column); 86 double d2 = n2.doubleValue(); 87 88 if (d1 < d2) { 89 return -1; 90 } else if (d1 > d2) { 91 return 1; 92 } else { 93 return 0; 94 } 95 } else if (type == java.util.Date .class) { 96 Date d1 = (Date)data.getValueAt(row1, column); 97 long n1 = d1.getTime(); 98 Date d2 = (Date)data.getValueAt(row2, column); 99 long n2 = d2.getTime(); 100 101 if (n1 < n2) { 102 return -1; 103 } else if (n1 > n2) { 104 return 1; 105 } else { 106 return 0; 107 } 108 } else if (type == String .class) { 109 String s1 = (String )data.getValueAt(row1, column); 110 String s2 = (String )data.getValueAt(row2, column); 111 int result = s1.compareTo(s2); 112 113 if (result < 0) { 114 return -1; 115 } else if (result > 0) { 116 return 1; 117 } else { 118 return 0; 119 } 120 } else if (type == Boolean .class) { 121 Boolean bool1 = (Boolean )data.getValueAt(row1, column); 122 boolean b1 = bool1.booleanValue(); 123 Boolean bool2 = (Boolean )data.getValueAt(row2, column); 124 boolean b2 = bool2.booleanValue(); 125 126 if (b1 == b2) { 127 return 0; 128 } else if (b1) { return 1; 130 } else { 131 return -1; 132 } 133 } else { 134 Object v1 = data.getValueAt(row1, column); 135 String s1 = v1.toString(); 136 Object v2 = data.getValueAt(row2, column); 137 String s2 = v2.toString(); 138 int result = s1.compareTo(s2); 139 140 if (result < 0) { 141 return -1; 142 } else if (result > 0) { 143 return 1; 144 } else { 145 return 0; 146 } 147 } 148 } 149 150 synchronized public int compare(int row1, int row2) { 151 compares++; 152 for (int level = 0; level < sortingColumns.size(); level++) { 153 Integer column = (Integer )sortingColumns.elementAt(level); 154 int result = compareRowsByColumn(row1, row2, column.intValue()); 155 if (result != 0) { 156 return ascending ? result : -result; 157 } 158 } 159 return 0; 160 } 161 162 synchronized public void reallocateIndexes() { 163 int rowCount = model.getRowCount(); 164 165 indexes = new int[rowCount]; 168 169 for (int row = 0; row < rowCount; row++) { 171 indexes[row] = row; 172 } 173 } 174 175 public void tableChanged(TableModelEvent e) { 176 reallocateIndexes(); 178 179 super.tableChanged(e); 180 } 181 182 public void checkModel() { 183 if (indexes.length != model.getRowCount()) { 184 System.err.println("Sorter not informed of a change in model."); 185 } 186 } 187 188 synchronized public void sort(Object sender) { 189 checkModel(); 190 191 compares = 0; 192 shuttlesort((int[])indexes.clone(), indexes, 0, indexes.length); 195 } 197 198 public void n2sort() { 199 for (int i = 0; i < getRowCount(); i++) { 200 for (int j = i+1; j < getRowCount(); j++) { 201 if (compare(indexes[i], indexes[j]) == -1) { 202 swap(i, j); 203 } 204 } 205 } 206 } 207 208 public void shuttlesort(int from[], int to[], int low, int high) { 216 if (high - low < 2) { 217 return; 218 } 219 int middle = (low + high)/2; 220 shuttlesort(to, from, low, middle); 221 shuttlesort(to, from, middle, high); 222 223 int p = low; 224 int q = middle; 225 226 240 241 if (high - low >= 4 && compare(from[middle-1], from[middle]) <= 0) { 242 for (int i = low; i < high; i++) { 243 to[i] = from[i]; 244 } 245 return; 246 } 247 248 250 for (int i = low; i < high; i++) { 251 if (q >= high || (p < middle && compare(from[p], from[q]) <= 0)) { 252 to[i] = from[p++]; 253 } 254 else { 255 to[i] = from[q++]; 256 } 257 } 258 } 259 260 public void swap(int i, int j) { 261 int tmp = indexes[i]; 262 indexes[i] = indexes[j]; 263 indexes[j] = tmp; 264 } 265 266 269 synchronized public Object getValueAt(int aRow, int aColumn) { 270 checkModel(); 271 return model.getValueAt(indexes[aRow], aColumn); 272 } 273 274 synchronized public void setValueAt(Object aValue, int aRow, int aColumn) { 275 checkModel(); 276 model.setValueAt(aValue, indexes[aRow], aColumn); 277 } 278 279 public void sortByColumn(int column) { 280 sortByColumn(column, true); 281 } 282 283 synchronized public void sortByColumn(int column, boolean ascending) { 284 this.ascending = ascending; 285 sortingColumns.removeAllElements(); 286 sortingColumns.addElement(new Integer (column)); 287 sort(this); 288 super.tableChanged(new TableModelEvent (this)); 289 } 290 291 public void addMouseListenerToHeaderInTable(JTable table) { 295 final TableSorter sorter = this; 296 tableView = table; 297 tableView.setColumnSelectionAllowed(false); 298 MouseAdapter listMouseListener = new MouseAdapter () { 299 public void mouseClicked(MouseEvent e) { 300 TableColumnModel columnModel = tableView.getColumnModel(); 301 int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 302 int column = tableView.convertColumnIndexToModel(viewColumn); 303 if (e.getClickCount() == 1 && column != -1) { 304 int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK; 305 boolean ascending = (shiftPressed == 0); 306 sorter.sortByColumn(column, ascending); 307 TableSorter.this.tableView.clearSelection(); 308 } 309 } 310 }; 311 JTableHeader th = tableView.getTableHeader(); 312 th.addMouseListener(listMouseListener); 313 } 314 } 315 | Popular Tags |