001: /*
002: * Created on 15.03.2006
003: *
004: */
005: package org.jdesktop.swingx.decorator;
006:
007: import java.util.Comparator;
008: import java.util.List;
009:
010: /**
011: * A column and how its sorted.
012: */
013: public class SortKey {
014: private final SortOrder sortOrder;
015: private final int column;
016: private final Comparator comparator;
017:
018: /**
019: * @param sortOrder one of {@link SortOrder#ASCENDING},
020: * {@link SortOrder#DESCENDING} or {@link SortOrder#UNSORTED}.
021: * @param column a column in terms of <strong>model</strong> index.
022: */
023: public SortKey(SortOrder sortOrder, int column) {
024: this (sortOrder, column, null);
025: }
026:
027: /**
028: * @param sortOrder one of {@link SortOrder#ASCENDING},
029: * {@link SortOrder#DESCENDING} or {@link SortOrder#UNSORTED}.
030: * @param column a column in terms of <strong>model</strong> index.
031: * @param comparator the comparator to use with this sort.
032: */
033: public SortKey(SortOrder sortOrder, int column,
034: Comparator comparator) {
035: if (sortOrder == null)
036: throw new IllegalArgumentException();
037: if (column < 0)
038: throw new IllegalArgumentException();
039: this .column = column;
040: this .comparator = comparator;
041: this .sortOrder = sortOrder;
042: }
043:
044: /**
045: * The sort order, ascending, descending or unsorted.
046: */
047: public SortOrder getSortOrder() {
048: return sortOrder;
049: }
050:
051: /**
052: * The sorting column in terms of <strong>model</strong> index.
053: */
054: public int getColumn() {
055: return column;
056: }
057:
058: /**
059: * The comparator to use, might be null.
060: */
061: public Comparator getComparator() {
062: return comparator;
063: }
064:
065: /** {@inheritDoc} */
066: public boolean equals(Object o) {
067: if (this == o)
068: return true;
069: if (o == null || getClass() != o.getClass())
070: return false;
071:
072: final SortKey sortKey = (SortKey) o;
073:
074: if (column != sortKey.column)
075: return false;
076: if (sortOrder != null ? !sortOrder.equals(sortKey.sortOrder)
077: : sortKey.sortOrder != null)
078: return false;
079:
080: return true;
081: }
082:
083: /** {@inheritDoc} */
084: public int hashCode() {
085: int result;
086: result = (sortOrder != null ? sortOrder.hashCode() : 0);
087: result = 29 * result + column;
088: return result;
089: }
090:
091: //---------------------- static utility methods
092:
093: /**
094: * Returns the first SortKey in the list which is sorted.
095: * If none is sorted, null is returned.
096: *
097: * @param keys a list of SortKeys to search
098: * @return the first SortKey which is sorted or null, if no
099: * is found.
100: */
101: public static SortKey getFirstSortingKey(
102: List<? extends SortKey> keys) {
103: for (SortKey key : keys) {
104: if (key.getSortOrder().isSorted()) {
105: return key;
106: }
107: }
108: return null;
109: }
110:
111: /**
112: * Returns the first SortKey in the list for the given column,
113: * or null if the column has no SortKey.
114: *
115: * @param keys a list of SortKeys to search
116: * @param modelColumn the column index in model coordinates
117: * @return the first SortKey for the given column or null if none is
118: * found.
119: */
120: public static SortKey getFirstSortKeyForColumn(
121: List<? extends SortKey> keys, int modelColumn) {
122: for (SortKey key : keys) {
123: if (key.getColumn() == modelColumn) {
124: return key;
125: }
126: }
127: return null;
128: }
129:
130: }
|