1 7 package javax.swing; 8 9 import javax.swing.SortOrder ; 10 import javax.swing.event.*; 11 import java.util.*; 12 13 78 public abstract class RowSorter<M> { 79 private EventListenerList listenerList = new EventListenerList(); 80 81 84 public RowSorter() { 85 } 86 87 92 public abstract M getModel(); 93 94 113 public abstract void toggleSortOrder(int column); 114 115 126 public abstract int convertRowIndexToModel(int index); 127 128 140 public abstract int convertRowIndexToView(int index); 141 142 149 public abstract void setSortKeys(List<? extends SortKey> keys); 150 151 160 public abstract List<? extends SortKey> getSortKeys(); 161 162 170 public abstract int getViewRowCount(); 171 172 178 public abstract int getModelRowCount(); 179 180 188 public abstract void modelStructureChanged(); 189 190 200 public abstract void allRowsChanged(); 201 202 222 public abstract void rowsInserted(int firstRow, int endRow); 223 224 242 public abstract void rowsDeleted(int firstRow, int endRow); 243 244 257 public abstract void rowsUpdated(int firstRow, int endRow); 258 259 276 public abstract void rowsUpdated(int firstRow, int endRow, int column); 277 278 287 public void addRowSorterListener(RowSorterListener l) { 288 listenerList.add(RowSorterListener.class, l); 289 } 290 291 297 public void removeRowSorterListener(RowSorterListener l) { 298 listenerList.remove(RowSorterListener.class, l); 299 } 300 301 304 protected void fireSortOrderChanged() { 305 fireRowSorterChanged(new RowSorterEvent(this)); 306 } 307 308 314 protected void fireRowSorterChanged(int[] lastRowIndexToModel) { 315 fireRowSorterChanged(new RowSorterEvent(this, 316 RowSorterEvent.Type.SORTED, lastRowIndexToModel)); 317 } 318 319 void fireRowSorterChanged(RowSorterEvent event) { 320 Object [] listeners = listenerList.getListenerList(); 321 for (int i = listeners.length - 2; i >= 0; i -= 2) { 322 if (listeners[i] == RowSorterListener.class) { 323 ((RowSorterListener)listeners[i + 1]). 324 sorterChanged(event); 325 } 326 } 327 } 328 329 336 public static class SortKey { 337 private int column; 338 private SortOrder sortOrder; 339 340 349 public SortKey(int column, SortOrder sortOrder) { 350 if (sortOrder == null) { 351 throw new IllegalArgumentException ( 352 "sort order must be non-null"); 353 } 354 this.column = column; 355 this.sortOrder = sortOrder; 356 } 357 358 363 public final int getColumn() { 364 return column; 365 } 366 367 372 public final SortOrder getSortOrder() { 373 return sortOrder; 374 } 375 376 381 public int hashCode() { 382 int result = 17; 383 result = 37 * result + column; 384 result = 37 * result + sortOrder.hashCode(); 385 return result; 386 } 387 388 397 public boolean equals(Object o) { 398 if (o == this) { 399 return true; 400 } 401 if (o instanceof SortKey) { 402 return (((SortKey)o).column == column && 403 ((SortKey)o).sortOrder == sortOrder); 404 } 405 return false; 406 } 407 } 408 } 409 | Popular Tags |