View Javadoc

1   /***
2    * SortableTableColumn.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.swing.table;
27  
28  import java.util.HashSet;
29  import java.util.Iterator;
30  
31  import javax.swing.table.TableCellEditor;
32  import javax.swing.table.TableCellRenderer;
33  import javax.swing.table.TableColumn;
34  
35  import org.jcreme.enumerations.ComparatorType;
36  import org.jcreme.enumerations.SortOrder;
37  
38  /***
39   * This is an extension of the standard table columns. The additional features
40   * are the ability to handle an order and a comparator. These can be used to
41   * implement sorted tables.
42   * 
43   * @author $Author: dbregeon $
44   * @version $Revision: 1.1 $
45   */
46  public class SortableTableColumn extends TableColumn {
47      /***
48       * The current order of the column.
49       */
50      private SortOrder order = SortOrder.NONE;
51  
52      /***
53       * The place of the column in the sort.
54       */
55      private Integer orderInSort = null;
56  
57      /***
58       * The Class of the elements contained in this column.
59       */
60      private Class columnClass = null;
61  
62      /***
63       * The comparator to use to sort the values of this column.
64       */
65      private ComparatorType comparator = ComparatorType.NONE;
66  
67      /***
68       * The listeners that must be notified when the order changes.
69       */
70      private final HashSet listeners = new HashSet();
71  
72      /***
73       * Creates new SortableTableColumn
74       */
75      public SortableTableColumn() {
76          this(0);
77      }
78  
79      /***
80       * Creates new SortableTableColumn
81       * 
82       * @param modelIndex
83       *            the index of the column in the column model.
84       */
85      public SortableTableColumn(int modelIndex) {
86          this(modelIndex, 75);
87      }
88  
89      /***
90       * Creates new SortableTableColumn
91       * 
92       * @param modelIndex
93       *            the index of the column in the column model.
94       * @param width
95       *            the width of the column.
96       */
97      public SortableTableColumn(int modelIndex, int width) {
98          this(modelIndex, width, null, null);
99      }
100 
101     /***
102      * Creates new SortableTableColumn
103      * 
104      * @param modelIndex
105      *            the index of the column in the column model.
106      * @param width
107      *            the width of the column.
108      * @param cellRenderer
109      *            the renderer to use to display the values in this column.
110      * @param cellEditor
111      *            the editor to use to edit the values in this column.
112      */
113     public SortableTableColumn(int modelIndex, int width,
114             TableCellRenderer cellRenderer, TableCellEditor cellEditor) {
115         super(modelIndex, width, cellRenderer, cellEditor);
116         this.setHeaderRenderer(new SortableColumnTableHeaderRenderer(this));
117     }
118 
119     /***
120      * This method enables to set the sort order (ascending, descending, none)
121      * for the given column. The column will not be used in any sort if it has a
122      * SortOrder NONE or null.
123      * 
124      * @param order
125      *            the custom order. Erase any previously set SortOrder if it is
126      *            null or SortOrder.NONE.
127      */
128     public void setOrder(SortOrder order) {
129         if (order == null) {
130             order = SortOrder.NONE;
131         }
132         if (this.order != order) {
133             this.order = order;
134             fireTableColumnOrderEvent(new TableColumnOrderEvent(this));
135         }
136     }
137 
138     /***
139      * Gives access to the sort order of the column.
140      * 
141      * @return the SortOrder associated to the column.
142      */
143     public SortOrder getOrder() {
144         return this.order;
145     }
146 
147     /***
148      * Enables to change the order of the column in the sort.
149      * 
150      * @param orderInSort
151      *            the new order of the column in the sort.
152      */
153     public void setOrderInSort(Integer orderInSort) {
154         if ((orderInSort != this.orderInSort)
155                 && ((orderInSort != null) ? (!orderInSort
156                         .equals(this.orderInSort)) : (!this.orderInSort
157                         .equals(orderInSort)))) {
158             this.orderInSort = orderInSort;
159             fireTableColumnOrderEvent(new TableColumnOrderEvent(this));
160         }
161     }
162 
163     /***
164      * Gives access to the order of the column in the sort.
165      * 
166      * @return the current order of the column in the sort.
167      */
168     public Integer getOrderInSort() {
169         return this.orderInSort;
170     }
171 
172     /***
173      * Enables to change the Class of the elements presented in this Column.
174      * 
175      * @param clazz
176      */
177     public void setColumnClass(Class clazz) {
178         this.columnClass = clazz;
179     }
180 
181     /***
182      * Gives access to the Class of the elements presented in this Column.
183      * 
184      * @return the Class of the elements in this Column.
185      */
186     public Class getColumnClass() {
187         return this.columnClass;
188     }
189 
190     /***
191      * This method enables to set a custom comparator for this column.
192      * 
193      * @param cmp
194      *            the custom comparator. Erase any previously set comparator if
195      *            it is null.
196      */
197     public void setComparator(ComparatorType cmp) {
198         if (this.comparator != cmp) {
199             this.comparator = cmp;
200             fireTableColumnOrderEvent(new TableColumnOrderEvent(this));
201         }
202     }
203 
204     /***
205      * Gives access to the comparator that is in use in this column.
206      * 
207      * @return the comparator to used on values found in this column.
208      */
209     public ComparatorType getComparator() {
210         return this.comparator;
211     }
212 
213     /***
214      * Enables to add a listener that will be activated every time changes
215      * happen in the column that may alter the order of the values. (namely the
216      * order or the comparator).
217      * 
218      * @param listener
219      *            the listener to notify.
220      */
221     public void addTableColumnOrderListener(TableColumnOrderListener listener) {
222         if (listener != null) {
223             synchronized (this.listeners) {
224                 this.listeners.add(listener);
225             }
226         }
227     }
228 
229     /***
230      * Removes a listener from the list of the registered listeners.
231      * 
232      * @param listener
233      *            the listener to remove.
234      */
235     public void removeTableColumnOrderListener(TableColumnOrderListener listener) {
236         if (listener != null) {
237             synchronized (this.listeners) {
238                 this.listeners.remove(listener);
239             }
240         }
241     }
242 
243     /***
244      * The method called to activate the listeners.
245      * 
246      * @param event
247      *            the event to send to the listeners.
248      */
249     protected void fireTableColumnOrderEvent(TableColumnOrderEvent event) {
250         synchronized (this.listeners) {
251             Iterator iter = this.listeners.iterator();
252             while (iter.hasNext()) {
253                 ((TableColumnOrderListener) iter.next())
254                         .tableColumnOrderChanged(event);
255             }
256         }
257     }
258 }