1 7 package javax.swing.table; 8 9 import java.text.Collator ; 10 import java.util.*; 11 import javax.swing.DefaultRowSorter ; 12 import javax.swing.RowFilter ; 13 import javax.swing.SortOrder ; 14 15 113 public class TableRowSorter<M extends TableModel > extends DefaultRowSorter <M, Integer > { 114 117 private static final Comparator COMPARABLE_COMPARATOR = 118 new ComparableComparator(); 119 120 123 private M tableModel; 124 125 128 private TableStringConverter stringConverter; 129 130 131 134 public TableRowSorter() { 135 this(null); 136 } 137 138 145 public TableRowSorter(M model) { 146 setModel(model); 147 } 148 149 156 public void setModel(M model) { 157 tableModel = model; 158 setModelWrapper(new TableRowSorterModelWrapper()); 159 } 160 161 170 public void setStringConverter(TableStringConverter stringConverter) { 171 this.stringConverter = stringConverter; 172 } 173 174 180 public TableStringConverter getStringConverter() { 181 return stringConverter; 182 } 183 184 199 public Comparator<?> getComparator(int column) { 200 Comparator comparator = super.getComparator(column); 201 if (comparator != null) { 202 return comparator; 203 } 204 Class columnClass = getModel().getColumnClass(column); 205 if (columnClass == String .class) { 206 return Collator.getInstance(); 207 } 208 if (Comparable .class.isAssignableFrom(columnClass)) { 209 return COMPARABLE_COMPARATOR; 210 } 211 return Collator.getInstance(); 212 } 213 214 219 protected boolean useToString(int column) { 220 Comparator comparator = super.getComparator(column); 221 if (comparator != null) { 222 return false; 223 } 224 Class columnClass = getModel().getColumnClass(column); 225 if (columnClass == String .class) { 226 return false; 227 } 228 if (Comparable .class.isAssignableFrom(columnClass)) { 229 return false; 230 } 231 return true; 232 } 233 234 238 private class TableRowSorterModelWrapper extends ModelWrapper<M,Integer > { 239 public M getModel() { 240 return tableModel; 241 } 242 243 public int getColumnCount() { 244 return (tableModel == null) ? 0 : tableModel.getColumnCount(); 245 } 246 247 public int getRowCount() { 248 return (tableModel == null) ? 0 : tableModel.getRowCount(); 249 } 250 251 public Object getValueAt(int row, int column) { 252 return tableModel.getValueAt(row, column); 253 } 254 255 public String getStringValueAt(int row, int column) { 256 TableStringConverter converter = getStringConverter(); 257 if (converter != null) { 258 String value = converter.toString( 260 tableModel, row, column); 261 if (value != null) { 262 return value; 263 } 264 return ""; 265 } 266 267 Object o = getValueAt(row, column); 269 if (o == null) { 270 return ""; 271 } 272 String string = o.toString(); 273 if (string == null) { 274 return ""; 275 } 276 return string; 277 } 278 279 public Integer getIdentifier(int index) { 280 return index; 281 } 282 } 283 284 285 private static class ComparableComparator implements Comparator { 286 @SuppressWarnings ("unchecked") 287 public int compare(Object o1, Object o2) { 288 return ((Comparable )o1).compareTo(o2); 289 } 290 } 291 } 292 | Popular Tags |