KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > table > SortingModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.core.table;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Comparator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import javax.swing.event.EventListenerList JavaDoc;
29
30 /**
31  * Sortable tree table models should implement this interface
32  */

33 public class SortingModel {
34     /**
35      * Compares Comparables
36      */

37     public static class DefaultComparator implements
38             Comparator JavaDoc<Comparable JavaDoc<Object JavaDoc>> {
39         public int compare(Comparable JavaDoc<Object JavaDoc> o1, Comparable JavaDoc<Object JavaDoc> o2) {
40             if (o1 == null && o2 == null)
41                 return 0;
42             if (o1 == null)
43                 return -1;
44             if (o2 == null)
45                 return 1;
46             return o1.compareTo(o2);
47         }
48     }
49
50     public static Comparator JavaDoc DEFAULT_COMPARATOR =
51         new DefaultComparator();
52     
53     private EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
54     private List JavaDoc<Comparator JavaDoc> comparators = new ArrayList JavaDoc<Comparator JavaDoc>();
55     private int sortedColumn = -1;
56     private boolean descending = true;
57
58     /**
59      * Fires a change event that is used to notify listeners about the
60      * changes in SortingModel
61      */

62     protected void fireChange() {
63         // Guaranteed to return a non-null array
64
Object JavaDoc[] listeners = listenerList.getListenerList();
65         ChangeEvent JavaDoc e = null;
66         // Process the listeners last to first, notifying
67
// those that are interested in this event
68
for (int i = listeners.length-2; i>=0; i-=2) {
69             if (listeners[i]==ChangeListener JavaDoc.class) {
70                 // Lazily create the event:
71
if (e == null)
72                     e = new ChangeEvent JavaDoc(this);
73                 ((ChangeListener JavaDoc)listeners[i+1]).stateChanged(e);
74             }
75         }
76     }
77     
78     /**
79      * Returns a comparator for the specified model column
80      *
81      * @param modelColumn column number
82      * @return comparator or null if the column is not sortable
83      */

84     public Comparator JavaDoc getColumnComparator(int modelColumn) {
85         if (modelColumn >= comparators.size())
86             return null;
87         return (Comparator JavaDoc) comparators.get(modelColumn);
88     }
89     
90     /**
91      * Sets a comparator for the specified model column
92      *
93      * @param modelColumn column number
94      * @param c comparator or null if the column is not sortable
95      */

96     public void setColumnComparator(int modelColumn, Comparator JavaDoc c) {
97         while (modelColumn >= comparators.size())
98             comparators.add(null);
99         comparators.set(modelColumn, c);
100     }
101     
102     /**
103      * Returns sorting order
104      *
105      * @return true = descending, false = ascending
106      */

107     public boolean isSortOrderDescending() {
108         return descending;
109     }
110     
111     /**
112      * Sets sorting order
113      *
114      * @param d true = descending, false = ascending
115      */

116     public void setSortOrderDescending(boolean d) {
117         this.descending = d;
118         fireChange();
119     }
120     
121     /**
122      * Changes the sorted column
123      *
124      * @param index column number in the model or -1 if not sorted
125      */

126     public void setSortedColumn(int index) {
127         this.sortedColumn = index;
128         fireChange();
129     }
130     
131     /**
132      * Returns the sorted column
133      *
134      * @return column number in the model or -1 if not sorted
135      */

136     public int getSortedColumn() {
137         return sortedColumn;
138     }
139     
140     /**
141      * Adds a listener
142      *
143      * @param l a listener
144      */

145     public void addChangeListener(ChangeListener JavaDoc l) {
146         this.listenerList.add(ChangeListener JavaDoc.class, l);
147     }
148     
149     /**
150      * Removes a listener
151      *
152      * @param l a listener to be removed
153      */

154     public void removeChangeListener(ChangeListener JavaDoc l) {
155         this.listenerList.remove(ChangeListener JavaDoc.class, l);
156     }
157 }
158
Popular Tags