001/*
002 * Created on 15.03.2006
003 *
004 */
005package org.jdesktop.swingx.sort;
006
007import java.util.List;
008
009import javax.swing.RowSorter;
010import javax.swing.SortOrder;
011
012/**
013 * Collection of convenience methods.
014 */
015public class SortUtils {
016    
017//---------------------- static utility methods
018    
019    /**
020     * Returns the first SortKey in the list which is sorted. 
021     * If none is sorted, null is returned.
022     * 
023     * @param keys a list of SortKeys to search
024     * @return the first SortKey which is sorted or null, if no
025     *   is found.
026     */
027    public static RowSorter.SortKey getFirstSortingKey(List<? extends RowSorter.SortKey> keys) {
028        for (RowSorter.SortKey key : keys) {
029            if (isSorted(key.getSortOrder())) {
030                return key;
031            }
032        }
033        return null;
034    }
035
036    /**
037     * Returns the first SortKey in the list for the given column, 
038     * or null if the column has no SortKey. 
039     * 
040     * @param keys a list of SortKeys to search
041     * @param modelColumn the column index in model coordinates
042     * @return the first SortKey for the given column or null if none is
043     *   found.
044     */
045    public static RowSorter.SortKey getFirstSortKeyForColumn(List<? extends RowSorter.SortKey> keys, int modelColumn) {
046        for (RowSorter.SortKey key : keys) {
047            if (key.getColumn() == modelColumn) {
048                return key;
049            }
050        }
051        return null;
052    }
053
054    /**
055     * Removes and returns the first SortKey in the list for the given column, 
056     * or null if the column has no SortKey. 
057     * 
058     * @param keys a list of SortKeys to search
059     * @param modelColumn the column index in model coordinates
060     * @return the first SortKey for the given column or null if none is
061     *   found.
062     */
063    public static RowSorter.SortKey removeFirstSortKeyForColumn(List<? extends RowSorter.SortKey> keys, int modelColumn) {
064        for (RowSorter.SortKey key : keys) {
065            if (key.getColumn() == modelColumn) {
066                keys.remove(key);
067                return key;
068            }
069        }
070        return null;
071    }
072    public static boolean isSorted(SortOrder sortOrder) {
073        return sortOrder != null && (SortOrder.UNSORTED != sortOrder);
074    }
075    
076    /**
077     * Convenience to check for ascending sort order.
078     * PENDING: is this helpful at all?
079     * 
080     * @return true if ascendingly sorted, false for unsorted/descending.
081     */
082    public static boolean isAscending(SortOrder sortOrder) {
083        return sortOrder == SortOrder.ASCENDING;
084    }
085
086    public static boolean isSorted(SortOrder sortOrder, boolean ascending) {
087        return isSorted(sortOrder) && (ascending == isAscending(sortOrder));
088    }
089
090
091    private SortUtils() {};
092}