KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > examples > listexample > SortableList


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.examples.listexample;
17
18
19
20 /**
21  * Convenient base class for sortable lists.
22  * @author Thomas Spiegl (latest modification by $Author: matzew $)
23  * @version $Revision: 1.1 $ $Date: 2005/03/24 16:47:10 $
24  */

25 public abstract class SortableList
26 {
27     private String JavaDoc _sort;
28     private boolean _ascending;
29
30     protected SortableList(String JavaDoc defaultSortColumn)
31     {
32         _sort = defaultSortColumn;
33         _ascending = isDefaultAscending(defaultSortColumn);
34     }
35
36     /**
37      * Sort the list.
38      */

39     protected abstract void sort(String JavaDoc column, boolean ascending);
40
41     /**
42      * Is the default sort direction for the given column "ascending" ?
43      */

44     protected abstract boolean isDefaultAscending(String JavaDoc sortColumn);
45
46
47     public void sort(String JavaDoc sortColumn)
48     {
49         if (sortColumn == null)
50         {
51             throw new IllegalArgumentException JavaDoc("Argument sortColumn must not be null.");
52         }
53
54         if (_sort.equals(sortColumn))
55         {
56             //current sort equals new sortColumn -> reverse sort order
57
_ascending = !_ascending;
58         }
59         else
60         {
61             //sort new column in default direction
62
_sort = sortColumn;
63             _ascending = isDefaultAscending(_sort);
64         }
65
66         sort(_sort, _ascending);
67     }
68
69     public String JavaDoc getSort()
70     {
71         return _sort;
72     }
73
74     public void setSort(String JavaDoc sort)
75     {
76         _sort = sort;
77     }
78
79     public boolean isAscending()
80     {
81         return _ascending;
82     }
83
84     public void setAscending(boolean ascending)
85     {
86         _ascending = ascending;
87     }
88 }
89
Popular Tags