1 24 25 package org.objectweb.cjdbc.console.views; 26 27 import java.awt.event.InputEvent ; 28 import java.awt.event.MouseAdapter ; 29 import java.awt.event.MouseEvent ; 30 import java.util.Date ; 31 import java.util.Vector ; 32 33 import javax.swing.JTable ; 34 import javax.swing.event.TableModelEvent ; 35 import javax.swing.event.TableModelListener ; 36 import javax.swing.table.AbstractTableModel ; 37 import javax.swing.table.JTableHeader ; 38 import javax.swing.table.TableColumnModel ; 39 import javax.swing.table.TableModel ; 40 41 49 50 public class InfoTableSorter extends AbstractTableModel 51 implements 52 TableModelListener 53 { 54 private TableModel model; 55 private int[] indexes; 56 private Vector sortingColumns = new Vector (); 57 private boolean ascending = true; 58 private int compares; 59 60 65 public InfoTableSorter(TableModel model) 66 { 67 setModel(model); 68 } 69 70 private void setModel(TableModel model) 71 { 72 this.model = model; 73 model.addTableModelListener(this); 74 reallocateIndexes(); 75 } 76 77 80 public int getRowCount() 81 { 82 return (model == null) ? 0 : model.getRowCount(); 83 } 84 85 88 public int getColumnCount() 89 { 90 return (model == null) ? 0 : model.getColumnCount(); 91 } 92 93 96 public String getColumnName(int aColumn) 97 { 98 return model.getColumnName(aColumn); 99 } 100 101 104 public Class getColumnClass(int aColumn) 105 { 106 return model.getColumnClass(aColumn); 107 } 108 109 112 public void tableChanged(TableModelEvent e) 113 { 114 reallocateIndexes(); 115 fireTableChanged(e); 116 } 117 118 121 124 public Object getValueAt(int aRow, int aColumn) 125 { 126 return model.getValueAt(indexes[aRow], aColumn); 127 } 128 129 132 public void setValueAt(Object aValue, int aRow, int aColumn) 133 { 134 model.setValueAt(aValue, indexes[aRow], aColumn); 135 } 136 137 143 public void addMouseListenerToHeaderInTable(JTable table) 144 { 145 final InfoTableSorter sorter = this; 146 final JTable tableView = table; 147 tableView.setColumnSelectionAllowed(false); 148 MouseAdapter listMouseListener = new MouseAdapter () 149 { 150 public void mouseClicked(MouseEvent e) 151 { 152 TableColumnModel columnModel = tableView.getColumnModel(); 153 int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 154 int column = tableView.convertColumnIndexToModel(viewColumn); 155 if (e.getClickCount() == 1 && column != -1) 156 { 157 int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK; 158 sorter.sortByColumn(column, shiftPressed == 0); 159 } 160 } 161 }; 162 JTableHeader th = tableView.getTableHeader(); 163 th.addMouseListener(listMouseListener); 164 } 165 166 private void sortByColumn(int column, boolean ascending) 167 { 168 this.ascending = ascending; 169 sortingColumns.removeAllElements(); 170 sortingColumns.addElement(new Integer (column)); 171 compares = 0; 172 for (int i = 0; i < getRowCount(); i++) 173 for (int j = i + 1; j < getRowCount(); j++) 174 if (compare(indexes[i], indexes[j]) == -1) 175 swap(i, j); 176 177 fireTableChanged(new TableModelEvent (this)); 178 } 179 180 private int compareRowsByColumn(int row1, int row2, int column) 181 { 182 int result = 0; 183 Class type = model.getColumnClass(column); 184 TableModel data = model; 185 186 188 Object o1 = data.getValueAt(row1, column); 189 Object o2 = data.getValueAt(row2, column); 190 191 if (o1 == null && o2 == null) 193 return 0; 194 else if (o1 == null) 195 return -1; 197 else if (o2 == null) 198 return 1; 199 200 if (type.getSuperclass() == java.lang.Number .class) 201 { 202 Number n1 = (Number ) data.getValueAt(row1, column); 203 double d1 = n1.doubleValue(); 204 Number n2 = (Number ) data.getValueAt(row2, column); 205 double d2 = n2.doubleValue(); 206 result = (int) (d1 - d2); 207 } 208 else if (type == java.util.Date .class) 209 { 210 Date d1 = (Date ) data.getValueAt(row1, column); 211 long n1 = d1.getTime(); 212 Date d2 = (Date ) data.getValueAt(row2, column); 213 long n2 = d2.getTime(); 214 result = (int) (n1 - n2); 215 } 216 else if (type == String .class) 217 { 218 String s1 = (String ) data.getValueAt(row1, column); 219 String s2 = (String ) data.getValueAt(row2, column); 220 result = s1.compareTo(s2); 221 } 222 else 223 { 224 Object v1 = data.getValueAt(row1, column); 225 String s1 = v1.toString(); 226 Object v2 = data.getValueAt(row2, column); 227 String s2 = v2.toString(); 228 result = s1.compareTo(s2); 229 } 230 if (result < 0) 231 return -1; 232 else if (result > 0) 233 return 1; 234 else 235 return 0; 236 } 237 238 private int compare(int row1, int row2) 239 { 240 compares++; 241 for (int level = 0; level < sortingColumns.size(); level++) 242 { 243 Integer column = (Integer ) sortingColumns.elementAt(level); 244 int result = compareRowsByColumn(row1, row2, column.intValue()); 245 if (result != 0) 246 return ascending ? result : -result; 247 } 248 return 0; 249 } 250 251 private void reallocateIndexes() 252 { 253 int rowCount = model.getRowCount(); 254 255 indexes = new int[rowCount]; 258 259 for (int row = 0; row < rowCount; row++) 261 { 262 indexes[row] = row; 263 } 264 } 265 266 private void swap(int i, int j) 267 { 268 int tmp = indexes[i]; 269 indexes[i] = indexes[j]; 270 indexes[j] = tmp; 271 } 272 273 } 274 | Popular Tags |