KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > decorator > Sorter


1 /*
2  * $Id: Sorter.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.decorator;
9
10 /**
11  * Pluggable sorting filter.
12  *
13  * @author Ramesh Gupta
14  */

15 public abstract class Sorter extends Filter {
16     private boolean ascending = true;
17
18     public Sorter() {
19         this(0, true);
20     }
21
22     public Sorter(int col, boolean ascending) {
23         super(col);
24         setAscending(ascending);
25     }
26
27 /*
28     public void contentsChanged(PipelineEvent ev) {
29         if (memberOf((FilterPipeline) ev.getSource())) {
30             // Do nothing (to avoid unbounded recursion
31         }
32         else {
33             purge(); // purge stale indices of stand-alone sorter
34         }
35     }
36 */

37
38     /**
39      * Adopts the row mappings of the specified sorter by cloning the mappings.
40      *
41      * @param oldSorter <code>Sorter</code> whose mappings are to be cloned
42      */

43     protected abstract void adopt(Sorter oldSorter);
44
45     /**
46      * Interposes this sorter between a filter pipeline and the component that
47      * the pipeline is bound to, replacing oldSorter as the previously
48      * interposed sorter. You should not have to call this method directly.
49      * @todo Pass in just the ComponentAdapter, and add methods to that for
50      * fetching the filter pipeline and old sorter, if any.
51      *
52      * @param filters
53      * @param adapter
54      * @param oldSorter
55      */

56     public void interpose(FilterPipeline filters, ComponentAdapter adapter,
57                           Sorter oldSorter) {
58         if (filters != null) {
59             filters.setSorter(this);
60         }
61         adopt(oldSorter);
62         assign(filters);
63         assign(adapter);
64         refresh(oldSorter == null);
65     }
66
67
68     public int compare(int row1, int row2) {
69         int result = compare(row1, row2, getColumnIndex());
70         return ascending ? result : -result;
71     }
72
73     /* Adapted from Phil Milne's TableSorter implementation.
74         This implementation, however, is not coupled to TableModel in any way,
75         and may be used with list models and other types of models easily. */

76
77     private int compare(int row1, int row2, int col) {
78         Object JavaDoc o1 = getInputValue(row1, col);
79         Object JavaDoc o2 = getInputValue(row2, col);
80
81         // If both values are null return 0
82
if (o1 == null && o2 == null) {
83             return 0;
84         }
85         else if (o1 == null) { // Define null less than everything.
86
return -1;
87         }
88         else if (o2 == null) {
89             return 1;
90         }
91
92         if (o1 instanceof Comparable JavaDoc) {
93             Comparable JavaDoc c1 = (Comparable JavaDoc) o1;
94             Comparable JavaDoc c2 = (Comparable JavaDoc) o2;
95             return c1.compareTo(c2);
96         }
97         else if (o1 instanceof Boolean JavaDoc) {
98             try {
99                 Boolean JavaDoc bool1 = (Boolean JavaDoc) o1;
100                 boolean b1 = bool1.booleanValue();
101                 Boolean JavaDoc bool2 = (Boolean JavaDoc) o2;
102                 boolean b2 = bool2.booleanValue();
103
104                 if (b1 == b2) {
105                     return 0;
106                 }
107                 else if (b1) { // Define false < true
108
return 1;
109                 }
110                 else {
111                     return -1;
112                 }
113             }
114             catch (ClassCastException JavaDoc ex) {
115                 System.out.println("Column class mismatch: " + o1.getClass() +
116                                    " can't be compared to " + o2.getClass());
117             }
118         }
119         else {
120             return o1.toString().compareTo(o2.toString());
121         }
122
123         return 0;
124     }
125
126     public boolean isAscending() {
127         return ascending;
128     }
129
130     public void setAscending(boolean ascending) {
131         this.ascending = ascending;
132         refresh();
133     }
134
135     public void toggle() {
136         ascending = !ascending;
137         refresh();
138     }
139
140 }
Popular Tags