Handles translations between an indexed data source and a permutation of itself (like the translations between the rows in sorted table and the rows in the actual unsorted model).
| 546 | * rows in the actual unsorted model). |
| 547 | */ |
| 548 | protected class SortingModel extends AbstractTableModel |
| 549 | implements TableModelListener{ |
| 550 | |
| 551 | public SortingModel(TableModel sourceModel){ |
| 552 | compWrapper = new ValueHolderComparator(); |
| 553 | init(sourceModel); |
| 554 | } |
| 555 | |
| 556 | protected class ValueHolder{ |
| 557 | private Object value; |
| 558 | private int index; |
| 559 | public ValueHolder(Object value, int index) { |
| 560 | super(); |
| 561 | this.value = value; |
| 562 | this.index = index; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | @SuppressWarnings({"rawtypes","unchecked"}) |
| 567 | protected class ValueHolderComparator implements Comparator<ValueHolder>{ |
| 568 | private Comparator comparator; |
| 569 | |
| 570 | protected Comparator getComparator() { |
| 571 | return comparator; |
| 572 | } |
| 573 | |
| 574 | protected void setComparator(Comparator comparator) { |
| 575 | this.comparator = comparator; |
| 576 | } |
| 577 | |
| 578 | @Override |
| 579 | public int compare(ValueHolder o1, ValueHolder o2) { |
| 580 | return ascending ? comparator.compare(o1.value, o2.value) : |
| 581 | comparator.compare(o1.value, o2.value) * -1; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | protected void init(TableModel sourceModel){ |
| 586 | if(this.sourceModel != null) |
| 587 | this.sourceModel.removeTableModelListener(this); |
| 588 | this.sourceModel = sourceModel; |
| 589 | //start with the identity order |
| 590 | int size = sourceModel.getRowCount(); |
| 591 | sourceToTarget = new int[size]; |
| 592 | targetToSource = new int[size]; |
| 593 | for(int i = 0; i < size; i++) { |
| 594 | sourceToTarget[i] = i; |
| 595 | targetToSource[i] = i; |
| 596 | } |
| 597 | sourceModel.addTableModelListener(this); |
| 598 | if(isSortable() && sortedColumn == -1) setSortedColumn(0); |
| 599 | componentSizedProperly = false; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * This gets events from the source model and forwards them to the UI |
| 604 | */ |
| 605 | @Override |
nothing calls this directly
no outgoing calls
no test coverage detected